blob: d08d2466e68eb1d30102c73de6f28ec7682acf6d [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Jooyung Hanaf730952023-02-28 14:13:38 +0900787func TestFileContexts(t *testing.T) {
788 for _, useFileContextsAsIs := range []bool{true, false} {
789 prop := ""
790 if useFileContextsAsIs {
791 prop = "use_file_contexts_as_is: true,\n"
792 }
793 ctx := testApex(t, `
794 apex {
795 name: "myapex",
796 key: "myapex.key",
797 file_contexts: "file_contexts",
798 updatable: false,
799 vendor: true,
800 `+prop+`
801 }
802
803 apex_key {
804 name: "myapex.key",
805 public_key: "testkey.avbpubkey",
806 private_key: "testkey.pem",
807 }
808 `, withFiles(map[string][]byte{
809 "file_contexts": nil,
810 }))
811
812 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("file_contexts")
813 forceLabellingCommand := "apex_manifest\\\\.pb u:object_r:system_file:s0"
814 if useFileContextsAsIs {
815 android.AssertStringDoesNotContain(t, "should force-label",
816 rule.RuleParams.Command, forceLabellingCommand)
817 } else {
818 android.AssertStringDoesContain(t, "shouldn't force-label",
819 rule.RuleParams.Command, forceLabellingCommand)
820 }
821 }
822}
823
Alex Light5098a612018-11-29 17:12:15 -0800824func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800825 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800826 apex {
827 name: "myapex",
828 key: "myapex.key",
829 payload_type: "zip",
830 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000831 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800832 }
833
834 apex_key {
835 name: "myapex.key",
836 public_key: "testkey.avbpubkey",
837 private_key: "testkey.pem",
838 }
839
840 cc_library {
841 name: "mylib",
842 srcs: ["mylib.cpp"],
843 shared_libs: ["mylib2"],
844 system_shared_libs: [],
845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000846 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800847 }
848
849 cc_library {
850 name: "mylib2",
851 srcs: ["mylib.cpp"],
852 system_shared_libs: [],
853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000854 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800855 }
856 `)
857
Sundong Ahnabb64432019-10-22 13:58:29 +0900858 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800859 copyCmds := zipApexRule.Args["copy_commands"]
860
861 // Ensure that main rule creates an output
862 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
863
864 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700865 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800866
867 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800869
870 // Ensure that both direct and indirect deps are copied into apex
871 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
872 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873}
874
875func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800876 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900881 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000882 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 }
884
885 apex_key {
886 name: "myapex.key",
887 public_key: "testkey.avbpubkey",
888 private_key: "testkey.pem",
889 }
890
891 cc_library {
892 name: "mylib",
893 srcs: ["mylib.cpp"],
894 shared_libs: ["mylib2", "mylib3"],
895 system_shared_libs: [],
896 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000897 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898 }
899
900 cc_library {
901 name: "mylib2",
902 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900903 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 system_shared_libs: [],
905 stl: "none",
906 stubs: {
907 versions: ["1", "2", "3"],
908 },
909 }
910
911 cc_library {
912 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900913 srcs: ["mylib.cpp"],
914 shared_libs: ["mylib4"],
915 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 stl: "none",
917 stubs: {
918 versions: ["10", "11", "12"],
919 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900922
923 cc_library {
924 name: "mylib4",
925 srcs: ["mylib.cpp"],
926 system_shared_libs: [],
927 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000928 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900929 }
Jiyong Park105dc322021-06-11 17:22:09 +0900930
931 rust_binary {
932 name: "foo.rust",
933 srcs: ["foo.rs"],
934 shared_libs: ["libfoo.shared_from_rust"],
935 prefer_rlib: true,
936 apex_available: ["myapex"],
937 }
938
939 cc_library_shared {
940 name: "libfoo.shared_from_rust",
941 srcs: ["mylib.cpp"],
942 system_shared_libs: [],
943 stl: "none",
944 stubs: {
945 versions: ["10", "11", "12"],
946 },
947 }
948
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 `)
950
Sundong Ahnabb64432019-10-22 13:58:29 +0900951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 copyCmds := apexRule.Args["copy_commands"]
953
954 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956
957 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800958 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959
960 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800961 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962
Colin Crossaede88c2020-08-11 12:17:01 -0700963 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964
965 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900966 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900968 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969
970 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700971 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700973 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900974
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700975 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
976 // is replaced by sharing of "cFlags" in cc/builder.go.
977 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
978 // module variable representing "cflags". So it was not detected by ensureNotContains.
979 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
980 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
981 // including the original cflags's "-include mylib.h".
982 //
Jiyong Park64379952018-12-13 18:37:29 +0900983 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700984 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
985 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900986
Jiyong Park85cc35a2022-07-17 11:30:47 +0900987 // Ensure that genstub for platform-provided lib is invoked with --systemapi
988 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
989 // Ensure that genstub for apex-provided lib is invoked with --apex
990 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900991
Jooyung Hana57af4a2020-01-23 05:36:59 +0000992 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900993 "lib64/mylib.so",
994 "lib64/mylib3.so",
995 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900996 "bin/foo.rust",
997 "lib64/libc++.so", // by the implicit dependency from foo.rust
998 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900999 })
Jiyong Park105dc322021-06-11 17:22:09 +09001000
1001 // Ensure that stub dependency from a rust module is not included
1002 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1003 // The rust module is linked to the stub cc library
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001004 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001005 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1006 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001007
1008 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1009 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010}
1011
Jiyong Park1bc84122021-06-22 20:23:05 +09001012func TestApexCanUsePrivateApis(t *testing.T) {
1013 ctx := testApex(t, `
1014 apex {
1015 name: "myapex",
1016 key: "myapex.key",
1017 native_shared_libs: ["mylib"],
1018 binaries: ["foo.rust"],
1019 updatable: false,
1020 platform_apis: true,
1021 }
1022
1023 apex_key {
1024 name: "myapex.key",
1025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 cc_library {
1030 name: "mylib",
1031 srcs: ["mylib.cpp"],
1032 shared_libs: ["mylib2"],
1033 system_shared_libs: [],
1034 stl: "none",
1035 apex_available: [ "myapex" ],
1036 }
1037
1038 cc_library {
1039 name: "mylib2",
1040 srcs: ["mylib.cpp"],
1041 cflags: ["-include mylib.h"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 stubs: {
1045 versions: ["1", "2", "3"],
1046 },
1047 }
1048
1049 rust_binary {
1050 name: "foo.rust",
1051 srcs: ["foo.rs"],
1052 shared_libs: ["libfoo.shared_from_rust"],
1053 prefer_rlib: true,
1054 apex_available: ["myapex"],
1055 }
1056
1057 cc_library_shared {
1058 name: "libfoo.shared_from_rust",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["10", "11", "12"],
1064 },
1065 }
1066 `)
1067
1068 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1069 copyCmds := apexRule.Args["copy_commands"]
1070
1071 // Ensure that indirect stubs dep is not included
1072 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1073 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1074
1075 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1076 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001077 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001078 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1079 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001080 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001081 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1082 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1083}
1084
Colin Cross7812fd32020-09-25 12:35:10 -07001085func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1086 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001087 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001088 apex {
1089 name: "myapex",
1090 key: "myapex.key",
1091 native_shared_libs: ["mylib", "mylib3"],
1092 min_sdk_version: "29",
1093 }
1094
1095 apex_key {
1096 name: "myapex.key",
1097 public_key: "testkey.avbpubkey",
1098 private_key: "testkey.pem",
1099 }
1100
1101 cc_library {
1102 name: "mylib",
1103 srcs: ["mylib.cpp"],
1104 shared_libs: ["mylib2", "mylib3"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 min_sdk_version: "28",
1109 }
1110
1111 cc_library {
1112 name: "mylib2",
1113 srcs: ["mylib.cpp"],
1114 cflags: ["-include mylib.h"],
1115 system_shared_libs: [],
1116 stl: "none",
1117 stubs: {
1118 versions: ["28", "29", "30", "current"],
1119 },
1120 min_sdk_version: "28",
1121 }
1122
1123 cc_library {
1124 name: "mylib3",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib4"],
1127 system_shared_libs: [],
1128 stl: "none",
1129 stubs: {
1130 versions: ["28", "29", "30", "current"],
1131 },
1132 apex_available: [ "myapex" ],
1133 min_sdk_version: "28",
1134 }
1135
1136 cc_library {
1137 name: "mylib4",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 stl: "none",
1141 apex_available: [ "myapex" ],
1142 min_sdk_version: "28",
1143 }
1144 `)
1145
1146 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1147 copyCmds := apexRule.Args["copy_commands"]
1148
1149 // Ensure that direct non-stubs dep is always included
1150 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1151
1152 // Ensure that indirect stubs dep is not included
1153 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1154
1155 // Ensure that direct stubs dep is included
1156 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1157
1158 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1159
Jiyong Park55549df2021-02-26 23:57:23 +09001160 // Ensure that mylib is linking with the latest version of stub for mylib2
1161 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001162 // ... and not linking to the non-stub (impl) variant of mylib2
1163 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1164
1165 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1166 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1167 // .. and not linking to the stubs variant of mylib3
1168 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1169
1170 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001171 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001172 ensureNotContains(t, mylib2Cflags, "-include ")
1173
Jiyong Park85cc35a2022-07-17 11:30:47 +09001174 // Ensure that genstub is invoked with --systemapi
1175 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001176
1177 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1178 "lib64/mylib.so",
1179 "lib64/mylib3.so",
1180 "lib64/mylib4.so",
1181 })
1182}
1183
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001184func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1185 t.Parallel()
1186 // myapex (Z)
1187 // mylib -----------------.
1188 // |
1189 // otherapex (29) |
1190 // libstub's versions: 29 Z current
1191 // |
1192 // <platform> |
1193 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001194 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 min_sdk_version: "Z", // non-final
1200 }
1201
1202 cc_library {
1203 name: "mylib",
1204 srcs: ["mylib.cpp"],
1205 shared_libs: ["libstub"],
1206 apex_available: ["myapex"],
1207 min_sdk_version: "Z",
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 apex {
1217 name: "otherapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libstub"],
1220 min_sdk_version: "29",
1221 }
1222
1223 cc_library {
1224 name: "libstub",
1225 srcs: ["mylib.cpp"],
1226 stubs: {
1227 versions: ["29", "Z", "current"],
1228 },
1229 apex_available: ["otherapex"],
1230 min_sdk_version: "29",
1231 }
1232
1233 // platform module depending on libstub from otherapex should use the latest stub("current")
1234 cc_library {
1235 name: "libplatform",
1236 srcs: ["mylib.cpp"],
1237 shared_libs: ["libstub"],
1238 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001239 `,
1240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1241 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1242 variables.Platform_sdk_final = proptools.BoolPtr(false)
1243 variables.Platform_version_active_codenames = []string{"Z"}
1244 }),
1245 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001246
Jiyong Park55549df2021-02-26 23:57:23 +09001247 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001248 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001249 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001250 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001251 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001252
1253 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1254 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1255 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1256 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1257 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1258}
1259
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001260func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001261 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001262 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001263 name: "myapex2",
1264 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001265 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001266 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001267 }
1268
1269 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001270 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001271 public_key: "testkey.avbpubkey",
1272 private_key: "testkey.pem",
1273 }
1274
1275 cc_library {
1276 name: "mylib",
1277 srcs: ["mylib.cpp"],
1278 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001279 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280 system_shared_libs: [],
1281 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001282 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001283 }
1284
1285 cc_library {
1286 name: "libfoo",
1287 srcs: ["mylib.cpp"],
1288 shared_libs: ["libbar"],
1289 system_shared_libs: [],
1290 stl: "none",
1291 stubs: {
1292 versions: ["10", "20", "30"],
1293 },
1294 }
1295
1296 cc_library {
1297 name: "libbar",
1298 srcs: ["mylib.cpp"],
1299 system_shared_libs: [],
1300 stl: "none",
1301 }
1302
Jiyong Park678c8812020-02-07 17:25:49 +09001303 cc_library_static {
1304 name: "libbaz",
1305 srcs: ["mylib.cpp"],
1306 system_shared_libs: [],
1307 stl: "none",
1308 apex_available: [ "myapex2" ],
1309 }
1310
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 `)
1312
Jiyong Park83dc74b2020-01-14 18:38:44 +09001313 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 copyCmds := apexRule.Args["copy_commands"]
1315
1316 // Ensure that direct non-stubs dep is always included
1317 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1318
1319 // Ensure that indirect stubs dep is not included
1320 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1321
1322 // Ensure that dependency of stubs is not included
1323 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1324
Colin Crossaede88c2020-08-11 12:17:01 -07001325 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001326
1327 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001328 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001330 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001331
Jiyong Park3ff16992019-12-27 14:11:47 +09001332 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001333
1334 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1335 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001336
Artur Satayeva8bd1132020-04-27 18:07:06 +01001337 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001338 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001339
Artur Satayeva8bd1132020-04-27 18:07:06 +01001340 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001341 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001342}
1343
Jooyung Hand3639552019-08-09 12:57:43 +09001344func TestApexWithRuntimeLibsDependency(t *testing.T) {
1345 /*
1346 myapex
1347 |
1348 v (runtime_libs)
1349 mylib ------+------> libfoo [provides stub]
1350 |
1351 `------> libbar
1352 */
Colin Cross1c460562021-02-16 17:55:47 -08001353 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001358 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366
1367 cc_library {
1368 name: "mylib",
1369 srcs: ["mylib.cpp"],
1370 runtime_libs: ["libfoo", "libbar"],
1371 system_shared_libs: [],
1372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001373 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001374 }
1375
1376 cc_library {
1377 name: "libfoo",
1378 srcs: ["mylib.cpp"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 stubs: {
1382 versions: ["10", "20", "30"],
1383 },
1384 }
1385
1386 cc_library {
1387 name: "libbar",
1388 srcs: ["mylib.cpp"],
1389 system_shared_libs: [],
1390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001391 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001392 }
1393
1394 `)
1395
Sundong Ahnabb64432019-10-22 13:58:29 +09001396 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001397 copyCmds := apexRule.Args["copy_commands"]
1398
1399 // Ensure that direct non-stubs dep is always included
1400 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1401
1402 // Ensure that indirect stubs dep is not included
1403 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1404
1405 // Ensure that runtime_libs dep in included
1406 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1407
Sundong Ahnabb64432019-10-22 13:58:29 +09001408 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001409 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1410 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001411
1412}
1413
Paul Duffina02cae32021-03-09 01:44:06 +00001414var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1415 cc.PrepareForTestWithCcBuildComponents,
1416 PrepareForTestWithApexBuildComponents,
1417 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001418 apex {
1419 name: "com.android.runtime",
1420 key: "com.android.runtime.key",
1421 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001422 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001423 }
1424
1425 apex_key {
1426 name: "com.android.runtime.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
Paul Duffina02cae32021-03-09 01:44:06 +00001430 `),
1431 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1432)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001433
Paul Duffina02cae32021-03-09 01:44:06 +00001434func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001435 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436 cc_library {
1437 name: "libc",
1438 no_libcrt: true,
1439 nocrt: true,
1440 stl: "none",
1441 system_shared_libs: [],
1442 stubs: { versions: ["1"] },
1443 apex_available: ["com.android.runtime"],
1444
1445 sanitize: {
1446 hwaddress: true,
1447 }
1448 }
1449
1450 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001451 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001452 no_libcrt: true,
1453 nocrt: true,
1454 stl: "none",
1455 system_shared_libs: [],
1456 srcs: [""],
1457 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001458 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 sanitize: {
1461 never: true,
1462 },
Paul Duffina02cae32021-03-09 01:44:06 +00001463 } `)
1464 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001465
1466 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1467 "lib64/bionic/libc.so",
1468 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1469 })
1470
Colin Cross4c4c1be2022-02-10 11:41:18 -08001471 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001472
1473 installed := hwasan.Description("install libclang_rt.hwasan")
1474 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1475
1476 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1477 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1478 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1479}
1480
1481func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001482 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001483 prepareForTestOfRuntimeApexWithHwasan,
1484 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1485 variables.SanitizeDevice = []string{"hwaddress"}
1486 }),
1487 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001488 cc_library {
1489 name: "libc",
1490 no_libcrt: true,
1491 nocrt: true,
1492 stl: "none",
1493 system_shared_libs: [],
1494 stubs: { versions: ["1"] },
1495 apex_available: ["com.android.runtime"],
1496 }
1497
1498 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001499 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001500 no_libcrt: true,
1501 nocrt: true,
1502 stl: "none",
1503 system_shared_libs: [],
1504 srcs: [""],
1505 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001506 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507
1508 sanitize: {
1509 never: true,
1510 },
1511 }
Paul Duffina02cae32021-03-09 01:44:06 +00001512 `)
1513 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001514
1515 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1516 "lib64/bionic/libc.so",
1517 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1518 })
1519
Colin Cross4c4c1be2022-02-10 11:41:18 -08001520 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001521
1522 installed := hwasan.Description("install libclang_rt.hwasan")
1523 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1524
1525 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1526 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1527 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1528}
1529
Jooyung Han61b66e92020-03-21 14:21:46 +00001530func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1531 testcases := []struct {
1532 name string
1533 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001534 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001535 shouldLink string
1536 shouldNotLink []string
1537 }{
1538 {
Jiyong Park55549df2021-02-26 23:57:23 +09001539 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001540 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001541 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001542 shouldLink: "current",
1543 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 },
1545 {
Jiyong Park55549df2021-02-26 23:57:23 +09001546 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001547 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001548 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001549 shouldLink: "current",
1550 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001551 },
1552 }
1553 for _, tc := range testcases {
1554 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001555 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 apex {
1557 name: "myapex",
1558 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001559 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001560 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001561 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001563
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001569
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 cc_library {
1571 name: "mylib",
1572 srcs: ["mylib.cpp"],
1573 vendor_available: true,
1574 shared_libs: ["libbar"],
1575 system_shared_libs: [],
1576 stl: "none",
1577 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001578 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001579 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001580
Jooyung Han61b66e92020-03-21 14:21:46 +00001581 cc_library {
1582 name: "libbar",
1583 srcs: ["mylib.cpp"],
1584 system_shared_libs: [],
1585 stl: "none",
1586 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001587 llndk: {
1588 symbol_file: "libbar.map.txt",
1589 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001590 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001591 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001592 withUnbundledBuild,
1593 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001594
Jooyung Han61b66e92020-03-21 14:21:46 +00001595 // Ensure that LLNDK dep is not included
1596 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1597 "lib64/mylib.so",
1598 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001599
Jooyung Han61b66e92020-03-21 14:21:46 +00001600 // Ensure that LLNDK dep is required
1601 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1602 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1603 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001604
Steven Moreland2c4000c2021-04-27 02:08:49 +00001605 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1606 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001607 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001608 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001609 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001610
Steven Moreland2c4000c2021-04-27 02:08:49 +00001611 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001612 ver := tc.shouldLink
1613 if tc.shouldLink == "current" {
1614 ver = strconv.Itoa(android.FutureApiLevelInt)
1615 }
1616 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001617 })
1618 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001619}
1620
Jiyong Park25fc6a92018-11-18 18:02:45 +09001621func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001622 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623 apex {
1624 name: "myapex",
1625 key: "myapex.key",
1626 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001627 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_library {
1637 name: "mylib",
1638 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001639 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001640 shared_libs: ["libdl#27"],
1641 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001642 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 }
1644
1645 cc_library_shared {
1646 name: "mylib_shared",
1647 srcs: ["mylib.cpp"],
1648 shared_libs: ["libdl#27"],
1649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001650 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 }
1652
1653 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001654 name: "libBootstrap",
1655 srcs: ["mylib.cpp"],
1656 stl: "none",
1657 bootstrap: true,
1658 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001659 `)
1660
Sundong Ahnabb64432019-10-22 13:58:29 +09001661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001662 copyCmds := apexRule.Args["copy_commands"]
1663
1664 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001666 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1667 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001668
1669 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001670 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001671
Colin Crossaede88c2020-08-11 12:17:01 -07001672 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1673 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1674 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001675
1676 // For dependency to libc
1677 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001678 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001679 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001680 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001681 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001682 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1683 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001684
1685 // For dependency to libm
1686 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001687 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001688 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001689 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001690 // ... and is not compiling with the stub
1691 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1692 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1693
1694 // For dependency to libdl
1695 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001696 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001697 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001698 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1699 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001700 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001701 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001702 // ... Cflags from stub is correctly exported to mylib
1703 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1704 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001705
1706 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001707 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1708 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1709 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1710 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001711}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001712
Jooyung Han749dc692020-04-15 11:03:39 +09001713func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001714 // there are three links between liba --> libz.
1715 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001716 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001717 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001718 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001719 apex {
1720 name: "myapex",
1721 key: "myapex.key",
1722 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001723 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001724 }
1725
1726 apex {
1727 name: "otherapex",
1728 key: "myapex.key",
1729 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001730 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
1739 cc_library {
1740 name: "libx",
1741 shared_libs: ["liba"],
1742 system_shared_libs: [],
1743 stl: "none",
1744 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001745 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001746 }
1747
1748 cc_library {
1749 name: "liby",
1750 shared_libs: ["liba"],
1751 system_shared_libs: [],
1752 stl: "none",
1753 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001754 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001755 }
1756
1757 cc_library {
1758 name: "liba",
1759 shared_libs: ["libz"],
1760 system_shared_libs: [],
1761 stl: "none",
1762 apex_available: [
1763 "//apex_available:anyapex",
1764 "//apex_available:platform",
1765 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001766 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001767 }
1768
1769 cc_library {
1770 name: "libz",
1771 system_shared_libs: [],
1772 stl: "none",
1773 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001774 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001775 },
1776 }
Jooyung Han749dc692020-04-15 11:03:39 +09001777 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001778
1779 expectLink := func(from, from_variant, to, to_variant string) {
1780 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1781 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1782 }
1783 expectNoLink := func(from, from_variant, to, to_variant string) {
1784 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1785 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1786 }
1787 // platform liba is linked to non-stub version
1788 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001789 // liba in myapex is linked to current
1790 expectLink("liba", "shared_apex29", "libz", "shared_current")
1791 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001792 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001793 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 // liba in otherapex is linked to current
1795 expectLink("liba", "shared_apex30", "libz", "shared_current")
1796 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001797 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1798 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001802 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libx"],
1807 min_sdk_version: "R",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 cc_library {
1817 name: "libx",
1818 shared_libs: ["libz"],
1819 system_shared_libs: [],
1820 stl: "none",
1821 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001822 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001823 }
1824
1825 cc_library {
1826 name: "libz",
1827 system_shared_libs: [],
1828 stl: "none",
1829 stubs: {
1830 versions: ["29", "R"],
1831 },
1832 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001833 `,
1834 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1835 variables.Platform_version_active_codenames = []string{"R"}
1836 }),
1837 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001838
1839 expectLink := func(from, from_variant, to, to_variant string) {
1840 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1841 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1842 }
1843 expectNoLink := func(from, from_variant, to, to_variant string) {
1844 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1845 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1846 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001847 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1848 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001849 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1850 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001851}
1852
Jooyung Han4c4da062021-06-23 10:23:16 +09001853func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1854 testApex(t, `
1855 apex {
1856 name: "myapex",
1857 key: "myapex.key",
1858 java_libs: ["libx"],
1859 min_sdk_version: "S",
1860 }
1861
1862 apex_key {
1863 name: "myapex.key",
1864 public_key: "testkey.avbpubkey",
1865 private_key: "testkey.pem",
1866 }
1867
1868 java_library {
1869 name: "libx",
1870 srcs: ["a.java"],
1871 apex_available: [ "myapex" ],
1872 sdk_version: "current",
1873 min_sdk_version: "S", // should be okay
1874 }
1875 `,
1876 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1877 variables.Platform_version_active_codenames = []string{"S"}
1878 variables.Platform_sdk_codename = proptools.StringPtr("S")
1879 }),
1880 )
1881}
1882
Jooyung Han749dc692020-04-15 11:03:39 +09001883func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001884 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001885 apex {
1886 name: "myapex",
1887 key: "myapex.key",
1888 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001889 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library {
1899 name: "libx",
1900 shared_libs: ["libz"],
1901 system_shared_libs: [],
1902 stl: "none",
1903 apex_available: [ "myapex" ],
1904 }
1905
1906 cc_library {
1907 name: "libz",
1908 system_shared_libs: [],
1909 stl: "none",
1910 stubs: {
1911 versions: ["1", "2"],
1912 },
1913 }
1914 `)
1915
1916 expectLink := func(from, from_variant, to, to_variant string) {
1917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
1920 expectNoLink := func(from, from_variant, to, to_variant string) {
1921 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1922 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1923 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001924 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001925 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001926 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001927 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001928}
1929
Jooyung Handfc864c2023-03-20 18:19:07 +09001930func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09001931 ctx := testApex(t, `
1932 apex {
1933 name: "myapex",
1934 key: "myapex.key",
1935 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09001936 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001937 vendor: true,
1938 min_sdk_version: "29",
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09001949 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09001950 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001951 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09001952 shared_libs: ["libbar"],
1953 }
1954
1955 cc_library {
1956 name: "libbar",
1957 stubs: { versions: ["29", "30"] },
1958 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09001959 }
1960 `)
1961
1962 vendorVariant := "android_vendor.29_arm64_armv8-a"
1963
Jooyung Handfc864c2023-03-20 18:19:07 +09001964 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_myapex")
1965
1966 // Ensure that mylib links with "current" LLNDK
1967 libFlags := names(mylib.Rule("ld").Args["libFlags"])
1968 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared_current/libbar.so")
1969
1970 // Ensure that mylib is targeting 29
1971 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
1972 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
1973
1974 // Ensure that the correct variant of crtbegin_so is used.
1975 crtBegin := mylib.Rule("ld").Args["crtBegin"]
1976 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09001977
1978 // Ensure that the crtbegin_so used by the APEX is targeting 29
1979 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1980 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1981}
1982
Jooyung Han03b51852020-02-26 22:45:42 +09001983func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001984 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001985 apex {
1986 name: "myapex",
1987 key: "myapex.key",
1988 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001989 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001990 }
1991
1992 apex_key {
1993 name: "myapex.key",
1994 public_key: "testkey.avbpubkey",
1995 private_key: "testkey.pem",
1996 }
1997
1998 cc_library {
1999 name: "libx",
2000 system_shared_libs: [],
2001 stl: "none",
2002 apex_available: [ "myapex" ],
2003 stubs: {
2004 versions: ["1", "2"],
2005 },
2006 }
2007
2008 cc_library {
2009 name: "libz",
2010 shared_libs: ["libx"],
2011 system_shared_libs: [],
2012 stl: "none",
2013 }
2014 `)
2015
2016 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002017 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002018 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2019 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2020 }
2021 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002022 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002023 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2024 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2025 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002026 expectLink("libz", "shared", "libx", "shared_current")
2027 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002028 expectNoLink("libz", "shared", "libz", "shared_1")
2029 expectNoLink("libz", "shared", "libz", "shared")
2030}
2031
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002032var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2033 func(variables android.FixtureProductVariables) {
2034 variables.SanitizeDevice = []string{"hwaddress"}
2035 },
2036)
2037
Jooyung Han75568392020-03-20 04:29:24 +09002038func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002039 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002040 apex {
2041 name: "myapex",
2042 key: "myapex.key",
2043 native_shared_libs: ["libx"],
2044 min_sdk_version: "29",
2045 }
2046
2047 apex_key {
2048 name: "myapex.key",
2049 public_key: "testkey.avbpubkey",
2050 private_key: "testkey.pem",
2051 }
2052
2053 cc_library {
2054 name: "libx",
2055 shared_libs: ["libbar"],
2056 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002057 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002058 }
2059
2060 cc_library {
2061 name: "libbar",
2062 stubs: {
2063 versions: ["29", "30"],
2064 },
2065 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002066 `,
2067 prepareForTestWithSantitizeHwaddress,
2068 )
Jooyung Han03b51852020-02-26 22:45:42 +09002069 expectLink := func(from, from_variant, to, to_variant string) {
2070 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2071 libFlags := ld.Args["libFlags"]
2072 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2073 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002074 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002075}
2076
Jooyung Han75568392020-03-20 04:29:24 +09002077func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002078 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002079 apex {
2080 name: "myapex",
2081 key: "myapex.key",
2082 native_shared_libs: ["libx"],
2083 min_sdk_version: "29",
2084 }
2085
2086 apex_key {
2087 name: "myapex.key",
2088 public_key: "testkey.avbpubkey",
2089 private_key: "testkey.pem",
2090 }
2091
2092 cc_library {
2093 name: "libx",
2094 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002095 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002096 }
Jooyung Han75568392020-03-20 04:29:24 +09002097 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002098
2099 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002100 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002101 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002102 // note that platform variant is not.
2103 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002104 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002105}
2106
Jooyung Han749dc692020-04-15 11:03:39 +09002107func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2108 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002112 native_shared_libs: ["mylib"],
2113 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
Jooyung Han749dc692020-04-15 11:03:39 +09002121
2122 cc_library {
2123 name: "mylib",
2124 srcs: ["mylib.cpp"],
2125 system_shared_libs: [],
2126 stl: "none",
2127 apex_available: [
2128 "myapex",
2129 ],
2130 min_sdk_version: "30",
2131 }
2132 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002133
2134 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo.ffi"],
2139 min_sdk_version: "29",
2140 }
2141
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147
2148 rust_ffi_shared {
2149 name: "libfoo.ffi",
2150 srcs: ["foo.rs"],
2151 crate_name: "foo",
2152 apex_available: [
2153 "myapex",
2154 ],
2155 min_sdk_version: "30",
2156 }
2157 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002158
2159 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2160 apex {
2161 name: "myapex",
2162 key: "myapex.key",
2163 java_libs: ["libfoo"],
2164 min_sdk_version: "29",
2165 }
2166
2167 apex_key {
2168 name: "myapex.key",
2169 public_key: "testkey.avbpubkey",
2170 private_key: "testkey.pem",
2171 }
2172
2173 java_import {
2174 name: "libfoo",
2175 jars: ["libfoo.jar"],
2176 apex_available: [
2177 "myapex",
2178 ],
2179 min_sdk_version: "30",
2180 }
2181 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002182
2183 // Skip check for modules compiling against core API surface
2184 testApex(t, `
2185 apex {
2186 name: "myapex",
2187 key: "myapex.key",
2188 java_libs: ["libfoo"],
2189 min_sdk_version: "29",
2190 }
2191
2192 apex_key {
2193 name: "myapex.key",
2194 public_key: "testkey.avbpubkey",
2195 private_key: "testkey.pem",
2196 }
2197
2198 java_library {
2199 name: "libfoo",
2200 srcs: ["Foo.java"],
2201 apex_available: [
2202 "myapex",
2203 ],
2204 // Compile against core API surface
2205 sdk_version: "core_current",
2206 min_sdk_version: "30",
2207 }
2208 `)
2209
Jooyung Han749dc692020-04-15 11:03:39 +09002210}
2211
2212func TestApexMinSdkVersion_Okay(t *testing.T) {
2213 testApex(t, `
2214 apex {
2215 name: "myapex",
2216 key: "myapex.key",
2217 native_shared_libs: ["libfoo"],
2218 java_libs: ["libbar"],
2219 min_sdk_version: "29",
2220 }
2221
2222 apex_key {
2223 name: "myapex.key",
2224 public_key: "testkey.avbpubkey",
2225 private_key: "testkey.pem",
2226 }
2227
2228 cc_library {
2229 name: "libfoo",
2230 srcs: ["mylib.cpp"],
2231 shared_libs: ["libfoo_dep"],
2232 apex_available: ["myapex"],
2233 min_sdk_version: "29",
2234 }
2235
2236 cc_library {
2237 name: "libfoo_dep",
2238 srcs: ["mylib.cpp"],
2239 apex_available: ["myapex"],
2240 min_sdk_version: "29",
2241 }
2242
2243 java_library {
2244 name: "libbar",
2245 sdk_version: "current",
2246 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002247 static_libs: [
2248 "libbar_dep",
2249 "libbar_import_dep",
2250 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002251 apex_available: ["myapex"],
2252 min_sdk_version: "29",
2253 }
2254
2255 java_library {
2256 name: "libbar_dep",
2257 sdk_version: "current",
2258 srcs: ["a.java"],
2259 apex_available: ["myapex"],
2260 min_sdk_version: "29",
2261 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002262
2263 java_import {
2264 name: "libbar_import_dep",
2265 jars: ["libbar.jar"],
2266 apex_available: ["myapex"],
2267 min_sdk_version: "29",
2268 }
Jooyung Han03b51852020-02-26 22:45:42 +09002269 `)
2270}
2271
Colin Cross8ca61c12022-10-06 21:00:14 -07002272func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2273 // Tests that an apex dependency with min_sdk_version higher than the
2274 // min_sdk_version of the apex is allowed as long as the dependency's
2275 // min_sdk_version is less than or equal to the api level that the
2276 // architecture was introduced in. In this case, arm64 didn't exist
2277 // until api level 21, so the arm64 code will never need to run on
2278 // an api level 20 device, even if other architectures of the apex
2279 // will.
2280 testApex(t, `
2281 apex {
2282 name: "myapex",
2283 key: "myapex.key",
2284 native_shared_libs: ["libfoo"],
2285 min_sdk_version: "20",
2286 }
2287
2288 apex_key {
2289 name: "myapex.key",
2290 public_key: "testkey.avbpubkey",
2291 private_key: "testkey.pem",
2292 }
2293
2294 cc_library {
2295 name: "libfoo",
2296 srcs: ["mylib.cpp"],
2297 apex_available: ["myapex"],
2298 min_sdk_version: "21",
2299 stl: "none",
2300 }
2301 `)
2302}
2303
Artur Satayev8cf899a2020-04-15 17:29:42 +01002304func TestJavaStableSdkVersion(t *testing.T) {
2305 testCases := []struct {
2306 name string
2307 expectedError string
2308 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002309 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002310 }{
2311 {
2312 name: "Non-updatable apex with non-stable dep",
2313 bp: `
2314 apex {
2315 name: "myapex",
2316 java_libs: ["myjar"],
2317 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002318 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002319 }
2320 apex_key {
2321 name: "myapex.key",
2322 public_key: "testkey.avbpubkey",
2323 private_key: "testkey.pem",
2324 }
2325 java_library {
2326 name: "myjar",
2327 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002328 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002329 apex_available: ["myapex"],
2330 }
2331 `,
2332 },
2333 {
2334 name: "Updatable apex with stable dep",
2335 bp: `
2336 apex {
2337 name: "myapex",
2338 java_libs: ["myjar"],
2339 key: "myapex.key",
2340 updatable: true,
2341 min_sdk_version: "29",
2342 }
2343 apex_key {
2344 name: "myapex.key",
2345 public_key: "testkey.avbpubkey",
2346 private_key: "testkey.pem",
2347 }
2348 java_library {
2349 name: "myjar",
2350 srcs: ["foo/bar/MyClass.java"],
2351 sdk_version: "current",
2352 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002353 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002354 }
2355 `,
2356 },
2357 {
2358 name: "Updatable apex with non-stable dep",
2359 expectedError: "cannot depend on \"myjar\"",
2360 bp: `
2361 apex {
2362 name: "myapex",
2363 java_libs: ["myjar"],
2364 key: "myapex.key",
2365 updatable: true,
2366 }
2367 apex_key {
2368 name: "myapex.key",
2369 public_key: "testkey.avbpubkey",
2370 private_key: "testkey.pem",
2371 }
2372 java_library {
2373 name: "myjar",
2374 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002375 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002376 apex_available: ["myapex"],
2377 }
2378 `,
2379 },
2380 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002381 name: "Updatable apex with non-stable legacy core platform dep",
2382 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2383 bp: `
2384 apex {
2385 name: "myapex",
2386 java_libs: ["myjar-uses-legacy"],
2387 key: "myapex.key",
2388 updatable: true,
2389 }
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395 java_library {
2396 name: "myjar-uses-legacy",
2397 srcs: ["foo/bar/MyClass.java"],
2398 sdk_version: "core_platform",
2399 apex_available: ["myapex"],
2400 }
2401 `,
2402 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2403 },
2404 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002405 name: "Updatable apex with non-stable transitive dep",
2406 // This is not actually detecting that the transitive dependency is unstable, rather it is
2407 // detecting that the transitive dependency is building against a wider API surface than the
2408 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002409 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002410 bp: `
2411 apex {
2412 name: "myapex",
2413 java_libs: ["myjar"],
2414 key: "myapex.key",
2415 updatable: true,
2416 }
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 java_library {
2423 name: "myjar",
2424 srcs: ["foo/bar/MyClass.java"],
2425 sdk_version: "current",
2426 apex_available: ["myapex"],
2427 static_libs: ["transitive-jar"],
2428 }
2429 java_library {
2430 name: "transitive-jar",
2431 srcs: ["foo/bar/MyClass.java"],
2432 sdk_version: "core_platform",
2433 apex_available: ["myapex"],
2434 }
2435 `,
2436 },
2437 }
2438
2439 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002440 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2441 continue
2442 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002443 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002444 errorHandler := android.FixtureExpectsNoErrors
2445 if test.expectedError != "" {
2446 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002447 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002448 android.GroupFixturePreparers(
2449 java.PrepareForTestWithJavaDefaultModules,
2450 PrepareForTestWithApexBuildComponents,
2451 prepareForTestWithMyapex,
2452 android.OptionalFixturePreparer(test.preparer),
2453 ).
2454 ExtendWithErrorHandler(errorHandler).
2455 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002456 })
2457 }
2458}
2459
Jooyung Han749dc692020-04-15 11:03:39 +09002460func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2461 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2462 apex {
2463 name: "myapex",
2464 key: "myapex.key",
2465 native_shared_libs: ["mylib"],
2466 min_sdk_version: "29",
2467 }
2468
2469 apex_key {
2470 name: "myapex.key",
2471 public_key: "testkey.avbpubkey",
2472 private_key: "testkey.pem",
2473 }
2474
2475 cc_library {
2476 name: "mylib",
2477 srcs: ["mylib.cpp"],
2478 shared_libs: ["mylib2"],
2479 system_shared_libs: [],
2480 stl: "none",
2481 apex_available: [
2482 "myapex",
2483 ],
2484 min_sdk_version: "29",
2485 }
2486
2487 // indirect part of the apex
2488 cc_library {
2489 name: "mylib2",
2490 srcs: ["mylib.cpp"],
2491 system_shared_libs: [],
2492 stl: "none",
2493 apex_available: [
2494 "myapex",
2495 ],
2496 min_sdk_version: "30",
2497 }
2498 `)
2499}
2500
2501func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2502 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2503 apex {
2504 name: "myapex",
2505 key: "myapex.key",
2506 apps: ["AppFoo"],
2507 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002508 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002509 }
2510
2511 apex_key {
2512 name: "myapex.key",
2513 public_key: "testkey.avbpubkey",
2514 private_key: "testkey.pem",
2515 }
2516
2517 android_app {
2518 name: "AppFoo",
2519 srcs: ["foo/bar/MyClass.java"],
2520 sdk_version: "current",
2521 min_sdk_version: "29",
2522 system_modules: "none",
2523 stl: "none",
2524 static_libs: ["bar"],
2525 apex_available: [ "myapex" ],
2526 }
2527
2528 java_library {
2529 name: "bar",
2530 sdk_version: "current",
2531 srcs: ["a.java"],
2532 apex_available: [ "myapex" ],
2533 }
2534 `)
2535}
2536
2537func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002538 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002539 apex {
2540 name: "myapex",
2541 key: "myapex.key",
2542 native_shared_libs: ["mylib"],
2543 min_sdk_version: "29",
2544 }
2545
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
2551
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002552 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002553 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2554 cc_library {
2555 name: "mylib",
2556 srcs: ["mylib.cpp"],
2557 shared_libs: ["mylib2"],
2558 system_shared_libs: [],
2559 stl: "none",
2560 apex_available: ["myapex", "otherapex"],
2561 min_sdk_version: "29",
2562 }
2563
2564 cc_library {
2565 name: "mylib2",
2566 srcs: ["mylib.cpp"],
2567 system_shared_libs: [],
2568 stl: "none",
2569 apex_available: ["otherapex"],
2570 stubs: { versions: ["29", "30"] },
2571 min_sdk_version: "30",
2572 }
2573
2574 apex {
2575 name: "otherapex",
2576 key: "myapex.key",
2577 native_shared_libs: ["mylib", "mylib2"],
2578 min_sdk_version: "30",
2579 }
2580 `)
2581 expectLink := func(from, from_variant, to, to_variant string) {
2582 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2583 libFlags := ld.Args["libFlags"]
2584 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2585 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002586 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002587 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002588}
2589
Jooyung Haned124c32021-01-26 11:43:46 +09002590func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002591 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2592 func(variables android.FixtureProductVariables) {
2593 variables.Platform_sdk_codename = proptools.StringPtr("S")
2594 variables.Platform_version_active_codenames = []string{"S"}
2595 },
2596 )
Jooyung Haned124c32021-01-26 11:43:46 +09002597 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2598 apex {
2599 name: "myapex",
2600 key: "myapex.key",
2601 native_shared_libs: ["libfoo"],
2602 min_sdk_version: "S",
2603 }
2604 apex_key {
2605 name: "myapex.key",
2606 public_key: "testkey.avbpubkey",
2607 private_key: "testkey.pem",
2608 }
2609 cc_library {
2610 name: "libfoo",
2611 shared_libs: ["libbar"],
2612 apex_available: ["myapex"],
2613 min_sdk_version: "29",
2614 }
2615 cc_library {
2616 name: "libbar",
2617 apex_available: ["myapex"],
2618 }
2619 `, withSAsActiveCodeNames)
2620}
2621
2622func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002623 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2624 variables.Platform_sdk_codename = proptools.StringPtr("S")
2625 variables.Platform_version_active_codenames = []string{"S", "T"}
2626 })
Colin Cross1c460562021-02-16 17:55:47 -08002627 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002628 apex {
2629 name: "myapex",
2630 key: "myapex.key",
2631 native_shared_libs: ["libfoo"],
2632 min_sdk_version: "S",
2633 }
2634 apex_key {
2635 name: "myapex.key",
2636 public_key: "testkey.avbpubkey",
2637 private_key: "testkey.pem",
2638 }
2639 cc_library {
2640 name: "libfoo",
2641 shared_libs: ["libbar"],
2642 apex_available: ["myapex"],
2643 min_sdk_version: "S",
2644 }
2645 cc_library {
2646 name: "libbar",
2647 stubs: {
2648 symbol_file: "libbar.map.txt",
2649 versions: ["30", "S", "T"],
2650 },
2651 }
2652 `, withSAsActiveCodeNames)
2653
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002654 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002655 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2656 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002657 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002658}
2659
Jiyong Park7c2ee712018-12-07 00:42:25 +09002660func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002661 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002662 apex {
2663 name: "myapex",
2664 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002665 native_shared_libs: ["mylib"],
2666 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002667 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002668 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002669 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002670 }
2671
2672 apex_key {
2673 name: "myapex.key",
2674 public_key: "testkey.avbpubkey",
2675 private_key: "testkey.pem",
2676 }
2677
2678 prebuilt_etc {
2679 name: "myetc",
2680 src: "myprebuilt",
2681 sub_dir: "foo/bar",
2682 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002683
2684 cc_library {
2685 name: "mylib",
2686 srcs: ["mylib.cpp"],
2687 relative_install_path: "foo/bar",
2688 system_shared_libs: [],
2689 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002690 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002691 }
2692
2693 cc_binary {
2694 name: "mybin",
2695 srcs: ["mylib.cpp"],
2696 relative_install_path: "foo/bar",
2697 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002698 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002699 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002700 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002701 `)
2702
Sundong Ahnabb64432019-10-22 13:58:29 +09002703 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002704 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002705
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002706 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002707 ensureContains(t, cmd, "/etc ")
2708 ensureContains(t, cmd, "/etc/foo ")
2709 ensureContains(t, cmd, "/etc/foo/bar ")
2710 ensureContains(t, cmd, "/lib64 ")
2711 ensureContains(t, cmd, "/lib64/foo ")
2712 ensureContains(t, cmd, "/lib64/foo/bar ")
2713 ensureContains(t, cmd, "/lib ")
2714 ensureContains(t, cmd, "/lib/foo ")
2715 ensureContains(t, cmd, "/lib/foo/bar ")
2716 ensureContains(t, cmd, "/bin ")
2717 ensureContains(t, cmd, "/bin/foo ")
2718 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002719}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002720
Jooyung Han35155c42020-02-06 17:33:20 +09002721func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002722 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 multilib: {
2727 both: {
2728 native_shared_libs: ["mylib"],
2729 binaries: ["mybin"],
2730 },
2731 },
2732 compile_multilib: "both",
2733 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002734 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002735 }
2736
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742
2743 cc_library {
2744 name: "mylib",
2745 relative_install_path: "foo/bar",
2746 system_shared_libs: [],
2747 stl: "none",
2748 apex_available: [ "myapex" ],
2749 native_bridge_supported: true,
2750 }
2751
2752 cc_binary {
2753 name: "mybin",
2754 relative_install_path: "foo/bar",
2755 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002756 stl: "none",
2757 apex_available: [ "myapex" ],
2758 native_bridge_supported: true,
2759 compile_multilib: "both", // default is "first" for binary
2760 multilib: {
2761 lib64: {
2762 suffix: "64",
2763 },
2764 },
2765 }
2766 `, withNativeBridgeEnabled)
2767 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2768 "bin/foo/bar/mybin",
2769 "bin/foo/bar/mybin64",
2770 "bin/arm/foo/bar/mybin",
2771 "bin/arm64/foo/bar/mybin64",
2772 "lib/foo/bar/mylib.so",
2773 "lib/arm/foo/bar/mylib.so",
2774 "lib64/foo/bar/mylib.so",
2775 "lib64/arm64/foo/bar/mylib.so",
2776 })
2777}
2778
Jooyung Han85d61762020-06-24 23:50:26 +09002779func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002780 result := android.GroupFixturePreparers(
2781 prepareForApexTest,
2782 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2783 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002784 apex {
2785 name: "myapex",
2786 key: "myapex.key",
2787 binaries: ["mybin"],
2788 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002789 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002790 }
2791 apex_key {
2792 name: "myapex.key",
2793 public_key: "testkey.avbpubkey",
2794 private_key: "testkey.pem",
2795 }
2796 cc_binary {
2797 name: "mybin",
2798 vendor: true,
2799 shared_libs: ["libfoo"],
2800 }
2801 cc_library {
2802 name: "libfoo",
2803 proprietary: true,
2804 }
2805 `)
2806
Colin Crossc68db4b2021-11-11 18:59:15 -08002807 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002808 "bin/mybin",
2809 "lib64/libfoo.so",
2810 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2811 "lib64/libc++.so",
2812 })
2813
Colin Crossc68db4b2021-11-11 18:59:15 -08002814 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2815 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002816 name := apexBundle.BaseModuleName()
2817 prefix := "TARGET_"
2818 var builder strings.Builder
2819 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002820 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002821 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002822 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002823
Colin Crossc68db4b2021-11-11 18:59:15 -08002824 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002825 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2826 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002827}
2828
Jooyung Hanc5a96762022-02-04 11:54:50 +09002829func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2830 testApexError(t, `Trying to include a VNDK library`, `
2831 apex {
2832 name: "myapex",
2833 key: "myapex.key",
2834 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2835 vendor: true,
2836 use_vndk_as_stable: true,
2837 updatable: false,
2838 }
2839 apex_key {
2840 name: "myapex.key",
2841 public_key: "testkey.avbpubkey",
2842 private_key: "testkey.pem",
2843 }`)
2844}
2845
Jooyung Handf78e212020-07-22 15:54:47 +09002846func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002847 // myapex myapex2
2848 // | |
2849 // mybin ------. mybin2
2850 // \ \ / |
2851 // (stable) .---\--------` |
2852 // \ / \ |
2853 // \ / \ /
2854 // libvndk libvendor
2855 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002856 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002857 apex {
2858 name: "myapex",
2859 key: "myapex.key",
2860 binaries: ["mybin"],
2861 vendor: true,
2862 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002863 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002864 }
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870 cc_binary {
2871 name: "mybin",
2872 vendor: true,
2873 shared_libs: ["libvndk", "libvendor"],
2874 }
2875 cc_library {
2876 name: "libvndk",
2877 vndk: {
2878 enabled: true,
2879 },
2880 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002881 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002882 }
2883 cc_library {
2884 name: "libvendor",
2885 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002886 stl: "none",
2887 }
2888 apex {
2889 name: "myapex2",
2890 key: "myapex.key",
2891 binaries: ["mybin2"],
2892 vendor: true,
2893 use_vndk_as_stable: false,
2894 updatable: false,
2895 }
2896 cc_binary {
2897 name: "mybin2",
2898 vendor: true,
2899 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002900 }
2901 `)
2902
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002903 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002904
Jooyung Han91f92032022-02-04 12:36:33 +09002905 for _, tc := range []struct {
2906 name string
2907 apexName string
2908 moduleName string
2909 moduleVariant string
2910 libs []string
2911 contents []string
2912 requireVndkNamespace bool
2913 }{
2914 {
2915 name: "use_vndk_as_stable",
2916 apexName: "myapex",
2917 moduleName: "mybin",
2918 moduleVariant: vendorVariant + "_apex10000",
2919 libs: []string{
2920 // should link with vendor variants of VNDK libs(libvndk/libc++)
2921 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2922 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2923 // unstable Vendor libs as APEX variant
2924 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2925 },
2926 contents: []string{
2927 "bin/mybin",
2928 "lib64/libvendor.so",
2929 // VNDK libs (libvndk/libc++) are not included
2930 },
2931 requireVndkNamespace: true,
2932 },
2933 {
2934 name: "!use_vndk_as_stable",
2935 apexName: "myapex2",
2936 moduleName: "mybin2",
2937 moduleVariant: vendorVariant + "_myapex2",
2938 libs: []string{
2939 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2940 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2941 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2942 // unstable vendor libs have "merged" APEX variants
2943 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2944 },
2945 contents: []string{
2946 "bin/mybin2",
2947 "lib64/libvendor.so",
2948 // VNDK libs are included as well
2949 "lib64/libvndk.so",
2950 "lib64/libc++.so",
2951 },
2952 requireVndkNamespace: false,
2953 },
2954 } {
2955 t.Run(tc.name, func(t *testing.T) {
2956 // Check linked libs
2957 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2958 libs := names(ldRule.Args["libFlags"])
2959 for _, lib := range tc.libs {
2960 ensureListContains(t, libs, lib)
2961 }
2962 // Check apex contents
2963 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002964
Jooyung Han91f92032022-02-04 12:36:33 +09002965 // Check "requireNativeLibs"
2966 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2967 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2968 if tc.requireVndkNamespace {
2969 ensureListContains(t, requireNativeLibs, ":vndk")
2970 } else {
2971 ensureListNotContains(t, requireNativeLibs, ":vndk")
2972 }
2973 })
2974 }
Jooyung Handf78e212020-07-22 15:54:47 +09002975}
2976
Justin Yun13decfb2021-03-08 19:25:55 +09002977func TestProductVariant(t *testing.T) {
2978 ctx := testApex(t, `
2979 apex {
2980 name: "myapex",
2981 key: "myapex.key",
2982 updatable: false,
2983 product_specific: true,
2984 binaries: ["foo"],
2985 }
2986
2987 apex_key {
2988 name: "myapex.key",
2989 public_key: "testkey.avbpubkey",
2990 private_key: "testkey.pem",
2991 }
2992
2993 cc_binary {
2994 name: "foo",
2995 product_available: true,
2996 apex_available: ["myapex"],
2997 srcs: ["foo.cpp"],
2998 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002999 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3000 variables.ProductVndkVersion = proptools.StringPtr("current")
3001 }),
3002 )
Justin Yun13decfb2021-03-08 19:25:55 +09003003
3004 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09003005 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003006 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3007 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3008 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3009 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3010}
3011
Jooyung Han8e5685d2020-09-21 11:02:57 +09003012func TestApex_withPrebuiltFirmware(t *testing.T) {
3013 testCases := []struct {
3014 name string
3015 additionalProp string
3016 }{
3017 {"system apex with prebuilt_firmware", ""},
3018 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3019 }
3020 for _, tc := range testCases {
3021 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003022 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003023 apex {
3024 name: "myapex",
3025 key: "myapex.key",
3026 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003027 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003028 `+tc.additionalProp+`
3029 }
3030 apex_key {
3031 name: "myapex.key",
3032 public_key: "testkey.avbpubkey",
3033 private_key: "testkey.pem",
3034 }
3035 prebuilt_firmware {
3036 name: "myfirmware",
3037 src: "myfirmware.bin",
3038 filename_from_src: true,
3039 `+tc.additionalProp+`
3040 }
3041 `)
3042 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3043 "etc/firmware/myfirmware.bin",
3044 })
3045 })
3046 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003047}
3048
Jooyung Hanefb184e2020-06-25 17:14:25 +09003049func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003050 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003051 apex {
3052 name: "myapex",
3053 key: "myapex.key",
3054 vendor: true,
3055 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003056 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003057 }
3058
3059 apex_key {
3060 name: "myapex.key",
3061 public_key: "testkey.avbpubkey",
3062 private_key: "testkey.pem",
3063 }
3064
3065 cc_library {
3066 name: "mylib",
3067 vendor_available: true,
3068 }
3069 `)
3070
3071 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003072 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003073 name := apexBundle.BaseModuleName()
3074 prefix := "TARGET_"
3075 var builder strings.Builder
3076 data.Custom(&builder, name, prefix, "", data)
3077 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00003078 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003079}
3080
Jooyung Han2ed99d02020-06-24 23:26:26 +09003081func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003082 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003083 apex {
3084 name: "myapex",
3085 key: "myapex.key",
3086 vintf_fragments: ["fragment.xml"],
3087 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003088 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003089 }
3090 apex_key {
3091 name: "myapex.key",
3092 public_key: "testkey.avbpubkey",
3093 private_key: "testkey.pem",
3094 }
3095 cc_binary {
3096 name: "mybin",
3097 }
3098 `)
3099
3100 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003101 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003102 name := apexBundle.BaseModuleName()
3103 prefix := "TARGET_"
3104 var builder strings.Builder
3105 data.Custom(&builder, name, prefix, "", data)
3106 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003107 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003108 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003109}
3110
Jiyong Park16e91a02018-12-20 18:18:08 +09003111func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003112 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003113 apex {
3114 name: "myapex",
3115 key: "myapex.key",
3116 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003117 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003118 }
3119
3120 apex_key {
3121 name: "myapex.key",
3122 public_key: "testkey.avbpubkey",
3123 private_key: "testkey.pem",
3124 }
3125
3126 cc_library {
3127 name: "mylib",
3128 srcs: ["mylib.cpp"],
3129 system_shared_libs: [],
3130 stl: "none",
3131 stubs: {
3132 versions: ["1", "2", "3"],
3133 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003134 apex_available: [
3135 "//apex_available:platform",
3136 "myapex",
3137 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003138 }
3139
3140 cc_binary {
3141 name: "not_in_apex",
3142 srcs: ["mylib.cpp"],
3143 static_libs: ["mylib"],
3144 static_executable: true,
3145 system_shared_libs: [],
3146 stl: "none",
3147 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003148 `)
3149
Colin Cross7113d202019-11-20 16:39:12 -08003150 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003151
3152 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003153 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003154}
Jiyong Park9335a262018-12-24 11:31:58 +09003155
3156func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003157 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003158 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003159 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003160 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003161 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003162 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003163 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003164 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003165 }
3166
3167 cc_library {
3168 name: "mylib",
3169 srcs: ["mylib.cpp"],
3170 system_shared_libs: [],
3171 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003172 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003173 }
3174
3175 apex_key {
3176 name: "myapex.key",
3177 public_key: "testkey.avbpubkey",
3178 private_key: "testkey.pem",
3179 }
3180
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003181 android_app_certificate {
3182 name: "myapex.certificate",
3183 certificate: "testkey",
3184 }
3185
3186 android_app_certificate {
3187 name: "myapex.certificate.override",
3188 certificate: "testkey.override",
3189 }
3190
Jiyong Park9335a262018-12-24 11:31:58 +09003191 `)
3192
3193 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003194 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003195
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003196 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3197 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003198 "vendor/foo/devkeys/testkey.avbpubkey")
3199 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003200 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3201 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003202 "vendor/foo/devkeys/testkey.pem")
3203 }
3204
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003205 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003206 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003207 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003208 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003209 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003210 }
3211}
Jiyong Park58e364a2019-01-19 19:24:06 +09003212
Jooyung Hanf121a652019-12-17 14:30:11 +09003213func TestCertificate(t *testing.T) {
3214 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003215 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003216 apex {
3217 name: "myapex",
3218 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003219 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003220 }
3221 apex_key {
3222 name: "myapex.key",
3223 public_key: "testkey.avbpubkey",
3224 private_key: "testkey.pem",
3225 }`)
3226 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3227 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3228 if actual := rule.Args["certificates"]; actual != expected {
3229 t.Errorf("certificates should be %q, not %q", expected, actual)
3230 }
3231 })
3232 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003233 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003234 apex {
3235 name: "myapex_keytest",
3236 key: "myapex.key",
3237 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003238 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003239 }
3240 apex_key {
3241 name: "myapex.key",
3242 public_key: "testkey.avbpubkey",
3243 private_key: "testkey.pem",
3244 }
3245 android_app_certificate {
3246 name: "myapex.certificate.override",
3247 certificate: "testkey.override",
3248 }`)
3249 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3250 expected := "testkey.override.x509.pem testkey.override.pk8"
3251 if actual := rule.Args["certificates"]; actual != expected {
3252 t.Errorf("certificates should be %q, not %q", expected, actual)
3253 }
3254 })
3255 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003256 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003257 apex {
3258 name: "myapex",
3259 key: "myapex.key",
3260 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003261 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003262 }
3263 apex_key {
3264 name: "myapex.key",
3265 public_key: "testkey.avbpubkey",
3266 private_key: "testkey.pem",
3267 }
3268 android_app_certificate {
3269 name: "myapex.certificate",
3270 certificate: "testkey",
3271 }`)
3272 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3273 expected := "testkey.x509.pem testkey.pk8"
3274 if actual := rule.Args["certificates"]; actual != expected {
3275 t.Errorf("certificates should be %q, not %q", expected, actual)
3276 }
3277 })
3278 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003279 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003280 apex {
3281 name: "myapex_keytest",
3282 key: "myapex.key",
3283 file_contexts: ":myapex-file_contexts",
3284 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003285 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003286 }
3287 apex_key {
3288 name: "myapex.key",
3289 public_key: "testkey.avbpubkey",
3290 private_key: "testkey.pem",
3291 }
3292 android_app_certificate {
3293 name: "myapex.certificate.override",
3294 certificate: "testkey.override",
3295 }`)
3296 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3297 expected := "testkey.override.x509.pem testkey.override.pk8"
3298 if actual := rule.Args["certificates"]; actual != expected {
3299 t.Errorf("certificates should be %q, not %q", expected, actual)
3300 }
3301 })
3302 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003303 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003304 apex {
3305 name: "myapex",
3306 key: "myapex.key",
3307 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003308 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003309 }
3310 apex_key {
3311 name: "myapex.key",
3312 public_key: "testkey.avbpubkey",
3313 private_key: "testkey.pem",
3314 }`)
3315 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3316 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3317 if actual := rule.Args["certificates"]; actual != expected {
3318 t.Errorf("certificates should be %q, not %q", expected, actual)
3319 }
3320 })
3321 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003322 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003323 apex {
3324 name: "myapex_keytest",
3325 key: "myapex.key",
3326 file_contexts: ":myapex-file_contexts",
3327 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003328 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003329 }
3330 apex_key {
3331 name: "myapex.key",
3332 public_key: "testkey.avbpubkey",
3333 private_key: "testkey.pem",
3334 }
3335 android_app_certificate {
3336 name: "myapex.certificate.override",
3337 certificate: "testkey.override",
3338 }`)
3339 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3340 expected := "testkey.override.x509.pem testkey.override.pk8"
3341 if actual := rule.Args["certificates"]; actual != expected {
3342 t.Errorf("certificates should be %q, not %q", expected, actual)
3343 }
3344 })
3345}
3346
Jiyong Park58e364a2019-01-19 19:24:06 +09003347func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003348 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003349 apex {
3350 name: "myapex",
3351 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003352 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003353 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003354 }
3355
3356 apex {
3357 name: "otherapex",
3358 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003359 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003360 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003361 }
3362
3363 apex_key {
3364 name: "myapex.key",
3365 public_key: "testkey.avbpubkey",
3366 private_key: "testkey.pem",
3367 }
3368
3369 cc_library {
3370 name: "mylib",
3371 srcs: ["mylib.cpp"],
3372 system_shared_libs: [],
3373 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003374 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003375 "myapex",
3376 "otherapex",
3377 ],
Jooyung Han24282772020-03-21 23:20:55 +09003378 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003379 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003380 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003381 cc_library {
3382 name: "mylib2",
3383 srcs: ["mylib.cpp"],
3384 system_shared_libs: [],
3385 stl: "none",
3386 apex_available: [
3387 "myapex",
3388 "otherapex",
3389 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003390 static_libs: ["mylib3"],
3391 recovery_available: true,
3392 min_sdk_version: "29",
3393 }
3394 cc_library {
3395 name: "mylib3",
3396 srcs: ["mylib.cpp"],
3397 system_shared_libs: [],
3398 stl: "none",
3399 apex_available: [
3400 "myapex",
3401 "otherapex",
3402 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003403 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003404 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003405 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003406 `)
3407
Jooyung Hanc87a0592020-03-02 17:44:33 +09003408 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003409 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003410 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003411
Vinh Tranf9754732023-01-19 22:41:46 -05003412 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003413 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003414 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003415
Vinh Tranf9754732023-01-19 22:41:46 -05003416 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003417 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003418 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003419
Colin Crossaede88c2020-08-11 12:17:01 -07003420 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3421 // each variant defines additional macros to distinguish which apex variant it is built for
3422
3423 // non-APEX variant does not have __ANDROID_APEX__ defined
3424 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3425 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3426
Vinh Tranf9754732023-01-19 22:41:46 -05003427 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003428 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3429 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003430
Jooyung Hanc87a0592020-03-02 17:44:33 +09003431 // non-APEX variant does not have __ANDROID_APEX__ defined
3432 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3433 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3434
Vinh Tranf9754732023-01-19 22:41:46 -05003435 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003436 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003437 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003438}
Jiyong Park7e636d02019-01-28 16:16:54 +09003439
3440func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003441 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003442 apex {
3443 name: "myapex",
3444 key: "myapex.key",
3445 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003446 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003447 }
3448
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 cc_library_headers {
3456 name: "mylib_headers",
3457 export_include_dirs: ["my_include"],
3458 system_shared_libs: [],
3459 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003460 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003461 }
3462
3463 cc_library {
3464 name: "mylib",
3465 srcs: ["mylib.cpp"],
3466 system_shared_libs: [],
3467 stl: "none",
3468 header_libs: ["mylib_headers"],
3469 export_header_lib_headers: ["mylib_headers"],
3470 stubs: {
3471 versions: ["1", "2", "3"],
3472 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003473 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003474 }
3475
3476 cc_library {
3477 name: "otherlib",
3478 srcs: ["mylib.cpp"],
3479 system_shared_libs: [],
3480 stl: "none",
3481 shared_libs: ["mylib"],
3482 }
3483 `)
3484
Colin Cross7113d202019-11-20 16:39:12 -08003485 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003486
3487 // Ensure that the include path of the header lib is exported to 'otherlib'
3488 ensureContains(t, cFlags, "-Imy_include")
3489}
Alex Light9670d332019-01-29 18:07:33 -08003490
Jiyong Park7cd10e32020-01-14 09:22:18 +09003491type fileInApex struct {
3492 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003493 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003494 isLink bool
3495}
3496
Jooyung Han1724d582022-12-21 10:17:44 +09003497func (f fileInApex) String() string {
3498 return f.src + ":" + f.path
3499}
3500
3501func (f fileInApex) match(expectation string) bool {
3502 parts := strings.Split(expectation, ":")
3503 if len(parts) == 1 {
3504 match, _ := path.Match(parts[0], f.path)
3505 return match
3506 }
3507 if len(parts) == 2 {
3508 matchSrc, _ := path.Match(parts[0], f.src)
3509 matchDst, _ := path.Match(parts[1], f.path)
3510 return matchSrc && matchDst
3511 }
3512 panic("invalid expected file specification: " + expectation)
3513}
3514
Jooyung Hana57af4a2020-01-23 05:36:59 +00003515func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003516 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003517 module := ctx.ModuleForTests(moduleName, variant)
3518 apexRule := module.MaybeRule("apexRule")
3519 apexDir := "/image.apex/"
3520 if apexRule.Rule == nil {
3521 apexRule = module.Rule("zipApexRule")
3522 apexDir = "/image.zipapex/"
3523 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003524 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003525 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003526 for _, cmd := range strings.Split(copyCmds, "&&") {
3527 cmd = strings.TrimSpace(cmd)
3528 if cmd == "" {
3529 continue
3530 }
3531 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003532 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003533 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003534 switch terms[0] {
3535 case "mkdir":
3536 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003537 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003538 t.Fatal("copyCmds contains invalid cp command", cmd)
3539 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003540 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003541 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003542 isLink = false
3543 case "ln":
3544 if len(terms) != 3 && len(terms) != 4 {
3545 // ln LINK TARGET or ln -s LINK TARGET
3546 t.Fatal("copyCmds contains invalid ln command", cmd)
3547 }
3548 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003549 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003550 isLink = true
3551 default:
3552 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3553 }
3554 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003555 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003556 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003557 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003558 }
Jooyung Han1724d582022-12-21 10:17:44 +09003559 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003560 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003561 }
3562 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003563 return ret
3564}
3565
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003566func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003567 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003568 var failed bool
3569 var surplus []string
3570 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003571 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003572 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003573 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003574 if file.match(expected) {
3575 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003576 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003577 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003578 }
3579 }
Jooyung Han1724d582022-12-21 10:17:44 +09003580 if !matchFound {
3581 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003582 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003583 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003584
Jooyung Han31c470b2019-10-18 16:26:59 +09003585 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003586 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 t.Log("surplus files", surplus)
3588 failed = true
3589 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003590
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003591 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003592 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003593 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003594 if !filesMatched[expected] {
3595 missing = append(missing, expected)
3596 }
3597 }
3598 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003599 t.Log("missing files", missing)
3600 failed = true
3601 }
3602 if failed {
3603 t.Fail()
3604 }
3605}
3606
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003607func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3608 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3609}
3610
3611func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3612 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3613 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3614 if deapexer.Output != nil {
3615 outputs = append(outputs, deapexer.Output.String())
3616 }
3617 for _, output := range deapexer.ImplicitOutputs {
3618 outputs = append(outputs, output.String())
3619 }
3620 actualFiles := make([]fileInApex, 0, len(outputs))
3621 for _, output := range outputs {
3622 dir := "/deapexer/"
3623 pos := strings.LastIndex(output, dir)
3624 if pos == -1 {
3625 t.Fatal("Unknown deapexer output ", output)
3626 }
3627 path := output[pos+len(dir):]
3628 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3629 }
3630 assertFileListEquals(t, files, actualFiles)
3631}
3632
Jooyung Han344d5432019-08-23 11:17:39 +09003633func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003634 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003635 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003636 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003637 "etc/llndk.libraries.29.txt",
3638 "etc/vndkcore.libraries.29.txt",
3639 "etc/vndksp.libraries.29.txt",
3640 "etc/vndkprivate.libraries.29.txt",
3641 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003642 }
3643 testCases := []struct {
3644 vndkVersion string
3645 expectedFiles []string
3646 }{
3647 {
3648 vndkVersion: "current",
3649 expectedFiles: append(commonFiles,
3650 "lib/libvndk.so",
3651 "lib/libvndksp.so",
3652 "lib64/libvndk.so",
3653 "lib64/libvndksp.so"),
3654 },
3655 {
3656 vndkVersion: "",
3657 expectedFiles: append(commonFiles,
3658 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3659 "lib/libvndksp.so",
3660 "lib64/libvndksp.so"),
3661 },
3662 }
3663 for _, tc := range testCases {
3664 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3665 ctx := testApex(t, `
3666 apex_vndk {
3667 name: "com.android.vndk.current",
3668 key: "com.android.vndk.current.key",
3669 updatable: false,
3670 }
3671
3672 apex_key {
3673 name: "com.android.vndk.current.key",
3674 public_key: "testkey.avbpubkey",
3675 private_key: "testkey.pem",
3676 }
3677
3678 cc_library {
3679 name: "libvndk",
3680 srcs: ["mylib.cpp"],
3681 vendor_available: true,
3682 product_available: true,
3683 vndk: {
3684 enabled: true,
3685 },
3686 system_shared_libs: [],
3687 stl: "none",
3688 apex_available: [ "com.android.vndk.current" ],
3689 }
3690
3691 cc_library {
3692 name: "libvndksp",
3693 srcs: ["mylib.cpp"],
3694 vendor_available: true,
3695 product_available: true,
3696 vndk: {
3697 enabled: true,
3698 support_system_process: true,
3699 },
3700 system_shared_libs: [],
3701 stl: "none",
3702 apex_available: [ "com.android.vndk.current" ],
3703 }
3704
3705 // VNDK-Ext should not cause any problems
3706
3707 cc_library {
3708 name: "libvndk.ext",
3709 srcs: ["mylib2.cpp"],
3710 vendor: true,
3711 vndk: {
3712 enabled: true,
3713 extends: "libvndk",
3714 },
3715 system_shared_libs: [],
3716 stl: "none",
3717 }
3718
3719 cc_library {
3720 name: "libvndksp.ext",
3721 srcs: ["mylib2.cpp"],
3722 vendor: true,
3723 vndk: {
3724 enabled: true,
3725 support_system_process: true,
3726 extends: "libvndksp",
3727 },
3728 system_shared_libs: [],
3729 stl: "none",
3730 }
3731 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3732 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3733 }))
3734 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3735 })
3736 }
Jooyung Han344d5432019-08-23 11:17:39 +09003737}
3738
3739func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003740 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003741 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003742 name: "com.android.vndk.current",
3743 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003744 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003745 }
3746
3747 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003748 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003749 public_key: "testkey.avbpubkey",
3750 private_key: "testkey.pem",
3751 }
3752
3753 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003754 name: "libvndk",
3755 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003756 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003757 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003758 vndk: {
3759 enabled: true,
3760 },
3761 system_shared_libs: [],
3762 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003763 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003764 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003765
3766 cc_prebuilt_library_shared {
3767 name: "libvndk.arm",
3768 srcs: ["libvndk.arm.so"],
3769 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003770 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003771 vndk: {
3772 enabled: true,
3773 },
3774 enabled: false,
3775 arch: {
3776 arm: {
3777 enabled: true,
3778 },
3779 },
3780 system_shared_libs: [],
3781 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003782 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003783 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003784 `+vndkLibrariesTxtFiles("current"),
3785 withFiles(map[string][]byte{
3786 "libvndk.so": nil,
3787 "libvndk.arm.so": nil,
3788 }))
Colin Cross2807f002021-03-02 10:15:29 -08003789 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003790 "lib/libvndk.so",
3791 "lib/libvndk.arm.so",
3792 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003793 "lib/libc++.so",
3794 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003795 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003796 })
Jooyung Han344d5432019-08-23 11:17:39 +09003797}
3798
Jooyung Han39edb6c2019-11-06 16:53:07 +09003799func vndkLibrariesTxtFiles(vers ...string) (result string) {
3800 for _, v := range vers {
3801 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003802 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003803 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003804 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003805 name: "` + txt + `.libraries.txt",
3806 }
3807 `
3808 }
3809 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003810 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003811 result += `
3812 prebuilt_etc {
3813 name: "` + txt + `.libraries.` + v + `.txt",
3814 src: "dummy.txt",
3815 }
3816 `
3817 }
3818 }
3819 }
3820 return
3821}
3822
Jooyung Han344d5432019-08-23 11:17:39 +09003823func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003824 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003825 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003826 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003827 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003828 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003829 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003830 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003831 }
3832
3833 apex_key {
3834 name: "myapex.key",
3835 public_key: "testkey.avbpubkey",
3836 private_key: "testkey.pem",
3837 }
3838
Jooyung Han31c470b2019-10-18 16:26:59 +09003839 vndk_prebuilt_shared {
3840 name: "libvndk27",
3841 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003842 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003843 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003844 vndk: {
3845 enabled: true,
3846 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 target_arch: "arm64",
3848 arch: {
3849 arm: {
3850 srcs: ["libvndk27_arm.so"],
3851 },
3852 arm64: {
3853 srcs: ["libvndk27_arm64.so"],
3854 },
3855 },
Colin Cross2807f002021-03-02 10:15:29 -08003856 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003857 }
3858
3859 vndk_prebuilt_shared {
3860 name: "libvndk27",
3861 version: "27",
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003864 vndk: {
3865 enabled: true,
3866 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003867 target_arch: "x86_64",
3868 arch: {
3869 x86: {
3870 srcs: ["libvndk27_x86.so"],
3871 },
3872 x86_64: {
3873 srcs: ["libvndk27_x86_64.so"],
3874 },
3875 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003876 }
3877 `+vndkLibrariesTxtFiles("27"),
3878 withFiles(map[string][]byte{
3879 "libvndk27_arm.so": nil,
3880 "libvndk27_arm64.so": nil,
3881 "libvndk27_x86.so": nil,
3882 "libvndk27_x86_64.so": nil,
3883 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003884
Colin Cross2807f002021-03-02 10:15:29 -08003885 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003886 "lib/libvndk27_arm.so",
3887 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003888 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003889 })
Jooyung Han344d5432019-08-23 11:17:39 +09003890}
3891
Jooyung Han90eee022019-10-01 20:02:42 +09003892func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003893 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003894 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003895 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003896 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003897 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003898 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003899 }
3900 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003901 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003902 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003903 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003904 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003905 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003906 }
3907 apex_key {
3908 name: "myapex.key",
3909 public_key: "testkey.avbpubkey",
3910 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003911 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003912
3913 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003914 module := ctx.ModuleForTests(moduleName, "android_common_image")
3915 apexManifestRule := module.Rule("apexManifestRule")
3916 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003917 }
3918
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003919 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003920 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003921}
3922
Jooyung Han344d5432019-08-23 11:17:39 +09003923func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003924 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003925 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003926 name: "com.android.vndk.current",
3927 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003928 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003929 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003930 }
3931
3932 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003933 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003934 public_key: "testkey.avbpubkey",
3935 private_key: "testkey.pem",
3936 }
3937
3938 cc_library {
3939 name: "libvndk",
3940 srcs: ["mylib.cpp"],
3941 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003942 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003943 native_bridge_supported: true,
3944 host_supported: true,
3945 vndk: {
3946 enabled: true,
3947 },
3948 system_shared_libs: [],
3949 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003950 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003951 }
Colin Cross2807f002021-03-02 10:15:29 -08003952 `+vndkLibrariesTxtFiles("current"),
3953 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003954
Colin Cross2807f002021-03-02 10:15:29 -08003955 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003956 "lib/libvndk.so",
3957 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003958 "lib/libc++.so",
3959 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003960 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003961 })
Jooyung Han344d5432019-08-23 11:17:39 +09003962}
3963
3964func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003965 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003966 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003967 name: "com.android.vndk.current",
3968 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003969 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003970 native_bridge_supported: true,
3971 }
3972
3973 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003974 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003975 public_key: "testkey.avbpubkey",
3976 private_key: "testkey.pem",
3977 }
3978
3979 cc_library {
3980 name: "libvndk",
3981 srcs: ["mylib.cpp"],
3982 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003983 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003984 native_bridge_supported: true,
3985 host_supported: true,
3986 vndk: {
3987 enabled: true,
3988 },
3989 system_shared_libs: [],
3990 stl: "none",
3991 }
3992 `)
3993}
3994
Jooyung Han31c470b2019-10-18 16:26:59 +09003995func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003996 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003997 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003998 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003999 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004000 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004001 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004002 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 }
4004
4005 apex_key {
4006 name: "myapex.key",
4007 public_key: "testkey.avbpubkey",
4008 private_key: "testkey.pem",
4009 }
4010
4011 vndk_prebuilt_shared {
4012 name: "libvndk27",
4013 version: "27",
4014 target_arch: "arm",
4015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004016 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004017 vndk: {
4018 enabled: true,
4019 },
4020 arch: {
4021 arm: {
4022 srcs: ["libvndk27.so"],
4023 }
4024 },
4025 }
4026
4027 vndk_prebuilt_shared {
4028 name: "libvndk27",
4029 version: "27",
4030 target_arch: "arm",
4031 binder32bit: true,
4032 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004033 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004034 vndk: {
4035 enabled: true,
4036 },
4037 arch: {
4038 arm: {
4039 srcs: ["libvndk27binder32.so"],
4040 }
4041 },
Colin Cross2807f002021-03-02 10:15:29 -08004042 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004043 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004044 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004045 withFiles(map[string][]byte{
4046 "libvndk27.so": nil,
4047 "libvndk27binder32.so": nil,
4048 }),
4049 withBinder32bit,
4050 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004051 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004052 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4053 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004054 },
4055 }),
4056 )
4057
Colin Cross2807f002021-03-02 10:15:29 -08004058 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004059 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004060 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004061 })
4062}
4063
Jooyung Han45a96772020-06-15 14:59:42 +09004064func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004065 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004066 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004067 name: "com.android.vndk.current",
4068 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004069 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004070 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004071 }
4072
4073 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004074 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004075 public_key: "testkey.avbpubkey",
4076 private_key: "testkey.pem",
4077 }
4078
4079 cc_library {
4080 name: "libz",
4081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004082 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004083 vndk: {
4084 enabled: true,
4085 },
4086 stubs: {
4087 symbol_file: "libz.map.txt",
4088 versions: ["30"],
4089 }
4090 }
4091 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4092 "libz.map.txt": nil,
4093 }))
4094
Colin Cross2807f002021-03-02 10:15:29 -08004095 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004096 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4097 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004098 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4099 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4100 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4101 "*/*",
4102 })
Jooyung Han45a96772020-06-15 14:59:42 +09004103}
4104
Jooyung Hane1633032019-08-01 17:41:43 +09004105func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004106 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004107 apex {
4108 name: "myapex_nodep",
4109 key: "myapex.key",
4110 native_shared_libs: ["lib_nodep"],
4111 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004112 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004113 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004114 }
4115
4116 apex {
4117 name: "myapex_dep",
4118 key: "myapex.key",
4119 native_shared_libs: ["lib_dep"],
4120 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004121 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004122 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004123 }
4124
4125 apex {
4126 name: "myapex_provider",
4127 key: "myapex.key",
4128 native_shared_libs: ["libfoo"],
4129 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004130 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004131 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004132 }
4133
4134 apex {
4135 name: "myapex_selfcontained",
4136 key: "myapex.key",
4137 native_shared_libs: ["lib_dep", "libfoo"],
4138 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004139 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004140 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004141 }
4142
4143 apex_key {
4144 name: "myapex.key",
4145 public_key: "testkey.avbpubkey",
4146 private_key: "testkey.pem",
4147 }
4148
4149 cc_library {
4150 name: "lib_nodep",
4151 srcs: ["mylib.cpp"],
4152 system_shared_libs: [],
4153 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004154 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004155 }
4156
4157 cc_library {
4158 name: "lib_dep",
4159 srcs: ["mylib.cpp"],
4160 shared_libs: ["libfoo"],
4161 system_shared_libs: [],
4162 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004163 apex_available: [
4164 "myapex_dep",
4165 "myapex_provider",
4166 "myapex_selfcontained",
4167 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004168 }
4169
4170 cc_library {
4171 name: "libfoo",
4172 srcs: ["mytest.cpp"],
4173 stubs: {
4174 versions: ["1"],
4175 },
4176 system_shared_libs: [],
4177 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004178 apex_available: [
4179 "myapex_provider",
4180 "myapex_selfcontained",
4181 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004182 }
4183 `)
4184
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004185 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004186 var provideNativeLibs, requireNativeLibs []string
4187
Sundong Ahnabb64432019-10-22 13:58:29 +09004188 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004189 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4190 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004191 ensureListEmpty(t, provideNativeLibs)
4192 ensureListEmpty(t, requireNativeLibs)
4193
Sundong Ahnabb64432019-10-22 13:58:29 +09004194 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004195 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4196 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004197 ensureListEmpty(t, provideNativeLibs)
4198 ensureListContains(t, requireNativeLibs, "libfoo.so")
4199
Sundong Ahnabb64432019-10-22 13:58:29 +09004200 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004201 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4202 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004203 ensureListContains(t, provideNativeLibs, "libfoo.so")
4204 ensureListEmpty(t, requireNativeLibs)
4205
Sundong Ahnabb64432019-10-22 13:58:29 +09004206 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004207 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4208 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004209 ensureListContains(t, provideNativeLibs, "libfoo.so")
4210 ensureListEmpty(t, requireNativeLibs)
4211}
4212
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004213func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4214 ctx := testApex(t, `
4215 apex {
4216 name: "myapex",
4217 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004218 native_shared_libs: ["mylib"],
4219 updatable: false,
4220 }
4221
4222 apex_key {
4223 name: "myapex.key",
4224 public_key: "testkey.avbpubkey",
4225 private_key: "testkey.pem",
4226 }
4227
4228 cc_library {
4229 name: "mylib",
4230 srcs: ["mylib.cpp"],
4231 system_shared_libs: [],
4232 stl: "none",
4233 apex_available: [
4234 "//apex_available:platform",
4235 "myapex",
4236 ],
4237 }
4238 `, android.FixtureMergeEnv(map[string]string{
4239 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4240 }))
4241
Jooyung Han63dff462023-02-09 00:11:27 +00004242 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004243 apexManifestRule := module.Rule("apexManifestRule")
4244 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4245}
4246
Vinh Tran8f5310f2022-10-07 18:16:47 -04004247func TestCompileMultilibProp(t *testing.T) {
4248 testCases := []struct {
4249 compileMultiLibProp string
4250 containedLibs []string
4251 notContainedLibs []string
4252 }{
4253 {
4254 containedLibs: []string{
4255 "image.apex/lib64/mylib.so",
4256 "image.apex/lib/mylib.so",
4257 },
4258 compileMultiLibProp: `compile_multilib: "both",`,
4259 },
4260 {
4261 containedLibs: []string{"image.apex/lib64/mylib.so"},
4262 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4263 compileMultiLibProp: `compile_multilib: "first",`,
4264 },
4265 {
4266 containedLibs: []string{"image.apex/lib64/mylib.so"},
4267 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4268 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4269 },
4270 {
4271 containedLibs: []string{"image.apex/lib64/mylib.so"},
4272 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4273 compileMultiLibProp: `compile_multilib: "64",`,
4274 },
4275 {
4276 containedLibs: []string{"image.apex/lib/mylib.so"},
4277 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4278 compileMultiLibProp: `compile_multilib: "32",`,
4279 },
4280 }
4281 for _, testCase := range testCases {
4282 ctx := testApex(t, fmt.Sprintf(`
4283 apex {
4284 name: "myapex",
4285 key: "myapex.key",
4286 %s
4287 native_shared_libs: ["mylib"],
4288 updatable: false,
4289 }
4290 apex_key {
4291 name: "myapex.key",
4292 public_key: "testkey.avbpubkey",
4293 private_key: "testkey.pem",
4294 }
4295 cc_library {
4296 name: "mylib",
4297 srcs: ["mylib.cpp"],
4298 apex_available: [
4299 "//apex_available:platform",
4300 "myapex",
4301 ],
4302 }
4303 `, testCase.compileMultiLibProp),
4304 )
4305 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4306 apexRule := module.Rule("apexRule")
4307 copyCmds := apexRule.Args["copy_commands"]
4308 for _, containedLib := range testCase.containedLibs {
4309 ensureContains(t, copyCmds, containedLib)
4310 }
4311 for _, notContainedLib := range testCase.notContainedLibs {
4312 ensureNotContains(t, copyCmds, notContainedLib)
4313 }
4314 }
4315}
4316
Alex Light0851b882019-02-07 13:20:53 -08004317func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004318 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004319 apex {
4320 name: "myapex",
4321 key: "myapex.key",
4322 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004323 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004324 }
4325
4326 apex_key {
4327 name: "myapex.key",
4328 public_key: "testkey.avbpubkey",
4329 private_key: "testkey.pem",
4330 }
4331
4332 cc_library {
4333 name: "mylib_common",
4334 srcs: ["mylib.cpp"],
4335 system_shared_libs: [],
4336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004337 apex_available: [
4338 "//apex_available:platform",
4339 "myapex",
4340 ],
Alex Light0851b882019-02-07 13:20:53 -08004341 }
4342 `)
4343
Sundong Ahnabb64432019-10-22 13:58:29 +09004344 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004345 apexRule := module.Rule("apexRule")
4346 copyCmds := apexRule.Args["copy_commands"]
4347
4348 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4349 t.Log("Apex was a test apex!")
4350 t.Fail()
4351 }
4352 // Ensure that main rule creates an output
4353 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4354
4355 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004356 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004357
4358 // Ensure that both direct and indirect deps are copied into apex
4359 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4360
Colin Cross7113d202019-11-20 16:39:12 -08004361 // Ensure that the platform variant ends with _shared
4362 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004363
Colin Cross56a83212020-09-15 18:30:11 -07004364 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004365 t.Log("Found mylib_common not in any apex!")
4366 t.Fail()
4367 }
4368}
4369
4370func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004371 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004372 apex_test {
4373 name: "myapex",
4374 key: "myapex.key",
4375 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004376 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004377 }
4378
4379 apex_key {
4380 name: "myapex.key",
4381 public_key: "testkey.avbpubkey",
4382 private_key: "testkey.pem",
4383 }
4384
4385 cc_library {
4386 name: "mylib_common_test",
4387 srcs: ["mylib.cpp"],
4388 system_shared_libs: [],
4389 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004390 // TODO: remove //apex_available:platform
4391 apex_available: [
4392 "//apex_available:platform",
4393 "myapex",
4394 ],
Alex Light0851b882019-02-07 13:20:53 -08004395 }
4396 `)
4397
Sundong Ahnabb64432019-10-22 13:58:29 +09004398 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004399 apexRule := module.Rule("apexRule")
4400 copyCmds := apexRule.Args["copy_commands"]
4401
4402 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4403 t.Log("Apex was not a test apex!")
4404 t.Fail()
4405 }
4406 // Ensure that main rule creates an output
4407 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4408
4409 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004410 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004411
4412 // Ensure that both direct and indirect deps are copied into apex
4413 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4414
Colin Cross7113d202019-11-20 16:39:12 -08004415 // Ensure that the platform variant ends with _shared
4416 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004417}
4418
Alex Light9670d332019-01-29 18:07:33 -08004419func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004420 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004421 apex {
4422 name: "myapex",
4423 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004424 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004425 multilib: {
4426 first: {
4427 native_shared_libs: ["mylib_common"],
4428 }
4429 },
4430 target: {
4431 android: {
4432 multilib: {
4433 first: {
4434 native_shared_libs: ["mylib"],
4435 }
4436 }
4437 },
4438 host: {
4439 multilib: {
4440 first: {
4441 native_shared_libs: ["mylib2"],
4442 }
4443 }
4444 }
4445 }
4446 }
4447
4448 apex_key {
4449 name: "myapex.key",
4450 public_key: "testkey.avbpubkey",
4451 private_key: "testkey.pem",
4452 }
4453
4454 cc_library {
4455 name: "mylib",
4456 srcs: ["mylib.cpp"],
4457 system_shared_libs: [],
4458 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004459 // TODO: remove //apex_available:platform
4460 apex_available: [
4461 "//apex_available:platform",
4462 "myapex",
4463 ],
Alex Light9670d332019-01-29 18:07:33 -08004464 }
4465
4466 cc_library {
4467 name: "mylib_common",
4468 srcs: ["mylib.cpp"],
4469 system_shared_libs: [],
4470 stl: "none",
4471 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004472 // TODO: remove //apex_available:platform
4473 apex_available: [
4474 "//apex_available:platform",
4475 "myapex",
4476 ],
Alex Light9670d332019-01-29 18:07:33 -08004477 }
4478
4479 cc_library {
4480 name: "mylib2",
4481 srcs: ["mylib.cpp"],
4482 system_shared_libs: [],
4483 stl: "none",
4484 compile_multilib: "first",
4485 }
4486 `)
4487
Sundong Ahnabb64432019-10-22 13:58:29 +09004488 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004489 copyCmds := apexRule.Args["copy_commands"]
4490
4491 // Ensure that main rule creates an output
4492 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4493
4494 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4496 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4497 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004498
4499 // Ensure that both direct and indirect deps are copied into apex
4500 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4501 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4502 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4503
Colin Cross7113d202019-11-20 16:39:12 -08004504 // Ensure that the platform variant ends with _shared
4505 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4506 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4507 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004508}
Jiyong Park04480cf2019-02-06 00:16:29 +09004509
Jiyong Park59140302020-12-14 18:44:04 +09004510func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004511 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004512 apex {
4513 name: "myapex",
4514 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004515 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004516 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004517 arch: {
4518 arm64: {
4519 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004520 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004521 },
4522 x86_64: {
4523 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004524 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004525 },
4526 }
4527 }
4528
4529 apex_key {
4530 name: "myapex.key",
4531 public_key: "testkey.avbpubkey",
4532 private_key: "testkey.pem",
4533 }
4534
4535 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004536 name: "mylib.generic",
4537 srcs: ["mylib.cpp"],
4538 system_shared_libs: [],
4539 stl: "none",
4540 // TODO: remove //apex_available:platform
4541 apex_available: [
4542 "//apex_available:platform",
4543 "myapex",
4544 ],
4545 }
4546
4547 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004548 name: "mylib.arm64",
4549 srcs: ["mylib.cpp"],
4550 system_shared_libs: [],
4551 stl: "none",
4552 // TODO: remove //apex_available:platform
4553 apex_available: [
4554 "//apex_available:platform",
4555 "myapex",
4556 ],
4557 }
4558
4559 cc_library {
4560 name: "mylib.x64",
4561 srcs: ["mylib.cpp"],
4562 system_shared_libs: [],
4563 stl: "none",
4564 // TODO: remove //apex_available:platform
4565 apex_available: [
4566 "//apex_available:platform",
4567 "myapex",
4568 ],
4569 }
4570 `)
4571
4572 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4573 copyCmds := apexRule.Args["copy_commands"]
4574
4575 // Ensure that apex variant is created for the direct dep
4576 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004577 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4579
4580 // Ensure that both direct and indirect deps are copied into apex
4581 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4582 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4583}
4584
Jiyong Park04480cf2019-02-06 00:16:29 +09004585func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004586 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004587 apex {
4588 name: "myapex",
4589 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004590 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004591 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004592 }
4593
4594 apex_key {
4595 name: "myapex.key",
4596 public_key: "testkey.avbpubkey",
4597 private_key: "testkey.pem",
4598 }
4599
4600 sh_binary {
4601 name: "myscript",
4602 src: "mylib.cpp",
4603 filename: "myscript.sh",
4604 sub_dir: "script",
4605 }
4606 `)
4607
Sundong Ahnabb64432019-10-22 13:58:29 +09004608 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004609 copyCmds := apexRule.Args["copy_commands"]
4610
4611 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4612}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004613
Jooyung Han91df2082019-11-20 01:49:42 +09004614func TestApexInVariousPartition(t *testing.T) {
4615 testcases := []struct {
4616 propName, parition, flattenedPartition string
4617 }{
4618 {"", "system", "system_ext"},
4619 {"product_specific: true", "product", "product"},
4620 {"soc_specific: true", "vendor", "vendor"},
4621 {"proprietary: true", "vendor", "vendor"},
4622 {"vendor: true", "vendor", "vendor"},
4623 {"system_ext_specific: true", "system_ext", "system_ext"},
4624 }
4625 for _, tc := range testcases {
4626 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004627 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004628 apex {
4629 name: "myapex",
4630 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004631 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004632 `+tc.propName+`
4633 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004634
Jooyung Han91df2082019-11-20 01:49:42 +09004635 apex_key {
4636 name: "myapex.key",
4637 public_key: "testkey.avbpubkey",
4638 private_key: "testkey.pem",
4639 }
4640 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004641
Jooyung Han91df2082019-11-20 01:49:42 +09004642 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004643 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4644 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004645 if actual != expected {
4646 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4647 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004648
Jooyung Han91df2082019-11-20 01:49:42 +09004649 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004650 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4651 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004652 if actual != expected {
4653 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4654 }
4655 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004656 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004657}
Jiyong Park67882562019-03-21 01:11:21 +09004658
Jooyung Han580eb4f2020-06-24 19:33:06 +09004659func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004660 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004661 apex {
4662 name: "myapex",
4663 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004664 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004665 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004666
Jooyung Han580eb4f2020-06-24 19:33:06 +09004667 apex_key {
4668 name: "myapex.key",
4669 public_key: "testkey.avbpubkey",
4670 private_key: "testkey.pem",
4671 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004672 `)
4673 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004674 rule := module.Output("file_contexts")
4675 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4676}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004677
Jooyung Han580eb4f2020-06-24 19:33:06 +09004678func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004679 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004680 apex {
4681 name: "myapex",
4682 key: "myapex.key",
4683 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004684 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004685 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004686
Jooyung Han580eb4f2020-06-24 19:33:06 +09004687 apex_key {
4688 name: "myapex.key",
4689 public_key: "testkey.avbpubkey",
4690 private_key: "testkey.pem",
4691 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004692 `, withFiles(map[string][]byte{
4693 "my_own_file_contexts": nil,
4694 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004695}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004696
Jooyung Han580eb4f2020-06-24 19:33:06 +09004697func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004698 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004699 apex {
4700 name: "myapex",
4701 key: "myapex.key",
4702 product_specific: true,
4703 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004704 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004705 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004706
Jooyung Han580eb4f2020-06-24 19:33:06 +09004707 apex_key {
4708 name: "myapex.key",
4709 public_key: "testkey.avbpubkey",
4710 private_key: "testkey.pem",
4711 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004712 `)
4713
Colin Cross1c460562021-02-16 17:55:47 -08004714 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004715 apex {
4716 name: "myapex",
4717 key: "myapex.key",
4718 product_specific: true,
4719 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004720 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004721 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004722
Jooyung Han580eb4f2020-06-24 19:33:06 +09004723 apex_key {
4724 name: "myapex.key",
4725 public_key: "testkey.avbpubkey",
4726 private_key: "testkey.pem",
4727 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004728 `, withFiles(map[string][]byte{
4729 "product_specific_file_contexts": nil,
4730 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004731 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4732 rule := module.Output("file_contexts")
4733 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4734}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004735
Jooyung Han580eb4f2020-06-24 19:33:06 +09004736func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004737 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004738 apex {
4739 name: "myapex",
4740 key: "myapex.key",
4741 product_specific: true,
4742 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004743 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004744 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004745
Jooyung Han580eb4f2020-06-24 19:33:06 +09004746 apex_key {
4747 name: "myapex.key",
4748 public_key: "testkey.avbpubkey",
4749 private_key: "testkey.pem",
4750 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004751
Jooyung Han580eb4f2020-06-24 19:33:06 +09004752 filegroup {
4753 name: "my-file-contexts",
4754 srcs: ["product_specific_file_contexts"],
4755 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004756 `, withFiles(map[string][]byte{
4757 "product_specific_file_contexts": nil,
4758 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004759 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4760 rule := module.Output("file_contexts")
4761 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004762}
4763
Jiyong Park67882562019-03-21 01:11:21 +09004764func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004765 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004766 apex_key {
4767 name: "myapex.key",
4768 public_key: ":my.avbpubkey",
4769 private_key: ":my.pem",
4770 product_specific: true,
4771 }
4772
4773 filegroup {
4774 name: "my.avbpubkey",
4775 srcs: ["testkey2.avbpubkey"],
4776 }
4777
4778 filegroup {
4779 name: "my.pem",
4780 srcs: ["testkey2.pem"],
4781 }
4782 `)
4783
4784 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4785 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004786 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004787 if actual_pubkey != expected_pubkey {
4788 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4789 }
4790 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004791 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004792 if actual_privkey != expected_privkey {
4793 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4794 }
4795}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004796
4797func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004798 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004799 prebuilt_apex {
4800 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004801 arch: {
4802 arm64: {
4803 src: "myapex-arm64.apex",
4804 },
4805 arm: {
4806 src: "myapex-arm.apex",
4807 },
4808 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004809 }
4810 `)
4811
Wei Li340ee8e2022-03-18 17:33:24 -07004812 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4813 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004814
Jiyong Parkc95714e2019-03-29 14:23:10 +09004815 expectedInput := "myapex-arm64.apex"
4816 if prebuilt.inputApex.String() != expectedInput {
4817 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4818 }
Wei Li340ee8e2022-03-18 17:33:24 -07004819 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4820 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4821 rule := testingModule.Rule("genProvenanceMetaData")
4822 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4823 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4824 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4825 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004826
4827 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4828 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004829}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004830
Paul Duffinc0609c62021-03-01 17:27:16 +00004831func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004832 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004833 prebuilt_apex {
4834 name: "myapex",
4835 }
4836 `)
4837}
4838
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004839func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004840 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004841 prebuilt_apex {
4842 name: "myapex",
4843 src: "myapex-arm.apex",
4844 filename: "notmyapex.apex",
4845 }
4846 `)
4847
Wei Li340ee8e2022-03-18 17:33:24 -07004848 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4849 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004850
4851 expected := "notmyapex.apex"
4852 if p.installFilename != expected {
4853 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4854 }
Wei Li340ee8e2022-03-18 17:33:24 -07004855 rule := testingModule.Rule("genProvenanceMetaData")
4856 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4857 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4858 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4859 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004860}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004861
Samiul Islam7c02e262021-09-08 17:48:28 +01004862func TestApexSetFilenameOverride(t *testing.T) {
4863 testApex(t, `
4864 apex_set {
4865 name: "com.company.android.myapex",
4866 apex_name: "com.android.myapex",
4867 set: "company-myapex.apks",
4868 filename: "com.company.android.myapex.apex"
4869 }
4870 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4871
4872 testApex(t, `
4873 apex_set {
4874 name: "com.company.android.myapex",
4875 apex_name: "com.android.myapex",
4876 set: "company-myapex.apks",
4877 filename: "com.company.android.myapex.capex"
4878 }
4879 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4880
4881 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4882 apex_set {
4883 name: "com.company.android.myapex",
4884 apex_name: "com.android.myapex",
4885 set: "company-myapex.apks",
4886 filename: "some-random-suffix"
4887 }
4888 `)
4889}
4890
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004891func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004892 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004893 prebuilt_apex {
4894 name: "myapex.prebuilt",
4895 src: "myapex-arm.apex",
4896 overrides: [
4897 "myapex",
4898 ],
4899 }
4900 `)
4901
Wei Li340ee8e2022-03-18 17:33:24 -07004902 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4903 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004904
4905 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004906 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004907 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004908 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004909 }
Wei Li340ee8e2022-03-18 17:33:24 -07004910 rule := testingModule.Rule("genProvenanceMetaData")
4911 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4912 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4913 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4914 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004915}
4916
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004917func TestPrebuiltApexName(t *testing.T) {
4918 testApex(t, `
4919 prebuilt_apex {
4920 name: "com.company.android.myapex",
4921 apex_name: "com.android.myapex",
4922 src: "company-myapex-arm.apex",
4923 }
4924 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4925
4926 testApex(t, `
4927 apex_set {
4928 name: "com.company.android.myapex",
4929 apex_name: "com.android.myapex",
4930 set: "company-myapex.apks",
4931 }
4932 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4933}
4934
4935func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4936 _ = android.GroupFixturePreparers(
4937 java.PrepareForTestWithJavaDefaultModules,
4938 PrepareForTestWithApexBuildComponents,
4939 android.FixtureWithRootAndroidBp(`
4940 platform_bootclasspath {
4941 name: "platform-bootclasspath",
4942 fragments: [
4943 {
4944 apex: "com.android.art",
4945 module: "art-bootclasspath-fragment",
4946 },
4947 ],
4948 }
4949
4950 prebuilt_apex {
4951 name: "com.company.android.art",
4952 apex_name: "com.android.art",
4953 src: "com.company.android.art-arm.apex",
4954 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4955 }
4956
4957 prebuilt_bootclasspath_fragment {
4958 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004959 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004960 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004961 hidden_api: {
4962 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4963 metadata: "my-bootclasspath-fragment/metadata.csv",
4964 index: "my-bootclasspath-fragment/index.csv",
4965 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4966 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4967 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004968 }
4969
4970 java_import {
4971 name: "core-oj",
4972 jars: ["prebuilt.jar"],
4973 }
4974 `),
4975 ).RunTest(t)
4976}
4977
Paul Duffin092153d2021-01-26 11:42:39 +00004978// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4979// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004980func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004981 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004982
Paul Duffin89886cb2021-02-05 16:44:03 +00004983 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004984 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004985 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004986 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004987 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004988 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004989 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4990 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4991 android.NormalizePathForTesting(dexJarBuildPath))
4992 }
4993
4994 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004995 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004996 // Make sure the import has been given the correct path to the dex jar.
4997 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4998 dexJarBuildPath := p.DexJarInstallPath()
4999 stem := android.RemoveOptionalPrebuiltPrefix(name)
5000 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
5001 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
5002 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00005003 }
5004
Paul Duffin39853512021-02-26 11:09:39 +00005005 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005006 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005007 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09005008 android.AssertArrayString(t, "Check if there is no source variant",
5009 []string{"android_common"},
5010 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00005011 }
5012
5013 t.Run("prebuilt only", func(t *testing.T) {
5014 bp := `
5015 prebuilt_apex {
5016 name: "myapex",
5017 arch: {
5018 arm64: {
5019 src: "myapex-arm64.apex",
5020 },
5021 arm: {
5022 src: "myapex-arm.apex",
5023 },
5024 },
Paul Duffin39853512021-02-26 11:09:39 +00005025 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005026 }
5027
5028 java_import {
5029 name: "libfoo",
5030 jars: ["libfoo.jar"],
5031 }
Paul Duffin39853512021-02-26 11:09:39 +00005032
5033 java_sdk_library_import {
5034 name: "libbar",
5035 public: {
5036 jars: ["libbar.jar"],
5037 },
5038 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005039 `
5040
5041 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5042 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5043
Martin Stjernholm44825602021-09-17 01:44:12 +01005044 deapexerName := deapexerModuleName("myapex")
5045 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5046
Paul Duffinf6932af2021-02-26 18:21:56 +00005047 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005048 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005049 rule := deapexer.Rule("deapexer")
5050 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5051 t.Errorf("expected: %q, found: %q", expected, actual)
5052 }
5053
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005054 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005055 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005056 rule = prebuiltApex.Rule("android/soong/android.Cp")
5057 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5058 t.Errorf("expected: %q, found: %q", expected, actual)
5059 }
5060
Paul Duffin89886cb2021-02-05 16:44:03 +00005061 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005062 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005063
5064 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005065 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005066 })
5067
5068 t.Run("prebuilt with source preferred", func(t *testing.T) {
5069
5070 bp := `
5071 prebuilt_apex {
5072 name: "myapex",
5073 arch: {
5074 arm64: {
5075 src: "myapex-arm64.apex",
5076 },
5077 arm: {
5078 src: "myapex-arm.apex",
5079 },
5080 },
Paul Duffin39853512021-02-26 11:09:39 +00005081 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005082 }
5083
5084 java_import {
5085 name: "libfoo",
5086 jars: ["libfoo.jar"],
5087 }
5088
5089 java_library {
5090 name: "libfoo",
5091 }
Paul Duffin39853512021-02-26 11:09:39 +00005092
5093 java_sdk_library_import {
5094 name: "libbar",
5095 public: {
5096 jars: ["libbar.jar"],
5097 },
5098 }
5099
5100 java_sdk_library {
5101 name: "libbar",
5102 srcs: ["foo/bar/MyClass.java"],
5103 unsafe_ignore_missing_latest_api: true,
5104 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005105 `
5106
5107 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5108 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5109
Paul Duffin89886cb2021-02-05 16:44:03 +00005110 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005111 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005112 ensureNoSourceVariant(t, ctx, "libfoo")
5113
5114 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005115 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005116 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005117 })
5118
5119 t.Run("prebuilt preferred with source", func(t *testing.T) {
5120 bp := `
5121 prebuilt_apex {
5122 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005123 arch: {
5124 arm64: {
5125 src: "myapex-arm64.apex",
5126 },
5127 arm: {
5128 src: "myapex-arm.apex",
5129 },
5130 },
Paul Duffin39853512021-02-26 11:09:39 +00005131 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005132 }
5133
5134 java_import {
5135 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005136 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005137 jars: ["libfoo.jar"],
5138 }
5139
5140 java_library {
5141 name: "libfoo",
5142 }
Paul Duffin39853512021-02-26 11:09:39 +00005143
5144 java_sdk_library_import {
5145 name: "libbar",
5146 prefer: true,
5147 public: {
5148 jars: ["libbar.jar"],
5149 },
5150 }
5151
5152 java_sdk_library {
5153 name: "libbar",
5154 srcs: ["foo/bar/MyClass.java"],
5155 unsafe_ignore_missing_latest_api: true,
5156 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005157 `
5158
5159 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5160 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5161
Paul Duffin89886cb2021-02-05 16:44:03 +00005162 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005163 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005164 ensureNoSourceVariant(t, ctx, "libfoo")
5165
5166 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005167 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005168 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005169 })
5170}
5171
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005172func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005173 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005174 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005175 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5176 // is disabled.
5177 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5178 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005179
Paul Duffin37856732021-02-26 14:24:15 +00005180 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5181 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005182 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005183 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005184 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005185 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005186 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005187 foundLibfooJar = true
5188 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005189 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005190 }
5191 }
5192 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005193 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005194 }
5195 }
5196
Paul Duffin40a3f652021-07-19 13:11:24 +01005197 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005198 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005199 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005200 var rule android.TestingBuildParams
5201
5202 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5203 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005204 }
5205
Paul Duffin40a3f652021-07-19 13:11:24 +01005206 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5207 t.Helper()
5208 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5209 var rule android.TestingBuildParams
5210
5211 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5212 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5213 }
5214
Paul Duffin89f570a2021-06-16 01:42:33 +01005215 fragment := java.ApexVariantReference{
5216 Apex: proptools.StringPtr("myapex"),
5217 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5218 }
5219
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005220 t.Run("prebuilt only", func(t *testing.T) {
5221 bp := `
5222 prebuilt_apex {
5223 name: "myapex",
5224 arch: {
5225 arm64: {
5226 src: "myapex-arm64.apex",
5227 },
5228 arm: {
5229 src: "myapex-arm.apex",
5230 },
5231 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005232 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5233 }
5234
5235 prebuilt_bootclasspath_fragment {
5236 name: "my-bootclasspath-fragment",
5237 contents: ["libfoo", "libbar"],
5238 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005239 hidden_api: {
5240 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5241 metadata: "my-bootclasspath-fragment/metadata.csv",
5242 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005243 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5244 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5245 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005246 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005247 }
5248
5249 java_import {
5250 name: "libfoo",
5251 jars: ["libfoo.jar"],
5252 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005253 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005254 }
Paul Duffin37856732021-02-26 14:24:15 +00005255
5256 java_sdk_library_import {
5257 name: "libbar",
5258 public: {
5259 jars: ["libbar.jar"],
5260 },
5261 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005262 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005263 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005264 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005265 `
5266
Paul Duffin89f570a2021-06-16 01:42:33 +01005267 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005268 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5269 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005270
Paul Duffin537ea3d2021-05-14 10:38:00 +01005271 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005272 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005273 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005274 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005275 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5276 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005277 })
5278
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005279 t.Run("apex_set only", func(t *testing.T) {
5280 bp := `
5281 apex_set {
5282 name: "myapex",
5283 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005284 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005285 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005286 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5287 }
5288
5289 java_import {
5290 name: "myjavalib",
5291 jars: ["myjavalib.jar"],
5292 apex_available: ["myapex"],
5293 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005294 }
5295
5296 prebuilt_bootclasspath_fragment {
5297 name: "my-bootclasspath-fragment",
5298 contents: ["libfoo", "libbar"],
5299 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005300 hidden_api: {
5301 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5302 metadata: "my-bootclasspath-fragment/metadata.csv",
5303 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005304 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5305 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5306 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005307 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005308 }
5309
Liz Kammer2dc72442023-04-20 10:10:48 -04005310 prebuilt_systemserverclasspath_fragment {
5311 name: "my-systemserverclasspath-fragment",
5312 contents: ["libbaz"],
5313 apex_available: ["myapex"],
5314 }
5315
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005316 java_import {
5317 name: "libfoo",
5318 jars: ["libfoo.jar"],
5319 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005320 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005321 }
5322
5323 java_sdk_library_import {
5324 name: "libbar",
5325 public: {
5326 jars: ["libbar.jar"],
5327 },
5328 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005329 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005330 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005331 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005332
5333 java_sdk_library_import {
5334 name: "libbaz",
5335 public: {
5336 jars: ["libbaz.jar"],
5337 },
5338 apex_available: ["myapex"],
5339 shared_library: false,
5340 permitted_packages: ["baz"],
5341 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005342 `
5343
Paul Duffin89f570a2021-06-16 01:42:33 +01005344 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005345 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5346 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5347
Paul Duffin537ea3d2021-05-14 10:38:00 +01005348 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005349 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005350 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005351 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005352 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5353 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005354
5355 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5356
5357 overrideNames := []string{
5358 "",
5359 "myjavalib.myapex",
5360 "libfoo.myapex",
5361 "libbar.myapex",
5362 "libbaz.myapex",
5363 }
5364 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5365 for i, e := range mkEntries {
5366 g := e.OverrideName
5367 if w := overrideNames[i]; w != g {
5368 t.Errorf("Expected override name %q, got %q", w, g)
5369 }
5370 }
5371
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005372 })
5373
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005374 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5375 bp := `
5376 prebuilt_apex {
5377 name: "myapex",
5378 arch: {
5379 arm64: {
5380 src: "myapex-arm64.apex",
5381 },
5382 arm: {
5383 src: "myapex-arm.apex",
5384 },
5385 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005386 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5387 }
5388
5389 prebuilt_bootclasspath_fragment {
5390 name: "my-bootclasspath-fragment",
5391 contents: ["libfoo", "libbar"],
5392 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005393 hidden_api: {
5394 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5395 metadata: "my-bootclasspath-fragment/metadata.csv",
5396 index: "my-bootclasspath-fragment/index.csv",
5397 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5398 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5399 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005400 }
5401
5402 java_import {
5403 name: "libfoo",
5404 jars: ["libfoo.jar"],
5405 apex_available: ["myapex"],
5406 }
5407
5408 java_library {
5409 name: "libfoo",
5410 srcs: ["foo/bar/MyClass.java"],
5411 apex_available: ["myapex"],
5412 }
Paul Duffin37856732021-02-26 14:24:15 +00005413
5414 java_sdk_library_import {
5415 name: "libbar",
5416 public: {
5417 jars: ["libbar.jar"],
5418 },
5419 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005420 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005421 }
5422
5423 java_sdk_library {
5424 name: "libbar",
5425 srcs: ["foo/bar/MyClass.java"],
5426 unsafe_ignore_missing_latest_api: true,
5427 apex_available: ["myapex"],
5428 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 `
5430
5431 // In this test the source (java_library) libfoo is active since the
5432 // prebuilt (java_import) defaults to prefer:false. However the
5433 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5434 // find the dex boot jar in it. We either need to disable the source libfoo
5435 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005436 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005437 // dexbootjar check is skipped if AllowMissingDependencies is true
5438 preparerAllowMissingDeps := android.GroupFixturePreparers(
5439 preparer,
5440 android.PrepareForTestWithAllowMissingDependencies,
5441 )
5442 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005443 })
5444
5445 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5446 bp := `
5447 prebuilt_apex {
5448 name: "myapex",
5449 arch: {
5450 arm64: {
5451 src: "myapex-arm64.apex",
5452 },
5453 arm: {
5454 src: "myapex-arm.apex",
5455 },
5456 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005457 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5458 }
5459
5460 prebuilt_bootclasspath_fragment {
5461 name: "my-bootclasspath-fragment",
5462 contents: ["libfoo", "libbar"],
5463 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005464 hidden_api: {
5465 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5466 metadata: "my-bootclasspath-fragment/metadata.csv",
5467 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005468 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5469 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5470 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005471 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005472 }
5473
5474 java_import {
5475 name: "libfoo",
5476 prefer: true,
5477 jars: ["libfoo.jar"],
5478 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005479 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005480 }
5481
5482 java_library {
5483 name: "libfoo",
5484 srcs: ["foo/bar/MyClass.java"],
5485 apex_available: ["myapex"],
5486 }
Paul Duffin37856732021-02-26 14:24:15 +00005487
5488 java_sdk_library_import {
5489 name: "libbar",
5490 prefer: true,
5491 public: {
5492 jars: ["libbar.jar"],
5493 },
5494 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005495 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005496 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005497 }
5498
5499 java_sdk_library {
5500 name: "libbar",
5501 srcs: ["foo/bar/MyClass.java"],
5502 unsafe_ignore_missing_latest_api: true,
5503 apex_available: ["myapex"],
5504 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005505 `
5506
Paul Duffin89f570a2021-06-16 01:42:33 +01005507 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005508 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5509 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005510
Paul Duffin537ea3d2021-05-14 10:38:00 +01005511 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005512 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005513 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005514 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005515 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5516 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005517 })
5518
5519 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5520 bp := `
5521 apex {
5522 name: "myapex",
5523 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005524 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005525 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005526 }
5527
5528 apex_key {
5529 name: "myapex.key",
5530 public_key: "testkey.avbpubkey",
5531 private_key: "testkey.pem",
5532 }
5533
5534 prebuilt_apex {
5535 name: "myapex",
5536 arch: {
5537 arm64: {
5538 src: "myapex-arm64.apex",
5539 },
5540 arm: {
5541 src: "myapex-arm.apex",
5542 },
5543 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005544 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5545 }
5546
5547 prebuilt_bootclasspath_fragment {
5548 name: "my-bootclasspath-fragment",
5549 contents: ["libfoo", "libbar"],
5550 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005551 hidden_api: {
5552 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5553 metadata: "my-bootclasspath-fragment/metadata.csv",
5554 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005555 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5556 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5557 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005558 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 }
5560
5561 java_import {
5562 name: "libfoo",
5563 jars: ["libfoo.jar"],
5564 apex_available: ["myapex"],
5565 }
5566
5567 java_library {
5568 name: "libfoo",
5569 srcs: ["foo/bar/MyClass.java"],
5570 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005571 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005572 }
Paul Duffin37856732021-02-26 14:24:15 +00005573
5574 java_sdk_library_import {
5575 name: "libbar",
5576 public: {
5577 jars: ["libbar.jar"],
5578 },
5579 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005580 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005581 }
5582
5583 java_sdk_library {
5584 name: "libbar",
5585 srcs: ["foo/bar/MyClass.java"],
5586 unsafe_ignore_missing_latest_api: true,
5587 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005588 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005589 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005590 `
5591
Paul Duffin89f570a2021-06-16 01:42:33 +01005592 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005593 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5594 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005595
Paul Duffin537ea3d2021-05-14 10:38:00 +01005596 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005597 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005598 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005599 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005600 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5601 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005602 })
5603
5604 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5605 bp := `
5606 apex {
5607 name: "myapex",
5608 enabled: false,
5609 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005610 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005611 }
5612
5613 apex_key {
5614 name: "myapex.key",
5615 public_key: "testkey.avbpubkey",
5616 private_key: "testkey.pem",
5617 }
5618
5619 prebuilt_apex {
5620 name: "myapex",
5621 arch: {
5622 arm64: {
5623 src: "myapex-arm64.apex",
5624 },
5625 arm: {
5626 src: "myapex-arm.apex",
5627 },
5628 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005629 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5630 }
5631
5632 prebuilt_bootclasspath_fragment {
5633 name: "my-bootclasspath-fragment",
5634 contents: ["libfoo", "libbar"],
5635 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005636 hidden_api: {
5637 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5638 metadata: "my-bootclasspath-fragment/metadata.csv",
5639 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005640 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5641 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5642 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005643 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005644 }
5645
5646 java_import {
5647 name: "libfoo",
5648 prefer: true,
5649 jars: ["libfoo.jar"],
5650 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005651 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005652 }
5653
5654 java_library {
5655 name: "libfoo",
5656 srcs: ["foo/bar/MyClass.java"],
5657 apex_available: ["myapex"],
5658 }
Paul Duffin37856732021-02-26 14:24:15 +00005659
5660 java_sdk_library_import {
5661 name: "libbar",
5662 prefer: true,
5663 public: {
5664 jars: ["libbar.jar"],
5665 },
5666 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005667 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005668 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005669 }
5670
5671 java_sdk_library {
5672 name: "libbar",
5673 srcs: ["foo/bar/MyClass.java"],
5674 unsafe_ignore_missing_latest_api: true,
5675 apex_available: ["myapex"],
5676 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005677 `
5678
Paul Duffin89f570a2021-06-16 01:42:33 +01005679 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005680 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5681 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005682
Paul Duffin537ea3d2021-05-14 10:38:00 +01005683 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005684 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005685 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005686 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005687 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5688 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005689 })
5690}
5691
Roland Levillain630846d2019-06-26 12:48:34 +01005692func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005693 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005694 apex_test {
5695 name: "myapex",
5696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005697 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005698 tests: [
5699 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005700 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005701 ],
5702 }
5703
5704 apex_key {
5705 name: "myapex.key",
5706 public_key: "testkey.avbpubkey",
5707 private_key: "testkey.pem",
5708 }
5709
Liz Kammer1c14a212020-05-12 15:26:55 -07005710 filegroup {
5711 name: "fg",
5712 srcs: [
5713 "baz",
5714 "bar/baz"
5715 ],
5716 }
5717
Roland Levillain630846d2019-06-26 12:48:34 +01005718 cc_test {
5719 name: "mytest",
5720 gtest: false,
5721 srcs: ["mytest.cpp"],
5722 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005723 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005724 system_shared_libs: [],
5725 static_executable: true,
5726 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005727 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005728 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005729
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005730 cc_library {
5731 name: "mylib",
5732 srcs: ["mylib.cpp"],
5733 system_shared_libs: [],
5734 stl: "none",
5735 }
5736
Liz Kammer5bd365f2020-05-27 15:15:11 -07005737 filegroup {
5738 name: "fg2",
5739 srcs: [
5740 "testdata/baz"
5741 ],
5742 }
5743
Roland Levillain9b5fde92019-06-28 15:41:19 +01005744 cc_test {
5745 name: "mytests",
5746 gtest: false,
5747 srcs: [
5748 "mytest1.cpp",
5749 "mytest2.cpp",
5750 "mytest3.cpp",
5751 ],
5752 test_per_src: true,
5753 relative_install_path: "test",
5754 system_shared_libs: [],
5755 static_executable: true,
5756 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005757 data: [
5758 ":fg",
5759 ":fg2",
5760 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005761 }
Roland Levillain630846d2019-06-26 12:48:34 +01005762 `)
5763
Sundong Ahnabb64432019-10-22 13:58:29 +09005764 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005765 copyCmds := apexRule.Args["copy_commands"]
5766
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005767 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005768 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005769 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005770
Liz Kammer1c14a212020-05-12 15:26:55 -07005771 //Ensure that test data are copied into apex.
5772 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5773 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5774
Roland Levillain9b5fde92019-06-28 15:41:19 +01005775 // Ensure that test deps built with `test_per_src` are copied into apex.
5776 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5777 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5778 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005779
5780 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005781 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005782 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005783 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005784 prefix := "TARGET_"
5785 var builder strings.Builder
5786 data.Custom(&builder, name, prefix, "", data)
5787 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005788 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5789 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5790 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5791 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5792 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5793 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005794 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005795
5796 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005797 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005798 data.Custom(&builder, name, prefix, "", data)
5799 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005800 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5801 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005802}
5803
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005804func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005805 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005806 apex {
5807 name: "myapex",
5808 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005809 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005810 }
5811 apex_key {
5812 name: "myapex.key",
5813 public_key: "testkey.avbpubkey",
5814 private_key: "testkey.pem",
5815 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005816 `,
5817 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5818 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5819 }),
5820 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005821 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005822 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005823 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005824 var builder strings.Builder
5825 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5826 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005827 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005828}
5829
Jooyung Hand48f3c32019-08-23 11:18:57 +09005830func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5831 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5832 apex {
5833 name: "myapex",
5834 key: "myapex.key",
5835 native_shared_libs: ["libfoo"],
5836 }
5837
5838 apex_key {
5839 name: "myapex.key",
5840 public_key: "testkey.avbpubkey",
5841 private_key: "testkey.pem",
5842 }
5843
5844 cc_library {
5845 name: "libfoo",
5846 stl: "none",
5847 system_shared_libs: [],
5848 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005849 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005850 }
5851 `)
5852 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5853 apex {
5854 name: "myapex",
5855 key: "myapex.key",
5856 java_libs: ["myjar"],
5857 }
5858
5859 apex_key {
5860 name: "myapex.key",
5861 public_key: "testkey.avbpubkey",
5862 private_key: "testkey.pem",
5863 }
5864
5865 java_library {
5866 name: "myjar",
5867 srcs: ["foo/bar/MyClass.java"],
5868 sdk_version: "none",
5869 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005870 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005871 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005872 }
5873 `)
5874}
5875
Bill Peckhama41a6962021-01-11 10:58:54 -08005876func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005877 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005878 apex {
5879 name: "myapex",
5880 key: "myapex.key",
5881 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005882 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005883 }
5884
5885 apex_key {
5886 name: "myapex.key",
5887 public_key: "testkey.avbpubkey",
5888 private_key: "testkey.pem",
5889 }
5890
5891 java_import {
5892 name: "myjavaimport",
5893 apex_available: ["myapex"],
5894 jars: ["my.jar"],
5895 compile_dex: true,
5896 }
5897 `)
5898
5899 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5900 apexRule := module.Rule("apexRule")
5901 copyCmds := apexRule.Args["copy_commands"]
5902 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5903}
5904
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005905func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005906 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005907 apex {
5908 name: "myapex",
5909 key: "myapex.key",
5910 apps: [
5911 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005912 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005913 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005914 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005915 }
5916
5917 apex_key {
5918 name: "myapex.key",
5919 public_key: "testkey.avbpubkey",
5920 private_key: "testkey.pem",
5921 }
5922
5923 android_app {
5924 name: "AppFoo",
5925 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005926 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005927 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005928 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005929 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005930 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005931 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005932
5933 android_app {
5934 name: "AppFooPriv",
5935 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005936 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005937 system_modules: "none",
5938 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005939 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005940 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005941 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005942
5943 cc_library_shared {
5944 name: "libjni",
5945 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005946 shared_libs: ["libfoo"],
5947 stl: "none",
5948 system_shared_libs: [],
5949 apex_available: [ "myapex" ],
5950 sdk_version: "current",
5951 }
5952
5953 cc_library_shared {
5954 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005955 stl: "none",
5956 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005957 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005958 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005959 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005960 `)
5961
Sundong Ahnabb64432019-10-22 13:58:29 +09005962 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005963 apexRule := module.Rule("apexRule")
5964 copyCmds := apexRule.Args["copy_commands"]
5965
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005966 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5967 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005968
Colin Crossaede88c2020-08-11 12:17:01 -07005969 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005970 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005971 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005972 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005973 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005974 // JNI libraries including transitive deps are
5975 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005976 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005977 // ... embedded inside APK (jnilibs.zip)
5978 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5979 // ... and not directly inside the APEX
5980 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5981 }
Dario Frenicde2a032019-10-27 00:29:22 +01005982}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005983
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005984func TestApexWithAppImportBuildId(t *testing.T) {
5985 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5986 for _, id := range invalidBuildIds {
5987 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5988 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5989 variables.BuildId = proptools.StringPtr(id)
5990 })
5991 testApexError(t, message, `apex {
5992 name: "myapex",
5993 key: "myapex.key",
5994 apps: ["AppFooPrebuilt"],
5995 updatable: false,
5996 }
5997
5998 apex_key {
5999 name: "myapex.key",
6000 public_key: "testkey.avbpubkey",
6001 private_key: "testkey.pem",
6002 }
6003
6004 android_app_import {
6005 name: "AppFooPrebuilt",
6006 apk: "PrebuiltAppFoo.apk",
6007 presigned: true,
6008 apex_available: ["myapex"],
6009 }
6010 `, fixture)
6011 }
6012}
6013
Dario Frenicde2a032019-10-27 00:29:22 +01006014func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006015 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006016 apex {
6017 name: "myapex",
6018 key: "myapex.key",
6019 apps: [
6020 "AppFooPrebuilt",
6021 "AppFooPrivPrebuilt",
6022 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006023 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006024 }
6025
6026 apex_key {
6027 name: "myapex.key",
6028 public_key: "testkey.avbpubkey",
6029 private_key: "testkey.pem",
6030 }
6031
6032 android_app_import {
6033 name: "AppFooPrebuilt",
6034 apk: "PrebuiltAppFoo.apk",
6035 presigned: true,
6036 dex_preopt: {
6037 enabled: false,
6038 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006039 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006040 }
6041
6042 android_app_import {
6043 name: "AppFooPrivPrebuilt",
6044 apk: "PrebuiltAppFooPriv.apk",
6045 privileged: true,
6046 presigned: true,
6047 dex_preopt: {
6048 enabled: false,
6049 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006050 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006051 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006052 }
6053 `)
6054
Sundong Ahnabb64432019-10-22 13:58:29 +09006055 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01006056 apexRule := module.Rule("apexRule")
6057 copyCmds := apexRule.Args["copy_commands"]
6058
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006059 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6060 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006061}
6062
6063func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006064 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006065 apex {
6066 name: "myapex",
6067 key: "myapex.key",
6068 apps: [
6069 "AppFoo",
6070 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006071 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006072 }
6073
6074 apex_key {
6075 name: "myapex.key",
6076 public_key: "testkey.avbpubkey",
6077 private_key: "testkey.pem",
6078 }
6079
6080 android_app {
6081 name: "AppFoo",
6082 srcs: ["foo/bar/MyClass.java"],
6083 sdk_version: "none",
6084 system_modules: "none",
6085 apex_available: [ "myapex" ],
6086 }
6087
6088 android_app_import {
6089 name: "AppFoo",
6090 apk: "AppFooPrebuilt.apk",
6091 filename: "AppFooPrebuilt.apk",
6092 presigned: true,
6093 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006094 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006095 }
6096 `, withFiles(map[string][]byte{
6097 "AppFooPrebuilt.apk": nil,
6098 }))
6099
6100 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006101 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006102 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006103}
6104
Dario Freni6f3937c2019-12-20 22:58:03 +00006105func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006106 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006107 apex {
6108 name: "myapex",
6109 key: "myapex.key",
6110 apps: [
6111 "TesterHelpAppFoo",
6112 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006113 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006114 }
6115
6116 apex_key {
6117 name: "myapex.key",
6118 public_key: "testkey.avbpubkey",
6119 private_key: "testkey.pem",
6120 }
6121
6122 android_test_helper_app {
6123 name: "TesterHelpAppFoo",
6124 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006125 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006126 }
6127
6128 `)
6129
6130 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6131 apexRule := module.Rule("apexRule")
6132 copyCmds := apexRule.Args["copy_commands"]
6133
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006134 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006135}
6136
Jooyung Han18020ea2019-11-13 10:50:48 +09006137func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6138 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006139 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006140 apex {
6141 name: "myapex",
6142 key: "myapex.key",
6143 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006144 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006145 }
6146
6147 apex_key {
6148 name: "myapex.key",
6149 public_key: "testkey.avbpubkey",
6150 private_key: "testkey.pem",
6151 }
6152
6153 apex {
6154 name: "otherapex",
6155 key: "myapex.key",
6156 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006157 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006158 }
6159
6160 cc_defaults {
6161 name: "libfoo-defaults",
6162 apex_available: ["otherapex"],
6163 }
6164
6165 cc_library {
6166 name: "libfoo",
6167 defaults: ["libfoo-defaults"],
6168 stl: "none",
6169 system_shared_libs: [],
6170 }`)
6171}
6172
Paul Duffine52e66f2020-03-30 17:54:29 +01006173func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006174 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006175 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006176 apex {
6177 name: "myapex",
6178 key: "myapex.key",
6179 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006180 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006181 }
6182
6183 apex_key {
6184 name: "myapex.key",
6185 public_key: "testkey.avbpubkey",
6186 private_key: "testkey.pem",
6187 }
6188
6189 apex {
6190 name: "otherapex",
6191 key: "otherapex.key",
6192 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006193 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006194 }
6195
6196 apex_key {
6197 name: "otherapex.key",
6198 public_key: "testkey.avbpubkey",
6199 private_key: "testkey.pem",
6200 }
6201
6202 cc_library {
6203 name: "libfoo",
6204 stl: "none",
6205 system_shared_libs: [],
6206 apex_available: ["otherapex"],
6207 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006208}
Jiyong Park127b40b2019-09-30 16:04:35 +09006209
Paul Duffine52e66f2020-03-30 17:54:29 +01006210func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006211 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006212 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006213.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006214.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006215.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006216.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006217.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006218.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006219 apex {
6220 name: "myapex",
6221 key: "myapex.key",
6222 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006223 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006224 }
6225
6226 apex_key {
6227 name: "myapex.key",
6228 public_key: "testkey.avbpubkey",
6229 private_key: "testkey.pem",
6230 }
6231
Jiyong Park127b40b2019-09-30 16:04:35 +09006232 cc_library {
6233 name: "libfoo",
6234 stl: "none",
6235 shared_libs: ["libbar"],
6236 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006237 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006238 }
6239
6240 cc_library {
6241 name: "libbar",
6242 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006243 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006244 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006245 apex_available: ["myapex"],
6246 }
6247
6248 cc_library {
6249 name: "libbaz",
6250 stl: "none",
6251 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006252 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006253}
Jiyong Park127b40b2019-09-30 16:04:35 +09006254
Paul Duffine52e66f2020-03-30 17:54:29 +01006255func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006256 testApexError(t, "\"otherapex\" is not a valid module name", `
6257 apex {
6258 name: "myapex",
6259 key: "myapex.key",
6260 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006261 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006262 }
6263
6264 apex_key {
6265 name: "myapex.key",
6266 public_key: "testkey.avbpubkey",
6267 private_key: "testkey.pem",
6268 }
6269
6270 cc_library {
6271 name: "libfoo",
6272 stl: "none",
6273 system_shared_libs: [],
6274 apex_available: ["otherapex"],
6275 }`)
6276
Paul Duffine52e66f2020-03-30 17:54:29 +01006277 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006278 apex {
6279 name: "myapex",
6280 key: "myapex.key",
6281 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006282 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006283 }
6284
6285 apex_key {
6286 name: "myapex.key",
6287 public_key: "testkey.avbpubkey",
6288 private_key: "testkey.pem",
6289 }
6290
6291 cc_library {
6292 name: "libfoo",
6293 stl: "none",
6294 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006295 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006296 apex_available: ["myapex"],
6297 }
6298
6299 cc_library {
6300 name: "libbar",
6301 stl: "none",
6302 system_shared_libs: [],
6303 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006304 }
6305
6306 cc_library {
6307 name: "libbaz",
6308 stl: "none",
6309 system_shared_libs: [],
6310 stubs: {
6311 versions: ["10", "20", "30"],
6312 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006313 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006314}
Jiyong Park127b40b2019-09-30 16:04:35 +09006315
Jiyong Park89e850a2020-04-07 16:37:39 +09006316func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006317 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006318 apex {
6319 name: "myapex",
6320 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006321 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006322 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006323 }
6324
6325 apex_key {
6326 name: "myapex.key",
6327 public_key: "testkey.avbpubkey",
6328 private_key: "testkey.pem",
6329 }
6330
6331 cc_library {
6332 name: "libfoo",
6333 stl: "none",
6334 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006335 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006336 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006337 }
6338
6339 cc_library {
6340 name: "libfoo2",
6341 stl: "none",
6342 system_shared_libs: [],
6343 shared_libs: ["libbaz"],
6344 apex_available: ["//apex_available:platform"],
6345 }
6346
6347 cc_library {
6348 name: "libbar",
6349 stl: "none",
6350 system_shared_libs: [],
6351 apex_available: ["myapex"],
6352 }
6353
6354 cc_library {
6355 name: "libbaz",
6356 stl: "none",
6357 system_shared_libs: [],
6358 apex_available: ["myapex"],
6359 stubs: {
6360 versions: ["1"],
6361 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006362 }`)
6363
Jiyong Park89e850a2020-04-07 16:37:39 +09006364 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6365 // because it depends on libbar which isn't available to platform
6366 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6367 if libfoo.NotAvailableForPlatform() != true {
6368 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6369 }
6370
6371 // libfoo2 however can be available to platform because it depends on libbaz which provides
6372 // stubs
6373 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6374 if libfoo2.NotAvailableForPlatform() == true {
6375 t.Errorf("%q should be available to platform", libfoo2.String())
6376 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006377}
Jiyong Parka90ca002019-10-07 15:47:24 +09006378
Paul Duffine52e66f2020-03-30 17:54:29 +01006379func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006380 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006381 apex {
6382 name: "myapex",
6383 key: "myapex.key",
6384 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006385 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006386 }
6387
6388 apex_key {
6389 name: "myapex.key",
6390 public_key: "testkey.avbpubkey",
6391 private_key: "testkey.pem",
6392 }
6393
6394 cc_library {
6395 name: "libfoo",
6396 stl: "none",
6397 system_shared_libs: [],
6398 apex_available: ["myapex"],
6399 static: {
6400 apex_available: ["//apex_available:platform"],
6401 },
6402 }`)
6403
Jiyong Park89e850a2020-04-07 16:37:39 +09006404 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6405 if libfooShared.NotAvailableForPlatform() != true {
6406 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6407 }
6408 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6409 if libfooStatic.NotAvailableForPlatform() != false {
6410 t.Errorf("%q should be available to platform", libfooStatic.String())
6411 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006412}
6413
Jiyong Park5d790c32019-11-15 18:40:32 +09006414func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006415 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006416 apex {
6417 name: "myapex",
6418 key: "myapex.key",
6419 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006420 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006421 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006422 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006423 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006424 }
6425
6426 override_apex {
6427 name: "override_myapex",
6428 base: "myapex",
6429 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006430 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006431 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006432 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006433 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006434 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006435 key: "mynewapex.key",
6436 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006437 }
6438
6439 apex_key {
6440 name: "myapex.key",
6441 public_key: "testkey.avbpubkey",
6442 private_key: "testkey.pem",
6443 }
6444
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006445 apex_key {
6446 name: "mynewapex.key",
6447 public_key: "testkey2.avbpubkey",
6448 private_key: "testkey2.pem",
6449 }
6450
6451 android_app_certificate {
6452 name: "myapex.certificate",
6453 certificate: "testkey",
6454 }
6455
Jiyong Park5d790c32019-11-15 18:40:32 +09006456 android_app {
6457 name: "app",
6458 srcs: ["foo/bar/MyClass.java"],
6459 package_name: "foo",
6460 sdk_version: "none",
6461 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006462 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006463 }
6464
6465 override_android_app {
6466 name: "override_app",
6467 base: "app",
6468 package_name: "bar",
6469 }
markchien7c803b82021-08-26 22:10:06 +08006470
6471 bpf {
6472 name: "bpf",
6473 srcs: ["bpf.c"],
6474 }
6475
6476 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006477 name: "overrideBpf",
6478 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006479 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006480
6481 prebuilt_etc {
6482 name: "myetc",
6483 src: "myprebuilt",
6484 }
6485
6486 prebuilt_etc {
6487 name: "override_myetc",
6488 src: "override_myprebuilt",
6489 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006490 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006491
Jiyong Park317645e2019-12-05 13:20:58 +09006492 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6493 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6494 if originalVariant.GetOverriddenBy() != "" {
6495 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6496 }
6497 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6498 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6499 }
6500
Jiyong Park5d790c32019-11-15 18:40:32 +09006501 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6502 apexRule := module.Rule("apexRule")
6503 copyCmds := apexRule.Args["copy_commands"]
6504
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006505 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6506 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006507
markchien7c803b82021-08-26 22:10:06 +08006508 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006509 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006510
Daniel Norman5a3ce132021-08-26 15:44:43 -07006511 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6512 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6513
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006514 apexBundle := module.Module().(*apexBundle)
6515 name := apexBundle.Name()
6516 if name != "override_myapex" {
6517 t.Errorf("name should be \"override_myapex\", but was %q", name)
6518 }
6519
Baligh Uddin004d7172020-02-19 21:29:28 -08006520 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6521 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6522 }
6523
Jiyong Park20bacab2020-03-03 11:45:41 +09006524 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006525 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006526 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6527
6528 signApkRule := module.Rule("signapk")
6529 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006530
Colin Crossaa255532020-07-03 13:18:24 -07006531 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006532 var builder strings.Builder
6533 data.Custom(&builder, name, "TARGET_", "", data)
6534 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006535 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6536 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6537 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006538 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006539 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006540 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006541 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006542 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006543 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6544 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006545}
6546
Albert Martineefabcf2022-03-21 20:11:16 +00006547func TestMinSdkVersionOverride(t *testing.T) {
6548 // Override from 29 to 31
6549 minSdkOverride31 := "31"
6550 ctx := testApex(t, `
6551 apex {
6552 name: "myapex",
6553 key: "myapex.key",
6554 native_shared_libs: ["mylib"],
6555 updatable: true,
6556 min_sdk_version: "29"
6557 }
6558
6559 override_apex {
6560 name: "override_myapex",
6561 base: "myapex",
6562 logging_parent: "com.foo.bar",
6563 package_name: "test.overridden.package"
6564 }
6565
6566 apex_key {
6567 name: "myapex.key",
6568 public_key: "testkey.avbpubkey",
6569 private_key: "testkey.pem",
6570 }
6571
6572 cc_library {
6573 name: "mylib",
6574 srcs: ["mylib.cpp"],
6575 runtime_libs: ["libbar"],
6576 system_shared_libs: [],
6577 stl: "none",
6578 apex_available: [ "myapex" ],
6579 min_sdk_version: "apex_inherit"
6580 }
6581
6582 cc_library {
6583 name: "libbar",
6584 srcs: ["mylib.cpp"],
6585 system_shared_libs: [],
6586 stl: "none",
6587 apex_available: [ "myapex" ],
6588 min_sdk_version: "apex_inherit"
6589 }
6590
6591 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6592
6593 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6594 copyCmds := apexRule.Args["copy_commands"]
6595
6596 // Ensure that direct non-stubs dep is always included
6597 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6598
6599 // Ensure that runtime_libs dep in included
6600 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6601
6602 // Ensure libraries target overridden min_sdk_version value
6603 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6604}
6605
6606func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6607 // Attempt to override from 31 to 29, should be a NOOP
6608 minSdkOverride29 := "29"
6609 ctx := testApex(t, `
6610 apex {
6611 name: "myapex",
6612 key: "myapex.key",
6613 native_shared_libs: ["mylib"],
6614 updatable: true,
6615 min_sdk_version: "31"
6616 }
6617
6618 override_apex {
6619 name: "override_myapex",
6620 base: "myapex",
6621 logging_parent: "com.foo.bar",
6622 package_name: "test.overridden.package"
6623 }
6624
6625 apex_key {
6626 name: "myapex.key",
6627 public_key: "testkey.avbpubkey",
6628 private_key: "testkey.pem",
6629 }
6630
6631 cc_library {
6632 name: "mylib",
6633 srcs: ["mylib.cpp"],
6634 runtime_libs: ["libbar"],
6635 system_shared_libs: [],
6636 stl: "none",
6637 apex_available: [ "myapex" ],
6638 min_sdk_version: "apex_inherit"
6639 }
6640
6641 cc_library {
6642 name: "libbar",
6643 srcs: ["mylib.cpp"],
6644 system_shared_libs: [],
6645 stl: "none",
6646 apex_available: [ "myapex" ],
6647 min_sdk_version: "apex_inherit"
6648 }
6649
6650 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6651
6652 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6653 copyCmds := apexRule.Args["copy_commands"]
6654
6655 // Ensure that direct non-stubs dep is always included
6656 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6657
6658 // Ensure that runtime_libs dep in included
6659 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6660
6661 // Ensure libraries target the original min_sdk_version value rather than the overridden
6662 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6663}
6664
Jooyung Han214bf372019-11-12 13:03:50 +09006665func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006666 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006667 apex {
6668 name: "myapex",
6669 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006670 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006671 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006672 }
6673
6674 apex_key {
6675 name: "myapex.key",
6676 public_key: "testkey.avbpubkey",
6677 private_key: "testkey.pem",
6678 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006679
6680 cc_library {
6681 name: "mylib",
6682 srcs: ["mylib.cpp"],
6683 stl: "libc++",
6684 system_shared_libs: [],
6685 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006686 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006687 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006688 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006689
6690 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6691 args := module.Rule("apexRule").Args
6692 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006693 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006694
6695 // The copies of the libraries in the apex should have one more dependency than
6696 // the ones outside the apex, namely the unwinder. Ideally we should check
6697 // the dependency names directly here but for some reason the names are blank in
6698 // this test.
6699 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006700 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006701 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6702 if len(apexImplicits) != len(nonApexImplicits)+1 {
6703 t.Errorf("%q missing unwinder dep", lib)
6704 }
6705 }
Jooyung Han214bf372019-11-12 13:03:50 +09006706}
6707
Paul Duffine05480a2021-03-08 15:07:14 +00006708var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006709 "api/current.txt": nil,
6710 "api/removed.txt": nil,
6711 "api/system-current.txt": nil,
6712 "api/system-removed.txt": nil,
6713 "api/test-current.txt": nil,
6714 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006715
Anton Hanssondff2c782020-12-21 17:10:01 +00006716 "100/public/api/foo.txt": nil,
6717 "100/public/api/foo-removed.txt": nil,
6718 "100/system/api/foo.txt": nil,
6719 "100/system/api/foo-removed.txt": nil,
6720
Paul Duffineedc5d52020-06-12 17:46:39 +01006721 // For java_sdk_library_import
6722 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006723}
6724
Jooyung Han58f26ab2019-12-18 15:34:32 +09006725func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006726 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006727 apex {
6728 name: "myapex",
6729 key: "myapex.key",
6730 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006731 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006732 }
6733
6734 apex_key {
6735 name: "myapex.key",
6736 public_key: "testkey.avbpubkey",
6737 private_key: "testkey.pem",
6738 }
6739
6740 java_sdk_library {
6741 name: "foo",
6742 srcs: ["a.java"],
6743 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006744 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006745 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006746
6747 prebuilt_apis {
6748 name: "sdk",
6749 api_dirs: ["100"],
6750 }
Paul Duffin9b879592020-05-26 13:21:35 +01006751 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006752
6753 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006754 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006755 "javalib/foo.jar",
6756 "etc/permissions/foo.xml",
6757 })
6758 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006759 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006760 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 +09006761}
6762
Paul Duffin9b879592020-05-26 13:21:35 +01006763func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006764 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006765 apex {
6766 name: "myapex",
6767 key: "myapex.key",
6768 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006769 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006770 }
6771
6772 apex_key {
6773 name: "myapex.key",
6774 public_key: "testkey.avbpubkey",
6775 private_key: "testkey.pem",
6776 }
6777
6778 java_sdk_library {
6779 name: "foo",
6780 srcs: ["a.java"],
6781 api_packages: ["foo"],
6782 apex_available: ["myapex"],
6783 sdk_version: "none",
6784 system_modules: "none",
6785 }
6786
6787 java_library {
6788 name: "bar",
6789 srcs: ["a.java"],
6790 libs: ["foo"],
6791 apex_available: ["myapex"],
6792 sdk_version: "none",
6793 system_modules: "none",
6794 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006795
6796 prebuilt_apis {
6797 name: "sdk",
6798 api_dirs: ["100"],
6799 }
Paul Duffin9b879592020-05-26 13:21:35 +01006800 `, withFiles(filesForSdkLibrary))
6801
6802 // java_sdk_library installs both impl jar and permission XML
6803 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6804 "javalib/bar.jar",
6805 "javalib/foo.jar",
6806 "etc/permissions/foo.xml",
6807 })
6808
6809 // The bar library should depend on the implementation jar.
6810 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006811 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006812 t.Errorf("expected %q, found %#q", expected, actual)
6813 }
6814}
6815
6816func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006817 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006818 apex {
6819 name: "myapex",
6820 key: "myapex.key",
6821 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006822 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006823 }
6824
6825 apex_key {
6826 name: "myapex.key",
6827 public_key: "testkey.avbpubkey",
6828 private_key: "testkey.pem",
6829 }
6830
6831 java_sdk_library {
6832 name: "foo",
6833 srcs: ["a.java"],
6834 api_packages: ["foo"],
6835 apex_available: ["myapex"],
6836 sdk_version: "none",
6837 system_modules: "none",
6838 }
6839
6840 java_library {
6841 name: "bar",
6842 srcs: ["a.java"],
6843 libs: ["foo"],
6844 sdk_version: "none",
6845 system_modules: "none",
6846 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006847
6848 prebuilt_apis {
6849 name: "sdk",
6850 api_dirs: ["100"],
6851 }
Paul Duffin9b879592020-05-26 13:21:35 +01006852 `, withFiles(filesForSdkLibrary))
6853
6854 // java_sdk_library installs both impl jar and permission XML
6855 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6856 "javalib/foo.jar",
6857 "etc/permissions/foo.xml",
6858 })
6859
6860 // The bar library should depend on the stubs jar.
6861 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006862 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006863 t.Errorf("expected %q, found %#q", expected, actual)
6864 }
6865}
6866
Paul Duffineedc5d52020-06-12 17:46:39 +01006867func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006868 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006869 prebuilt_apis {
6870 name: "sdk",
6871 api_dirs: ["100"],
6872 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006873 withFiles(map[string][]byte{
6874 "apex/a.java": nil,
6875 "apex/apex_manifest.json": nil,
6876 "apex/Android.bp": []byte(`
6877 package {
6878 default_visibility: ["//visibility:private"],
6879 }
6880
6881 apex {
6882 name: "myapex",
6883 key: "myapex.key",
6884 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006885 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006886 }
6887
6888 apex_key {
6889 name: "myapex.key",
6890 public_key: "testkey.avbpubkey",
6891 private_key: "testkey.pem",
6892 }
6893
6894 java_library {
6895 name: "bar",
6896 srcs: ["a.java"],
6897 libs: ["foo"],
6898 apex_available: ["myapex"],
6899 sdk_version: "none",
6900 system_modules: "none",
6901 }
6902`),
6903 "source/a.java": nil,
6904 "source/api/current.txt": nil,
6905 "source/api/removed.txt": nil,
6906 "source/Android.bp": []byte(`
6907 package {
6908 default_visibility: ["//visibility:private"],
6909 }
6910
6911 java_sdk_library {
6912 name: "foo",
6913 visibility: ["//apex"],
6914 srcs: ["a.java"],
6915 api_packages: ["foo"],
6916 apex_available: ["myapex"],
6917 sdk_version: "none",
6918 system_modules: "none",
6919 public: {
6920 enabled: true,
6921 },
6922 }
6923`),
6924 "prebuilt/a.jar": nil,
6925 "prebuilt/Android.bp": []byte(`
6926 package {
6927 default_visibility: ["//visibility:private"],
6928 }
6929
6930 java_sdk_library_import {
6931 name: "foo",
6932 visibility: ["//apex", "//source"],
6933 apex_available: ["myapex"],
6934 prefer: true,
6935 public: {
6936 jars: ["a.jar"],
6937 },
6938 }
6939`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006940 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006941 )
6942
6943 // java_sdk_library installs both impl jar and permission XML
6944 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6945 "javalib/bar.jar",
6946 "javalib/foo.jar",
6947 "etc/permissions/foo.xml",
6948 })
6949
6950 // The bar library should depend on the implementation jar.
6951 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006952 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006953 t.Errorf("expected %q, found %#q", expected, actual)
6954 }
6955}
6956
6957func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6958 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6959 apex {
6960 name: "myapex",
6961 key: "myapex.key",
6962 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006963 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006964 }
6965
6966 apex_key {
6967 name: "myapex.key",
6968 public_key: "testkey.avbpubkey",
6969 private_key: "testkey.pem",
6970 }
6971
6972 java_sdk_library_import {
6973 name: "foo",
6974 apex_available: ["myapex"],
6975 prefer: true,
6976 public: {
6977 jars: ["a.jar"],
6978 },
6979 }
6980
6981 `, withFiles(filesForSdkLibrary))
6982}
6983
atrost6e126252020-01-27 17:01:16 +00006984func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006985 result := android.GroupFixturePreparers(
6986 prepareForApexTest,
6987 java.PrepareForTestWithPlatformCompatConfig,
6988 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006989 apex {
6990 name: "myapex",
6991 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006992 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006993 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006994 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006995 }
6996
6997 apex_key {
6998 name: "myapex.key",
6999 public_key: "testkey.avbpubkey",
7000 private_key: "testkey.pem",
7001 }
7002
7003 platform_compat_config {
7004 name: "myjar-platform-compat-config",
7005 src: ":myjar",
7006 }
7007
7008 java_library {
7009 name: "myjar",
7010 srcs: ["foo/bar/MyClass.java"],
7011 sdk_version: "none",
7012 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007013 apex_available: [ "myapex" ],
7014 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007015
7016 // Make sure that a preferred prebuilt does not affect the apex contents.
7017 prebuilt_platform_compat_config {
7018 name: "myjar-platform-compat-config",
7019 metadata: "compat-config/metadata.xml",
7020 prefer: true,
7021 }
atrost6e126252020-01-27 17:01:16 +00007022 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007023 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007024 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7025 "etc/compatconfig/myjar-platform-compat-config.xml",
7026 "javalib/myjar.jar",
7027 })
7028}
7029
Jooyung Han862c0d62022-12-21 10:15:37 +09007030func TestNoDupeApexFiles(t *testing.T) {
7031 android.GroupFixturePreparers(
7032 android.PrepareForTestWithAndroidBuildComponents,
7033 PrepareForTestWithApexBuildComponents,
7034 prepareForTestWithMyapex,
7035 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7036 ).
7037 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7038 RunTestWithBp(t, `
7039 apex {
7040 name: "myapex",
7041 key: "myapex.key",
7042 prebuilts: ["foo", "bar"],
7043 updatable: false,
7044 }
7045
7046 apex_key {
7047 name: "myapex.key",
7048 public_key: "testkey.avbpubkey",
7049 private_key: "testkey.pem",
7050 }
7051
7052 prebuilt_etc {
7053 name: "foo",
7054 src: "myprebuilt",
7055 filename_from_src: true,
7056 }
7057
7058 prebuilt_etc {
7059 name: "bar",
7060 src: "myprebuilt",
7061 filename_from_src: true,
7062 }
7063 `)
7064}
7065
Jiyong Park479321d2019-12-16 11:47:12 +09007066func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7067 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7068 apex {
7069 name: "myapex",
7070 key: "myapex.key",
7071 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007072 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007073 }
7074
7075 apex_key {
7076 name: "myapex.key",
7077 public_key: "testkey.avbpubkey",
7078 private_key: "testkey.pem",
7079 }
7080
7081 java_library {
7082 name: "myjar",
7083 srcs: ["foo/bar/MyClass.java"],
7084 sdk_version: "none",
7085 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007086 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007087 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007088 }
7089 `)
7090}
7091
Jiyong Park7afd1072019-12-30 16:56:33 +09007092func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007093 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007094 apex {
7095 name: "myapex",
7096 key: "myapex.key",
7097 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007098 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007099 }
7100
7101 apex_key {
7102 name: "myapex.key",
7103 public_key: "testkey.avbpubkey",
7104 private_key: "testkey.pem",
7105 }
7106
7107 cc_library {
7108 name: "mylib",
7109 srcs: ["mylib.cpp"],
7110 system_shared_libs: [],
7111 stl: "none",
7112 required: ["a", "b"],
7113 host_required: ["c", "d"],
7114 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007115 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007116 }
7117 `)
7118
7119 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007120 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007121 name := apexBundle.BaseModuleName()
7122 prefix := "TARGET_"
7123 var builder strings.Builder
7124 data.Custom(&builder, name, prefix, "", data)
7125 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007126 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 -08007127 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7128 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007129}
7130
Jiyong Park7cd10e32020-01-14 09:22:18 +09007131func TestSymlinksFromApexToSystem(t *testing.T) {
7132 bp := `
7133 apex {
7134 name: "myapex",
7135 key: "myapex.key",
7136 native_shared_libs: ["mylib"],
7137 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007138 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007139 }
7140
Jiyong Park9d677202020-02-19 16:29:35 +09007141 apex {
7142 name: "myapex.updatable",
7143 key: "myapex.key",
7144 native_shared_libs: ["mylib"],
7145 java_libs: ["myjar"],
7146 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007147 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007148 }
7149
Jiyong Park7cd10e32020-01-14 09:22:18 +09007150 apex_key {
7151 name: "myapex.key",
7152 public_key: "testkey.avbpubkey",
7153 private_key: "testkey.pem",
7154 }
7155
7156 cc_library {
7157 name: "mylib",
7158 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007159 shared_libs: [
7160 "myotherlib",
7161 "myotherlib_ext",
7162 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007163 system_shared_libs: [],
7164 stl: "none",
7165 apex_available: [
7166 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007167 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007168 "//apex_available:platform",
7169 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007170 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007171 }
7172
7173 cc_library {
7174 name: "myotherlib",
7175 srcs: ["mylib.cpp"],
7176 system_shared_libs: [],
7177 stl: "none",
7178 apex_available: [
7179 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007180 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007181 "//apex_available:platform",
7182 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007183 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007184 }
7185
Jiyong Parkce243632023-02-17 18:22:25 +09007186 cc_library {
7187 name: "myotherlib_ext",
7188 srcs: ["mylib.cpp"],
7189 system_shared_libs: [],
7190 system_ext_specific: true,
7191 stl: "none",
7192 apex_available: [
7193 "myapex",
7194 "myapex.updatable",
7195 "//apex_available:platform",
7196 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007197 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007198 }
7199
Jiyong Park7cd10e32020-01-14 09:22:18 +09007200 java_library {
7201 name: "myjar",
7202 srcs: ["foo/bar/MyClass.java"],
7203 sdk_version: "none",
7204 system_modules: "none",
7205 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007206 apex_available: [
7207 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007208 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007209 "//apex_available:platform",
7210 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007211 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007212 }
7213
7214 java_library {
7215 name: "myotherjar",
7216 srcs: ["foo/bar/MyClass.java"],
7217 sdk_version: "none",
7218 system_modules: "none",
7219 apex_available: [
7220 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007221 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007222 "//apex_available:platform",
7223 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007224 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007225 }
7226 `
7227
7228 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7229 for _, f := range files {
7230 if f.path == file {
7231 if f.isLink {
7232 t.Errorf("%q is not a real file", file)
7233 }
7234 return
7235 }
7236 }
7237 t.Errorf("%q is not found", file)
7238 }
7239
Jiyong Parkce243632023-02-17 18:22:25 +09007240 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007241 for _, f := range files {
7242 if f.path == file {
7243 if !f.isLink {
7244 t.Errorf("%q is not a symlink", file)
7245 }
Jiyong Parkce243632023-02-17 18:22:25 +09007246 if f.src != target {
7247 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7248 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007249 return
7250 }
7251 }
7252 t.Errorf("%q is not found", file)
7253 }
7254
Jiyong Park9d677202020-02-19 16:29:35 +09007255 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7256 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007257 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007258 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007259 ensureRealfileExists(t, files, "javalib/myjar.jar")
7260 ensureRealfileExists(t, files, "lib64/mylib.so")
7261 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007262 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007263
Jiyong Park9d677202020-02-19 16:29:35 +09007264 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7265 ensureRealfileExists(t, files, "javalib/myjar.jar")
7266 ensureRealfileExists(t, files, "lib64/mylib.so")
7267 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007268 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007269
7270 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007271 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007272 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007273 ensureRealfileExists(t, files, "javalib/myjar.jar")
7274 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007275 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7276 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007277
7278 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7279 ensureRealfileExists(t, files, "javalib/myjar.jar")
7280 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007281 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7282 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007283}
7284
Yo Chiange8128052020-07-23 20:09:18 +08007285func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007286 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007287 apex {
7288 name: "myapex",
7289 key: "myapex.key",
7290 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007291 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007292 }
7293
7294 apex_key {
7295 name: "myapex.key",
7296 public_key: "testkey.avbpubkey",
7297 private_key: "testkey.pem",
7298 }
7299
7300 cc_library_shared {
7301 name: "mylib",
7302 srcs: ["mylib.cpp"],
7303 shared_libs: ["myotherlib"],
7304 system_shared_libs: [],
7305 stl: "none",
7306 apex_available: [
7307 "myapex",
7308 "//apex_available:platform",
7309 ],
7310 }
7311
7312 cc_prebuilt_library_shared {
7313 name: "myotherlib",
7314 srcs: ["prebuilt.so"],
7315 system_shared_libs: [],
7316 stl: "none",
7317 apex_available: [
7318 "myapex",
7319 "//apex_available:platform",
7320 ],
7321 }
7322 `)
7323
Prerana Patilb1896c82022-11-09 18:14:34 +00007324 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007325 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007326 var builder strings.Builder
7327 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7328 androidMk := builder.String()
7329 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007330 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007331 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7332 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7333 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007334 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 +08007335}
7336
Jooyung Han643adc42020-02-27 13:50:06 +09007337func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007338 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007339 apex {
7340 name: "myapex",
7341 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007342 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007343 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007344 }
7345
7346 apex_key {
7347 name: "myapex.key",
7348 public_key: "testkey.avbpubkey",
7349 private_key: "testkey.pem",
7350 }
7351
7352 cc_library {
7353 name: "mylib",
7354 srcs: ["mylib.cpp"],
7355 shared_libs: ["mylib2"],
7356 system_shared_libs: [],
7357 stl: "none",
7358 apex_available: [ "myapex" ],
7359 }
7360
7361 cc_library {
7362 name: "mylib2",
7363 srcs: ["mylib.cpp"],
7364 system_shared_libs: [],
7365 stl: "none",
7366 apex_available: [ "myapex" ],
7367 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007368
7369 rust_ffi_shared {
7370 name: "libfoo.rust",
7371 crate_name: "foo",
7372 srcs: ["foo.rs"],
7373 shared_libs: ["libfoo.shared_from_rust"],
7374 prefer_rlib: true,
7375 apex_available: ["myapex"],
7376 }
7377
7378 cc_library_shared {
7379 name: "libfoo.shared_from_rust",
7380 srcs: ["mylib.cpp"],
7381 system_shared_libs: [],
7382 stl: "none",
7383 stubs: {
7384 versions: ["10", "11", "12"],
7385 },
7386 }
7387
Jooyung Han643adc42020-02-27 13:50:06 +09007388 `)
7389
7390 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7391 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007392 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007393 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7394 "lib64/mylib.so",
7395 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007396 "lib64/libfoo.rust.so",
7397 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7398 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007399 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007400
7401 // b/220397949
7402 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007403}
7404
Jooyung Han49f67012020-04-17 13:43:10 +09007405func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007406 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007407 apex {
7408 name: "myapex",
7409 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007410 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007411 }
7412 apex_key {
7413 name: "myapex.key",
7414 public_key: "testkey.avbpubkey",
7415 private_key: "testkey.pem",
7416 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007417 `,
7418 android.FixtureModifyConfig(func(config android.Config) {
7419 delete(config.Targets, android.Android)
7420 config.AndroidCommonTarget = android.Target{}
7421 }),
7422 )
Jooyung Han49f67012020-04-17 13:43:10 +09007423
7424 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7425 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7426 }
7427}
7428
Jiyong Parkbd159612020-02-28 15:22:21 +09007429func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007430 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007431 apex {
7432 name: "myapex",
7433 key: "myapex.key",
7434 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007435 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007436 }
7437
7438 apex_key {
7439 name: "myapex.key",
7440 public_key: "testkey.avbpubkey",
7441 private_key: "testkey.pem",
7442 }
7443
7444 android_app {
7445 name: "AppFoo",
7446 srcs: ["foo/bar/MyClass.java"],
7447 sdk_version: "none",
7448 system_modules: "none",
7449 apex_available: [ "myapex" ],
7450 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007451 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007452
Colin Crosscf371cc2020-11-13 11:48:42 -08007453 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007454 content := bundleConfigRule.Args["content"]
7455
7456 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007457 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 +09007458}
7459
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007460func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007461 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007462 apex {
7463 name: "myapex",
7464 key: "myapex.key",
7465 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007466 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007467 }
7468
7469 apex_key {
7470 name: "myapex.key",
7471 public_key: "testkey.avbpubkey",
7472 private_key: "testkey.pem",
7473 }
7474
7475 android_app_set {
7476 name: "AppSet",
7477 set: "AppSet.apks",
7478 }`)
7479 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007480 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007481 content := bundleConfigRule.Args["content"]
7482 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7483 s := mod.Rule("apexRule").Args["copy_commands"]
7484 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007485 if len(copyCmds) != 4 {
7486 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007487 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007488 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7489 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007490 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7491 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007492
7493 // Ensure that canned_fs_config has an entry for the app set zip file
7494 generateFsRule := mod.Rule("generateFsConfig")
7495 cmd := generateFsRule.RuleParams.Command
7496 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007497}
7498
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007499func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007500 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007501 apex_set {
7502 name: "myapex",
7503 filename: "foo_v2.apex",
7504 sanitized: {
7505 none: { set: "myapex.apks", },
7506 hwaddress: { set: "myapex.hwasan.apks", },
7507 },
Paul Duffin24704672021-04-06 16:09:30 +01007508 }
7509 `
7510 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007511
Paul Duffin24704672021-04-06 16:09:30 +01007512 // Check that the extractor produces the correct output file from the correct input file.
7513 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007514
Paul Duffin24704672021-04-06 16:09:30 +01007515 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7516 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007517
Paul Duffin24704672021-04-06 16:09:30 +01007518 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7519
7520 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007521 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7522 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007523
7524 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007525}
7526
Pranav Guptaeba03b02022-09-27 00:27:08 +00007527func TestApexSetApksModuleAssignment(t *testing.T) {
7528 ctx := testApex(t, `
7529 apex_set {
7530 name: "myapex",
7531 set: ":myapex_apks_file",
7532 }
7533
7534 filegroup {
7535 name: "myapex_apks_file",
7536 srcs: ["myapex.apks"],
7537 }
7538 `)
7539
7540 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7541
7542 // Check that the extractor produces the correct apks file from the input module
7543 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7544 extractedApex := m.Output(extractorOutput)
7545
7546 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7547}
7548
Paul Duffin89f570a2021-06-16 01:42:33 +01007549func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007550 t.Helper()
7551
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007552 bp := `
7553 java_library {
7554 name: "some-updatable-apex-lib",
7555 srcs: ["a.java"],
7556 sdk_version: "current",
7557 apex_available: [
7558 "some-updatable-apex",
7559 ],
satayevabcd5972021-08-06 17:49:46 +01007560 permitted_packages: ["some.updatable.apex.lib"],
Spandan Dascc9d9422023-04-06 18:07:43 +00007561 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007562 }
7563
7564 java_library {
7565 name: "some-non-updatable-apex-lib",
7566 srcs: ["a.java"],
7567 apex_available: [
7568 "some-non-updatable-apex",
7569 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007570 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007571 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007572 }
7573
7574 bootclasspath_fragment {
7575 name: "some-non-updatable-fragment",
7576 contents: ["some-non-updatable-apex-lib"],
7577 apex_available: [
7578 "some-non-updatable-apex",
7579 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007580 hidden_api: {
7581 split_packages: ["*"],
7582 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007583 }
7584
7585 java_library {
7586 name: "some-platform-lib",
7587 srcs: ["a.java"],
7588 sdk_version: "current",
7589 installable: true,
7590 }
7591
7592 java_library {
7593 name: "some-art-lib",
7594 srcs: ["a.java"],
7595 sdk_version: "current",
7596 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007597 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007598 ],
7599 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007600 compile_dex: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007601 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007602 }
7603
7604 apex {
7605 name: "some-updatable-apex",
7606 key: "some-updatable-apex.key",
7607 java_libs: ["some-updatable-apex-lib"],
7608 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007609 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007610 }
7611
7612 apex {
7613 name: "some-non-updatable-apex",
7614 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007615 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007616 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007617 }
7618
7619 apex_key {
7620 name: "some-updatable-apex.key",
7621 }
7622
7623 apex_key {
7624 name: "some-non-updatable-apex.key",
7625 }
7626
7627 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007628 name: "com.android.art.debug",
7629 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007630 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007631 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007632 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007633 }
7634
Paul Duffinf23bc472021-04-27 12:42:20 +01007635 bootclasspath_fragment {
7636 name: "art-bootclasspath-fragment",
7637 image_name: "art",
7638 contents: ["some-art-lib"],
7639 apex_available: [
7640 "com.android.art.debug",
7641 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007642 hidden_api: {
7643 split_packages: ["*"],
7644 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007645 }
7646
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007647 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007648 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007649 }
7650
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007651 filegroup {
7652 name: "some-updatable-apex-file_contexts",
7653 srcs: [
7654 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7655 ],
7656 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007657
7658 filegroup {
7659 name: "some-non-updatable-apex-file_contexts",
7660 srcs: [
7661 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7662 ],
7663 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007664 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007665
Paul Duffin89f570a2021-06-16 01:42:33 +01007666 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007667}
7668
Paul Duffin89f570a2021-06-16 01:42:33 +01007669func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007670 t.Helper()
7671
Paul Duffin55607122021-03-30 23:32:51 +01007672 fs := android.MockFS{
7673 "a.java": nil,
7674 "a.jar": nil,
7675 "apex_manifest.json": nil,
7676 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007677 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007678 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7679 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7680 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007681 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007682 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007683
Paul Duffin55607122021-03-30 23:32:51 +01007684 errorHandler := android.FixtureExpectsNoErrors
7685 if errmsg != "" {
7686 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007687 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007688
Paul Duffin55607122021-03-30 23:32:51 +01007689 result := android.GroupFixturePreparers(
7690 cc.PrepareForTestWithCcDefaultModules,
7691 java.PrepareForTestWithHiddenApiBuildComponents,
7692 java.PrepareForTestWithJavaDefaultModules,
7693 java.PrepareForTestWithJavaSdkLibraryFiles,
7694 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007695 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007696 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007697 android.FixtureModifyMockFS(func(fs android.MockFS) {
7698 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7699 insert := ""
7700 for _, fragment := range fragments {
7701 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7702 }
7703 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7704 platform_bootclasspath {
7705 name: "platform-bootclasspath",
7706 fragments: [
7707 %s
7708 ],
7709 }
7710 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007711 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007712 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007713 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007714 ).
7715 ExtendWithErrorHandler(errorHandler).
7716 RunTestWithBp(t, bp)
7717
7718 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007719}
7720
Paul Duffin5556c5f2022-06-09 17:32:21 +00007721func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007722 preparers := android.GroupFixturePreparers(
7723 java.PrepareForTestWithJavaDefaultModules,
7724 PrepareForTestWithApexBuildComponents,
7725 ).
7726 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7727 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7728
7729 bpBase := `
7730 apex_set {
7731 name: "com.android.myapex",
7732 installable: true,
7733 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7734 set: "myapex.apks",
7735 }
7736
7737 apex_set {
7738 name: "com.mycompany.android.myapex",
7739 apex_name: "com.android.myapex",
7740 installable: true,
7741 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7742 set: "company-myapex.apks",
7743 }
7744
7745 prebuilt_bootclasspath_fragment {
7746 name: "my-bootclasspath-fragment",
7747 apex_available: ["com.android.myapex"],
7748 %s
7749 }
7750 `
7751
7752 t.Run("java_import", func(t *testing.T) {
7753 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7754 java_import {
7755 name: "libfoo",
7756 jars: ["libfoo.jar"],
7757 apex_available: ["com.android.myapex"],
7758 }
7759 `)
7760 })
7761
7762 t.Run("java_sdk_library_import", func(t *testing.T) {
7763 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7764 java_sdk_library_import {
7765 name: "libfoo",
7766 public: {
7767 jars: ["libbar.jar"],
7768 },
7769 apex_available: ["com.android.myapex"],
7770 }
7771 `)
7772 })
7773
7774 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7775 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7776 image_name: "art",
7777 contents: ["libfoo"],
7778 `)+`
7779 java_sdk_library_import {
7780 name: "libfoo",
7781 public: {
7782 jars: ["libbar.jar"],
7783 },
7784 apex_available: ["com.android.myapex"],
7785 }
7786 `)
7787 })
7788}
7789
Paul Duffin5556c5f2022-06-09 17:32:21 +00007790func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7791 preparers := android.GroupFixturePreparers(
7792 java.PrepareForTestWithJavaDefaultModules,
7793 PrepareForTestWithApexBuildComponents,
7794 )
7795
7796 bpBase := `
7797 apex_set {
7798 name: "com.android.myapex",
7799 installable: true,
7800 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7801 set: "myapex.apks",
7802 }
7803
7804 apex_set {
7805 name: "com.android.myapex_compressed",
7806 apex_name: "com.android.myapex",
7807 installable: true,
7808 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7809 set: "myapex_compressed.apks",
7810 }
7811
7812 prebuilt_bootclasspath_fragment {
7813 name: "my-bootclasspath-fragment",
7814 apex_available: [
7815 "com.android.myapex",
7816 "com.android.myapex_compressed",
7817 ],
7818 hidden_api: {
7819 annotation_flags: "annotation-flags.csv",
7820 metadata: "metadata.csv",
7821 index: "index.csv",
7822 signature_patterns: "signature_patterns.csv",
7823 },
7824 %s
7825 }
7826 `
7827
7828 t.Run("java_import", func(t *testing.T) {
7829 result := preparers.RunTestWithBp(t,
7830 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7831 java_import {
7832 name: "libfoo",
7833 jars: ["libfoo.jar"],
7834 apex_available: [
7835 "com.android.myapex",
7836 "com.android.myapex_compressed",
7837 ],
7838 }
7839 `)
7840
7841 module := result.Module("libfoo", "android_common_com.android.myapex")
7842 usesLibraryDep := module.(java.UsesLibraryDependency)
7843 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7844 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7845 usesLibraryDep.DexJarBuildPath().Path())
7846 })
7847
7848 t.Run("java_sdk_library_import", func(t *testing.T) {
7849 result := preparers.RunTestWithBp(t,
7850 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7851 java_sdk_library_import {
7852 name: "libfoo",
7853 public: {
7854 jars: ["libbar.jar"],
7855 },
7856 apex_available: [
7857 "com.android.myapex",
7858 "com.android.myapex_compressed",
7859 ],
7860 compile_dex: true,
7861 }
7862 `)
7863
7864 module := result.Module("libfoo", "android_common_com.android.myapex")
7865 usesLibraryDep := module.(java.UsesLibraryDependency)
7866 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7867 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7868 usesLibraryDep.DexJarBuildPath().Path())
7869 })
7870
7871 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7872 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7873 image_name: "art",
7874 contents: ["libfoo"],
7875 `)+`
7876 java_sdk_library_import {
7877 name: "libfoo",
7878 public: {
7879 jars: ["libbar.jar"],
7880 },
7881 apex_available: [
7882 "com.android.myapex",
7883 "com.android.myapex_compressed",
7884 ],
7885 compile_dex: true,
7886 }
7887 `)
7888 })
7889}
7890
Jooyung Han548640b2020-04-27 12:10:30 +09007891func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7892 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7893 apex {
7894 name: "myapex",
7895 key: "myapex.key",
7896 updatable: true,
7897 }
7898
7899 apex_key {
7900 name: "myapex.key",
7901 public_key: "testkey.avbpubkey",
7902 private_key: "testkey.pem",
7903 }
7904 `)
7905}
7906
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007907func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7908 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7909 apex {
7910 name: "myapex",
7911 key: "myapex.key",
7912 }
7913
7914 apex_key {
7915 name: "myapex.key",
7916 public_key: "testkey.avbpubkey",
7917 private_key: "testkey.pem",
7918 }
7919 `)
7920}
7921
Jooyung Handfc864c2023-03-20 18:19:07 +09007922func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
7923 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08007924 apex {
7925 name: "myapex",
7926 key: "myapex.key",
7927 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09007928 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08007929 soc_specific: true,
7930 }
7931
7932 apex_key {
7933 name: "myapex.key",
7934 public_key: "testkey.avbpubkey",
7935 private_key: "testkey.pem",
7936 }
7937 `)
7938}
7939
Jooyung Han02873da2023-03-22 17:41:03 +09007940func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
7941 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
7942 apex {
7943 name: "myapex",
7944 key: "myapex.key",
7945 updatable: false,
7946 min_sdk_version: "29",
7947 use_vndk_as_stable: true,
7948 vendor: true,
7949 }
7950
7951 apex_key {
7952 name: "myapex.key",
7953 public_key: "testkey.avbpubkey",
7954 private_key: "testkey.pem",
7955 }
7956 `)
7957}
7958
Jooyung Handfc864c2023-03-20 18:19:07 +09007959func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
7960 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
7961 apex {
7962 name: "myapex",
7963 key: "myapex.key",
7964 updatable: false,
7965 use_vndk_as_stable: true,
7966 }
7967
7968 apex_key {
7969 name: "myapex.key",
7970 public_key: "testkey.avbpubkey",
7971 private_key: "testkey.pem",
7972 }
7973 `)
7974}
7975
satayevb98371c2021-06-15 16:49:50 +01007976func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7977 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7978 apex {
7979 name: "myapex",
7980 key: "myapex.key",
7981 systemserverclasspath_fragments: [
7982 "mysystemserverclasspathfragment",
7983 ],
7984 min_sdk_version: "29",
7985 updatable: true,
7986 }
7987
7988 apex_key {
7989 name: "myapex.key",
7990 public_key: "testkey.avbpubkey",
7991 private_key: "testkey.pem",
7992 }
7993
7994 java_library {
7995 name: "foo",
7996 srcs: ["b.java"],
7997 min_sdk_version: "29",
7998 installable: true,
7999 apex_available: [
8000 "myapex",
8001 ],
8002 }
8003
8004 systemserverclasspath_fragment {
8005 name: "mysystemserverclasspathfragment",
8006 generate_classpaths_proto: false,
8007 contents: [
8008 "foo",
8009 ],
8010 apex_available: [
8011 "myapex",
8012 ],
8013 }
satayevabcd5972021-08-06 17:49:46 +01008014 `,
8015 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8016 )
satayevb98371c2021-06-15 16:49:50 +01008017}
8018
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008019func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008020 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
8021 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
8022 // modules to be included in the BootJars.
8023 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
8024 return android.GroupFixturePreparers(
8025 dexpreopt.FixtureSetBootJars(bootJars...),
8026 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8027 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8028 }),
8029 )
8030 }
8031
8032 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
8033 // same value. This can result in an invalid configuration as it allows non art apex jars to be
8034 // specified in the ArtApexJars configuration.
8035 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
8036 return android.GroupFixturePreparers(
8037 dexpreopt.FixtureSetArtBootJars(bootJars...),
8038 dexpreopt.FixtureSetBootJars(bootJars...),
8039 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8040 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8041 }),
8042 )
8043 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008044
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008045 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008046 preparer := android.GroupFixturePreparers(
8047 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
8048 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8049 )
8050 fragments := []java.ApexVariantReference{
8051 {
8052 Apex: proptools.StringPtr("com.android.art.debug"),
8053 Module: proptools.StringPtr("art-bootclasspath-fragment"),
8054 },
8055 {
8056 Apex: proptools.StringPtr("some-non-updatable-apex"),
8057 Module: proptools.StringPtr("some-non-updatable-fragment"),
8058 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008059 }
satayevabcd5972021-08-06 17:49:46 +01008060 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008061 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008062
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008063 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008064 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
8065 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01008066 preparer := android.GroupFixturePreparers(
8067 prepareSetBootJars("com.android.art.debug:some-art-lib"),
8068 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8069 )
Paul Duffin60264a02021-04-12 20:02:36 +01008070 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008071 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008072
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008073 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 +01008074 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 +01008075 // Update the dexpreopt ArtApexJars directly.
8076 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
8077 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008078 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008079
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008080 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 +01008081 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 +01008082 // Update the dexpreopt ArtApexJars directly.
8083 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8084 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008085 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008086
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008087 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 +01008088 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 +01008089 preparer := android.GroupFixturePreparers(
8090 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8091 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8092 )
Paul Duffin60264a02021-04-12 20:02:36 +01008093 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008094 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008095
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008096 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 +01008097 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008098 fragment := java.ApexVariantReference{
8099 Apex: proptools.StringPtr("some-non-updatable-apex"),
8100 Module: proptools.StringPtr("some-non-updatable-fragment"),
8101 }
8102 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008103 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008104
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008105 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008106 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008107 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8108 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008109 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008110
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008111 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008112 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008113 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8114 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008115 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008116
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008117 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008118 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008119 // Update the dexpreopt ArtApexJars directly.
8120 preparer := prepareSetArtJars("platform:some-platform-lib")
8121 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008122 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008123
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008124 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008125 preparer := android.GroupFixturePreparers(
8126 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8127 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8128 )
8129 fragments := []java.ApexVariantReference{
8130 {
8131 Apex: proptools.StringPtr("some-non-updatable-apex"),
8132 Module: proptools.StringPtr("some-non-updatable-fragment"),
8133 },
8134 }
8135 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008136 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008137}
8138
8139func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008140 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008141 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008142 fragment := java.ApexVariantReference{
8143 Apex: proptools.StringPtr("myapex"),
8144 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8145 }
8146
Paul Duffin064b70c2020-11-02 17:32:38 +00008147 testDexpreoptWithApexes(t, `
8148 prebuilt_apex {
8149 name: "myapex" ,
8150 arch: {
8151 arm64: {
8152 src: "myapex-arm64.apex",
8153 },
8154 arm: {
8155 src: "myapex-arm.apex",
8156 },
8157 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008158 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8159 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008160
Paul Duffin89f570a2021-06-16 01:42:33 +01008161 prebuilt_bootclasspath_fragment {
8162 name: "my-bootclasspath-fragment",
8163 contents: ["libfoo"],
8164 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008165 hidden_api: {
8166 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8167 metadata: "my-bootclasspath-fragment/metadata.csv",
8168 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008169 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8170 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8171 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008172 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008173 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008174
Paul Duffin89f570a2021-06-16 01:42:33 +01008175 java_import {
8176 name: "libfoo",
8177 jars: ["libfoo.jar"],
8178 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008179 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008180 }
8181 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008182 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008183}
8184
Spandan Dasf14e2542021-11-12 00:01:37 +00008185func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008186 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008187 bp += `
8188 apex_key {
8189 name: "myapex.key",
8190 public_key: "testkey.avbpubkey",
8191 private_key: "testkey.pem",
8192 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008193 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008194 "lib1/src/A.java": nil,
8195 "lib2/src/B.java": nil,
8196 "system/sepolicy/apex/myapex-file_contexts": nil,
8197 }
8198
Paul Duffin45338f02021-03-30 23:07:52 +01008199 errorHandler := android.FixtureExpectsNoErrors
8200 if errmsg != "" {
8201 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008202 }
Colin Crossae8600b2020-10-29 17:09:13 -07008203
Paul Duffin45338f02021-03-30 23:07:52 +01008204 android.GroupFixturePreparers(
8205 android.PrepareForTestWithAndroidBuildComponents,
8206 java.PrepareForTestWithJavaBuildComponents,
8207 PrepareForTestWithApexBuildComponents,
8208 android.PrepareForTestWithNeverallowRules(rules),
8209 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008210 apexBootJars := make([]string, 0, len(bootJars))
8211 for _, apexBootJar := range bootJars {
8212 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008213 }
satayevd604b212021-07-21 14:23:52 +01008214 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008215 }),
8216 fs.AddToFixture(),
8217 ).
8218 ExtendWithErrorHandler(errorHandler).
8219 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008220}
8221
8222func TestApexPermittedPackagesRules(t *testing.T) {
8223 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008224 name string
8225 expectedError string
8226 bp string
8227 bootJars []string
8228 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008229 }{
8230
8231 {
8232 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8233 expectedError: "",
8234 bp: `
8235 java_library {
8236 name: "bcp_lib1",
8237 srcs: ["lib1/src/*.java"],
8238 permitted_packages: ["foo.bar"],
8239 apex_available: ["myapex"],
8240 sdk_version: "none",
8241 system_modules: "none",
8242 }
8243 java_library {
8244 name: "nonbcp_lib2",
8245 srcs: ["lib2/src/*.java"],
8246 apex_available: ["myapex"],
8247 permitted_packages: ["a.b"],
8248 sdk_version: "none",
8249 system_modules: "none",
8250 }
8251 apex {
8252 name: "myapex",
8253 key: "myapex.key",
8254 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008255 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008256 }`,
8257 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008258 bcpPermittedPackages: map[string][]string{
8259 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008260 "foo.bar",
8261 },
8262 },
8263 },
8264 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008265 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008266 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 +01008267 bp: `
8268 java_library {
8269 name: "bcp_lib1",
8270 srcs: ["lib1/src/*.java"],
8271 apex_available: ["myapex"],
8272 permitted_packages: ["foo.bar"],
8273 sdk_version: "none",
8274 system_modules: "none",
8275 }
8276 java_library {
8277 name: "bcp_lib2",
8278 srcs: ["lib2/src/*.java"],
8279 apex_available: ["myapex"],
8280 permitted_packages: ["foo.bar", "bar.baz"],
8281 sdk_version: "none",
8282 system_modules: "none",
8283 }
8284 apex {
8285 name: "myapex",
8286 key: "myapex.key",
8287 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008288 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008289 }
8290 `,
8291 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008292 bcpPermittedPackages: map[string][]string{
8293 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008294 "foo.bar",
8295 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008296 "bcp_lib2": []string{
8297 "foo.bar",
8298 },
8299 },
8300 },
8301 {
8302 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8303 expectedError: "",
8304 bp: `
8305 java_library {
8306 name: "bcp_lib_restricted",
8307 srcs: ["lib1/src/*.java"],
8308 apex_available: ["myapex"],
8309 permitted_packages: ["foo.bar"],
8310 sdk_version: "none",
8311 min_sdk_version: "29",
8312 system_modules: "none",
8313 }
8314 java_library {
8315 name: "bcp_lib_unrestricted",
8316 srcs: ["lib2/src/*.java"],
8317 apex_available: ["myapex"],
8318 permitted_packages: ["foo.bar", "bar.baz"],
8319 sdk_version: "none",
8320 min_sdk_version: "29",
8321 system_modules: "none",
8322 }
8323 apex {
8324 name: "myapex",
8325 key: "myapex.key",
8326 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8327 updatable: true,
8328 min_sdk_version: "29",
8329 }
8330 `,
8331 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8332 bcpPermittedPackages: map[string][]string{
8333 "bcp_lib1_non_updateable": []string{
8334 "foo.bar",
8335 },
8336 // 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 +01008337 },
8338 },
8339 }
8340 for _, tc := range testcases {
8341 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008342 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8343 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008344 })
8345 }
8346}
8347
Jiyong Park62304bb2020-04-13 16:19:48 +09008348func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008349 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008350 apex {
8351 name: "myapex",
8352 key: "myapex.key",
8353 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008354 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008355 }
8356
8357 apex_key {
8358 name: "myapex.key",
8359 public_key: "testkey.avbpubkey",
8360 private_key: "testkey.pem",
8361 }
8362
8363 cc_library {
8364 name: "mylib",
8365 srcs: ["mylib.cpp"],
8366 system_shared_libs: [],
8367 stl: "none",
8368 stubs: {
8369 versions: ["1"],
8370 },
8371 apex_available: ["myapex"],
8372 }
8373
8374 cc_library {
8375 name: "myprivlib",
8376 srcs: ["mylib.cpp"],
8377 system_shared_libs: [],
8378 stl: "none",
8379 apex_available: ["myapex"],
8380 }
8381
8382
8383 cc_test {
8384 name: "mytest",
8385 gtest: false,
8386 srcs: ["mylib.cpp"],
8387 system_shared_libs: [],
8388 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008389 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008390 test_for: ["myapex"]
8391 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008392
8393 cc_library {
8394 name: "mytestlib",
8395 srcs: ["mylib.cpp"],
8396 system_shared_libs: [],
8397 shared_libs: ["mylib", "myprivlib"],
8398 stl: "none",
8399 test_for: ["myapex"],
8400 }
8401
8402 cc_benchmark {
8403 name: "mybench",
8404 srcs: ["mylib.cpp"],
8405 system_shared_libs: [],
8406 shared_libs: ["mylib", "myprivlib"],
8407 stl: "none",
8408 test_for: ["myapex"],
8409 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008410 `)
8411
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008412 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008413 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008414 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8415 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8416 }
8417
8418 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008419 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008420 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8421 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8422 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8423}
Jiyong Park46a512f2020-12-04 18:02:13 +09008424
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008425func TestIndirectTestFor(t *testing.T) {
8426 ctx := testApex(t, `
8427 apex {
8428 name: "myapex",
8429 key: "myapex.key",
8430 native_shared_libs: ["mylib", "myprivlib"],
8431 updatable: false,
8432 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008433
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008434 apex_key {
8435 name: "myapex.key",
8436 public_key: "testkey.avbpubkey",
8437 private_key: "testkey.pem",
8438 }
8439
8440 cc_library {
8441 name: "mylib",
8442 srcs: ["mylib.cpp"],
8443 system_shared_libs: [],
8444 stl: "none",
8445 stubs: {
8446 versions: ["1"],
8447 },
8448 apex_available: ["myapex"],
8449 }
8450
8451 cc_library {
8452 name: "myprivlib",
8453 srcs: ["mylib.cpp"],
8454 system_shared_libs: [],
8455 stl: "none",
8456 shared_libs: ["mylib"],
8457 apex_available: ["myapex"],
8458 }
8459
8460 cc_library {
8461 name: "mytestlib",
8462 srcs: ["mylib.cpp"],
8463 system_shared_libs: [],
8464 shared_libs: ["myprivlib"],
8465 stl: "none",
8466 test_for: ["myapex"],
8467 }
8468 `)
8469
8470 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008471 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008472 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8473 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8474 }
8475
8476 // The platform variant of mytestlib links to the platform variant of the
8477 // internal myprivlib.
8478 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8479
8480 // The platform variant of myprivlib links to the platform variant of mylib
8481 // and bypasses its stubs.
8482 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 +09008483}
8484
Martin Stjernholmec009002021-03-27 15:18:31 +00008485func TestTestForForLibInOtherApex(t *testing.T) {
8486 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8487 _ = testApex(t, `
8488 apex {
8489 name: "com.android.art",
8490 key: "myapex.key",
8491 native_shared_libs: ["mylib"],
8492 updatable: false,
8493 }
8494
8495 apex {
8496 name: "com.android.art.debug",
8497 key: "myapex.key",
8498 native_shared_libs: ["mylib", "mytestlib"],
8499 updatable: false,
8500 }
8501
8502 apex_key {
8503 name: "myapex.key",
8504 public_key: "testkey.avbpubkey",
8505 private_key: "testkey.pem",
8506 }
8507
8508 cc_library {
8509 name: "mylib",
8510 srcs: ["mylib.cpp"],
8511 system_shared_libs: [],
8512 stl: "none",
8513 stubs: {
8514 versions: ["1"],
8515 },
8516 apex_available: ["com.android.art", "com.android.art.debug"],
8517 }
8518
8519 cc_library {
8520 name: "mytestlib",
8521 srcs: ["mylib.cpp"],
8522 system_shared_libs: [],
8523 shared_libs: ["mylib"],
8524 stl: "none",
8525 apex_available: ["com.android.art.debug"],
8526 test_for: ["com.android.art"],
8527 }
8528 `,
8529 android.MockFS{
8530 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8531 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8532 }.AddToFixture())
8533}
8534
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008535// TODO(jungjw): Move this to proptools
8536func intPtr(i int) *int {
8537 return &i
8538}
8539
8540func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008541 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008542 apex_set {
8543 name: "myapex",
8544 set: "myapex.apks",
8545 filename: "foo_v2.apex",
8546 overrides: ["foo"],
8547 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008548 `,
8549 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8550 variables.Platform_sdk_version = intPtr(30)
8551 }),
8552 android.FixtureModifyConfig(func(config android.Config) {
8553 config.Targets[android.Android] = []android.Target{
8554 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8555 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8556 }
8557 }),
8558 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008559
Paul Duffin24704672021-04-06 16:09:30 +01008560 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008561
8562 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008563 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008564 actual := extractedApex.Args["abis"]
8565 expected := "ARMEABI_V7A,ARM64_V8A"
8566 if actual != expected {
8567 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8568 }
8569 actual = extractedApex.Args["sdk-version"]
8570 expected = "30"
8571 if actual != expected {
8572 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8573 }
8574
Paul Duffin6717d882021-06-15 19:09:41 +01008575 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008576 a := m.Module().(*ApexSet)
8577 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008578 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008579 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8580 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8581 }
8582}
8583
Anton Hansson805e0a52022-11-25 14:06:46 +00008584func TestApexSet_NativeBridge(t *testing.T) {
8585 ctx := testApex(t, `
8586 apex_set {
8587 name: "myapex",
8588 set: "myapex.apks",
8589 filename: "foo_v2.apex",
8590 overrides: ["foo"],
8591 }
8592 `,
8593 android.FixtureModifyConfig(func(config android.Config) {
8594 config.Targets[android.Android] = []android.Target{
8595 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8596 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8597 }
8598 }),
8599 )
8600
8601 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8602
8603 // Check extract_apks tool parameters. No native bridge arch expected
8604 extractedApex := m.Output("extracted/myapex.apks")
8605 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8606}
8607
Jiyong Park7d95a512020-05-10 15:16:24 +09008608func TestNoStaticLinkingToStubsLib(t *testing.T) {
8609 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8610 apex {
8611 name: "myapex",
8612 key: "myapex.key",
8613 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008614 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008615 }
8616
8617 apex_key {
8618 name: "myapex.key",
8619 public_key: "testkey.avbpubkey",
8620 private_key: "testkey.pem",
8621 }
8622
8623 cc_library {
8624 name: "mylib",
8625 srcs: ["mylib.cpp"],
8626 static_libs: ["otherlib"],
8627 system_shared_libs: [],
8628 stl: "none",
8629 apex_available: [ "myapex" ],
8630 }
8631
8632 cc_library {
8633 name: "otherlib",
8634 srcs: ["mylib.cpp"],
8635 system_shared_libs: [],
8636 stl: "none",
8637 stubs: {
8638 versions: ["1", "2", "3"],
8639 },
8640 apex_available: [ "myapex" ],
8641 }
8642 `)
8643}
8644
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008645func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008646 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008647 apex {
8648 name: "myapex",
8649 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008650 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008651 custom_sign_tool: "sign_myapex",
8652 }
8653
8654 apex_key {
8655 name: "myapex.key",
8656 public_key: "testkey.avbpubkey",
8657 private_key: "testkey.pem",
8658 }
8659 `)
8660
8661 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8662 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8663 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"`)
8664}
8665
8666func TestApexKeysTxtOverrides(t *testing.T) {
8667 ctx := testApex(t, `
8668 apex {
8669 name: "myapex",
8670 key: "myapex.key",
8671 updatable: false,
8672 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008673 }
8674
8675 apex_key {
8676 name: "myapex.key",
8677 public_key: "testkey.avbpubkey",
8678 private_key: "testkey.pem",
8679 }
8680
8681 prebuilt_apex {
8682 name: "myapex",
8683 prefer: true,
8684 arch: {
8685 arm64: {
8686 src: "myapex-arm64.apex",
8687 },
8688 arm: {
8689 src: "myapex-arm.apex",
8690 },
8691 },
8692 }
8693
8694 apex_set {
8695 name: "myapex_set",
8696 set: "myapex.apks",
8697 filename: "myapex_set.apex",
8698 overrides: ["myapex"],
8699 }
8700 `)
8701
8702 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8703 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8704 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 +09008705 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 +09008706}
8707
Jooyung Han938b5932020-06-20 12:47:47 +09008708func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008709 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008710 apex {
8711 name: "myapex",
8712 key: "myapex.key",
8713 apps: ["app"],
8714 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008715 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008716 }
8717
8718 apex_key {
8719 name: "myapex.key",
8720 public_key: "testkey.avbpubkey",
8721 private_key: "testkey.pem",
8722 }
8723
8724 android_app {
8725 name: "app",
8726 srcs: ["foo/bar/MyClass.java"],
8727 package_name: "foo",
8728 sdk_version: "none",
8729 system_modules: "none",
8730 apex_available: [ "myapex" ],
8731 }
8732 `, withFiles(map[string][]byte{
8733 "sub/Android.bp": []byte(`
8734 override_apex {
8735 name: "override_myapex",
8736 base: "myapex",
8737 apps: ["override_app"],
8738 allowed_files: ":allowed",
8739 }
8740 // Overridable "path" property should be referenced indirectly
8741 filegroup {
8742 name: "allowed",
8743 srcs: ["allowed.txt"],
8744 }
8745 override_android_app {
8746 name: "override_app",
8747 base: "app",
8748 package_name: "bar",
8749 }
8750 `),
8751 }))
8752
8753 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8754 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8755 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8756 }
8757
8758 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8759 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8760 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8761 }
8762}
8763
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008764func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008765 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008766 apex {
8767 name: "myapex",
8768 key: "myapex.key",
8769 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008770 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008771 }
8772
8773 apex_key {
8774 name: "myapex.key",
8775 public_key: "testkey.avbpubkey",
8776 private_key: "testkey.pem",
8777 }
8778
8779 cc_library {
8780 name: "mylib",
8781 srcs: ["mylib.cpp"],
8782 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008783 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008784 },
8785 apex_available: ["myapex"],
8786 }
8787
8788 cc_prebuilt_library_shared {
8789 name: "mylib",
8790 prefer: false,
8791 srcs: ["prebuilt.so"],
8792 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008793 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008794 },
8795 apex_available: ["myapex"],
8796 }
8797 `)
8798}
8799
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008800func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008801 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008802 apex {
8803 name: "myapex",
8804 key: "myapex.key",
8805 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008806 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008807 }
8808 apex_key {
8809 name: "myapex.key",
8810 public_key: "testkey.avbpubkey",
8811 private_key: "testkey.pem",
8812 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008813 `,
8814 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8815 variables.CompressedApex = proptools.BoolPtr(true)
8816 }),
8817 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008818
8819 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8820 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8821
8822 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8823 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8824
8825 // Make sure output of bundle is .capex
8826 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8827 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8828
8829 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008830 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008831 var builder strings.Builder
8832 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8833 androidMk := builder.String()
8834 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8835}
8836
Martin Stjernholm2856c662020-12-02 15:03:42 +00008837func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008838 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008839 apex {
8840 name: "myapex",
8841 key: "myapex.key",
8842 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008843 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008844 }
8845
8846 apex_key {
8847 name: "myapex.key",
8848 public_key: "testkey.avbpubkey",
8849 private_key: "testkey.pem",
8850 }
8851
8852 cc_library {
8853 name: "mylib",
8854 srcs: ["mylib.cpp"],
8855 apex_available: ["myapex"],
8856 shared_libs: ["otherlib"],
8857 system_shared_libs: [],
8858 }
8859
8860 cc_library {
8861 name: "otherlib",
8862 srcs: ["mylib.cpp"],
8863 stubs: {
8864 versions: ["current"],
8865 },
8866 }
8867
8868 cc_prebuilt_library_shared {
8869 name: "otherlib",
8870 prefer: true,
8871 srcs: ["prebuilt.so"],
8872 stubs: {
8873 versions: ["current"],
8874 },
8875 }
8876 `)
8877
8878 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008879 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008880 var builder strings.Builder
8881 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8882 androidMk := builder.String()
8883
8884 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8885 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008886 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 +00008887}
8888
Jiyong Parke3867542020-12-03 17:28:25 +09008889func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008890 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008891 apex {
8892 name: "myapex",
8893 key: "myapex.key",
8894 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008895 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008896 }
8897
8898 apex_key {
8899 name: "myapex.key",
8900 public_key: "testkey.avbpubkey",
8901 private_key: "testkey.pem",
8902 }
8903
8904 cc_library {
8905 name: "mylib",
8906 srcs: ["mylib.cpp"],
8907 system_shared_libs: [],
8908 stl: "none",
8909 apex_available: ["myapex"],
8910 shared_libs: ["mylib2"],
8911 target: {
8912 apex: {
8913 exclude_shared_libs: ["mylib2"],
8914 },
8915 },
8916 }
8917
8918 cc_library {
8919 name: "mylib2",
8920 srcs: ["mylib.cpp"],
8921 system_shared_libs: [],
8922 stl: "none",
8923 }
8924 `)
8925
8926 // Check if mylib is linked to mylib2 for the non-apex target
8927 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8928 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8929
8930 // Make sure that the link doesn't occur for the apex target
8931 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8932 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8933
8934 // It shouldn't appear in the copy cmd as well.
8935 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8936 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8937}
8938
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008939func TestPrebuiltStubLibDep(t *testing.T) {
8940 bpBase := `
8941 apex {
8942 name: "myapex",
8943 key: "myapex.key",
8944 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008945 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008946 }
8947 apex_key {
8948 name: "myapex.key",
8949 public_key: "testkey.avbpubkey",
8950 private_key: "testkey.pem",
8951 }
8952 cc_library {
8953 name: "mylib",
8954 srcs: ["mylib.cpp"],
8955 apex_available: ["myapex"],
8956 shared_libs: ["stublib"],
8957 system_shared_libs: [],
8958 }
8959 apex {
8960 name: "otherapex",
8961 enabled: %s,
8962 key: "myapex.key",
8963 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008964 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008965 }
8966 `
8967
8968 stublibSourceBp := `
8969 cc_library {
8970 name: "stublib",
8971 srcs: ["mylib.cpp"],
8972 apex_available: ["otherapex"],
8973 system_shared_libs: [],
8974 stl: "none",
8975 stubs: {
8976 versions: ["1"],
8977 },
8978 }
8979 `
8980
8981 stublibPrebuiltBp := `
8982 cc_prebuilt_library_shared {
8983 name: "stublib",
8984 srcs: ["prebuilt.so"],
8985 apex_available: ["otherapex"],
8986 stubs: {
8987 versions: ["1"],
8988 },
8989 %s
8990 }
8991 `
8992
8993 tests := []struct {
8994 name string
8995 stublibBp string
8996 usePrebuilt bool
8997 modNames []string // Modules to collect AndroidMkEntries for
8998 otherApexEnabled []string
8999 }{
9000 {
9001 name: "only_source",
9002 stublibBp: stublibSourceBp,
9003 usePrebuilt: false,
9004 modNames: []string{"stublib"},
9005 otherApexEnabled: []string{"true", "false"},
9006 },
9007 {
9008 name: "source_preferred",
9009 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9010 usePrebuilt: false,
9011 modNames: []string{"stublib", "prebuilt_stublib"},
9012 otherApexEnabled: []string{"true", "false"},
9013 },
9014 {
9015 name: "prebuilt_preferred",
9016 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9017 usePrebuilt: true,
9018 modNames: []string{"stublib", "prebuilt_stublib"},
9019 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9020 },
9021 {
9022 name: "only_prebuilt",
9023 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9024 usePrebuilt: true,
9025 modNames: []string{"stublib"},
9026 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9027 },
9028 }
9029
9030 for _, test := range tests {
9031 t.Run(test.name, func(t *testing.T) {
9032 for _, otherApexEnabled := range test.otherApexEnabled {
9033 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009034 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009035
9036 type modAndMkEntries struct {
9037 mod *cc.Module
9038 mkEntries android.AndroidMkEntries
9039 }
9040 entries := []*modAndMkEntries{}
9041
9042 // Gather shared lib modules that are installable
9043 for _, modName := range test.modNames {
9044 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9045 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9046 continue
9047 }
9048 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009049 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009050 continue
9051 }
Colin Crossaa255532020-07-03 13:18:24 -07009052 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009053 if ent.Disabled {
9054 continue
9055 }
9056 entries = append(entries, &modAndMkEntries{
9057 mod: mod,
9058 mkEntries: ent,
9059 })
9060 }
9061 }
9062 }
9063
9064 var entry *modAndMkEntries = nil
9065 for _, ent := range entries {
9066 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9067 if entry != nil {
9068 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9069 } else {
9070 entry = ent
9071 }
9072 }
9073 }
9074
9075 if entry == nil {
9076 t.Errorf("AndroidMk entry for \"stublib\" missing")
9077 } else {
9078 isPrebuilt := entry.mod.Prebuilt() != nil
9079 if isPrebuilt != test.usePrebuilt {
9080 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9081 }
9082 if !entry.mod.IsStubs() {
9083 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9084 }
9085 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9086 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9087 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009088 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009089 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009090 if !android.InList(expected, cflags) {
9091 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9092 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009093 }
9094 })
9095 }
9096 })
9097 }
9098}
9099
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009100func TestHostApexInHostOnlyBuild(t *testing.T) {
9101 testApex(t, `
9102 apex {
9103 name: "myapex",
9104 host_supported: true,
9105 key: "myapex.key",
9106 updatable: false,
9107 payload_type: "zip",
9108 }
9109 apex_key {
9110 name: "myapex.key",
9111 public_key: "testkey.avbpubkey",
9112 private_key: "testkey.pem",
9113 }
9114 `,
9115 android.FixtureModifyConfig(func(config android.Config) {
9116 // We may not have device targets in all builds, e.g. in
9117 // prebuilts/build-tools/build-prebuilts.sh
9118 config.Targets[android.Android] = []android.Target{}
9119 }))
9120}
9121
Colin Crossc33e5212021-05-25 18:16:02 -07009122func TestApexJavaCoverage(t *testing.T) {
9123 bp := `
9124 apex {
9125 name: "myapex",
9126 key: "myapex.key",
9127 java_libs: ["mylib"],
9128 bootclasspath_fragments: ["mybootclasspathfragment"],
9129 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9130 updatable: false,
9131 }
9132
9133 apex_key {
9134 name: "myapex.key",
9135 public_key: "testkey.avbpubkey",
9136 private_key: "testkey.pem",
9137 }
9138
9139 java_library {
9140 name: "mylib",
9141 srcs: ["mylib.java"],
9142 apex_available: ["myapex"],
9143 compile_dex: true,
9144 }
9145
9146 bootclasspath_fragment {
9147 name: "mybootclasspathfragment",
9148 contents: ["mybootclasspathlib"],
9149 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009150 hidden_api: {
9151 split_packages: ["*"],
9152 },
Colin Crossc33e5212021-05-25 18:16:02 -07009153 }
9154
9155 java_library {
9156 name: "mybootclasspathlib",
9157 srcs: ["mybootclasspathlib.java"],
9158 apex_available: ["myapex"],
9159 compile_dex: true,
9160 }
9161
9162 systemserverclasspath_fragment {
9163 name: "mysystemserverclasspathfragment",
9164 contents: ["mysystemserverclasspathlib"],
9165 apex_available: ["myapex"],
9166 }
9167
9168 java_library {
9169 name: "mysystemserverclasspathlib",
9170 srcs: ["mysystemserverclasspathlib.java"],
9171 apex_available: ["myapex"],
9172 compile_dex: true,
9173 }
9174 `
9175
9176 result := android.GroupFixturePreparers(
9177 PrepareForTestWithApexBuildComponents,
9178 prepareForTestWithMyapex,
9179 java.PrepareForTestWithJavaDefaultModules,
9180 android.PrepareForTestWithAndroidBuildComponents,
9181 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009182 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9183 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009184 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009185 ).RunTest(t)
9186
9187 // Make sure jacoco ran on both mylib and mybootclasspathlib
9188 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9189 t.Errorf("Failed to find jacoco rule for mylib")
9190 }
9191 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9192 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9193 }
9194 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9195 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9196 }
9197}
9198
Jiyong Park192600a2021-08-03 07:52:17 +00009199func TestProhibitStaticExecutable(t *testing.T) {
9200 testApexError(t, `executable mybin is static`, `
9201 apex {
9202 name: "myapex",
9203 key: "myapex.key",
9204 binaries: ["mybin"],
9205 min_sdk_version: "29",
9206 }
9207
9208 apex_key {
9209 name: "myapex.key",
9210 public_key: "testkey.avbpubkey",
9211 private_key: "testkey.pem",
9212 }
9213
9214 cc_binary {
9215 name: "mybin",
9216 srcs: ["mylib.cpp"],
9217 relative_install_path: "foo/bar",
9218 static_executable: true,
9219 system_shared_libs: [],
9220 stl: "none",
9221 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009222 min_sdk_version: "29",
9223 }
9224 `)
9225
9226 testApexError(t, `executable mybin.rust is static`, `
9227 apex {
9228 name: "myapex",
9229 key: "myapex.key",
9230 binaries: ["mybin.rust"],
9231 min_sdk_version: "29",
9232 }
9233
9234 apex_key {
9235 name: "myapex.key",
9236 public_key: "testkey.avbpubkey",
9237 private_key: "testkey.pem",
9238 }
9239
9240 rust_binary {
9241 name: "mybin.rust",
9242 srcs: ["foo.rs"],
9243 static_executable: true,
9244 apex_available: ["myapex"],
9245 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009246 }
9247 `)
9248}
9249
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009250func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9251 ctx := testApex(t, `
9252 apex {
9253 name: "myapex",
9254 key: "myapex.key",
9255 updatable: false,
9256 java_libs: ["foo"],
9257 }
9258
9259 apex_key {
9260 name: "myapex.key",
9261 public_key: "testkey.avbpubkey",
9262 private_key: "testkey.pem",
9263 }
9264
9265 java_library {
9266 name: "foo",
9267 srcs: ["foo.java"],
9268 apex_available: ["myapex"],
9269 installable: true,
9270 }
9271 `,
9272 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9273 )
9274
9275 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9276 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9277 var builder strings.Builder
9278 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9279 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009280 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 +00009281}
9282
9283func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9284 ctx := testApex(t, `
9285 prebuilt_apex {
9286 name: "myapex",
9287 arch: {
9288 arm64: {
9289 src: "myapex-arm64.apex",
9290 },
9291 arm: {
9292 src: "myapex-arm.apex",
9293 },
9294 },
9295 exported_java_libs: ["foo"],
9296 }
9297
9298 java_import {
9299 name: "foo",
9300 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009301 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009302 }
9303 `,
9304 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9305 )
9306
9307 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9308 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9309 mainModuleEntries := entriesList[0]
9310 android.AssertArrayString(t,
9311 "LOCAL_REQUIRED_MODULES",
9312 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9313 []string{
9314 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9315 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9316 })
9317}
9318
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009319func TestAndroidMk_RequiredModules(t *testing.T) {
9320 ctx := testApex(t, `
9321 apex {
9322 name: "myapex",
9323 key: "myapex.key",
9324 updatable: false,
9325 java_libs: ["foo"],
9326 required: ["otherapex"],
9327 }
9328
9329 apex {
9330 name: "otherapex",
9331 key: "myapex.key",
9332 updatable: false,
9333 java_libs: ["foo"],
9334 required: ["otherapex"],
9335 }
9336
9337 apex_key {
9338 name: "myapex.key",
9339 public_key: "testkey.avbpubkey",
9340 private_key: "testkey.pem",
9341 }
9342
9343 java_library {
9344 name: "foo",
9345 srcs: ["foo.java"],
9346 apex_available: ["myapex", "otherapex"],
9347 installable: true,
9348 }
9349 `)
9350
9351 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9352 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9353 var builder strings.Builder
9354 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9355 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009356 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009357}
9358
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009359func TestAndroidMk_RequiredDeps(t *testing.T) {
9360 ctx := testApex(t, `
9361 apex {
9362 name: "myapex",
9363 key: "myapex.key",
9364 updatable: false,
9365 }
9366
9367 apex_key {
9368 name: "myapex.key",
9369 public_key: "testkey.avbpubkey",
9370 private_key: "testkey.pem",
9371 }
9372 `)
9373
9374 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009375 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009376 data := android.AndroidMkDataForTest(t, ctx, bundle)
9377 var builder strings.Builder
9378 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9379 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009380 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009381
9382 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009383 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009384 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9385 var flattenedBuilder strings.Builder
9386 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9387 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009388 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009389}
9390
Jooyung Hana6d36672022-02-24 13:58:07 +09009391func TestApexOutputFileProducer(t *testing.T) {
9392 for _, tc := range []struct {
9393 name string
9394 ref string
9395 expected_data []string
9396 }{
9397 {
9398 name: "test_using_output",
9399 ref: ":myapex",
9400 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9401 },
9402 {
9403 name: "test_using_apex",
9404 ref: ":myapex{.apex}",
9405 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9406 },
9407 } {
9408 t.Run(tc.name, func(t *testing.T) {
9409 ctx := testApex(t, `
9410 apex {
9411 name: "myapex",
9412 key: "myapex.key",
9413 compressible: true,
9414 updatable: false,
9415 }
9416
9417 apex_key {
9418 name: "myapex.key",
9419 public_key: "testkey.avbpubkey",
9420 private_key: "testkey.pem",
9421 }
9422
9423 java_test {
9424 name: "`+tc.name+`",
9425 srcs: ["a.java"],
9426 data: ["`+tc.ref+`"],
9427 }
9428 `,
9429 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9430 variables.CompressedApex = proptools.BoolPtr(true)
9431 }))
9432 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9433 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9434 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9435 })
9436 }
9437}
9438
satayev758968a2021-12-06 11:42:40 +00009439func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9440 preparer := android.GroupFixturePreparers(
9441 PrepareForTestWithApexBuildComponents,
9442 prepareForTestWithMyapex,
9443 java.PrepareForTestWithJavaSdkLibraryFiles,
9444 java.PrepareForTestWithJavaDefaultModules,
9445 android.PrepareForTestWithAndroidBuildComponents,
9446 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9447 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9448 )
9449
9450 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9451 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9452 preparer.RunTestWithBp(t, `
9453 apex {
9454 name: "myapex",
9455 key: "myapex.key",
9456 bootclasspath_fragments: ["mybootclasspathfragment"],
9457 min_sdk_version: "30",
9458 updatable: false,
9459 }
9460
9461 apex_key {
9462 name: "myapex.key",
9463 public_key: "testkey.avbpubkey",
9464 private_key: "testkey.pem",
9465 }
9466
9467 bootclasspath_fragment {
9468 name: "mybootclasspathfragment",
9469 contents: ["mybootclasspathlib"],
9470 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009471 hidden_api: {
9472 split_packages: ["*"],
9473 },
satayev758968a2021-12-06 11:42:40 +00009474 }
9475
9476 java_sdk_library {
9477 name: "mybootclasspathlib",
9478 srcs: ["mybootclasspathlib.java"],
9479 apex_available: ["myapex"],
9480 compile_dex: true,
9481 unsafe_ignore_missing_latest_api: true,
9482 min_sdk_version: "31",
9483 static_libs: ["util"],
9484 }
9485
9486 java_library {
9487 name: "util",
9488 srcs: ["a.java"],
9489 apex_available: ["myapex"],
9490 min_sdk_version: "31",
9491 static_libs: ["another_util"],
9492 }
9493
9494 java_library {
9495 name: "another_util",
9496 srcs: ["a.java"],
9497 min_sdk_version: "31",
9498 apex_available: ["myapex"],
9499 }
9500 `)
9501 })
9502
9503 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9504 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9505 preparer.RunTestWithBp(t, `
9506 apex {
9507 name: "myapex",
9508 key: "myapex.key",
9509 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9510 min_sdk_version: "30",
9511 updatable: false,
9512 }
9513
9514 apex_key {
9515 name: "myapex.key",
9516 public_key: "testkey.avbpubkey",
9517 private_key: "testkey.pem",
9518 }
9519
9520 systemserverclasspath_fragment {
9521 name: "mysystemserverclasspathfragment",
9522 contents: ["mysystemserverclasspathlib"],
9523 apex_available: ["myapex"],
9524 }
9525
9526 java_sdk_library {
9527 name: "mysystemserverclasspathlib",
9528 srcs: ["mysystemserverclasspathlib.java"],
9529 apex_available: ["myapex"],
9530 compile_dex: true,
9531 min_sdk_version: "32",
9532 unsafe_ignore_missing_latest_api: true,
9533 static_libs: ["util"],
9534 }
9535
9536 java_library {
9537 name: "util",
9538 srcs: ["a.java"],
9539 apex_available: ["myapex"],
9540 min_sdk_version: "31",
9541 static_libs: ["another_util"],
9542 }
9543
9544 java_library {
9545 name: "another_util",
9546 srcs: ["a.java"],
9547 min_sdk_version: "31",
9548 apex_available: ["myapex"],
9549 }
9550 `)
9551 })
9552
9553 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9554 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9555 RunTestWithBp(t, `
9556 apex {
9557 name: "myapex",
9558 key: "myapex.key",
9559 bootclasspath_fragments: ["mybootclasspathfragment"],
9560 min_sdk_version: "30",
9561 updatable: false,
9562 }
9563
9564 apex_key {
9565 name: "myapex.key",
9566 public_key: "testkey.avbpubkey",
9567 private_key: "testkey.pem",
9568 }
9569
9570 bootclasspath_fragment {
9571 name: "mybootclasspathfragment",
9572 contents: ["mybootclasspathlib"],
9573 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009574 hidden_api: {
9575 split_packages: ["*"],
9576 },
satayev758968a2021-12-06 11:42:40 +00009577 }
9578
9579 java_sdk_library {
9580 name: "mybootclasspathlib",
9581 srcs: ["mybootclasspathlib.java"],
9582 apex_available: ["myapex"],
9583 compile_dex: true,
9584 unsafe_ignore_missing_latest_api: true,
9585 }
9586 `)
9587 })
9588
9589 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9590 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9591 RunTestWithBp(t, `
9592 apex {
9593 name: "myapex",
9594 key: "myapex.key",
9595 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9596 min_sdk_version: "30",
9597 updatable: false,
9598 }
9599
9600 apex_key {
9601 name: "myapex.key",
9602 public_key: "testkey.avbpubkey",
9603 private_key: "testkey.pem",
9604 }
9605
9606 systemserverclasspath_fragment {
9607 name: "mysystemserverclasspathfragment",
9608 contents: ["mysystemserverclasspathlib"],
9609 apex_available: ["myapex"],
9610 }
9611
9612 java_sdk_library {
9613 name: "mysystemserverclasspathlib",
9614 srcs: ["mysystemserverclasspathlib.java"],
9615 apex_available: ["myapex"],
9616 compile_dex: true,
9617 unsafe_ignore_missing_latest_api: true,
9618 }
9619 `)
9620 })
9621}
9622
Jiakai Zhang6decef92022-01-12 17:56:19 +00009623// Verifies that the APEX depends on all the Make modules in the list.
9624func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9625 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9626 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009627 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009628 }
9629}
9630
9631// Verifies that the APEX does not depend on any of the Make modules in the list.
9632func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9633 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9634 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009635 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009636 }
9637}
9638
Cole Faust1021ccd2023-02-26 21:15:25 -08009639// TODO(b/193460475): Re-enable this test
9640//func TestApexStrictUpdtabilityLint(t *testing.T) {
9641// bpTemplate := `
9642// apex {
9643// name: "myapex",
9644// key: "myapex.key",
9645// java_libs: ["myjavalib"],
9646// updatable: %v,
9647// min_sdk_version: "29",
9648// }
9649// apex_key {
9650// name: "myapex.key",
9651// }
9652// java_library {
9653// name: "myjavalib",
9654// srcs: ["MyClass.java"],
9655// apex_available: [ "myapex" ],
9656// lint: {
9657// strict_updatability_linting: %v,
9658// },
9659// sdk_version: "current",
9660// min_sdk_version: "29",
9661// }
9662// `
9663// fs := android.MockFS{
9664// "lint-baseline.xml": nil,
9665// }
9666//
9667// testCases := []struct {
9668// testCaseName string
9669// apexUpdatable bool
9670// javaStrictUpdtabilityLint bool
9671// lintFileExists bool
9672// disallowedFlagExpected bool
9673// }{
9674// {
9675// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9676// apexUpdatable: true,
9677// javaStrictUpdtabilityLint: true,
9678// lintFileExists: false,
9679// disallowedFlagExpected: false,
9680// },
9681// {
9682// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9683// apexUpdatable: false,
9684// javaStrictUpdtabilityLint: false,
9685// lintFileExists: true,
9686// disallowedFlagExpected: false,
9687// },
9688// {
9689// testCaseName: "non-updatable apex respects strict updatability of javalib",
9690// apexUpdatable: false,
9691// javaStrictUpdtabilityLint: true,
9692// lintFileExists: true,
9693// disallowedFlagExpected: true,
9694// },
9695// {
9696// testCaseName: "updatable apex sets strict updatability of javalib to true",
9697// apexUpdatable: true,
9698// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9699// lintFileExists: true,
9700// disallowedFlagExpected: true,
9701// },
9702// }
9703//
9704// for _, testCase := range testCases {
9705// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9706// fixtures := []android.FixturePreparer{}
9707// if testCase.lintFileExists {
9708// fixtures = append(fixtures, fs.AddToFixture())
9709// }
9710//
9711// result := testApex(t, bp, fixtures...)
9712// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9713// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9714// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9715//
9716// if disallowedFlagActual != testCase.disallowedFlagExpected {
9717// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9718// }
9719// }
9720//}
9721//
9722//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9723// bp := `
9724// apex {
9725// name: "myapex",
9726// key: "myapex.key",
9727// java_libs: ["myjavalib"],
9728// updatable: true,
9729// min_sdk_version: "29",
9730// }
9731// apex_key {
9732// name: "myapex.key",
9733// }
9734// java_library {
9735// name: "myjavalib",
9736// srcs: ["MyClass.java"],
9737// apex_available: [ "myapex" ],
9738// sdk_version: "current",
9739// min_sdk_version: "29",
9740// }
9741// `
9742//
9743// testCases := []struct {
9744// testCaseName string
9745// moduleDirectory string
9746// disallowedFlagExpected bool
9747// }{
9748// {
9749// testCaseName: "lintable module defined outside libcore",
9750// moduleDirectory: "",
9751// disallowedFlagExpected: true,
9752// },
9753// {
9754// testCaseName: "lintable module defined in libcore root directory",
9755// moduleDirectory: "libcore/",
9756// disallowedFlagExpected: false,
9757// },
9758// {
9759// testCaseName: "lintable module defined in libcore child directory",
9760// moduleDirectory: "libcore/childdir/",
9761// disallowedFlagExpected: true,
9762// },
9763// }
9764//
9765// for _, testCase := range testCases {
9766// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9767// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9768// result := testApex(t, "", lintFileCreator, bpFileCreator)
9769// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9770// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9771// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9772// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9773//
9774// if disallowedFlagActual != testCase.disallowedFlagExpected {
9775// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9776// }
9777// }
9778//}
9779//
9780//// checks transtive deps of an apex coming from bootclasspath_fragment
9781//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9782// bp := `
9783// apex {
9784// name: "myapex",
9785// key: "myapex.key",
9786// bootclasspath_fragments: ["mybootclasspathfragment"],
9787// updatable: true,
9788// min_sdk_version: "29",
9789// }
9790// apex_key {
9791// name: "myapex.key",
9792// }
9793// bootclasspath_fragment {
9794// name: "mybootclasspathfragment",
9795// contents: ["myjavalib"],
9796// apex_available: ["myapex"],
9797// hidden_api: {
9798// split_packages: ["*"],
9799// },
9800// }
9801// java_library {
9802// name: "myjavalib",
9803// srcs: ["MyClass.java"],
9804// apex_available: [ "myapex" ],
9805// sdk_version: "current",
9806// min_sdk_version: "29",
9807// compile_dex: true,
9808// }
9809// `
9810// fs := android.MockFS{
9811// "lint-baseline.xml": nil,
9812// }
9813//
9814// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9815// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9816// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9817// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9818// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9819// }
9820//}
Spandan Das66773252022-01-15 00:23:18 +00009821
Spandan Das42e89502022-05-06 22:12:55 +00009822// updatable apexes should propagate updatable=true to its apps
9823func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9824 bp := `
9825 apex {
9826 name: "myapex",
9827 key: "myapex.key",
9828 updatable: %v,
9829 apps: [
9830 "myapp",
9831 ],
9832 min_sdk_version: "30",
9833 }
9834 apex_key {
9835 name: "myapex.key",
9836 }
9837 android_app {
9838 name: "myapp",
9839 updatable: %v,
9840 apex_available: [
9841 "myapex",
9842 ],
9843 sdk_version: "current",
9844 min_sdk_version: "30",
9845 }
9846 `
9847 testCases := []struct {
9848 name string
9849 apex_is_updatable_bp bool
9850 app_is_updatable_bp bool
9851 app_is_updatable_expected bool
9852 }{
9853 {
9854 name: "Non-updatable apex respects updatable property of non-updatable app",
9855 apex_is_updatable_bp: false,
9856 app_is_updatable_bp: false,
9857 app_is_updatable_expected: false,
9858 },
9859 {
9860 name: "Non-updatable apex respects updatable property of updatable app",
9861 apex_is_updatable_bp: false,
9862 app_is_updatable_bp: true,
9863 app_is_updatable_expected: true,
9864 },
9865 {
9866 name: "Updatable apex respects updatable property of updatable app",
9867 apex_is_updatable_bp: true,
9868 app_is_updatable_bp: true,
9869 app_is_updatable_expected: true,
9870 },
9871 {
9872 name: "Updatable apex sets updatable=true on non-updatable app",
9873 apex_is_updatable_bp: true,
9874 app_is_updatable_bp: false,
9875 app_is_updatable_expected: true,
9876 },
9877 }
9878 for _, testCase := range testCases {
9879 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9880 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9881 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9882 }
9883}
9884
Kiyoung Kim487689e2022-07-26 09:48:22 +09009885func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9886 bp := `
9887 apex {
9888 name: "myapex",
9889 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009890 native_shared_libs: ["libbaz"],
9891 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009892 min_sdk_version: "29",
9893 }
9894 apex_key {
9895 name: "myapex.key",
9896 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009897 cc_binary {
9898 name: "binfoo",
9899 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009900 apex_available: ["myapex"],
9901 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009902 recovery_available: false,
9903 }
9904 cc_library {
9905 name: "libbar",
9906 srcs: ["libbar.cc"],
9907 stubs: {
9908 symbol_file: "libbar.map.txt",
9909 versions: [
9910 "29",
9911 ],
9912 },
9913 }
9914 cc_library {
9915 name: "libbaz",
9916 srcs: ["libbaz.cc"],
9917 apex_available: ["myapex"],
9918 min_sdk_version: "29",
9919 stubs: {
9920 symbol_file: "libbaz.map.txt",
9921 versions: [
9922 "29",
9923 ],
9924 },
Kiyoung Kim487689e2022-07-26 09:48:22 +09009925 }
9926 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009927 name: "libbar",
9928 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009929 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009930 variants: ["apex.29"],
9931 }
9932 cc_api_variant {
9933 name: "libbar",
9934 variant: "apex",
9935 version: "29",
9936 src: "libbar_apex_29.so",
9937 }
9938 cc_api_library {
9939 name: "libbaz",
9940 src: "libbaz_stub.so",
9941 min_sdk_version: "29",
9942 variants: ["apex.29"],
9943 }
9944 cc_api_variant {
9945 name: "libbaz",
9946 variant: "apex",
9947 version: "29",
9948 src: "libbaz_apex_29.so",
9949 }
9950 cc_api_library {
9951 name: "libqux",
9952 src: "libqux_stub.so",
9953 min_sdk_version: "29",
9954 variants: ["apex.29"],
9955 }
9956 cc_api_variant {
9957 name: "libqux",
9958 variant: "apex",
9959 version: "29",
9960 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009961 }
9962 api_imports {
9963 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009964 apex_shared_libs: [
9965 "libbar",
9966 "libbaz",
9967 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009968 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009969 }
9970 `
9971 result := testApex(t, bp)
9972
9973 hasDep := func(m android.Module, wantDep android.Module) bool {
9974 t.Helper()
9975 var found bool
9976 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9977 if dep == wantDep {
9978 found = true
9979 }
9980 })
9981 return found
9982 }
9983
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009984 // Library defines stubs and cc_api_library should be used with cc_api_library
9985 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
9986 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
9987 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +09009988
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009989 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
9990 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +09009991
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009992 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
9993 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
9994 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
9995 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
9996
9997 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
9998 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
9999 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10000 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10001 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10002
10003 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10004 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10005 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10006
10007 // cc_api_library defined without original library should be linked with cc_api_library
10008 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10009 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10010 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10011}
10012
10013func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10014 bp := `
10015 apex {
10016 name: "myapex",
10017 key: "myapex.key",
10018 native_shared_libs: ["libbar"],
10019 min_sdk_version: "29",
10020 }
10021 apex_key {
10022 name: "myapex.key",
10023 }
10024 cc_binary {
10025 name: "binfoo",
10026 shared_libs: ["libbar"],
10027 recovery_available: false,
10028 }
10029 cc_library {
10030 name: "libbar",
10031 srcs: ["libbar.cc"],
10032 apex_available: ["myapex"],
10033 min_sdk_version: "29",
10034 stubs: {
10035 symbol_file: "libbar.map.txt",
10036 versions: [
10037 "29",
10038 ],
10039 },
10040 }
10041 cc_api_library {
10042 name: "libbar",
10043 src: "libbar_stub.so",
10044 variants: ["apex.29"],
10045 }
10046 cc_api_variant {
10047 name: "libbar",
10048 variant: "apex",
10049 version: "29",
10050 src: "libbar_apex_29.so",
10051 }
10052 api_imports {
10053 name: "api_imports",
10054 apex_shared_libs: [
10055 "libbar",
10056 ],
10057 }
10058 `
10059
10060 result := testApex(t, bp)
10061
10062 hasDep := func(m android.Module, wantDep android.Module) bool {
10063 t.Helper()
10064 var found bool
10065 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10066 if dep == wantDep {
10067 found = true
10068 }
10069 })
10070 return found
10071 }
10072
10073 // Library defines stubs and cc_api_library should be used with cc_api_library
10074 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10075 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10076 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10077
10078 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10079 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10080
10081 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10082 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10083 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10084 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010085}
Dennis Shend4f5d932023-01-31 20:27:21 +000010086
10087func TestTrimmedApex(t *testing.T) {
10088 bp := `
10089 apex {
10090 name: "myapex",
10091 key: "myapex.key",
10092 native_shared_libs: ["libfoo","libbaz"],
10093 min_sdk_version: "29",
10094 trim_against: "mydcla",
10095 }
10096 apex {
10097 name: "mydcla",
10098 key: "myapex.key",
10099 native_shared_libs: ["libfoo","libbar"],
10100 min_sdk_version: "29",
10101 file_contexts: ":myapex-file_contexts",
10102 dynamic_common_lib_apex: true,
10103 }
10104 apex_key {
10105 name: "myapex.key",
10106 }
10107 cc_library {
10108 name: "libfoo",
10109 shared_libs: ["libc"],
10110 apex_available: ["myapex","mydcla"],
10111 min_sdk_version: "29",
10112 }
10113 cc_library {
10114 name: "libbar",
10115 shared_libs: ["libc"],
10116 apex_available: ["myapex","mydcla"],
10117 min_sdk_version: "29",
10118 }
10119 cc_library {
10120 name: "libbaz",
10121 shared_libs: ["libc"],
10122 apex_available: ["myapex","mydcla"],
10123 min_sdk_version: "29",
10124 }
10125 cc_api_library {
10126 name: "libc",
10127 src: "libc.so",
10128 min_sdk_version: "29",
10129 recovery_available: true,
10130 }
10131 api_imports {
10132 name: "api_imports",
10133 shared_libs: [
10134 "libc",
10135 ],
10136 header_libs: [],
10137 }
10138 `
10139 ctx := testApex(t, bp)
10140 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10141 apexRule := module.MaybeRule("apexRule")
10142 if apexRule.Rule == nil {
10143 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10144 }
10145
10146 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
10147 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
10148 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10149 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10150 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10151 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10152}
Jingwen Chendea7a642023-03-28 11:30:50 +000010153
10154func TestCannedFsConfig(t *testing.T) {
10155 ctx := testApex(t, `
10156 apex {
10157 name: "myapex",
10158 key: "myapex.key",
10159 updatable: false,
10160 }
10161
10162 apex_key {
10163 name: "myapex.key",
10164 public_key: "testkey.avbpubkey",
10165 private_key: "testkey.pem",
10166 }`)
10167 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10168 generateFsRule := mod.Rule("generateFsConfig")
10169 cmd := generateFsRule.RuleParams.Command
10170
10171 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10172}
10173
10174func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10175 ctx := testApex(t, `
10176 apex {
10177 name: "myapex",
10178 key: "myapex.key",
10179 canned_fs_config: "my_config",
10180 updatable: false,
10181 }
10182
10183 apex_key {
10184 name: "myapex.key",
10185 public_key: "testkey.avbpubkey",
10186 private_key: "testkey.pem",
10187 }`)
10188 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10189 generateFsRule := mod.Rule("generateFsConfig")
10190 cmd := generateFsRule.RuleParams.Command
10191
10192 // Ensure that canned_fs_config has "cat my_config" at the end
10193 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10194}