blob: 53e922cd34616aa1607c89213eeb3c9a15901dd1 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Alex Light5098a612018-11-29 17:12:15 -0800787func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800788 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800789 apex {
790 name: "myapex",
791 key: "myapex.key",
792 payload_type: "zip",
793 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000794 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800795 }
796
797 apex_key {
798 name: "myapex.key",
799 public_key: "testkey.avbpubkey",
800 private_key: "testkey.pem",
801 }
802
803 cc_library {
804 name: "mylib",
805 srcs: ["mylib.cpp"],
806 shared_libs: ["mylib2"],
807 system_shared_libs: [],
808 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000809 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800810 }
811
812 cc_library {
813 name: "mylib2",
814 srcs: ["mylib.cpp"],
815 system_shared_libs: [],
816 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000817 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800818 }
819 `)
820
Sundong Ahnabb64432019-10-22 13:58:29 +0900821 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800822 copyCmds := zipApexRule.Args["copy_commands"]
823
824 // Ensure that main rule creates an output
825 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
826
827 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700828 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800829
830 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700831 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800832
833 // Ensure that both direct and indirect deps are copied into apex
834 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
835 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900836}
837
838func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800839 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 apex {
841 name: "myapex",
842 key: "myapex.key",
843 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900844 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000845 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900846 }
847
848 apex_key {
849 name: "myapex.key",
850 public_key: "testkey.avbpubkey",
851 private_key: "testkey.pem",
852 }
853
854 cc_library {
855 name: "mylib",
856 srcs: ["mylib.cpp"],
857 shared_libs: ["mylib2", "mylib3"],
858 system_shared_libs: [],
859 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000860 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900861 }
862
863 cc_library {
864 name: "mylib2",
865 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900866 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900867 system_shared_libs: [],
868 stl: "none",
869 stubs: {
870 versions: ["1", "2", "3"],
871 },
872 }
873
874 cc_library {
875 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900876 srcs: ["mylib.cpp"],
877 shared_libs: ["mylib4"],
878 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900879 stl: "none",
880 stubs: {
881 versions: ["10", "11", "12"],
882 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000883 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900884 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900885
886 cc_library {
887 name: "mylib4",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000891 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900892 }
Jiyong Park105dc322021-06-11 17:22:09 +0900893
894 rust_binary {
895 name: "foo.rust",
896 srcs: ["foo.rs"],
897 shared_libs: ["libfoo.shared_from_rust"],
898 prefer_rlib: true,
899 apex_available: ["myapex"],
900 }
901
902 cc_library_shared {
903 name: "libfoo.shared_from_rust",
904 srcs: ["mylib.cpp"],
905 system_shared_libs: [],
906 stl: "none",
907 stubs: {
908 versions: ["10", "11", "12"],
909 },
910 }
911
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 `)
913
Sundong Ahnabb64432019-10-22 13:58:29 +0900914 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915 copyCmds := apexRule.Args["copy_commands"]
916
917 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800918 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919
920 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800921 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922
923 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800924 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925
Colin Crossaede88c2020-08-11 12:17:01 -0700926 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927
928 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900929 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900930 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900931 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932
933 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700934 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700936 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900937
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700938 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
939 // is replaced by sharing of "cFlags" in cc/builder.go.
940 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
941 // module variable representing "cflags". So it was not detected by ensureNotContains.
942 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
943 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
944 // including the original cflags's "-include mylib.h".
945 //
Jiyong Park64379952018-12-13 18:37:29 +0900946 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700947 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
948 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900949
Jiyong Park85cc35a2022-07-17 11:30:47 +0900950 // Ensure that genstub for platform-provided lib is invoked with --systemapi
951 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
952 // Ensure that genstub for apex-provided lib is invoked with --apex
953 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900954
Jooyung Hana57af4a2020-01-23 05:36:59 +0000955 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900956 "lib64/mylib.so",
957 "lib64/mylib3.so",
958 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900959 "bin/foo.rust",
960 "lib64/libc++.so", // by the implicit dependency from foo.rust
961 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900962 })
Jiyong Park105dc322021-06-11 17:22:09 +0900963
964 // Ensure that stub dependency from a rust module is not included
965 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
966 // The rust module is linked to the stub cc library
967 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
968 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
969 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900970
971 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
972 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973}
974
Jiyong Park1bc84122021-06-22 20:23:05 +0900975func TestApexCanUsePrivateApis(t *testing.T) {
976 ctx := testApex(t, `
977 apex {
978 name: "myapex",
979 key: "myapex.key",
980 native_shared_libs: ["mylib"],
981 binaries: ["foo.rust"],
982 updatable: false,
983 platform_apis: true,
984 }
985
986 apex_key {
987 name: "myapex.key",
988 public_key: "testkey.avbpubkey",
989 private_key: "testkey.pem",
990 }
991
992 cc_library {
993 name: "mylib",
994 srcs: ["mylib.cpp"],
995 shared_libs: ["mylib2"],
996 system_shared_libs: [],
997 stl: "none",
998 apex_available: [ "myapex" ],
999 }
1000
1001 cc_library {
1002 name: "mylib2",
1003 srcs: ["mylib.cpp"],
1004 cflags: ["-include mylib.h"],
1005 system_shared_libs: [],
1006 stl: "none",
1007 stubs: {
1008 versions: ["1", "2", "3"],
1009 },
1010 }
1011
1012 rust_binary {
1013 name: "foo.rust",
1014 srcs: ["foo.rs"],
1015 shared_libs: ["libfoo.shared_from_rust"],
1016 prefer_rlib: true,
1017 apex_available: ["myapex"],
1018 }
1019
1020 cc_library_shared {
1021 name: "libfoo.shared_from_rust",
1022 srcs: ["mylib.cpp"],
1023 system_shared_libs: [],
1024 stl: "none",
1025 stubs: {
1026 versions: ["10", "11", "12"],
1027 },
1028 }
1029 `)
1030
1031 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1032 copyCmds := apexRule.Args["copy_commands"]
1033
1034 // Ensure that indirect stubs dep is not included
1035 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1036 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1037
1038 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1039 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001040 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001041 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1042 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Parkd4a00632022-04-12 12:23:20 +09001043 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001044 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1045 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1046}
1047
Colin Cross7812fd32020-09-25 12:35:10 -07001048func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1049 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001050 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
1054 native_shared_libs: ["mylib", "mylib3"],
1055 min_sdk_version: "29",
1056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 cc_library {
1065 name: "mylib",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["mylib2", "mylib3"],
1068 system_shared_libs: [],
1069 stl: "none",
1070 apex_available: [ "myapex" ],
1071 min_sdk_version: "28",
1072 }
1073
1074 cc_library {
1075 name: "mylib2",
1076 srcs: ["mylib.cpp"],
1077 cflags: ["-include mylib.h"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 stubs: {
1081 versions: ["28", "29", "30", "current"],
1082 },
1083 min_sdk_version: "28",
1084 }
1085
1086 cc_library {
1087 name: "mylib3",
1088 srcs: ["mylib.cpp"],
1089 shared_libs: ["mylib4"],
1090 system_shared_libs: [],
1091 stl: "none",
1092 stubs: {
1093 versions: ["28", "29", "30", "current"],
1094 },
1095 apex_available: [ "myapex" ],
1096 min_sdk_version: "28",
1097 }
1098
1099 cc_library {
1100 name: "mylib4",
1101 srcs: ["mylib.cpp"],
1102 system_shared_libs: [],
1103 stl: "none",
1104 apex_available: [ "myapex" ],
1105 min_sdk_version: "28",
1106 }
1107 `)
1108
1109 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1110 copyCmds := apexRule.Args["copy_commands"]
1111
1112 // Ensure that direct non-stubs dep is always included
1113 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1114
1115 // Ensure that indirect stubs dep is not included
1116 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1117
1118 // Ensure that direct stubs dep is included
1119 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1120
1121 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1122
Jiyong Park55549df2021-02-26 23:57:23 +09001123 // Ensure that mylib is linking with the latest version of stub for mylib2
1124 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001125 // ... and not linking to the non-stub (impl) variant of mylib2
1126 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1127
1128 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1129 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1130 // .. and not linking to the stubs variant of mylib3
1131 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1132
1133 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001134 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001135 ensureNotContains(t, mylib2Cflags, "-include ")
1136
Jiyong Park85cc35a2022-07-17 11:30:47 +09001137 // Ensure that genstub is invoked with --systemapi
1138 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001139
1140 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1141 "lib64/mylib.so",
1142 "lib64/mylib3.so",
1143 "lib64/mylib4.so",
1144 })
1145}
1146
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001147func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1148 t.Parallel()
1149 // myapex (Z)
1150 // mylib -----------------.
1151 // |
1152 // otherapex (29) |
1153 // libstub's versions: 29 Z current
1154 // |
1155 // <platform> |
1156 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001157 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001158 apex {
1159 name: "myapex",
1160 key: "myapex.key",
1161 native_shared_libs: ["mylib"],
1162 min_sdk_version: "Z", // non-final
1163 }
1164
1165 cc_library {
1166 name: "mylib",
1167 srcs: ["mylib.cpp"],
1168 shared_libs: ["libstub"],
1169 apex_available: ["myapex"],
1170 min_sdk_version: "Z",
1171 }
1172
1173 apex_key {
1174 name: "myapex.key",
1175 public_key: "testkey.avbpubkey",
1176 private_key: "testkey.pem",
1177 }
1178
1179 apex {
1180 name: "otherapex",
1181 key: "myapex.key",
1182 native_shared_libs: ["libstub"],
1183 min_sdk_version: "29",
1184 }
1185
1186 cc_library {
1187 name: "libstub",
1188 srcs: ["mylib.cpp"],
1189 stubs: {
1190 versions: ["29", "Z", "current"],
1191 },
1192 apex_available: ["otherapex"],
1193 min_sdk_version: "29",
1194 }
1195
1196 // platform module depending on libstub from otherapex should use the latest stub("current")
1197 cc_library {
1198 name: "libplatform",
1199 srcs: ["mylib.cpp"],
1200 shared_libs: ["libstub"],
1201 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001202 `,
1203 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1204 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1205 variables.Platform_sdk_final = proptools.BoolPtr(false)
1206 variables.Platform_version_active_codenames = []string{"Z"}
1207 }),
1208 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001209
Jiyong Park55549df2021-02-26 23:57:23 +09001210 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001211 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001212 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001213 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001214 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001215
1216 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1217 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1218 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1219 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1220 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1221}
1222
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001223func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001224 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001225 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001226 name: "myapex2",
1227 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001228 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001229 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001230 }
1231
1232 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001233 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "mylib",
1240 srcs: ["mylib.cpp"],
1241 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001242 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001243 system_shared_libs: [],
1244 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001245 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001246 }
1247
1248 cc_library {
1249 name: "libfoo",
1250 srcs: ["mylib.cpp"],
1251 shared_libs: ["libbar"],
1252 system_shared_libs: [],
1253 stl: "none",
1254 stubs: {
1255 versions: ["10", "20", "30"],
1256 },
1257 }
1258
1259 cc_library {
1260 name: "libbar",
1261 srcs: ["mylib.cpp"],
1262 system_shared_libs: [],
1263 stl: "none",
1264 }
1265
Jiyong Park678c8812020-02-07 17:25:49 +09001266 cc_library_static {
1267 name: "libbaz",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 apex_available: [ "myapex2" ],
1272 }
1273
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001274 `)
1275
Jiyong Park83dc74b2020-01-14 18:38:44 +09001276 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001277 copyCmds := apexRule.Args["copy_commands"]
1278
1279 // Ensure that direct non-stubs dep is always included
1280 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1281
1282 // Ensure that indirect stubs dep is not included
1283 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1284
1285 // Ensure that dependency of stubs is not included
1286 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1287
Colin Crossaede88c2020-08-11 12:17:01 -07001288 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289
1290 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001291 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001292 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001293 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001294
Jiyong Park3ff16992019-12-27 14:11:47 +09001295 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001296
1297 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1298 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001299
Artur Satayeva8bd1132020-04-27 18:07:06 +01001300 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001301 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001302
Artur Satayeva8bd1132020-04-27 18:07:06 +01001303 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001304 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001305}
1306
Jooyung Hand3639552019-08-09 12:57:43 +09001307func TestApexWithRuntimeLibsDependency(t *testing.T) {
1308 /*
1309 myapex
1310 |
1311 v (runtime_libs)
1312 mylib ------+------> libfoo [provides stub]
1313 |
1314 `------> libbar
1315 */
Colin Cross1c460562021-02-16 17:55:47 -08001316 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001317 apex {
1318 name: "myapex",
1319 key: "myapex.key",
1320 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001321 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001322 }
1323
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329
1330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 runtime_libs: ["libfoo", "libbar"],
1334 system_shared_libs: [],
1335 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001336 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001337 }
1338
1339 cc_library {
1340 name: "libfoo",
1341 srcs: ["mylib.cpp"],
1342 system_shared_libs: [],
1343 stl: "none",
1344 stubs: {
1345 versions: ["10", "20", "30"],
1346 },
1347 }
1348
1349 cc_library {
1350 name: "libbar",
1351 srcs: ["mylib.cpp"],
1352 system_shared_libs: [],
1353 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001354 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001355 }
1356
1357 `)
1358
Sundong Ahnabb64432019-10-22 13:58:29 +09001359 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001360 copyCmds := apexRule.Args["copy_commands"]
1361
1362 // Ensure that direct non-stubs dep is always included
1363 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1364
1365 // Ensure that indirect stubs dep is not included
1366 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1367
1368 // Ensure that runtime_libs dep in included
1369 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1370
Sundong Ahnabb64432019-10-22 13:58:29 +09001371 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001372 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1373 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001374
1375}
1376
Paul Duffina02cae32021-03-09 01:44:06 +00001377var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1378 cc.PrepareForTestWithCcBuildComponents,
1379 PrepareForTestWithApexBuildComponents,
1380 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001381 apex {
1382 name: "com.android.runtime",
1383 key: "com.android.runtime.key",
1384 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001385 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001386 }
1387
1388 apex_key {
1389 name: "com.android.runtime.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
Paul Duffina02cae32021-03-09 01:44:06 +00001393 `),
1394 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1395)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001396
Paul Duffina02cae32021-03-09 01:44:06 +00001397func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001398 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001399 cc_library {
1400 name: "libc",
1401 no_libcrt: true,
1402 nocrt: true,
1403 stl: "none",
1404 system_shared_libs: [],
1405 stubs: { versions: ["1"] },
1406 apex_available: ["com.android.runtime"],
1407
1408 sanitize: {
1409 hwaddress: true,
1410 }
1411 }
1412
1413 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001414 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001415 no_libcrt: true,
1416 nocrt: true,
1417 stl: "none",
1418 system_shared_libs: [],
1419 srcs: [""],
1420 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001421 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001422
1423 sanitize: {
1424 never: true,
1425 },
Paul Duffina02cae32021-03-09 01:44:06 +00001426 } `)
1427 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001428
1429 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1430 "lib64/bionic/libc.so",
1431 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1432 })
1433
Colin Cross4c4c1be2022-02-10 11:41:18 -08001434 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001435
1436 installed := hwasan.Description("install libclang_rt.hwasan")
1437 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1438
1439 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1440 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1441 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1442}
1443
1444func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001445 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001446 prepareForTestOfRuntimeApexWithHwasan,
1447 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1448 variables.SanitizeDevice = []string{"hwaddress"}
1449 }),
1450 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001451 cc_library {
1452 name: "libc",
1453 no_libcrt: true,
1454 nocrt: true,
1455 stl: "none",
1456 system_shared_libs: [],
1457 stubs: { versions: ["1"] },
1458 apex_available: ["com.android.runtime"],
1459 }
1460
1461 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001462 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001463 no_libcrt: true,
1464 nocrt: true,
1465 stl: "none",
1466 system_shared_libs: [],
1467 srcs: [""],
1468 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001469 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001470
1471 sanitize: {
1472 never: true,
1473 },
1474 }
Paul Duffina02cae32021-03-09 01:44:06 +00001475 `)
1476 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001477
1478 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1479 "lib64/bionic/libc.so",
1480 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1481 })
1482
Colin Cross4c4c1be2022-02-10 11:41:18 -08001483 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001484
1485 installed := hwasan.Description("install libclang_rt.hwasan")
1486 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1487
1488 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1489 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1490 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1491}
1492
Jooyung Han61b66e92020-03-21 14:21:46 +00001493func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1494 testcases := []struct {
1495 name string
1496 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001497 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001498 shouldLink string
1499 shouldNotLink []string
1500 }{
1501 {
Jiyong Park55549df2021-02-26 23:57:23 +09001502 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001503 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001504 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001505 shouldLink: "current",
1506 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001507 },
1508 {
Jiyong Park55549df2021-02-26 23:57:23 +09001509 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001510 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001511 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001512 shouldLink: "current",
1513 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001514 },
1515 }
1516 for _, tc := range testcases {
1517 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001518 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001519 apex {
1520 name: "myapex",
1521 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001522 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001523 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001524 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001526
Jooyung Han61b66e92020-03-21 14:21:46 +00001527 apex_key {
1528 name: "myapex.key",
1529 public_key: "testkey.avbpubkey",
1530 private_key: "testkey.pem",
1531 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001532
Jooyung Han61b66e92020-03-21 14:21:46 +00001533 cc_library {
1534 name: "mylib",
1535 srcs: ["mylib.cpp"],
1536 vendor_available: true,
1537 shared_libs: ["libbar"],
1538 system_shared_libs: [],
1539 stl: "none",
1540 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001541 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001542 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001543
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 cc_library {
1545 name: "libbar",
1546 srcs: ["mylib.cpp"],
1547 system_shared_libs: [],
1548 stl: "none",
1549 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001550 llndk: {
1551 symbol_file: "libbar.map.txt",
1552 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001553 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001554 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001555 withUnbundledBuild,
1556 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001557
Jooyung Han61b66e92020-03-21 14:21:46 +00001558 // Ensure that LLNDK dep is not included
1559 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1560 "lib64/mylib.so",
1561 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001562
Jooyung Han61b66e92020-03-21 14:21:46 +00001563 // Ensure that LLNDK dep is required
1564 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1565 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1566 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001567
Steven Moreland2c4000c2021-04-27 02:08:49 +00001568 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1569 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001571 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001572 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001573
Steven Moreland2c4000c2021-04-27 02:08:49 +00001574 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001575 ver := tc.shouldLink
1576 if tc.shouldLink == "current" {
1577 ver = strconv.Itoa(android.FutureApiLevelInt)
1578 }
1579 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001580 })
1581 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001582}
1583
Jiyong Park25fc6a92018-11-18 18:02:45 +09001584func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001585 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001586 apex {
1587 name: "myapex",
1588 key: "myapex.key",
1589 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001590 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001591 }
1592
1593 apex_key {
1594 name: "myapex.key",
1595 public_key: "testkey.avbpubkey",
1596 private_key: "testkey.pem",
1597 }
1598
1599 cc_library {
1600 name: "mylib",
1601 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001602 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001603 shared_libs: ["libdl#27"],
1604 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001605 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001606 }
1607
1608 cc_library_shared {
1609 name: "mylib_shared",
1610 srcs: ["mylib.cpp"],
1611 shared_libs: ["libdl#27"],
1612 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001613 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001614 }
1615
1616 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001617 name: "libBootstrap",
1618 srcs: ["mylib.cpp"],
1619 stl: "none",
1620 bootstrap: true,
1621 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001622 `)
1623
Sundong Ahnabb64432019-10-22 13:58:29 +09001624 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001625 copyCmds := apexRule.Args["copy_commands"]
1626
1627 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001628 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001629 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1630 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001631
1632 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001633 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001634
Colin Crossaede88c2020-08-11 12:17:01 -07001635 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1636 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1637 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001638
1639 // For dependency to libc
1640 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001641 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001642 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001643 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001644 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001645 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1646 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647
1648 // For dependency to libm
1649 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001650 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001652 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001653 // ... and is not compiling with the stub
1654 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1655 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1656
1657 // For dependency to libdl
1658 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001659 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001660 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001661 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1662 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001663 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001664 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001665 // ... Cflags from stub is correctly exported to mylib
1666 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1667 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001668
1669 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001670 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1671 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1672 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1673 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001674}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001675
Jooyung Han749dc692020-04-15 11:03:39 +09001676func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001677 // there are three links between liba --> libz.
1678 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001679 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001680 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001681 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001686 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001687 }
1688
1689 apex {
1690 name: "otherapex",
1691 key: "myapex.key",
1692 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001693 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001694 }
1695
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701
1702 cc_library {
1703 name: "libx",
1704 shared_libs: ["liba"],
1705 system_shared_libs: [],
1706 stl: "none",
1707 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001708 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001709 }
1710
1711 cc_library {
1712 name: "liby",
1713 shared_libs: ["liba"],
1714 system_shared_libs: [],
1715 stl: "none",
1716 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001717 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001718 }
1719
1720 cc_library {
1721 name: "liba",
1722 shared_libs: ["libz"],
1723 system_shared_libs: [],
1724 stl: "none",
1725 apex_available: [
1726 "//apex_available:anyapex",
1727 "//apex_available:platform",
1728 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001729 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001730 }
1731
1732 cc_library {
1733 name: "libz",
1734 system_shared_libs: [],
1735 stl: "none",
1736 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001737 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001738 },
1739 }
Jooyung Han749dc692020-04-15 11:03:39 +09001740 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001741
1742 expectLink := func(from, from_variant, to, to_variant string) {
1743 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1744 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1745 }
1746 expectNoLink := func(from, from_variant, to, to_variant string) {
1747 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1748 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1749 }
1750 // platform liba is linked to non-stub version
1751 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001752 // liba in myapex is linked to current
1753 expectLink("liba", "shared_apex29", "libz", "shared_current")
1754 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001755 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001756 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001757 // liba in otherapex is linked to current
1758 expectLink("liba", "shared_apex30", "libz", "shared_current")
1759 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001760 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1761 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001762}
1763
Jooyung Hanaed150d2020-04-02 01:41:41 +09001764func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001765 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001766 apex {
1767 name: "myapex",
1768 key: "myapex.key",
1769 native_shared_libs: ["libx"],
1770 min_sdk_version: "R",
1771 }
1772
1773 apex_key {
1774 name: "myapex.key",
1775 public_key: "testkey.avbpubkey",
1776 private_key: "testkey.pem",
1777 }
1778
1779 cc_library {
1780 name: "libx",
1781 shared_libs: ["libz"],
1782 system_shared_libs: [],
1783 stl: "none",
1784 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001785 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001786 }
1787
1788 cc_library {
1789 name: "libz",
1790 system_shared_libs: [],
1791 stl: "none",
1792 stubs: {
1793 versions: ["29", "R"],
1794 },
1795 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001796 `,
1797 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1798 variables.Platform_version_active_codenames = []string{"R"}
1799 }),
1800 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801
1802 expectLink := func(from, from_variant, to, to_variant string) {
1803 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1804 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1805 }
1806 expectNoLink := func(from, from_variant, to, to_variant string) {
1807 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1808 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1809 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001810 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1811 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001812 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1813 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001814}
1815
Jooyung Han4c4da062021-06-23 10:23:16 +09001816func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1817 testApex(t, `
1818 apex {
1819 name: "myapex",
1820 key: "myapex.key",
1821 java_libs: ["libx"],
1822 min_sdk_version: "S",
1823 }
1824
1825 apex_key {
1826 name: "myapex.key",
1827 public_key: "testkey.avbpubkey",
1828 private_key: "testkey.pem",
1829 }
1830
1831 java_library {
1832 name: "libx",
1833 srcs: ["a.java"],
1834 apex_available: [ "myapex" ],
1835 sdk_version: "current",
1836 min_sdk_version: "S", // should be okay
1837 }
1838 `,
1839 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1840 variables.Platform_version_active_codenames = []string{"S"}
1841 variables.Platform_sdk_codename = proptools.StringPtr("S")
1842 }),
1843 )
1844}
1845
Jooyung Han749dc692020-04-15 11:03:39 +09001846func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001847 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001848 apex {
1849 name: "myapex",
1850 key: "myapex.key",
1851 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001852 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001853 }
1854
1855 apex_key {
1856 name: "myapex.key",
1857 public_key: "testkey.avbpubkey",
1858 private_key: "testkey.pem",
1859 }
1860
1861 cc_library {
1862 name: "libx",
1863 shared_libs: ["libz"],
1864 system_shared_libs: [],
1865 stl: "none",
1866 apex_available: [ "myapex" ],
1867 }
1868
1869 cc_library {
1870 name: "libz",
1871 system_shared_libs: [],
1872 stl: "none",
1873 stubs: {
1874 versions: ["1", "2"],
1875 },
1876 }
1877 `)
1878
1879 expectLink := func(from, from_variant, to, to_variant string) {
1880 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1881 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1882 }
1883 expectNoLink := func(from, from_variant, to, to_variant string) {
1884 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1885 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1886 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001887 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001888 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001889 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001890 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001891}
1892
Jiyong Park5df7bd32021-08-25 16:18:46 +09001893func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1894 ctx := testApex(t, `
1895 apex {
1896 name: "myapex",
1897 key: "myapex.key",
1898 native_shared_libs: ["mylib"],
1899 updatable: false,
1900 vendor: true,
1901 min_sdk_version: "29",
1902 }
1903
1904 apex_key {
1905 name: "myapex.key",
1906 public_key: "testkey.avbpubkey",
1907 private_key: "testkey.pem",
1908 }
1909
1910 cc_library {
1911 name: "mylib",
1912 vendor_available: true,
1913 system_shared_libs: [],
1914 stl: "none",
1915 apex_available: [ "myapex" ],
1916 min_sdk_version: "29",
1917 }
1918 `)
1919
1920 vendorVariant := "android_vendor.29_arm64_armv8-a"
1921
1922 // First check that the correct variant of crtbegin_so is used.
1923 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1924 crtBegin := names(ldRule.Args["crtBegin"])
1925 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1926
1927 // Ensure that the crtbegin_so used by the APEX is targeting 29
1928 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1929 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1930}
1931
Jooyung Han03b51852020-02-26 22:45:42 +09001932func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001933 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001934 apex {
1935 name: "myapex",
1936 key: "myapex.key",
1937 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001938 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "libx",
1949 system_shared_libs: [],
1950 stl: "none",
1951 apex_available: [ "myapex" ],
1952 stubs: {
1953 versions: ["1", "2"],
1954 },
1955 }
1956
1957 cc_library {
1958 name: "libz",
1959 shared_libs: ["libx"],
1960 system_shared_libs: [],
1961 stl: "none",
1962 }
1963 `)
1964
1965 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001966 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001967 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1968 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1969 }
1970 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001971 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001972 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1973 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1974 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001975 expectLink("libz", "shared", "libx", "shared_current")
1976 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001977 expectNoLink("libz", "shared", "libz", "shared_1")
1978 expectNoLink("libz", "shared", "libz", "shared")
1979}
1980
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001981var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1982 func(variables android.FixtureProductVariables) {
1983 variables.SanitizeDevice = []string{"hwaddress"}
1984 },
1985)
1986
Jooyung Han75568392020-03-20 04:29:24 +09001987func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001988 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001989 apex {
1990 name: "myapex",
1991 key: "myapex.key",
1992 native_shared_libs: ["libx"],
1993 min_sdk_version: "29",
1994 }
1995
1996 apex_key {
1997 name: "myapex.key",
1998 public_key: "testkey.avbpubkey",
1999 private_key: "testkey.pem",
2000 }
2001
2002 cc_library {
2003 name: "libx",
2004 shared_libs: ["libbar"],
2005 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002006 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002007 }
2008
2009 cc_library {
2010 name: "libbar",
2011 stubs: {
2012 versions: ["29", "30"],
2013 },
2014 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002015 `,
2016 prepareForTestWithSantitizeHwaddress,
2017 )
Jooyung Han03b51852020-02-26 22:45:42 +09002018 expectLink := func(from, from_variant, to, to_variant string) {
2019 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2020 libFlags := ld.Args["libFlags"]
2021 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2022 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002023 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002024}
2025
Jooyung Han75568392020-03-20 04:29:24 +09002026func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002027 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002028 apex {
2029 name: "myapex",
2030 key: "myapex.key",
2031 native_shared_libs: ["libx"],
2032 min_sdk_version: "29",
2033 }
2034
2035 apex_key {
2036 name: "myapex.key",
2037 public_key: "testkey.avbpubkey",
2038 private_key: "testkey.pem",
2039 }
2040
2041 cc_library {
2042 name: "libx",
2043 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002044 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002045 }
Jooyung Han75568392020-03-20 04:29:24 +09002046 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002047
2048 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002049 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002050 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002051 // note that platform variant is not.
2052 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002053 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002054}
2055
Jooyung Han749dc692020-04-15 11:03:39 +09002056func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2057 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002058 apex {
2059 name: "myapex",
2060 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002061 native_shared_libs: ["mylib"],
2062 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002063 }
2064
2065 apex_key {
2066 name: "myapex.key",
2067 public_key: "testkey.avbpubkey",
2068 private_key: "testkey.pem",
2069 }
Jooyung Han749dc692020-04-15 11:03:39 +09002070
2071 cc_library {
2072 name: "mylib",
2073 srcs: ["mylib.cpp"],
2074 system_shared_libs: [],
2075 stl: "none",
2076 apex_available: [
2077 "myapex",
2078 ],
2079 min_sdk_version: "30",
2080 }
2081 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002082
2083 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2084 apex {
2085 name: "myapex",
2086 key: "myapex.key",
2087 native_shared_libs: ["libfoo.ffi"],
2088 min_sdk_version: "29",
2089 }
2090
2091 apex_key {
2092 name: "myapex.key",
2093 public_key: "testkey.avbpubkey",
2094 private_key: "testkey.pem",
2095 }
2096
2097 rust_ffi_shared {
2098 name: "libfoo.ffi",
2099 srcs: ["foo.rs"],
2100 crate_name: "foo",
2101 apex_available: [
2102 "myapex",
2103 ],
2104 min_sdk_version: "30",
2105 }
2106 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002107
2108 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
2112 java_libs: ["libfoo"],
2113 min_sdk_version: "29",
2114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
2121
2122 java_import {
2123 name: "libfoo",
2124 jars: ["libfoo.jar"],
2125 apex_available: [
2126 "myapex",
2127 ],
2128 min_sdk_version: "30",
2129 }
2130 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002131
2132 // Skip check for modules compiling against core API surface
2133 testApex(t, `
2134 apex {
2135 name: "myapex",
2136 key: "myapex.key",
2137 java_libs: ["libfoo"],
2138 min_sdk_version: "29",
2139 }
2140
2141 apex_key {
2142 name: "myapex.key",
2143 public_key: "testkey.avbpubkey",
2144 private_key: "testkey.pem",
2145 }
2146
2147 java_library {
2148 name: "libfoo",
2149 srcs: ["Foo.java"],
2150 apex_available: [
2151 "myapex",
2152 ],
2153 // Compile against core API surface
2154 sdk_version: "core_current",
2155 min_sdk_version: "30",
2156 }
2157 `)
2158
Jooyung Han749dc692020-04-15 11:03:39 +09002159}
2160
2161func TestApexMinSdkVersion_Okay(t *testing.T) {
2162 testApex(t, `
2163 apex {
2164 name: "myapex",
2165 key: "myapex.key",
2166 native_shared_libs: ["libfoo"],
2167 java_libs: ["libbar"],
2168 min_sdk_version: "29",
2169 }
2170
2171 apex_key {
2172 name: "myapex.key",
2173 public_key: "testkey.avbpubkey",
2174 private_key: "testkey.pem",
2175 }
2176
2177 cc_library {
2178 name: "libfoo",
2179 srcs: ["mylib.cpp"],
2180 shared_libs: ["libfoo_dep"],
2181 apex_available: ["myapex"],
2182 min_sdk_version: "29",
2183 }
2184
2185 cc_library {
2186 name: "libfoo_dep",
2187 srcs: ["mylib.cpp"],
2188 apex_available: ["myapex"],
2189 min_sdk_version: "29",
2190 }
2191
2192 java_library {
2193 name: "libbar",
2194 sdk_version: "current",
2195 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002196 static_libs: [
2197 "libbar_dep",
2198 "libbar_import_dep",
2199 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002200 apex_available: ["myapex"],
2201 min_sdk_version: "29",
2202 }
2203
2204 java_library {
2205 name: "libbar_dep",
2206 sdk_version: "current",
2207 srcs: ["a.java"],
2208 apex_available: ["myapex"],
2209 min_sdk_version: "29",
2210 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002211
2212 java_import {
2213 name: "libbar_import_dep",
2214 jars: ["libbar.jar"],
2215 apex_available: ["myapex"],
2216 min_sdk_version: "29",
2217 }
Jooyung Han03b51852020-02-26 22:45:42 +09002218 `)
2219}
2220
Colin Cross8ca61c12022-10-06 21:00:14 -07002221func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2222 // Tests that an apex dependency with min_sdk_version higher than the
2223 // min_sdk_version of the apex is allowed as long as the dependency's
2224 // min_sdk_version is less than or equal to the api level that the
2225 // architecture was introduced in. In this case, arm64 didn't exist
2226 // until api level 21, so the arm64 code will never need to run on
2227 // an api level 20 device, even if other architectures of the apex
2228 // will.
2229 testApex(t, `
2230 apex {
2231 name: "myapex",
2232 key: "myapex.key",
2233 native_shared_libs: ["libfoo"],
2234 min_sdk_version: "20",
2235 }
2236
2237 apex_key {
2238 name: "myapex.key",
2239 public_key: "testkey.avbpubkey",
2240 private_key: "testkey.pem",
2241 }
2242
2243 cc_library {
2244 name: "libfoo",
2245 srcs: ["mylib.cpp"],
2246 apex_available: ["myapex"],
2247 min_sdk_version: "21",
2248 stl: "none",
2249 }
2250 `)
2251}
2252
Artur Satayev8cf899a2020-04-15 17:29:42 +01002253func TestJavaStableSdkVersion(t *testing.T) {
2254 testCases := []struct {
2255 name string
2256 expectedError string
2257 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002258 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002259 }{
2260 {
2261 name: "Non-updatable apex with non-stable dep",
2262 bp: `
2263 apex {
2264 name: "myapex",
2265 java_libs: ["myjar"],
2266 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002267 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002268 }
2269 apex_key {
2270 name: "myapex.key",
2271 public_key: "testkey.avbpubkey",
2272 private_key: "testkey.pem",
2273 }
2274 java_library {
2275 name: "myjar",
2276 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002277 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002278 apex_available: ["myapex"],
2279 }
2280 `,
2281 },
2282 {
2283 name: "Updatable apex with stable dep",
2284 bp: `
2285 apex {
2286 name: "myapex",
2287 java_libs: ["myjar"],
2288 key: "myapex.key",
2289 updatable: true,
2290 min_sdk_version: "29",
2291 }
2292 apex_key {
2293 name: "myapex.key",
2294 public_key: "testkey.avbpubkey",
2295 private_key: "testkey.pem",
2296 }
2297 java_library {
2298 name: "myjar",
2299 srcs: ["foo/bar/MyClass.java"],
2300 sdk_version: "current",
2301 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002302 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002303 }
2304 `,
2305 },
2306 {
2307 name: "Updatable apex with non-stable dep",
2308 expectedError: "cannot depend on \"myjar\"",
2309 bp: `
2310 apex {
2311 name: "myapex",
2312 java_libs: ["myjar"],
2313 key: "myapex.key",
2314 updatable: true,
2315 }
2316 apex_key {
2317 name: "myapex.key",
2318 public_key: "testkey.avbpubkey",
2319 private_key: "testkey.pem",
2320 }
2321 java_library {
2322 name: "myjar",
2323 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002324 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002325 apex_available: ["myapex"],
2326 }
2327 `,
2328 },
2329 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002330 name: "Updatable apex with non-stable legacy core platform dep",
2331 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2332 bp: `
2333 apex {
2334 name: "myapex",
2335 java_libs: ["myjar-uses-legacy"],
2336 key: "myapex.key",
2337 updatable: true,
2338 }
2339 apex_key {
2340 name: "myapex.key",
2341 public_key: "testkey.avbpubkey",
2342 private_key: "testkey.pem",
2343 }
2344 java_library {
2345 name: "myjar-uses-legacy",
2346 srcs: ["foo/bar/MyClass.java"],
2347 sdk_version: "core_platform",
2348 apex_available: ["myapex"],
2349 }
2350 `,
2351 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2352 },
2353 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002354 name: "Updatable apex with non-stable transitive dep",
2355 // This is not actually detecting that the transitive dependency is unstable, rather it is
2356 // detecting that the transitive dependency is building against a wider API surface than the
2357 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002358 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002359 bp: `
2360 apex {
2361 name: "myapex",
2362 java_libs: ["myjar"],
2363 key: "myapex.key",
2364 updatable: true,
2365 }
2366 apex_key {
2367 name: "myapex.key",
2368 public_key: "testkey.avbpubkey",
2369 private_key: "testkey.pem",
2370 }
2371 java_library {
2372 name: "myjar",
2373 srcs: ["foo/bar/MyClass.java"],
2374 sdk_version: "current",
2375 apex_available: ["myapex"],
2376 static_libs: ["transitive-jar"],
2377 }
2378 java_library {
2379 name: "transitive-jar",
2380 srcs: ["foo/bar/MyClass.java"],
2381 sdk_version: "core_platform",
2382 apex_available: ["myapex"],
2383 }
2384 `,
2385 },
2386 }
2387
2388 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002389 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2390 continue
2391 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002392 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002393 errorHandler := android.FixtureExpectsNoErrors
2394 if test.expectedError != "" {
2395 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002396 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002397 android.GroupFixturePreparers(
2398 java.PrepareForTestWithJavaDefaultModules,
2399 PrepareForTestWithApexBuildComponents,
2400 prepareForTestWithMyapex,
2401 android.OptionalFixturePreparer(test.preparer),
2402 ).
2403 ExtendWithErrorHandler(errorHandler).
2404 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002405 })
2406 }
2407}
2408
Jooyung Han749dc692020-04-15 11:03:39 +09002409func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2410 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2411 apex {
2412 name: "myapex",
2413 key: "myapex.key",
2414 native_shared_libs: ["mylib"],
2415 min_sdk_version: "29",
2416 }
2417
2418 apex_key {
2419 name: "myapex.key",
2420 public_key: "testkey.avbpubkey",
2421 private_key: "testkey.pem",
2422 }
2423
2424 cc_library {
2425 name: "mylib",
2426 srcs: ["mylib.cpp"],
2427 shared_libs: ["mylib2"],
2428 system_shared_libs: [],
2429 stl: "none",
2430 apex_available: [
2431 "myapex",
2432 ],
2433 min_sdk_version: "29",
2434 }
2435
2436 // indirect part of the apex
2437 cc_library {
2438 name: "mylib2",
2439 srcs: ["mylib.cpp"],
2440 system_shared_libs: [],
2441 stl: "none",
2442 apex_available: [
2443 "myapex",
2444 ],
2445 min_sdk_version: "30",
2446 }
2447 `)
2448}
2449
2450func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2451 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2452 apex {
2453 name: "myapex",
2454 key: "myapex.key",
2455 apps: ["AppFoo"],
2456 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002457 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002458 }
2459
2460 apex_key {
2461 name: "myapex.key",
2462 public_key: "testkey.avbpubkey",
2463 private_key: "testkey.pem",
2464 }
2465
2466 android_app {
2467 name: "AppFoo",
2468 srcs: ["foo/bar/MyClass.java"],
2469 sdk_version: "current",
2470 min_sdk_version: "29",
2471 system_modules: "none",
2472 stl: "none",
2473 static_libs: ["bar"],
2474 apex_available: [ "myapex" ],
2475 }
2476
2477 java_library {
2478 name: "bar",
2479 sdk_version: "current",
2480 srcs: ["a.java"],
2481 apex_available: [ "myapex" ],
2482 }
2483 `)
2484}
2485
2486func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002487 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002488 apex {
2489 name: "myapex",
2490 key: "myapex.key",
2491 native_shared_libs: ["mylib"],
2492 min_sdk_version: "29",
2493 }
2494
2495 apex_key {
2496 name: "myapex.key",
2497 public_key: "testkey.avbpubkey",
2498 private_key: "testkey.pem",
2499 }
2500
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002501 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002502 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2503 cc_library {
2504 name: "mylib",
2505 srcs: ["mylib.cpp"],
2506 shared_libs: ["mylib2"],
2507 system_shared_libs: [],
2508 stl: "none",
2509 apex_available: ["myapex", "otherapex"],
2510 min_sdk_version: "29",
2511 }
2512
2513 cc_library {
2514 name: "mylib2",
2515 srcs: ["mylib.cpp"],
2516 system_shared_libs: [],
2517 stl: "none",
2518 apex_available: ["otherapex"],
2519 stubs: { versions: ["29", "30"] },
2520 min_sdk_version: "30",
2521 }
2522
2523 apex {
2524 name: "otherapex",
2525 key: "myapex.key",
2526 native_shared_libs: ["mylib", "mylib2"],
2527 min_sdk_version: "30",
2528 }
2529 `)
2530 expectLink := func(from, from_variant, to, to_variant string) {
2531 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2532 libFlags := ld.Args["libFlags"]
2533 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2534 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002535 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002536 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002537}
2538
Jooyung Haned124c32021-01-26 11:43:46 +09002539func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002540 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2541 func(variables android.FixtureProductVariables) {
2542 variables.Platform_sdk_codename = proptools.StringPtr("S")
2543 variables.Platform_version_active_codenames = []string{"S"}
2544 },
2545 )
Jooyung Haned124c32021-01-26 11:43:46 +09002546 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2547 apex {
2548 name: "myapex",
2549 key: "myapex.key",
2550 native_shared_libs: ["libfoo"],
2551 min_sdk_version: "S",
2552 }
2553 apex_key {
2554 name: "myapex.key",
2555 public_key: "testkey.avbpubkey",
2556 private_key: "testkey.pem",
2557 }
2558 cc_library {
2559 name: "libfoo",
2560 shared_libs: ["libbar"],
2561 apex_available: ["myapex"],
2562 min_sdk_version: "29",
2563 }
2564 cc_library {
2565 name: "libbar",
2566 apex_available: ["myapex"],
2567 }
2568 `, withSAsActiveCodeNames)
2569}
2570
2571func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002572 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2573 variables.Platform_sdk_codename = proptools.StringPtr("S")
2574 variables.Platform_version_active_codenames = []string{"S", "T"}
2575 })
Colin Cross1c460562021-02-16 17:55:47 -08002576 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002577 apex {
2578 name: "myapex",
2579 key: "myapex.key",
2580 native_shared_libs: ["libfoo"],
2581 min_sdk_version: "S",
2582 }
2583 apex_key {
2584 name: "myapex.key",
2585 public_key: "testkey.avbpubkey",
2586 private_key: "testkey.pem",
2587 }
2588 cc_library {
2589 name: "libfoo",
2590 shared_libs: ["libbar"],
2591 apex_available: ["myapex"],
2592 min_sdk_version: "S",
2593 }
2594 cc_library {
2595 name: "libbar",
2596 stubs: {
2597 symbol_file: "libbar.map.txt",
2598 versions: ["30", "S", "T"],
2599 },
2600 }
2601 `, withSAsActiveCodeNames)
2602
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002603 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002604 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2605 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002606 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002607}
2608
Jiyong Park7c2ee712018-12-07 00:42:25 +09002609func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002610 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002611 apex {
2612 name: "myapex",
2613 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002614 native_shared_libs: ["mylib"],
2615 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002616 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002617 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002618 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002619 }
2620
2621 apex_key {
2622 name: "myapex.key",
2623 public_key: "testkey.avbpubkey",
2624 private_key: "testkey.pem",
2625 }
2626
2627 prebuilt_etc {
2628 name: "myetc",
2629 src: "myprebuilt",
2630 sub_dir: "foo/bar",
2631 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002632
2633 cc_library {
2634 name: "mylib",
2635 srcs: ["mylib.cpp"],
2636 relative_install_path: "foo/bar",
2637 system_shared_libs: [],
2638 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002639 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002640 }
2641
2642 cc_binary {
2643 name: "mybin",
2644 srcs: ["mylib.cpp"],
2645 relative_install_path: "foo/bar",
2646 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002648 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002649 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002650 `)
2651
Sundong Ahnabb64432019-10-22 13:58:29 +09002652 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002653 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002654
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002655 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002656 ensureContains(t, cmd, "/etc ")
2657 ensureContains(t, cmd, "/etc/foo ")
2658 ensureContains(t, cmd, "/etc/foo/bar ")
2659 ensureContains(t, cmd, "/lib64 ")
2660 ensureContains(t, cmd, "/lib64/foo ")
2661 ensureContains(t, cmd, "/lib64/foo/bar ")
2662 ensureContains(t, cmd, "/lib ")
2663 ensureContains(t, cmd, "/lib/foo ")
2664 ensureContains(t, cmd, "/lib/foo/bar ")
2665 ensureContains(t, cmd, "/bin ")
2666 ensureContains(t, cmd, "/bin/foo ")
2667 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002668}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002669
Jooyung Han35155c42020-02-06 17:33:20 +09002670func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002671 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002672 apex {
2673 name: "myapex",
2674 key: "myapex.key",
2675 multilib: {
2676 both: {
2677 native_shared_libs: ["mylib"],
2678 binaries: ["mybin"],
2679 },
2680 },
2681 compile_multilib: "both",
2682 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002683 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002684 }
2685
2686 apex_key {
2687 name: "myapex.key",
2688 public_key: "testkey.avbpubkey",
2689 private_key: "testkey.pem",
2690 }
2691
2692 cc_library {
2693 name: "mylib",
2694 relative_install_path: "foo/bar",
2695 system_shared_libs: [],
2696 stl: "none",
2697 apex_available: [ "myapex" ],
2698 native_bridge_supported: true,
2699 }
2700
2701 cc_binary {
2702 name: "mybin",
2703 relative_install_path: "foo/bar",
2704 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002705 stl: "none",
2706 apex_available: [ "myapex" ],
2707 native_bridge_supported: true,
2708 compile_multilib: "both", // default is "first" for binary
2709 multilib: {
2710 lib64: {
2711 suffix: "64",
2712 },
2713 },
2714 }
2715 `, withNativeBridgeEnabled)
2716 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2717 "bin/foo/bar/mybin",
2718 "bin/foo/bar/mybin64",
2719 "bin/arm/foo/bar/mybin",
2720 "bin/arm64/foo/bar/mybin64",
2721 "lib/foo/bar/mylib.so",
2722 "lib/arm/foo/bar/mylib.so",
2723 "lib64/foo/bar/mylib.so",
2724 "lib64/arm64/foo/bar/mylib.so",
2725 })
2726}
2727
Jooyung Han85d61762020-06-24 23:50:26 +09002728func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002729 result := android.GroupFixturePreparers(
2730 prepareForApexTest,
2731 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2732 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002733 apex {
2734 name: "myapex",
2735 key: "myapex.key",
2736 binaries: ["mybin"],
2737 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002738 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002739 }
2740 apex_key {
2741 name: "myapex.key",
2742 public_key: "testkey.avbpubkey",
2743 private_key: "testkey.pem",
2744 }
2745 cc_binary {
2746 name: "mybin",
2747 vendor: true,
2748 shared_libs: ["libfoo"],
2749 }
2750 cc_library {
2751 name: "libfoo",
2752 proprietary: true,
2753 }
2754 `)
2755
Colin Crossc68db4b2021-11-11 18:59:15 -08002756 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002757 "bin/mybin",
2758 "lib64/libfoo.so",
2759 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2760 "lib64/libc++.so",
2761 })
2762
Colin Crossc68db4b2021-11-11 18:59:15 -08002763 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2764 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002765 name := apexBundle.BaseModuleName()
2766 prefix := "TARGET_"
2767 var builder strings.Builder
2768 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002769 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002770 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002771 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002772
Colin Crossc68db4b2021-11-11 18:59:15 -08002773 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002774 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2775 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002776}
2777
Jooyung Hanc5a96762022-02-04 11:54:50 +09002778func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2779 testApexError(t, `Trying to include a VNDK library`, `
2780 apex {
2781 name: "myapex",
2782 key: "myapex.key",
2783 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2784 vendor: true,
2785 use_vndk_as_stable: true,
2786 updatable: false,
2787 }
2788 apex_key {
2789 name: "myapex.key",
2790 public_key: "testkey.avbpubkey",
2791 private_key: "testkey.pem",
2792 }`)
2793}
2794
Jooyung Handf78e212020-07-22 15:54:47 +09002795func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002796 // myapex myapex2
2797 // | |
2798 // mybin ------. mybin2
2799 // \ \ / |
2800 // (stable) .---\--------` |
2801 // \ / \ |
2802 // \ / \ /
2803 // libvndk libvendor
2804 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002805 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002806 apex {
2807 name: "myapex",
2808 key: "myapex.key",
2809 binaries: ["mybin"],
2810 vendor: true,
2811 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002812 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002813 }
2814 apex_key {
2815 name: "myapex.key",
2816 public_key: "testkey.avbpubkey",
2817 private_key: "testkey.pem",
2818 }
2819 cc_binary {
2820 name: "mybin",
2821 vendor: true,
2822 shared_libs: ["libvndk", "libvendor"],
2823 }
2824 cc_library {
2825 name: "libvndk",
2826 vndk: {
2827 enabled: true,
2828 },
2829 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002830 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002831 }
2832 cc_library {
2833 name: "libvendor",
2834 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002835 stl: "none",
2836 }
2837 apex {
2838 name: "myapex2",
2839 key: "myapex.key",
2840 binaries: ["mybin2"],
2841 vendor: true,
2842 use_vndk_as_stable: false,
2843 updatable: false,
2844 }
2845 cc_binary {
2846 name: "mybin2",
2847 vendor: true,
2848 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002849 }
2850 `)
2851
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002852 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002853
Jooyung Han91f92032022-02-04 12:36:33 +09002854 for _, tc := range []struct {
2855 name string
2856 apexName string
2857 moduleName string
2858 moduleVariant string
2859 libs []string
2860 contents []string
2861 requireVndkNamespace bool
2862 }{
2863 {
2864 name: "use_vndk_as_stable",
2865 apexName: "myapex",
2866 moduleName: "mybin",
2867 moduleVariant: vendorVariant + "_apex10000",
2868 libs: []string{
2869 // should link with vendor variants of VNDK libs(libvndk/libc++)
2870 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2871 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2872 // unstable Vendor libs as APEX variant
2873 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2874 },
2875 contents: []string{
2876 "bin/mybin",
2877 "lib64/libvendor.so",
2878 // VNDK libs (libvndk/libc++) are not included
2879 },
2880 requireVndkNamespace: true,
2881 },
2882 {
2883 name: "!use_vndk_as_stable",
2884 apexName: "myapex2",
2885 moduleName: "mybin2",
2886 moduleVariant: vendorVariant + "_myapex2",
2887 libs: []string{
2888 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2889 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2890 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2891 // unstable vendor libs have "merged" APEX variants
2892 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2893 },
2894 contents: []string{
2895 "bin/mybin2",
2896 "lib64/libvendor.so",
2897 // VNDK libs are included as well
2898 "lib64/libvndk.so",
2899 "lib64/libc++.so",
2900 },
2901 requireVndkNamespace: false,
2902 },
2903 } {
2904 t.Run(tc.name, func(t *testing.T) {
2905 // Check linked libs
2906 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2907 libs := names(ldRule.Args["libFlags"])
2908 for _, lib := range tc.libs {
2909 ensureListContains(t, libs, lib)
2910 }
2911 // Check apex contents
2912 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002913
Jooyung Han91f92032022-02-04 12:36:33 +09002914 // Check "requireNativeLibs"
2915 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2916 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2917 if tc.requireVndkNamespace {
2918 ensureListContains(t, requireNativeLibs, ":vndk")
2919 } else {
2920 ensureListNotContains(t, requireNativeLibs, ":vndk")
2921 }
2922 })
2923 }
Jooyung Handf78e212020-07-22 15:54:47 +09002924}
2925
Justin Yun13decfb2021-03-08 19:25:55 +09002926func TestProductVariant(t *testing.T) {
2927 ctx := testApex(t, `
2928 apex {
2929 name: "myapex",
2930 key: "myapex.key",
2931 updatable: false,
2932 product_specific: true,
2933 binaries: ["foo"],
2934 }
2935
2936 apex_key {
2937 name: "myapex.key",
2938 public_key: "testkey.avbpubkey",
2939 private_key: "testkey.pem",
2940 }
2941
2942 cc_binary {
2943 name: "foo",
2944 product_available: true,
2945 apex_available: ["myapex"],
2946 srcs: ["foo.cpp"],
2947 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002948 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2949 variables.ProductVndkVersion = proptools.StringPtr("current")
2950 }),
2951 )
Justin Yun13decfb2021-03-08 19:25:55 +09002952
2953 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002954 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002955 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2956 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2957 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2958 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2959}
2960
Jooyung Han8e5685d2020-09-21 11:02:57 +09002961func TestApex_withPrebuiltFirmware(t *testing.T) {
2962 testCases := []struct {
2963 name string
2964 additionalProp string
2965 }{
2966 {"system apex with prebuilt_firmware", ""},
2967 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2968 }
2969 for _, tc := range testCases {
2970 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002971 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002972 apex {
2973 name: "myapex",
2974 key: "myapex.key",
2975 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002976 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002977 `+tc.additionalProp+`
2978 }
2979 apex_key {
2980 name: "myapex.key",
2981 public_key: "testkey.avbpubkey",
2982 private_key: "testkey.pem",
2983 }
2984 prebuilt_firmware {
2985 name: "myfirmware",
2986 src: "myfirmware.bin",
2987 filename_from_src: true,
2988 `+tc.additionalProp+`
2989 }
2990 `)
2991 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2992 "etc/firmware/myfirmware.bin",
2993 })
2994 })
2995 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002996}
2997
Jooyung Hanefb184e2020-06-25 17:14:25 +09002998func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002999 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003000 apex {
3001 name: "myapex",
3002 key: "myapex.key",
3003 vendor: true,
3004 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003005 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003006 }
3007
3008 apex_key {
3009 name: "myapex.key",
3010 public_key: "testkey.avbpubkey",
3011 private_key: "testkey.pem",
3012 }
3013
3014 cc_library {
3015 name: "mylib",
3016 vendor_available: true,
3017 }
3018 `)
3019
3020 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003021 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003022 name := apexBundle.BaseModuleName()
3023 prefix := "TARGET_"
3024 var builder strings.Builder
3025 data.Custom(&builder, name, prefix, "", data)
3026 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00003027 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 +09003028}
3029
Jooyung Han2ed99d02020-06-24 23:26:26 +09003030func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003031 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003032 apex {
3033 name: "myapex",
3034 key: "myapex.key",
3035 vintf_fragments: ["fragment.xml"],
3036 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003037 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003038 }
3039 apex_key {
3040 name: "myapex.key",
3041 public_key: "testkey.avbpubkey",
3042 private_key: "testkey.pem",
3043 }
3044 cc_binary {
3045 name: "mybin",
3046 }
3047 `)
3048
3049 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003050 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003051 name := apexBundle.BaseModuleName()
3052 prefix := "TARGET_"
3053 var builder strings.Builder
3054 data.Custom(&builder, name, prefix, "", data)
3055 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003056 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003057 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003058}
3059
Jiyong Park16e91a02018-12-20 18:18:08 +09003060func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003061 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003062 apex {
3063 name: "myapex",
3064 key: "myapex.key",
3065 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003066 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003067 }
3068
3069 apex_key {
3070 name: "myapex.key",
3071 public_key: "testkey.avbpubkey",
3072 private_key: "testkey.pem",
3073 }
3074
3075 cc_library {
3076 name: "mylib",
3077 srcs: ["mylib.cpp"],
3078 system_shared_libs: [],
3079 stl: "none",
3080 stubs: {
3081 versions: ["1", "2", "3"],
3082 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003083 apex_available: [
3084 "//apex_available:platform",
3085 "myapex",
3086 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003087 }
3088
3089 cc_binary {
3090 name: "not_in_apex",
3091 srcs: ["mylib.cpp"],
3092 static_libs: ["mylib"],
3093 static_executable: true,
3094 system_shared_libs: [],
3095 stl: "none",
3096 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003097 `)
3098
Colin Cross7113d202019-11-20 16:39:12 -08003099 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003100
3101 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003102 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003103}
Jiyong Park9335a262018-12-24 11:31:58 +09003104
3105func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003106 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003107 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003108 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003109 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003110 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003111 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003112 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003113 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003114 }
3115
3116 cc_library {
3117 name: "mylib",
3118 srcs: ["mylib.cpp"],
3119 system_shared_libs: [],
3120 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003121 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003122 }
3123
3124 apex_key {
3125 name: "myapex.key",
3126 public_key: "testkey.avbpubkey",
3127 private_key: "testkey.pem",
3128 }
3129
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003130 android_app_certificate {
3131 name: "myapex.certificate",
3132 certificate: "testkey",
3133 }
3134
3135 android_app_certificate {
3136 name: "myapex.certificate.override",
3137 certificate: "testkey.override",
3138 }
3139
Jiyong Park9335a262018-12-24 11:31:58 +09003140 `)
3141
3142 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003143 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003144
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003145 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3146 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003147 "vendor/foo/devkeys/testkey.avbpubkey")
3148 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003149 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3150 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003151 "vendor/foo/devkeys/testkey.pem")
3152 }
3153
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003154 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003155 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003156 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003157 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003158 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003159 }
3160}
Jiyong Park58e364a2019-01-19 19:24:06 +09003161
Jooyung Hanf121a652019-12-17 14:30:11 +09003162func TestCertificate(t *testing.T) {
3163 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003164 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003165 apex {
3166 name: "myapex",
3167 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003168 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003169 }
3170 apex_key {
3171 name: "myapex.key",
3172 public_key: "testkey.avbpubkey",
3173 private_key: "testkey.pem",
3174 }`)
3175 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3176 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3177 if actual := rule.Args["certificates"]; actual != expected {
3178 t.Errorf("certificates should be %q, not %q", expected, actual)
3179 }
3180 })
3181 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003182 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003183 apex {
3184 name: "myapex_keytest",
3185 key: "myapex.key",
3186 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003187 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003188 }
3189 apex_key {
3190 name: "myapex.key",
3191 public_key: "testkey.avbpubkey",
3192 private_key: "testkey.pem",
3193 }
3194 android_app_certificate {
3195 name: "myapex.certificate.override",
3196 certificate: "testkey.override",
3197 }`)
3198 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3199 expected := "testkey.override.x509.pem testkey.override.pk8"
3200 if actual := rule.Args["certificates"]; actual != expected {
3201 t.Errorf("certificates should be %q, not %q", expected, actual)
3202 }
3203 })
3204 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003205 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003206 apex {
3207 name: "myapex",
3208 key: "myapex.key",
3209 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003210 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003211 }
3212 apex_key {
3213 name: "myapex.key",
3214 public_key: "testkey.avbpubkey",
3215 private_key: "testkey.pem",
3216 }
3217 android_app_certificate {
3218 name: "myapex.certificate",
3219 certificate: "testkey",
3220 }`)
3221 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3222 expected := "testkey.x509.pem testkey.pk8"
3223 if actual := rule.Args["certificates"]; actual != expected {
3224 t.Errorf("certificates should be %q, not %q", expected, actual)
3225 }
3226 })
3227 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003228 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003229 apex {
3230 name: "myapex_keytest",
3231 key: "myapex.key",
3232 file_contexts: ":myapex-file_contexts",
3233 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003234 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003235 }
3236 apex_key {
3237 name: "myapex.key",
3238 public_key: "testkey.avbpubkey",
3239 private_key: "testkey.pem",
3240 }
3241 android_app_certificate {
3242 name: "myapex.certificate.override",
3243 certificate: "testkey.override",
3244 }`)
3245 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3246 expected := "testkey.override.x509.pem testkey.override.pk8"
3247 if actual := rule.Args["certificates"]; actual != expected {
3248 t.Errorf("certificates should be %q, not %q", expected, actual)
3249 }
3250 })
3251 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003252 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003253 apex {
3254 name: "myapex",
3255 key: "myapex.key",
3256 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003257 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003258 }
3259 apex_key {
3260 name: "myapex.key",
3261 public_key: "testkey.avbpubkey",
3262 private_key: "testkey.pem",
3263 }`)
3264 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3265 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3266 if actual := rule.Args["certificates"]; actual != expected {
3267 t.Errorf("certificates should be %q, not %q", expected, actual)
3268 }
3269 })
3270 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003271 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003272 apex {
3273 name: "myapex_keytest",
3274 key: "myapex.key",
3275 file_contexts: ":myapex-file_contexts",
3276 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003277 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003278 }
3279 apex_key {
3280 name: "myapex.key",
3281 public_key: "testkey.avbpubkey",
3282 private_key: "testkey.pem",
3283 }
3284 android_app_certificate {
3285 name: "myapex.certificate.override",
3286 certificate: "testkey.override",
3287 }`)
3288 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3289 expected := "testkey.override.x509.pem testkey.override.pk8"
3290 if actual := rule.Args["certificates"]; actual != expected {
3291 t.Errorf("certificates should be %q, not %q", expected, actual)
3292 }
3293 })
3294}
3295
Jiyong Park58e364a2019-01-19 19:24:06 +09003296func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003297 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003298 apex {
3299 name: "myapex",
3300 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003301 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003302 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003303 }
3304
3305 apex {
3306 name: "otherapex",
3307 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003308 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003309 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003310 }
3311
3312 apex_key {
3313 name: "myapex.key",
3314 public_key: "testkey.avbpubkey",
3315 private_key: "testkey.pem",
3316 }
3317
3318 cc_library {
3319 name: "mylib",
3320 srcs: ["mylib.cpp"],
3321 system_shared_libs: [],
3322 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003323 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003324 "myapex",
3325 "otherapex",
3326 ],
Jooyung Han24282772020-03-21 23:20:55 +09003327 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003328 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003329 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003330 cc_library {
3331 name: "mylib2",
3332 srcs: ["mylib.cpp"],
3333 system_shared_libs: [],
3334 stl: "none",
3335 apex_available: [
3336 "myapex",
3337 "otherapex",
3338 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003339 static_libs: ["mylib3"],
3340 recovery_available: true,
3341 min_sdk_version: "29",
3342 }
3343 cc_library {
3344 name: "mylib3",
3345 srcs: ["mylib.cpp"],
3346 system_shared_libs: [],
3347 stl: "none",
3348 apex_available: [
3349 "myapex",
3350 "otherapex",
3351 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003352 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003353 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003354 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003355 `)
3356
Jooyung Hanc87a0592020-03-02 17:44:33 +09003357 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003358 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003359 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003360
Vinh Tranf9754732023-01-19 22:41:46 -05003361 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003362 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003363 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003364
Vinh Tranf9754732023-01-19 22:41:46 -05003365 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003366 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003367 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003368
Colin Crossaede88c2020-08-11 12:17:01 -07003369 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3370 // each variant defines additional macros to distinguish which apex variant it is built for
3371
3372 // non-APEX variant does not have __ANDROID_APEX__ defined
3373 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3374 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3375
Vinh Tranf9754732023-01-19 22:41:46 -05003376 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003377 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3378 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003379
Jooyung Hanc87a0592020-03-02 17:44:33 +09003380 // non-APEX variant does not have __ANDROID_APEX__ defined
3381 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3382 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3383
Vinh Tranf9754732023-01-19 22:41:46 -05003384 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003385 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003386 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003387}
Jiyong Park7e636d02019-01-28 16:16:54 +09003388
3389func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003390 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003391 apex {
3392 name: "myapex",
3393 key: "myapex.key",
3394 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003395 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003396 }
3397
3398 apex_key {
3399 name: "myapex.key",
3400 public_key: "testkey.avbpubkey",
3401 private_key: "testkey.pem",
3402 }
3403
3404 cc_library_headers {
3405 name: "mylib_headers",
3406 export_include_dirs: ["my_include"],
3407 system_shared_libs: [],
3408 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003409 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003410 }
3411
3412 cc_library {
3413 name: "mylib",
3414 srcs: ["mylib.cpp"],
3415 system_shared_libs: [],
3416 stl: "none",
3417 header_libs: ["mylib_headers"],
3418 export_header_lib_headers: ["mylib_headers"],
3419 stubs: {
3420 versions: ["1", "2", "3"],
3421 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003422 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003423 }
3424
3425 cc_library {
3426 name: "otherlib",
3427 srcs: ["mylib.cpp"],
3428 system_shared_libs: [],
3429 stl: "none",
3430 shared_libs: ["mylib"],
3431 }
3432 `)
3433
Colin Cross7113d202019-11-20 16:39:12 -08003434 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003435
3436 // Ensure that the include path of the header lib is exported to 'otherlib'
3437 ensureContains(t, cFlags, "-Imy_include")
3438}
Alex Light9670d332019-01-29 18:07:33 -08003439
Jiyong Park7cd10e32020-01-14 09:22:18 +09003440type fileInApex struct {
3441 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003442 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003443 isLink bool
3444}
3445
Jooyung Han1724d582022-12-21 10:17:44 +09003446func (f fileInApex) String() string {
3447 return f.src + ":" + f.path
3448}
3449
3450func (f fileInApex) match(expectation string) bool {
3451 parts := strings.Split(expectation, ":")
3452 if len(parts) == 1 {
3453 match, _ := path.Match(parts[0], f.path)
3454 return match
3455 }
3456 if len(parts) == 2 {
3457 matchSrc, _ := path.Match(parts[0], f.src)
3458 matchDst, _ := path.Match(parts[1], f.path)
3459 return matchSrc && matchDst
3460 }
3461 panic("invalid expected file specification: " + expectation)
3462}
3463
Jooyung Hana57af4a2020-01-23 05:36:59 +00003464func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003465 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003466 module := ctx.ModuleForTests(moduleName, variant)
3467 apexRule := module.MaybeRule("apexRule")
3468 apexDir := "/image.apex/"
3469 if apexRule.Rule == nil {
3470 apexRule = module.Rule("zipApexRule")
3471 apexDir = "/image.zipapex/"
3472 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003473 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003474 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003475 for _, cmd := range strings.Split(copyCmds, "&&") {
3476 cmd = strings.TrimSpace(cmd)
3477 if cmd == "" {
3478 continue
3479 }
3480 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003481 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003482 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003483 switch terms[0] {
3484 case "mkdir":
3485 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003486 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003487 t.Fatal("copyCmds contains invalid cp command", cmd)
3488 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003489 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003490 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003491 isLink = false
3492 case "ln":
3493 if len(terms) != 3 && len(terms) != 4 {
3494 // ln LINK TARGET or ln -s LINK TARGET
3495 t.Fatal("copyCmds contains invalid ln command", cmd)
3496 }
3497 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003498 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003499 isLink = true
3500 default:
3501 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3502 }
3503 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003504 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003505 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003506 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003507 }
Jooyung Han1724d582022-12-21 10:17:44 +09003508 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003509 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003510 }
3511 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003512 return ret
3513}
3514
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003515func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003516 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003517 var failed bool
3518 var surplus []string
3519 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003520 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003521 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003522 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003523 if file.match(expected) {
3524 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003525 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003526 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003527 }
3528 }
Jooyung Han1724d582022-12-21 10:17:44 +09003529 if !matchFound {
3530 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003531 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003532 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003533
Jooyung Han31c470b2019-10-18 16:26:59 +09003534 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003535 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003536 t.Log("surplus files", surplus)
3537 failed = true
3538 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003539
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003540 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003541 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003542 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003543 if !filesMatched[expected] {
3544 missing = append(missing, expected)
3545 }
3546 }
3547 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003548 t.Log("missing files", missing)
3549 failed = true
3550 }
3551 if failed {
3552 t.Fail()
3553 }
3554}
3555
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003556func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3557 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3558}
3559
3560func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3561 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3562 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3563 if deapexer.Output != nil {
3564 outputs = append(outputs, deapexer.Output.String())
3565 }
3566 for _, output := range deapexer.ImplicitOutputs {
3567 outputs = append(outputs, output.String())
3568 }
3569 actualFiles := make([]fileInApex, 0, len(outputs))
3570 for _, output := range outputs {
3571 dir := "/deapexer/"
3572 pos := strings.LastIndex(output, dir)
3573 if pos == -1 {
3574 t.Fatal("Unknown deapexer output ", output)
3575 }
3576 path := output[pos+len(dir):]
3577 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3578 }
3579 assertFileListEquals(t, files, actualFiles)
3580}
3581
Jooyung Han344d5432019-08-23 11:17:39 +09003582func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003583 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003584 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003585 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003586 "etc/llndk.libraries.29.txt",
3587 "etc/vndkcore.libraries.29.txt",
3588 "etc/vndksp.libraries.29.txt",
3589 "etc/vndkprivate.libraries.29.txt",
3590 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003591 }
3592 testCases := []struct {
3593 vndkVersion string
3594 expectedFiles []string
3595 }{
3596 {
3597 vndkVersion: "current",
3598 expectedFiles: append(commonFiles,
3599 "lib/libvndk.so",
3600 "lib/libvndksp.so",
3601 "lib64/libvndk.so",
3602 "lib64/libvndksp.so"),
3603 },
3604 {
3605 vndkVersion: "",
3606 expectedFiles: append(commonFiles,
3607 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3608 "lib/libvndksp.so",
3609 "lib64/libvndksp.so"),
3610 },
3611 }
3612 for _, tc := range testCases {
3613 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3614 ctx := testApex(t, `
3615 apex_vndk {
3616 name: "com.android.vndk.current",
3617 key: "com.android.vndk.current.key",
3618 updatable: false,
3619 }
3620
3621 apex_key {
3622 name: "com.android.vndk.current.key",
3623 public_key: "testkey.avbpubkey",
3624 private_key: "testkey.pem",
3625 }
3626
3627 cc_library {
3628 name: "libvndk",
3629 srcs: ["mylib.cpp"],
3630 vendor_available: true,
3631 product_available: true,
3632 vndk: {
3633 enabled: true,
3634 },
3635 system_shared_libs: [],
3636 stl: "none",
3637 apex_available: [ "com.android.vndk.current" ],
3638 }
3639
3640 cc_library {
3641 name: "libvndksp",
3642 srcs: ["mylib.cpp"],
3643 vendor_available: true,
3644 product_available: true,
3645 vndk: {
3646 enabled: true,
3647 support_system_process: true,
3648 },
3649 system_shared_libs: [],
3650 stl: "none",
3651 apex_available: [ "com.android.vndk.current" ],
3652 }
3653
3654 // VNDK-Ext should not cause any problems
3655
3656 cc_library {
3657 name: "libvndk.ext",
3658 srcs: ["mylib2.cpp"],
3659 vendor: true,
3660 vndk: {
3661 enabled: true,
3662 extends: "libvndk",
3663 },
3664 system_shared_libs: [],
3665 stl: "none",
3666 }
3667
3668 cc_library {
3669 name: "libvndksp.ext",
3670 srcs: ["mylib2.cpp"],
3671 vendor: true,
3672 vndk: {
3673 enabled: true,
3674 support_system_process: true,
3675 extends: "libvndksp",
3676 },
3677 system_shared_libs: [],
3678 stl: "none",
3679 }
3680 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3681 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3682 }))
3683 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3684 })
3685 }
Jooyung Han344d5432019-08-23 11:17:39 +09003686}
3687
3688func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003689 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003690 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003691 name: "com.android.vndk.current",
3692 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003693 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003694 }
3695
3696 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003697 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003698 public_key: "testkey.avbpubkey",
3699 private_key: "testkey.pem",
3700 }
3701
3702 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003703 name: "libvndk",
3704 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003705 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003706 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003707 vndk: {
3708 enabled: true,
3709 },
3710 system_shared_libs: [],
3711 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003712 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003713 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003714
3715 cc_prebuilt_library_shared {
3716 name: "libvndk.arm",
3717 srcs: ["libvndk.arm.so"],
3718 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003719 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003720 vndk: {
3721 enabled: true,
3722 },
3723 enabled: false,
3724 arch: {
3725 arm: {
3726 enabled: true,
3727 },
3728 },
3729 system_shared_libs: [],
3730 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003731 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003732 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003733 `+vndkLibrariesTxtFiles("current"),
3734 withFiles(map[string][]byte{
3735 "libvndk.so": nil,
3736 "libvndk.arm.so": nil,
3737 }))
Colin Cross2807f002021-03-02 10:15:29 -08003738 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003739 "lib/libvndk.so",
3740 "lib/libvndk.arm.so",
3741 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003742 "lib/libc++.so",
3743 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003744 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003745 })
Jooyung Han344d5432019-08-23 11:17:39 +09003746}
3747
Jooyung Han39edb6c2019-11-06 16:53:07 +09003748func vndkLibrariesTxtFiles(vers ...string) (result string) {
3749 for _, v := range vers {
3750 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003751 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003752 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003753 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003754 name: "` + txt + `.libraries.txt",
3755 }
3756 `
3757 }
3758 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003759 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003760 result += `
3761 prebuilt_etc {
3762 name: "` + txt + `.libraries.` + v + `.txt",
3763 src: "dummy.txt",
3764 }
3765 `
3766 }
3767 }
3768 }
3769 return
3770}
3771
Jooyung Han344d5432019-08-23 11:17:39 +09003772func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003773 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003774 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003775 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003776 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003777 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003778 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003779 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003780 }
3781
3782 apex_key {
3783 name: "myapex.key",
3784 public_key: "testkey.avbpubkey",
3785 private_key: "testkey.pem",
3786 }
3787
Jooyung Han31c470b2019-10-18 16:26:59 +09003788 vndk_prebuilt_shared {
3789 name: "libvndk27",
3790 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003791 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003792 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003793 vndk: {
3794 enabled: true,
3795 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003796 target_arch: "arm64",
3797 arch: {
3798 arm: {
3799 srcs: ["libvndk27_arm.so"],
3800 },
3801 arm64: {
3802 srcs: ["libvndk27_arm64.so"],
3803 },
3804 },
Colin Cross2807f002021-03-02 10:15:29 -08003805 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003806 }
3807
3808 vndk_prebuilt_shared {
3809 name: "libvndk27",
3810 version: "27",
3811 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003812 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003813 vndk: {
3814 enabled: true,
3815 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003816 target_arch: "x86_64",
3817 arch: {
3818 x86: {
3819 srcs: ["libvndk27_x86.so"],
3820 },
3821 x86_64: {
3822 srcs: ["libvndk27_x86_64.so"],
3823 },
3824 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003825 }
3826 `+vndkLibrariesTxtFiles("27"),
3827 withFiles(map[string][]byte{
3828 "libvndk27_arm.so": nil,
3829 "libvndk27_arm64.so": nil,
3830 "libvndk27_x86.so": nil,
3831 "libvndk27_x86_64.so": nil,
3832 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003833
Colin Cross2807f002021-03-02 10:15:29 -08003834 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003835 "lib/libvndk27_arm.so",
3836 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003837 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003838 })
Jooyung Han344d5432019-08-23 11:17:39 +09003839}
3840
Jooyung Han90eee022019-10-01 20:02:42 +09003841func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003842 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003843 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003844 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003845 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003846 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003847 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003848 }
3849 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003850 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003851 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003852 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003853 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003854 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003855 }
3856 apex_key {
3857 name: "myapex.key",
3858 public_key: "testkey.avbpubkey",
3859 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003860 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003861
3862 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003863 module := ctx.ModuleForTests(moduleName, "android_common_image")
3864 apexManifestRule := module.Rule("apexManifestRule")
3865 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003866 }
3867
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003868 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003869 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003870}
3871
Jooyung Han344d5432019-08-23 11:17:39 +09003872func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003873 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003874 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003875 name: "com.android.vndk.current",
3876 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003877 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003878 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003879 }
3880
3881 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003882 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003883 public_key: "testkey.avbpubkey",
3884 private_key: "testkey.pem",
3885 }
3886
3887 cc_library {
3888 name: "libvndk",
3889 srcs: ["mylib.cpp"],
3890 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003891 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003892 native_bridge_supported: true,
3893 host_supported: true,
3894 vndk: {
3895 enabled: true,
3896 },
3897 system_shared_libs: [],
3898 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003899 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003900 }
Colin Cross2807f002021-03-02 10:15:29 -08003901 `+vndkLibrariesTxtFiles("current"),
3902 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003903
Colin Cross2807f002021-03-02 10:15:29 -08003904 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003905 "lib/libvndk.so",
3906 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003907 "lib/libc++.so",
3908 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003909 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003910 })
Jooyung Han344d5432019-08-23 11:17:39 +09003911}
3912
3913func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003914 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003915 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003916 name: "com.android.vndk.current",
3917 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003918 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003919 native_bridge_supported: true,
3920 }
3921
3922 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003923 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003924 public_key: "testkey.avbpubkey",
3925 private_key: "testkey.pem",
3926 }
3927
3928 cc_library {
3929 name: "libvndk",
3930 srcs: ["mylib.cpp"],
3931 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003932 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003933 native_bridge_supported: true,
3934 host_supported: true,
3935 vndk: {
3936 enabled: true,
3937 },
3938 system_shared_libs: [],
3939 stl: "none",
3940 }
3941 `)
3942}
3943
Jooyung Han31c470b2019-10-18 16:26:59 +09003944func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003945 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003946 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003947 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003948 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003949 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003950 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003951 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003952 }
3953
3954 apex_key {
3955 name: "myapex.key",
3956 public_key: "testkey.avbpubkey",
3957 private_key: "testkey.pem",
3958 }
3959
3960 vndk_prebuilt_shared {
3961 name: "libvndk27",
3962 version: "27",
3963 target_arch: "arm",
3964 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003965 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003966 vndk: {
3967 enabled: true,
3968 },
3969 arch: {
3970 arm: {
3971 srcs: ["libvndk27.so"],
3972 }
3973 },
3974 }
3975
3976 vndk_prebuilt_shared {
3977 name: "libvndk27",
3978 version: "27",
3979 target_arch: "arm",
3980 binder32bit: true,
3981 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003982 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003983 vndk: {
3984 enabled: true,
3985 },
3986 arch: {
3987 arm: {
3988 srcs: ["libvndk27binder32.so"],
3989 }
3990 },
Colin Cross2807f002021-03-02 10:15:29 -08003991 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003992 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003993 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003994 withFiles(map[string][]byte{
3995 "libvndk27.so": nil,
3996 "libvndk27binder32.so": nil,
3997 }),
3998 withBinder32bit,
3999 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004000 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004001 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4002 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 },
4004 }),
4005 )
4006
Colin Cross2807f002021-03-02 10:15:29 -08004007 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004008 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004009 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004010 })
4011}
4012
Jooyung Han45a96772020-06-15 14:59:42 +09004013func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004014 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004015 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004016 name: "com.android.vndk.current",
4017 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004018 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004019 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004020 }
4021
4022 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004023 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004024 public_key: "testkey.avbpubkey",
4025 private_key: "testkey.pem",
4026 }
4027
4028 cc_library {
4029 name: "libz",
4030 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004031 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004032 vndk: {
4033 enabled: true,
4034 },
4035 stubs: {
4036 symbol_file: "libz.map.txt",
4037 versions: ["30"],
4038 }
4039 }
4040 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4041 "libz.map.txt": nil,
4042 }))
4043
Colin Cross2807f002021-03-02 10:15:29 -08004044 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004045 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4046 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004047 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4048 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4049 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4050 "*/*",
4051 })
Jooyung Han45a96772020-06-15 14:59:42 +09004052}
4053
Jooyung Hane1633032019-08-01 17:41:43 +09004054func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004055 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004056 apex {
4057 name: "myapex_nodep",
4058 key: "myapex.key",
4059 native_shared_libs: ["lib_nodep"],
4060 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004061 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004062 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004063 }
4064
4065 apex {
4066 name: "myapex_dep",
4067 key: "myapex.key",
4068 native_shared_libs: ["lib_dep"],
4069 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004070 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004071 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004072 }
4073
4074 apex {
4075 name: "myapex_provider",
4076 key: "myapex.key",
4077 native_shared_libs: ["libfoo"],
4078 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004079 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004080 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004081 }
4082
4083 apex {
4084 name: "myapex_selfcontained",
4085 key: "myapex.key",
4086 native_shared_libs: ["lib_dep", "libfoo"],
4087 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004088 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004089 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004090 }
4091
4092 apex_key {
4093 name: "myapex.key",
4094 public_key: "testkey.avbpubkey",
4095 private_key: "testkey.pem",
4096 }
4097
4098 cc_library {
4099 name: "lib_nodep",
4100 srcs: ["mylib.cpp"],
4101 system_shared_libs: [],
4102 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004103 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004104 }
4105
4106 cc_library {
4107 name: "lib_dep",
4108 srcs: ["mylib.cpp"],
4109 shared_libs: ["libfoo"],
4110 system_shared_libs: [],
4111 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004112 apex_available: [
4113 "myapex_dep",
4114 "myapex_provider",
4115 "myapex_selfcontained",
4116 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004117 }
4118
4119 cc_library {
4120 name: "libfoo",
4121 srcs: ["mytest.cpp"],
4122 stubs: {
4123 versions: ["1"],
4124 },
4125 system_shared_libs: [],
4126 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004127 apex_available: [
4128 "myapex_provider",
4129 "myapex_selfcontained",
4130 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004131 }
4132 `)
4133
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004134 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004135 var provideNativeLibs, requireNativeLibs []string
4136
Sundong Ahnabb64432019-10-22 13:58:29 +09004137 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004138 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4139 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004140 ensureListEmpty(t, provideNativeLibs)
4141 ensureListEmpty(t, requireNativeLibs)
4142
Sundong Ahnabb64432019-10-22 13:58:29 +09004143 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004144 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4145 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004146 ensureListEmpty(t, provideNativeLibs)
4147 ensureListContains(t, requireNativeLibs, "libfoo.so")
4148
Sundong Ahnabb64432019-10-22 13:58:29 +09004149 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004150 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4151 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004152 ensureListContains(t, provideNativeLibs, "libfoo.so")
4153 ensureListEmpty(t, requireNativeLibs)
4154
Sundong Ahnabb64432019-10-22 13:58:29 +09004155 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004156 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4157 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004158 ensureListContains(t, provideNativeLibs, "libfoo.so")
4159 ensureListEmpty(t, requireNativeLibs)
4160}
4161
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004162func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4163 ctx := testApex(t, `
4164 apex {
4165 name: "myapex",
4166 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004167 native_shared_libs: ["mylib"],
4168 updatable: false,
4169 }
4170
4171 apex_key {
4172 name: "myapex.key",
4173 public_key: "testkey.avbpubkey",
4174 private_key: "testkey.pem",
4175 }
4176
4177 cc_library {
4178 name: "mylib",
4179 srcs: ["mylib.cpp"],
4180 system_shared_libs: [],
4181 stl: "none",
4182 apex_available: [
4183 "//apex_available:platform",
4184 "myapex",
4185 ],
4186 }
4187 `, android.FixtureMergeEnv(map[string]string{
4188 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4189 }))
4190
Jooyung Han63dff462023-02-09 00:11:27 +00004191 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004192 apexManifestRule := module.Rule("apexManifestRule")
4193 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4194}
4195
Vinh Tran8f5310f2022-10-07 18:16:47 -04004196func TestCompileMultilibProp(t *testing.T) {
4197 testCases := []struct {
4198 compileMultiLibProp string
4199 containedLibs []string
4200 notContainedLibs []string
4201 }{
4202 {
4203 containedLibs: []string{
4204 "image.apex/lib64/mylib.so",
4205 "image.apex/lib/mylib.so",
4206 },
4207 compileMultiLibProp: `compile_multilib: "both",`,
4208 },
4209 {
4210 containedLibs: []string{"image.apex/lib64/mylib.so"},
4211 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4212 compileMultiLibProp: `compile_multilib: "first",`,
4213 },
4214 {
4215 containedLibs: []string{"image.apex/lib64/mylib.so"},
4216 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4217 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4218 },
4219 {
4220 containedLibs: []string{"image.apex/lib64/mylib.so"},
4221 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4222 compileMultiLibProp: `compile_multilib: "64",`,
4223 },
4224 {
4225 containedLibs: []string{"image.apex/lib/mylib.so"},
4226 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4227 compileMultiLibProp: `compile_multilib: "32",`,
4228 },
4229 }
4230 for _, testCase := range testCases {
4231 ctx := testApex(t, fmt.Sprintf(`
4232 apex {
4233 name: "myapex",
4234 key: "myapex.key",
4235 %s
4236 native_shared_libs: ["mylib"],
4237 updatable: false,
4238 }
4239 apex_key {
4240 name: "myapex.key",
4241 public_key: "testkey.avbpubkey",
4242 private_key: "testkey.pem",
4243 }
4244 cc_library {
4245 name: "mylib",
4246 srcs: ["mylib.cpp"],
4247 apex_available: [
4248 "//apex_available:platform",
4249 "myapex",
4250 ],
4251 }
4252 `, testCase.compileMultiLibProp),
4253 )
4254 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4255 apexRule := module.Rule("apexRule")
4256 copyCmds := apexRule.Args["copy_commands"]
4257 for _, containedLib := range testCase.containedLibs {
4258 ensureContains(t, copyCmds, containedLib)
4259 }
4260 for _, notContainedLib := range testCase.notContainedLibs {
4261 ensureNotContains(t, copyCmds, notContainedLib)
4262 }
4263 }
4264}
4265
Alex Light0851b882019-02-07 13:20:53 -08004266func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004267 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004268 apex {
4269 name: "myapex",
4270 key: "myapex.key",
4271 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004272 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004273 }
4274
4275 apex_key {
4276 name: "myapex.key",
4277 public_key: "testkey.avbpubkey",
4278 private_key: "testkey.pem",
4279 }
4280
4281 cc_library {
4282 name: "mylib_common",
4283 srcs: ["mylib.cpp"],
4284 system_shared_libs: [],
4285 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004286 apex_available: [
4287 "//apex_available:platform",
4288 "myapex",
4289 ],
Alex Light0851b882019-02-07 13:20:53 -08004290 }
4291 `)
4292
Sundong Ahnabb64432019-10-22 13:58:29 +09004293 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004294 apexRule := module.Rule("apexRule")
4295 copyCmds := apexRule.Args["copy_commands"]
4296
4297 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4298 t.Log("Apex was a test apex!")
4299 t.Fail()
4300 }
4301 // Ensure that main rule creates an output
4302 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4303
4304 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004305 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004306
4307 // Ensure that both direct and indirect deps are copied into apex
4308 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4309
Colin Cross7113d202019-11-20 16:39:12 -08004310 // Ensure that the platform variant ends with _shared
4311 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004312
Colin Cross56a83212020-09-15 18:30:11 -07004313 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004314 t.Log("Found mylib_common not in any apex!")
4315 t.Fail()
4316 }
4317}
4318
4319func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004320 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004321 apex_test {
4322 name: "myapex",
4323 key: "myapex.key",
4324 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004325 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004326 }
4327
4328 apex_key {
4329 name: "myapex.key",
4330 public_key: "testkey.avbpubkey",
4331 private_key: "testkey.pem",
4332 }
4333
4334 cc_library {
4335 name: "mylib_common_test",
4336 srcs: ["mylib.cpp"],
4337 system_shared_libs: [],
4338 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004339 // TODO: remove //apex_available:platform
4340 apex_available: [
4341 "//apex_available:platform",
4342 "myapex",
4343 ],
Alex Light0851b882019-02-07 13:20:53 -08004344 }
4345 `)
4346
Sundong Ahnabb64432019-10-22 13:58:29 +09004347 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004348 apexRule := module.Rule("apexRule")
4349 copyCmds := apexRule.Args["copy_commands"]
4350
4351 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4352 t.Log("Apex was not a test apex!")
4353 t.Fail()
4354 }
4355 // Ensure that main rule creates an output
4356 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4357
4358 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004359 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004360
4361 // Ensure that both direct and indirect deps are copied into apex
4362 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4363
Colin Cross7113d202019-11-20 16:39:12 -08004364 // Ensure that the platform variant ends with _shared
4365 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004366}
4367
Alex Light9670d332019-01-29 18:07:33 -08004368func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004369 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004370 apex {
4371 name: "myapex",
4372 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004373 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004374 multilib: {
4375 first: {
4376 native_shared_libs: ["mylib_common"],
4377 }
4378 },
4379 target: {
4380 android: {
4381 multilib: {
4382 first: {
4383 native_shared_libs: ["mylib"],
4384 }
4385 }
4386 },
4387 host: {
4388 multilib: {
4389 first: {
4390 native_shared_libs: ["mylib2"],
4391 }
4392 }
4393 }
4394 }
4395 }
4396
4397 apex_key {
4398 name: "myapex.key",
4399 public_key: "testkey.avbpubkey",
4400 private_key: "testkey.pem",
4401 }
4402
4403 cc_library {
4404 name: "mylib",
4405 srcs: ["mylib.cpp"],
4406 system_shared_libs: [],
4407 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004408 // TODO: remove //apex_available:platform
4409 apex_available: [
4410 "//apex_available:platform",
4411 "myapex",
4412 ],
Alex Light9670d332019-01-29 18:07:33 -08004413 }
4414
4415 cc_library {
4416 name: "mylib_common",
4417 srcs: ["mylib.cpp"],
4418 system_shared_libs: [],
4419 stl: "none",
4420 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004421 // TODO: remove //apex_available:platform
4422 apex_available: [
4423 "//apex_available:platform",
4424 "myapex",
4425 ],
Alex Light9670d332019-01-29 18:07:33 -08004426 }
4427
4428 cc_library {
4429 name: "mylib2",
4430 srcs: ["mylib.cpp"],
4431 system_shared_libs: [],
4432 stl: "none",
4433 compile_multilib: "first",
4434 }
4435 `)
4436
Sundong Ahnabb64432019-10-22 13:58:29 +09004437 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004438 copyCmds := apexRule.Args["copy_commands"]
4439
4440 // Ensure that main rule creates an output
4441 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4442
4443 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004444 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4445 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4446 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004447
4448 // Ensure that both direct and indirect deps are copied into apex
4449 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4450 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4451 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4452
Colin Cross7113d202019-11-20 16:39:12 -08004453 // Ensure that the platform variant ends with _shared
4454 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4455 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4456 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004457}
Jiyong Park04480cf2019-02-06 00:16:29 +09004458
Jiyong Park59140302020-12-14 18:44:04 +09004459func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004460 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004461 apex {
4462 name: "myapex",
4463 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004464 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004465 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004466 arch: {
4467 arm64: {
4468 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004469 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004470 },
4471 x86_64: {
4472 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004473 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004474 },
4475 }
4476 }
4477
4478 apex_key {
4479 name: "myapex.key",
4480 public_key: "testkey.avbpubkey",
4481 private_key: "testkey.pem",
4482 }
4483
4484 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004485 name: "mylib.generic",
4486 srcs: ["mylib.cpp"],
4487 system_shared_libs: [],
4488 stl: "none",
4489 // TODO: remove //apex_available:platform
4490 apex_available: [
4491 "//apex_available:platform",
4492 "myapex",
4493 ],
4494 }
4495
4496 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004497 name: "mylib.arm64",
4498 srcs: ["mylib.cpp"],
4499 system_shared_libs: [],
4500 stl: "none",
4501 // TODO: remove //apex_available:platform
4502 apex_available: [
4503 "//apex_available:platform",
4504 "myapex",
4505 ],
4506 }
4507
4508 cc_library {
4509 name: "mylib.x64",
4510 srcs: ["mylib.cpp"],
4511 system_shared_libs: [],
4512 stl: "none",
4513 // TODO: remove //apex_available:platform
4514 apex_available: [
4515 "//apex_available:platform",
4516 "myapex",
4517 ],
4518 }
4519 `)
4520
4521 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4522 copyCmds := apexRule.Args["copy_commands"]
4523
4524 // Ensure that apex variant is created for the direct dep
4525 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004526 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004527 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4528
4529 // Ensure that both direct and indirect deps are copied into apex
4530 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4531 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4532}
4533
Jiyong Park04480cf2019-02-06 00:16:29 +09004534func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004535 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004536 apex {
4537 name: "myapex",
4538 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004539 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004540 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004541 }
4542
4543 apex_key {
4544 name: "myapex.key",
4545 public_key: "testkey.avbpubkey",
4546 private_key: "testkey.pem",
4547 }
4548
4549 sh_binary {
4550 name: "myscript",
4551 src: "mylib.cpp",
4552 filename: "myscript.sh",
4553 sub_dir: "script",
4554 }
4555 `)
4556
Sundong Ahnabb64432019-10-22 13:58:29 +09004557 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004558 copyCmds := apexRule.Args["copy_commands"]
4559
4560 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4561}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004562
Jooyung Han91df2082019-11-20 01:49:42 +09004563func TestApexInVariousPartition(t *testing.T) {
4564 testcases := []struct {
4565 propName, parition, flattenedPartition string
4566 }{
4567 {"", "system", "system_ext"},
4568 {"product_specific: true", "product", "product"},
4569 {"soc_specific: true", "vendor", "vendor"},
4570 {"proprietary: true", "vendor", "vendor"},
4571 {"vendor: true", "vendor", "vendor"},
4572 {"system_ext_specific: true", "system_ext", "system_ext"},
4573 }
4574 for _, tc := range testcases {
4575 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004576 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004577 apex {
4578 name: "myapex",
4579 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004580 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004581 `+tc.propName+`
4582 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004583
Jooyung Han91df2082019-11-20 01:49:42 +09004584 apex_key {
4585 name: "myapex.key",
4586 public_key: "testkey.avbpubkey",
4587 private_key: "testkey.pem",
4588 }
4589 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004590
Jooyung Han91df2082019-11-20 01:49:42 +09004591 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004592 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4593 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004594 if actual != expected {
4595 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4596 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004597
Jooyung Han91df2082019-11-20 01:49:42 +09004598 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004599 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4600 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004601 if actual != expected {
4602 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4603 }
4604 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004605 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004606}
Jiyong Park67882562019-03-21 01:11:21 +09004607
Jooyung Han580eb4f2020-06-24 19:33:06 +09004608func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004609 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004610 apex {
4611 name: "myapex",
4612 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004613 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004614 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004615
Jooyung Han580eb4f2020-06-24 19:33:06 +09004616 apex_key {
4617 name: "myapex.key",
4618 public_key: "testkey.avbpubkey",
4619 private_key: "testkey.pem",
4620 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004621 `)
4622 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004623 rule := module.Output("file_contexts")
4624 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4625}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004626
Jooyung Han580eb4f2020-06-24 19:33:06 +09004627func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004628 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004629 apex {
4630 name: "myapex",
4631 key: "myapex.key",
4632 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004633 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004634 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004635
Jooyung Han580eb4f2020-06-24 19:33:06 +09004636 apex_key {
4637 name: "myapex.key",
4638 public_key: "testkey.avbpubkey",
4639 private_key: "testkey.pem",
4640 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004641 `, withFiles(map[string][]byte{
4642 "my_own_file_contexts": nil,
4643 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004644}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004645
Jooyung Han580eb4f2020-06-24 19:33:06 +09004646func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004647 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004648 apex {
4649 name: "myapex",
4650 key: "myapex.key",
4651 product_specific: true,
4652 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004653 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004654 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004655
Jooyung Han580eb4f2020-06-24 19:33:06 +09004656 apex_key {
4657 name: "myapex.key",
4658 public_key: "testkey.avbpubkey",
4659 private_key: "testkey.pem",
4660 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004661 `)
4662
Colin Cross1c460562021-02-16 17:55:47 -08004663 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004664 apex {
4665 name: "myapex",
4666 key: "myapex.key",
4667 product_specific: true,
4668 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004669 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004670 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004671
Jooyung Han580eb4f2020-06-24 19:33:06 +09004672 apex_key {
4673 name: "myapex.key",
4674 public_key: "testkey.avbpubkey",
4675 private_key: "testkey.pem",
4676 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004677 `, withFiles(map[string][]byte{
4678 "product_specific_file_contexts": nil,
4679 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004680 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4681 rule := module.Output("file_contexts")
4682 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4683}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004684
Jooyung Han580eb4f2020-06-24 19:33:06 +09004685func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004686 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004687 apex {
4688 name: "myapex",
4689 key: "myapex.key",
4690 product_specific: true,
4691 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004692 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004693 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004694
Jooyung Han580eb4f2020-06-24 19:33:06 +09004695 apex_key {
4696 name: "myapex.key",
4697 public_key: "testkey.avbpubkey",
4698 private_key: "testkey.pem",
4699 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004700
Jooyung Han580eb4f2020-06-24 19:33:06 +09004701 filegroup {
4702 name: "my-file-contexts",
4703 srcs: ["product_specific_file_contexts"],
4704 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004705 `, withFiles(map[string][]byte{
4706 "product_specific_file_contexts": nil,
4707 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004708 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4709 rule := module.Output("file_contexts")
4710 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004711}
4712
Jiyong Park67882562019-03-21 01:11:21 +09004713func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004714 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004715 apex_key {
4716 name: "myapex.key",
4717 public_key: ":my.avbpubkey",
4718 private_key: ":my.pem",
4719 product_specific: true,
4720 }
4721
4722 filegroup {
4723 name: "my.avbpubkey",
4724 srcs: ["testkey2.avbpubkey"],
4725 }
4726
4727 filegroup {
4728 name: "my.pem",
4729 srcs: ["testkey2.pem"],
4730 }
4731 `)
4732
4733 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4734 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004735 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004736 if actual_pubkey != expected_pubkey {
4737 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4738 }
4739 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004740 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004741 if actual_privkey != expected_privkey {
4742 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4743 }
4744}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004745
4746func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004747 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004748 prebuilt_apex {
4749 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004750 arch: {
4751 arm64: {
4752 src: "myapex-arm64.apex",
4753 },
4754 arm: {
4755 src: "myapex-arm.apex",
4756 },
4757 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004758 }
4759 `)
4760
Wei Li340ee8e2022-03-18 17:33:24 -07004761 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4762 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004763
Jiyong Parkc95714e2019-03-29 14:23:10 +09004764 expectedInput := "myapex-arm64.apex"
4765 if prebuilt.inputApex.String() != expectedInput {
4766 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4767 }
Wei Li340ee8e2022-03-18 17:33:24 -07004768 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4769 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4770 rule := testingModule.Rule("genProvenanceMetaData")
4771 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4772 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4773 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4774 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004775}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004776
Paul Duffinc0609c62021-03-01 17:27:16 +00004777func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004778 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004779 prebuilt_apex {
4780 name: "myapex",
4781 }
4782 `)
4783}
4784
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004785func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004786 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004787 prebuilt_apex {
4788 name: "myapex",
4789 src: "myapex-arm.apex",
4790 filename: "notmyapex.apex",
4791 }
4792 `)
4793
Wei Li340ee8e2022-03-18 17:33:24 -07004794 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4795 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004796
4797 expected := "notmyapex.apex"
4798 if p.installFilename != expected {
4799 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4800 }
Wei Li340ee8e2022-03-18 17:33:24 -07004801 rule := testingModule.Rule("genProvenanceMetaData")
4802 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4803 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4804 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4805 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004806}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004807
Samiul Islam7c02e262021-09-08 17:48:28 +01004808func TestApexSetFilenameOverride(t *testing.T) {
4809 testApex(t, `
4810 apex_set {
4811 name: "com.company.android.myapex",
4812 apex_name: "com.android.myapex",
4813 set: "company-myapex.apks",
4814 filename: "com.company.android.myapex.apex"
4815 }
4816 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4817
4818 testApex(t, `
4819 apex_set {
4820 name: "com.company.android.myapex",
4821 apex_name: "com.android.myapex",
4822 set: "company-myapex.apks",
4823 filename: "com.company.android.myapex.capex"
4824 }
4825 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4826
4827 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4828 apex_set {
4829 name: "com.company.android.myapex",
4830 apex_name: "com.android.myapex",
4831 set: "company-myapex.apks",
4832 filename: "some-random-suffix"
4833 }
4834 `)
4835}
4836
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004837func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004838 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004839 prebuilt_apex {
4840 name: "myapex.prebuilt",
4841 src: "myapex-arm.apex",
4842 overrides: [
4843 "myapex",
4844 ],
4845 }
4846 `)
4847
Wei Li340ee8e2022-03-18 17:33:24 -07004848 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4849 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004850
4851 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004852 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004853 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004854 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004855 }
Wei Li340ee8e2022-03-18 17:33:24 -07004856 rule := testingModule.Rule("genProvenanceMetaData")
4857 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4858 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4859 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4860 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004861}
4862
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004863func TestPrebuiltApexName(t *testing.T) {
4864 testApex(t, `
4865 prebuilt_apex {
4866 name: "com.company.android.myapex",
4867 apex_name: "com.android.myapex",
4868 src: "company-myapex-arm.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 }
4878 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4879}
4880
4881func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4882 _ = android.GroupFixturePreparers(
4883 java.PrepareForTestWithJavaDefaultModules,
4884 PrepareForTestWithApexBuildComponents,
4885 android.FixtureWithRootAndroidBp(`
4886 platform_bootclasspath {
4887 name: "platform-bootclasspath",
4888 fragments: [
4889 {
4890 apex: "com.android.art",
4891 module: "art-bootclasspath-fragment",
4892 },
4893 ],
4894 }
4895
4896 prebuilt_apex {
4897 name: "com.company.android.art",
4898 apex_name: "com.android.art",
4899 src: "com.company.android.art-arm.apex",
4900 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4901 }
4902
4903 prebuilt_bootclasspath_fragment {
4904 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004905 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004906 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004907 hidden_api: {
4908 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4909 metadata: "my-bootclasspath-fragment/metadata.csv",
4910 index: "my-bootclasspath-fragment/index.csv",
4911 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4912 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4913 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004914 }
4915
4916 java_import {
4917 name: "core-oj",
4918 jars: ["prebuilt.jar"],
4919 }
4920 `),
4921 ).RunTest(t)
4922}
4923
Paul Duffin092153d2021-01-26 11:42:39 +00004924// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4925// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004926func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004927 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004928
Paul Duffin89886cb2021-02-05 16:44:03 +00004929 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004930 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004931 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004932 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004933 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004934 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004935 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4936 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4937 android.NormalizePathForTesting(dexJarBuildPath))
4938 }
4939
4940 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004941 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004942 // Make sure the import has been given the correct path to the dex jar.
4943 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4944 dexJarBuildPath := p.DexJarInstallPath()
4945 stem := android.RemoveOptionalPrebuiltPrefix(name)
4946 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4947 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4948 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004949 }
4950
Paul Duffin39853512021-02-26 11:09:39 +00004951 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004952 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004953 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004954 android.AssertArrayString(t, "Check if there is no source variant",
4955 []string{"android_common"},
4956 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004957 }
4958
4959 t.Run("prebuilt only", func(t *testing.T) {
4960 bp := `
4961 prebuilt_apex {
4962 name: "myapex",
4963 arch: {
4964 arm64: {
4965 src: "myapex-arm64.apex",
4966 },
4967 arm: {
4968 src: "myapex-arm.apex",
4969 },
4970 },
Paul Duffin39853512021-02-26 11:09:39 +00004971 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004972 }
4973
4974 java_import {
4975 name: "libfoo",
4976 jars: ["libfoo.jar"],
4977 }
Paul Duffin39853512021-02-26 11:09:39 +00004978
4979 java_sdk_library_import {
4980 name: "libbar",
4981 public: {
4982 jars: ["libbar.jar"],
4983 },
4984 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004985 `
4986
4987 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4988 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4989
Martin Stjernholm44825602021-09-17 01:44:12 +01004990 deapexerName := deapexerModuleName("myapex")
4991 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4992
Paul Duffinf6932af2021-02-26 18:21:56 +00004993 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004994 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004995 rule := deapexer.Rule("deapexer")
4996 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4997 t.Errorf("expected: %q, found: %q", expected, actual)
4998 }
4999
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005000 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005001 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005002 rule = prebuiltApex.Rule("android/soong/android.Cp")
5003 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5004 t.Errorf("expected: %q, found: %q", expected, actual)
5005 }
5006
Paul Duffin89886cb2021-02-05 16:44:03 +00005007 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005008 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005009
5010 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005011 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005012 })
5013
5014 t.Run("prebuilt with source preferred", func(t *testing.T) {
5015
5016 bp := `
5017 prebuilt_apex {
5018 name: "myapex",
5019 arch: {
5020 arm64: {
5021 src: "myapex-arm64.apex",
5022 },
5023 arm: {
5024 src: "myapex-arm.apex",
5025 },
5026 },
Paul Duffin39853512021-02-26 11:09:39 +00005027 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005028 }
5029
5030 java_import {
5031 name: "libfoo",
5032 jars: ["libfoo.jar"],
5033 }
5034
5035 java_library {
5036 name: "libfoo",
5037 }
Paul Duffin39853512021-02-26 11:09:39 +00005038
5039 java_sdk_library_import {
5040 name: "libbar",
5041 public: {
5042 jars: ["libbar.jar"],
5043 },
5044 }
5045
5046 java_sdk_library {
5047 name: "libbar",
5048 srcs: ["foo/bar/MyClass.java"],
5049 unsafe_ignore_missing_latest_api: true,
5050 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005051 `
5052
5053 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5054 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5055
Paul Duffin89886cb2021-02-05 16:44:03 +00005056 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005057 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005058 ensureNoSourceVariant(t, ctx, "libfoo")
5059
5060 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005061 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005062 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005063 })
5064
5065 t.Run("prebuilt preferred with source", func(t *testing.T) {
5066 bp := `
5067 prebuilt_apex {
5068 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005069 arch: {
5070 arm64: {
5071 src: "myapex-arm64.apex",
5072 },
5073 arm: {
5074 src: "myapex-arm.apex",
5075 },
5076 },
Paul Duffin39853512021-02-26 11:09:39 +00005077 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005078 }
5079
5080 java_import {
5081 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005082 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005083 jars: ["libfoo.jar"],
5084 }
5085
5086 java_library {
5087 name: "libfoo",
5088 }
Paul Duffin39853512021-02-26 11:09:39 +00005089
5090 java_sdk_library_import {
5091 name: "libbar",
5092 prefer: true,
5093 public: {
5094 jars: ["libbar.jar"],
5095 },
5096 }
5097
5098 java_sdk_library {
5099 name: "libbar",
5100 srcs: ["foo/bar/MyClass.java"],
5101 unsafe_ignore_missing_latest_api: true,
5102 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005103 `
5104
5105 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5106 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5107
Paul Duffin89886cb2021-02-05 16:44:03 +00005108 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005109 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005110 ensureNoSourceVariant(t, ctx, "libfoo")
5111
5112 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005113 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005114 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005115 })
5116}
5117
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005118func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005119 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005120 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005121 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5122 // is disabled.
5123 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5124 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005125
Paul Duffin37856732021-02-26 14:24:15 +00005126 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5127 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005128 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005129 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005130 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005131 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005132 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005133 foundLibfooJar = true
5134 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005135 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005136 }
5137 }
5138 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005139 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 +00005140 }
5141 }
5142
Paul Duffin40a3f652021-07-19 13:11:24 +01005143 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005144 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005145 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005146 var rule android.TestingBuildParams
5147
5148 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5149 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005150 }
5151
Paul Duffin40a3f652021-07-19 13:11:24 +01005152 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5153 t.Helper()
5154 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5155 var rule android.TestingBuildParams
5156
5157 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5158 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5159 }
5160
Paul Duffin89f570a2021-06-16 01:42:33 +01005161 fragment := java.ApexVariantReference{
5162 Apex: proptools.StringPtr("myapex"),
5163 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5164 }
5165
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005166 t.Run("prebuilt only", func(t *testing.T) {
5167 bp := `
5168 prebuilt_apex {
5169 name: "myapex",
5170 arch: {
5171 arm64: {
5172 src: "myapex-arm64.apex",
5173 },
5174 arm: {
5175 src: "myapex-arm.apex",
5176 },
5177 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005178 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5179 }
5180
5181 prebuilt_bootclasspath_fragment {
5182 name: "my-bootclasspath-fragment",
5183 contents: ["libfoo", "libbar"],
5184 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005185 hidden_api: {
5186 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5187 metadata: "my-bootclasspath-fragment/metadata.csv",
5188 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005189 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5190 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5191 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005192 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005193 }
5194
5195 java_import {
5196 name: "libfoo",
5197 jars: ["libfoo.jar"],
5198 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005199 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005200 }
Paul Duffin37856732021-02-26 14:24:15 +00005201
5202 java_sdk_library_import {
5203 name: "libbar",
5204 public: {
5205 jars: ["libbar.jar"],
5206 },
5207 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005208 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005209 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005210 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005211 `
5212
Paul Duffin89f570a2021-06-16 01:42:33 +01005213 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005214 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5215 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005216
Paul Duffin537ea3d2021-05-14 10:38:00 +01005217 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005218 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005219 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005220 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005221 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5222 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005223 })
5224
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005225 t.Run("apex_set only", func(t *testing.T) {
5226 bp := `
5227 apex_set {
5228 name: "myapex",
5229 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005230 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5231 }
5232
5233 prebuilt_bootclasspath_fragment {
5234 name: "my-bootclasspath-fragment",
5235 contents: ["libfoo", "libbar"],
5236 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005237 hidden_api: {
5238 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5239 metadata: "my-bootclasspath-fragment/metadata.csv",
5240 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005241 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5242 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5243 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005244 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005245 }
5246
5247 java_import {
5248 name: "libfoo",
5249 jars: ["libfoo.jar"],
5250 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005251 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005252 }
5253
5254 java_sdk_library_import {
5255 name: "libbar",
5256 public: {
5257 jars: ["libbar.jar"],
5258 },
5259 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005260 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005261 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005262 }
5263 `
5264
Paul Duffin89f570a2021-06-16 01:42:33 +01005265 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005266 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5267 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5268
Paul Duffin537ea3d2021-05-14 10:38:00 +01005269 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005270 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005271 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005272 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005273 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5274 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005275 })
5276
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005277 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5278 bp := `
5279 prebuilt_apex {
5280 name: "myapex",
5281 arch: {
5282 arm64: {
5283 src: "myapex-arm64.apex",
5284 },
5285 arm: {
5286 src: "myapex-arm.apex",
5287 },
5288 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005289 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5290 }
5291
5292 prebuilt_bootclasspath_fragment {
5293 name: "my-bootclasspath-fragment",
5294 contents: ["libfoo", "libbar"],
5295 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005296 hidden_api: {
5297 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5298 metadata: "my-bootclasspath-fragment/metadata.csv",
5299 index: "my-bootclasspath-fragment/index.csv",
5300 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5301 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5302 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005303 }
5304
5305 java_import {
5306 name: "libfoo",
5307 jars: ["libfoo.jar"],
5308 apex_available: ["myapex"],
5309 }
5310
5311 java_library {
5312 name: "libfoo",
5313 srcs: ["foo/bar/MyClass.java"],
5314 apex_available: ["myapex"],
5315 }
Paul Duffin37856732021-02-26 14:24:15 +00005316
5317 java_sdk_library_import {
5318 name: "libbar",
5319 public: {
5320 jars: ["libbar.jar"],
5321 },
5322 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005323 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005324 }
5325
5326 java_sdk_library {
5327 name: "libbar",
5328 srcs: ["foo/bar/MyClass.java"],
5329 unsafe_ignore_missing_latest_api: true,
5330 apex_available: ["myapex"],
5331 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005332 `
5333
5334 // In this test the source (java_library) libfoo is active since the
5335 // prebuilt (java_import) defaults to prefer:false. However the
5336 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5337 // find the dex boot jar in it. We either need to disable the source libfoo
5338 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005339 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005340 // dexbootjar check is skipped if AllowMissingDependencies is true
5341 preparerAllowMissingDeps := android.GroupFixturePreparers(
5342 preparer,
5343 android.PrepareForTestWithAllowMissingDependencies,
5344 )
5345 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005346 })
5347
5348 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5349 bp := `
5350 prebuilt_apex {
5351 name: "myapex",
5352 arch: {
5353 arm64: {
5354 src: "myapex-arm64.apex",
5355 },
5356 arm: {
5357 src: "myapex-arm.apex",
5358 },
5359 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005360 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5361 }
5362
5363 prebuilt_bootclasspath_fragment {
5364 name: "my-bootclasspath-fragment",
5365 contents: ["libfoo", "libbar"],
5366 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005367 hidden_api: {
5368 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5369 metadata: "my-bootclasspath-fragment/metadata.csv",
5370 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005371 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5372 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5373 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005374 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005375 }
5376
5377 java_import {
5378 name: "libfoo",
5379 prefer: true,
5380 jars: ["libfoo.jar"],
5381 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005382 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005383 }
5384
5385 java_library {
5386 name: "libfoo",
5387 srcs: ["foo/bar/MyClass.java"],
5388 apex_available: ["myapex"],
5389 }
Paul Duffin37856732021-02-26 14:24:15 +00005390
5391 java_sdk_library_import {
5392 name: "libbar",
5393 prefer: true,
5394 public: {
5395 jars: ["libbar.jar"],
5396 },
5397 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005398 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005399 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005400 }
5401
5402 java_sdk_library {
5403 name: "libbar",
5404 srcs: ["foo/bar/MyClass.java"],
5405 unsafe_ignore_missing_latest_api: true,
5406 apex_available: ["myapex"],
5407 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005408 `
5409
Paul Duffin89f570a2021-06-16 01:42:33 +01005410 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005411 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5412 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005413
Paul Duffin537ea3d2021-05-14 10:38:00 +01005414 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005415 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005416 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005417 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005418 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5419 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005420 })
5421
5422 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5423 bp := `
5424 apex {
5425 name: "myapex",
5426 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005427 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005428 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 }
5430
5431 apex_key {
5432 name: "myapex.key",
5433 public_key: "testkey.avbpubkey",
5434 private_key: "testkey.pem",
5435 }
5436
5437 prebuilt_apex {
5438 name: "myapex",
5439 arch: {
5440 arm64: {
5441 src: "myapex-arm64.apex",
5442 },
5443 arm: {
5444 src: "myapex-arm.apex",
5445 },
5446 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005447 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5448 }
5449
5450 prebuilt_bootclasspath_fragment {
5451 name: "my-bootclasspath-fragment",
5452 contents: ["libfoo", "libbar"],
5453 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005454 hidden_api: {
5455 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5456 metadata: "my-bootclasspath-fragment/metadata.csv",
5457 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005458 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5459 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5460 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005461 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005462 }
5463
5464 java_import {
5465 name: "libfoo",
5466 jars: ["libfoo.jar"],
5467 apex_available: ["myapex"],
5468 }
5469
5470 java_library {
5471 name: "libfoo",
5472 srcs: ["foo/bar/MyClass.java"],
5473 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005474 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005475 }
Paul Duffin37856732021-02-26 14:24:15 +00005476
5477 java_sdk_library_import {
5478 name: "libbar",
5479 public: {
5480 jars: ["libbar.jar"],
5481 },
5482 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005483 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005484 }
5485
5486 java_sdk_library {
5487 name: "libbar",
5488 srcs: ["foo/bar/MyClass.java"],
5489 unsafe_ignore_missing_latest_api: true,
5490 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005491 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005492 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005493 `
5494
Paul Duffin89f570a2021-06-16 01:42:33 +01005495 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005496 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5497 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005498
Paul Duffin537ea3d2021-05-14 10:38:00 +01005499 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005500 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005501 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005502 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005503 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5504 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005505 })
5506
5507 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5508 bp := `
5509 apex {
5510 name: "myapex",
5511 enabled: false,
5512 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005513 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005514 }
5515
5516 apex_key {
5517 name: "myapex.key",
5518 public_key: "testkey.avbpubkey",
5519 private_key: "testkey.pem",
5520 }
5521
5522 prebuilt_apex {
5523 name: "myapex",
5524 arch: {
5525 arm64: {
5526 src: "myapex-arm64.apex",
5527 },
5528 arm: {
5529 src: "myapex-arm.apex",
5530 },
5531 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005532 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5533 }
5534
5535 prebuilt_bootclasspath_fragment {
5536 name: "my-bootclasspath-fragment",
5537 contents: ["libfoo", "libbar"],
5538 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005539 hidden_api: {
5540 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5541 metadata: "my-bootclasspath-fragment/metadata.csv",
5542 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005543 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5544 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5545 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005546 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005547 }
5548
5549 java_import {
5550 name: "libfoo",
5551 prefer: true,
5552 jars: ["libfoo.jar"],
5553 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005554 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005555 }
5556
5557 java_library {
5558 name: "libfoo",
5559 srcs: ["foo/bar/MyClass.java"],
5560 apex_available: ["myapex"],
5561 }
Paul Duffin37856732021-02-26 14:24:15 +00005562
5563 java_sdk_library_import {
5564 name: "libbar",
5565 prefer: true,
5566 public: {
5567 jars: ["libbar.jar"],
5568 },
5569 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005570 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005571 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005572 }
5573
5574 java_sdk_library {
5575 name: "libbar",
5576 srcs: ["foo/bar/MyClass.java"],
5577 unsafe_ignore_missing_latest_api: true,
5578 apex_available: ["myapex"],
5579 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005580 `
5581
Paul Duffin89f570a2021-06-16 01:42:33 +01005582 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005583 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5584 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005585
Paul Duffin537ea3d2021-05-14 10:38:00 +01005586 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005587 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005588 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005589 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005590 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5591 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005592 })
5593}
5594
Roland Levillain630846d2019-06-26 12:48:34 +01005595func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005596 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005597 apex_test {
5598 name: "myapex",
5599 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005600 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005601 tests: [
5602 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005603 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005604 ],
5605 }
5606
5607 apex_key {
5608 name: "myapex.key",
5609 public_key: "testkey.avbpubkey",
5610 private_key: "testkey.pem",
5611 }
5612
Liz Kammer1c14a212020-05-12 15:26:55 -07005613 filegroup {
5614 name: "fg",
5615 srcs: [
5616 "baz",
5617 "bar/baz"
5618 ],
5619 }
5620
Roland Levillain630846d2019-06-26 12:48:34 +01005621 cc_test {
5622 name: "mytest",
5623 gtest: false,
5624 srcs: ["mytest.cpp"],
5625 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005626 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005627 system_shared_libs: [],
5628 static_executable: true,
5629 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005630 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005631 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005632
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005633 cc_library {
5634 name: "mylib",
5635 srcs: ["mylib.cpp"],
5636 system_shared_libs: [],
5637 stl: "none",
5638 }
5639
Liz Kammer5bd365f2020-05-27 15:15:11 -07005640 filegroup {
5641 name: "fg2",
5642 srcs: [
5643 "testdata/baz"
5644 ],
5645 }
5646
Roland Levillain9b5fde92019-06-28 15:41:19 +01005647 cc_test {
5648 name: "mytests",
5649 gtest: false,
5650 srcs: [
5651 "mytest1.cpp",
5652 "mytest2.cpp",
5653 "mytest3.cpp",
5654 ],
5655 test_per_src: true,
5656 relative_install_path: "test",
5657 system_shared_libs: [],
5658 static_executable: true,
5659 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005660 data: [
5661 ":fg",
5662 ":fg2",
5663 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005664 }
Roland Levillain630846d2019-06-26 12:48:34 +01005665 `)
5666
Sundong Ahnabb64432019-10-22 13:58:29 +09005667 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005668 copyCmds := apexRule.Args["copy_commands"]
5669
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005670 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005671 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005672 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005673
Liz Kammer1c14a212020-05-12 15:26:55 -07005674 //Ensure that test data are copied into apex.
5675 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5676 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5677
Roland Levillain9b5fde92019-06-28 15:41:19 +01005678 // Ensure that test deps built with `test_per_src` are copied into apex.
5679 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5680 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5681 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005682
5683 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005684 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005685 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005686 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005687 prefix := "TARGET_"
5688 var builder strings.Builder
5689 data.Custom(&builder, name, prefix, "", data)
5690 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005691 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5692 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5693 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5694 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5695 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5696 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005697 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005698
5699 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005700 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005701 data.Custom(&builder, name, prefix, "", data)
5702 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005703 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5704 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005705}
5706
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005707func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005708 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005709 apex {
5710 name: "myapex",
5711 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005712 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005713 }
5714 apex_key {
5715 name: "myapex.key",
5716 public_key: "testkey.avbpubkey",
5717 private_key: "testkey.pem",
5718 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005719 `,
5720 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5721 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5722 }),
5723 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005724 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005725 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005726 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005727 var builder strings.Builder
5728 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5729 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005730 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005731}
5732
Jooyung Hand48f3c32019-08-23 11:18:57 +09005733func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5734 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5735 apex {
5736 name: "myapex",
5737 key: "myapex.key",
5738 native_shared_libs: ["libfoo"],
5739 }
5740
5741 apex_key {
5742 name: "myapex.key",
5743 public_key: "testkey.avbpubkey",
5744 private_key: "testkey.pem",
5745 }
5746
5747 cc_library {
5748 name: "libfoo",
5749 stl: "none",
5750 system_shared_libs: [],
5751 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005752 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005753 }
5754 `)
5755 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5756 apex {
5757 name: "myapex",
5758 key: "myapex.key",
5759 java_libs: ["myjar"],
5760 }
5761
5762 apex_key {
5763 name: "myapex.key",
5764 public_key: "testkey.avbpubkey",
5765 private_key: "testkey.pem",
5766 }
5767
5768 java_library {
5769 name: "myjar",
5770 srcs: ["foo/bar/MyClass.java"],
5771 sdk_version: "none",
5772 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005773 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005774 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005775 }
5776 `)
5777}
5778
Bill Peckhama41a6962021-01-11 10:58:54 -08005779func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005780 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005781 apex {
5782 name: "myapex",
5783 key: "myapex.key",
5784 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005785 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005786 }
5787
5788 apex_key {
5789 name: "myapex.key",
5790 public_key: "testkey.avbpubkey",
5791 private_key: "testkey.pem",
5792 }
5793
5794 java_import {
5795 name: "myjavaimport",
5796 apex_available: ["myapex"],
5797 jars: ["my.jar"],
5798 compile_dex: true,
5799 }
5800 `)
5801
5802 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5803 apexRule := module.Rule("apexRule")
5804 copyCmds := apexRule.Args["copy_commands"]
5805 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5806}
5807
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005808func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005809 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005810 apex {
5811 name: "myapex",
5812 key: "myapex.key",
5813 apps: [
5814 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005815 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005816 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005817 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005818 }
5819
5820 apex_key {
5821 name: "myapex.key",
5822 public_key: "testkey.avbpubkey",
5823 private_key: "testkey.pem",
5824 }
5825
5826 android_app {
5827 name: "AppFoo",
5828 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005829 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005830 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005831 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005832 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005833 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005834 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005835
5836 android_app {
5837 name: "AppFooPriv",
5838 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005839 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005840 system_modules: "none",
5841 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005842 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005843 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005844 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005845
5846 cc_library_shared {
5847 name: "libjni",
5848 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005849 shared_libs: ["libfoo"],
5850 stl: "none",
5851 system_shared_libs: [],
5852 apex_available: [ "myapex" ],
5853 sdk_version: "current",
5854 }
5855
5856 cc_library_shared {
5857 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005858 stl: "none",
5859 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005860 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005861 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005862 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005863 `)
5864
Sundong Ahnabb64432019-10-22 13:58:29 +09005865 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005866 apexRule := module.Rule("apexRule")
5867 copyCmds := apexRule.Args["copy_commands"]
5868
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005869 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5870 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005871
Colin Crossaede88c2020-08-11 12:17:01 -07005872 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005873 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005874 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005875 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005876 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005877 // JNI libraries including transitive deps are
5878 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005879 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005880 // ... embedded inside APK (jnilibs.zip)
5881 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5882 // ... and not directly inside the APEX
5883 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5884 }
Dario Frenicde2a032019-10-27 00:29:22 +01005885}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005886
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005887func TestApexWithAppImportBuildId(t *testing.T) {
5888 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5889 for _, id := range invalidBuildIds {
5890 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5891 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5892 variables.BuildId = proptools.StringPtr(id)
5893 })
5894 testApexError(t, message, `apex {
5895 name: "myapex",
5896 key: "myapex.key",
5897 apps: ["AppFooPrebuilt"],
5898 updatable: false,
5899 }
5900
5901 apex_key {
5902 name: "myapex.key",
5903 public_key: "testkey.avbpubkey",
5904 private_key: "testkey.pem",
5905 }
5906
5907 android_app_import {
5908 name: "AppFooPrebuilt",
5909 apk: "PrebuiltAppFoo.apk",
5910 presigned: true,
5911 apex_available: ["myapex"],
5912 }
5913 `, fixture)
5914 }
5915}
5916
Dario Frenicde2a032019-10-27 00:29:22 +01005917func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005918 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005919 apex {
5920 name: "myapex",
5921 key: "myapex.key",
5922 apps: [
5923 "AppFooPrebuilt",
5924 "AppFooPrivPrebuilt",
5925 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005926 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005927 }
5928
5929 apex_key {
5930 name: "myapex.key",
5931 public_key: "testkey.avbpubkey",
5932 private_key: "testkey.pem",
5933 }
5934
5935 android_app_import {
5936 name: "AppFooPrebuilt",
5937 apk: "PrebuiltAppFoo.apk",
5938 presigned: true,
5939 dex_preopt: {
5940 enabled: false,
5941 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005942 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005943 }
5944
5945 android_app_import {
5946 name: "AppFooPrivPrebuilt",
5947 apk: "PrebuiltAppFooPriv.apk",
5948 privileged: true,
5949 presigned: true,
5950 dex_preopt: {
5951 enabled: false,
5952 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005953 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005954 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005955 }
5956 `)
5957
Sundong Ahnabb64432019-10-22 13:58:29 +09005958 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005959 apexRule := module.Rule("apexRule")
5960 copyCmds := apexRule.Args["copy_commands"]
5961
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005962 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5963 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005964}
5965
5966func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005967 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005968 apex {
5969 name: "myapex",
5970 key: "myapex.key",
5971 apps: [
5972 "AppFoo",
5973 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005974 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005975 }
5976
5977 apex_key {
5978 name: "myapex.key",
5979 public_key: "testkey.avbpubkey",
5980 private_key: "testkey.pem",
5981 }
5982
5983 android_app {
5984 name: "AppFoo",
5985 srcs: ["foo/bar/MyClass.java"],
5986 sdk_version: "none",
5987 system_modules: "none",
5988 apex_available: [ "myapex" ],
5989 }
5990
5991 android_app_import {
5992 name: "AppFoo",
5993 apk: "AppFooPrebuilt.apk",
5994 filename: "AppFooPrebuilt.apk",
5995 presigned: true,
5996 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005997 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005998 }
5999 `, withFiles(map[string][]byte{
6000 "AppFooPrebuilt.apk": nil,
6001 }))
6002
6003 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006004 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006005 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006006}
6007
Dario Freni6f3937c2019-12-20 22:58:03 +00006008func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006009 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006010 apex {
6011 name: "myapex",
6012 key: "myapex.key",
6013 apps: [
6014 "TesterHelpAppFoo",
6015 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006016 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006017 }
6018
6019 apex_key {
6020 name: "myapex.key",
6021 public_key: "testkey.avbpubkey",
6022 private_key: "testkey.pem",
6023 }
6024
6025 android_test_helper_app {
6026 name: "TesterHelpAppFoo",
6027 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006028 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006029 }
6030
6031 `)
6032
6033 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6034 apexRule := module.Rule("apexRule")
6035 copyCmds := apexRule.Args["copy_commands"]
6036
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006037 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006038}
6039
Jooyung Han18020ea2019-11-13 10:50:48 +09006040func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6041 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006042 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006043 apex {
6044 name: "myapex",
6045 key: "myapex.key",
6046 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006047 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006048 }
6049
6050 apex_key {
6051 name: "myapex.key",
6052 public_key: "testkey.avbpubkey",
6053 private_key: "testkey.pem",
6054 }
6055
6056 apex {
6057 name: "otherapex",
6058 key: "myapex.key",
6059 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006060 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006061 }
6062
6063 cc_defaults {
6064 name: "libfoo-defaults",
6065 apex_available: ["otherapex"],
6066 }
6067
6068 cc_library {
6069 name: "libfoo",
6070 defaults: ["libfoo-defaults"],
6071 stl: "none",
6072 system_shared_libs: [],
6073 }`)
6074}
6075
Paul Duffine52e66f2020-03-30 17:54:29 +01006076func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006077 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006078 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006079 apex {
6080 name: "myapex",
6081 key: "myapex.key",
6082 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006083 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006084 }
6085
6086 apex_key {
6087 name: "myapex.key",
6088 public_key: "testkey.avbpubkey",
6089 private_key: "testkey.pem",
6090 }
6091
6092 apex {
6093 name: "otherapex",
6094 key: "otherapex.key",
6095 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006096 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006097 }
6098
6099 apex_key {
6100 name: "otherapex.key",
6101 public_key: "testkey.avbpubkey",
6102 private_key: "testkey.pem",
6103 }
6104
6105 cc_library {
6106 name: "libfoo",
6107 stl: "none",
6108 system_shared_libs: [],
6109 apex_available: ["otherapex"],
6110 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006111}
Jiyong Park127b40b2019-09-30 16:04:35 +09006112
Paul Duffine52e66f2020-03-30 17:54:29 +01006113func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006114 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006115 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006116.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006117.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006118.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006119.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006120.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006121.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006122 apex {
6123 name: "myapex",
6124 key: "myapex.key",
6125 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006126 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006127 }
6128
6129 apex_key {
6130 name: "myapex.key",
6131 public_key: "testkey.avbpubkey",
6132 private_key: "testkey.pem",
6133 }
6134
Jiyong Park127b40b2019-09-30 16:04:35 +09006135 cc_library {
6136 name: "libfoo",
6137 stl: "none",
6138 shared_libs: ["libbar"],
6139 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006140 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006141 }
6142
6143 cc_library {
6144 name: "libbar",
6145 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006146 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006147 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006148 apex_available: ["myapex"],
6149 }
6150
6151 cc_library {
6152 name: "libbaz",
6153 stl: "none",
6154 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006155 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006156}
Jiyong Park127b40b2019-09-30 16:04:35 +09006157
Paul Duffine52e66f2020-03-30 17:54:29 +01006158func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006159 testApexError(t, "\"otherapex\" is not a valid module name", `
6160 apex {
6161 name: "myapex",
6162 key: "myapex.key",
6163 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006164 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006165 }
6166
6167 apex_key {
6168 name: "myapex.key",
6169 public_key: "testkey.avbpubkey",
6170 private_key: "testkey.pem",
6171 }
6172
6173 cc_library {
6174 name: "libfoo",
6175 stl: "none",
6176 system_shared_libs: [],
6177 apex_available: ["otherapex"],
6178 }`)
6179
Paul Duffine52e66f2020-03-30 17:54:29 +01006180 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006181 apex {
6182 name: "myapex",
6183 key: "myapex.key",
6184 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006185 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006186 }
6187
6188 apex_key {
6189 name: "myapex.key",
6190 public_key: "testkey.avbpubkey",
6191 private_key: "testkey.pem",
6192 }
6193
6194 cc_library {
6195 name: "libfoo",
6196 stl: "none",
6197 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006198 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006199 apex_available: ["myapex"],
6200 }
6201
6202 cc_library {
6203 name: "libbar",
6204 stl: "none",
6205 system_shared_libs: [],
6206 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006207 }
6208
6209 cc_library {
6210 name: "libbaz",
6211 stl: "none",
6212 system_shared_libs: [],
6213 stubs: {
6214 versions: ["10", "20", "30"],
6215 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006216 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006217}
Jiyong Park127b40b2019-09-30 16:04:35 +09006218
Jiyong Park89e850a2020-04-07 16:37:39 +09006219func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006220 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006221 apex {
6222 name: "myapex",
6223 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006224 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006225 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006226 }
6227
6228 apex_key {
6229 name: "myapex.key",
6230 public_key: "testkey.avbpubkey",
6231 private_key: "testkey.pem",
6232 }
6233
6234 cc_library {
6235 name: "libfoo",
6236 stl: "none",
6237 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006238 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006239 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006240 }
6241
6242 cc_library {
6243 name: "libfoo2",
6244 stl: "none",
6245 system_shared_libs: [],
6246 shared_libs: ["libbaz"],
6247 apex_available: ["//apex_available:platform"],
6248 }
6249
6250 cc_library {
6251 name: "libbar",
6252 stl: "none",
6253 system_shared_libs: [],
6254 apex_available: ["myapex"],
6255 }
6256
6257 cc_library {
6258 name: "libbaz",
6259 stl: "none",
6260 system_shared_libs: [],
6261 apex_available: ["myapex"],
6262 stubs: {
6263 versions: ["1"],
6264 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006265 }`)
6266
Jiyong Park89e850a2020-04-07 16:37:39 +09006267 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6268 // because it depends on libbar which isn't available to platform
6269 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6270 if libfoo.NotAvailableForPlatform() != true {
6271 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6272 }
6273
6274 // libfoo2 however can be available to platform because it depends on libbaz which provides
6275 // stubs
6276 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6277 if libfoo2.NotAvailableForPlatform() == true {
6278 t.Errorf("%q should be available to platform", libfoo2.String())
6279 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006280}
Jiyong Parka90ca002019-10-07 15:47:24 +09006281
Paul Duffine52e66f2020-03-30 17:54:29 +01006282func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006283 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006284 apex {
6285 name: "myapex",
6286 key: "myapex.key",
6287 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006288 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006289 }
6290
6291 apex_key {
6292 name: "myapex.key",
6293 public_key: "testkey.avbpubkey",
6294 private_key: "testkey.pem",
6295 }
6296
6297 cc_library {
6298 name: "libfoo",
6299 stl: "none",
6300 system_shared_libs: [],
6301 apex_available: ["myapex"],
6302 static: {
6303 apex_available: ["//apex_available:platform"],
6304 },
6305 }`)
6306
Jiyong Park89e850a2020-04-07 16:37:39 +09006307 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6308 if libfooShared.NotAvailableForPlatform() != true {
6309 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6310 }
6311 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6312 if libfooStatic.NotAvailableForPlatform() != false {
6313 t.Errorf("%q should be available to platform", libfooStatic.String())
6314 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006315}
6316
Jiyong Park5d790c32019-11-15 18:40:32 +09006317func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006318 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006319 apex {
6320 name: "myapex",
6321 key: "myapex.key",
6322 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006323 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006324 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006325 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006326 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006327 }
6328
6329 override_apex {
6330 name: "override_myapex",
6331 base: "myapex",
6332 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006333 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006334 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006335 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006336 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006337 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006338 key: "mynewapex.key",
6339 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006340 }
6341
6342 apex_key {
6343 name: "myapex.key",
6344 public_key: "testkey.avbpubkey",
6345 private_key: "testkey.pem",
6346 }
6347
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006348 apex_key {
6349 name: "mynewapex.key",
6350 public_key: "testkey2.avbpubkey",
6351 private_key: "testkey2.pem",
6352 }
6353
6354 android_app_certificate {
6355 name: "myapex.certificate",
6356 certificate: "testkey",
6357 }
6358
Jiyong Park5d790c32019-11-15 18:40:32 +09006359 android_app {
6360 name: "app",
6361 srcs: ["foo/bar/MyClass.java"],
6362 package_name: "foo",
6363 sdk_version: "none",
6364 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006365 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006366 }
6367
6368 override_android_app {
6369 name: "override_app",
6370 base: "app",
6371 package_name: "bar",
6372 }
markchien7c803b82021-08-26 22:10:06 +08006373
6374 bpf {
6375 name: "bpf",
6376 srcs: ["bpf.c"],
6377 }
6378
6379 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006380 name: "overrideBpf",
6381 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006382 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006383
6384 prebuilt_etc {
6385 name: "myetc",
6386 src: "myprebuilt",
6387 }
6388
6389 prebuilt_etc {
6390 name: "override_myetc",
6391 src: "override_myprebuilt",
6392 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006393 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006394
Jiyong Park317645e2019-12-05 13:20:58 +09006395 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6396 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6397 if originalVariant.GetOverriddenBy() != "" {
6398 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6399 }
6400 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6401 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6402 }
6403
Jiyong Park5d790c32019-11-15 18:40:32 +09006404 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6405 apexRule := module.Rule("apexRule")
6406 copyCmds := apexRule.Args["copy_commands"]
6407
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006408 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6409 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006410
markchien7c803b82021-08-26 22:10:06 +08006411 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006412 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006413
Daniel Norman5a3ce132021-08-26 15:44:43 -07006414 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6415 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6416
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006417 apexBundle := module.Module().(*apexBundle)
6418 name := apexBundle.Name()
6419 if name != "override_myapex" {
6420 t.Errorf("name should be \"override_myapex\", but was %q", name)
6421 }
6422
Baligh Uddin004d7172020-02-19 21:29:28 -08006423 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6424 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6425 }
6426
Jiyong Park20bacab2020-03-03 11:45:41 +09006427 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006428 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006429 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6430
6431 signApkRule := module.Rule("signapk")
6432 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006433
Colin Crossaa255532020-07-03 13:18:24 -07006434 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006435 var builder strings.Builder
6436 data.Custom(&builder, name, "TARGET_", "", data)
6437 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006438 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6439 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6440 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006441 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006442 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006443 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006444 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006445 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006446 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6447 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006448}
6449
Albert Martineefabcf2022-03-21 20:11:16 +00006450func TestMinSdkVersionOverride(t *testing.T) {
6451 // Override from 29 to 31
6452 minSdkOverride31 := "31"
6453 ctx := testApex(t, `
6454 apex {
6455 name: "myapex",
6456 key: "myapex.key",
6457 native_shared_libs: ["mylib"],
6458 updatable: true,
6459 min_sdk_version: "29"
6460 }
6461
6462 override_apex {
6463 name: "override_myapex",
6464 base: "myapex",
6465 logging_parent: "com.foo.bar",
6466 package_name: "test.overridden.package"
6467 }
6468
6469 apex_key {
6470 name: "myapex.key",
6471 public_key: "testkey.avbpubkey",
6472 private_key: "testkey.pem",
6473 }
6474
6475 cc_library {
6476 name: "mylib",
6477 srcs: ["mylib.cpp"],
6478 runtime_libs: ["libbar"],
6479 system_shared_libs: [],
6480 stl: "none",
6481 apex_available: [ "myapex" ],
6482 min_sdk_version: "apex_inherit"
6483 }
6484
6485 cc_library {
6486 name: "libbar",
6487 srcs: ["mylib.cpp"],
6488 system_shared_libs: [],
6489 stl: "none",
6490 apex_available: [ "myapex" ],
6491 min_sdk_version: "apex_inherit"
6492 }
6493
6494 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6495
6496 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6497 copyCmds := apexRule.Args["copy_commands"]
6498
6499 // Ensure that direct non-stubs dep is always included
6500 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6501
6502 // Ensure that runtime_libs dep in included
6503 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6504
6505 // Ensure libraries target overridden min_sdk_version value
6506 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6507}
6508
6509func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6510 // Attempt to override from 31 to 29, should be a NOOP
6511 minSdkOverride29 := "29"
6512 ctx := testApex(t, `
6513 apex {
6514 name: "myapex",
6515 key: "myapex.key",
6516 native_shared_libs: ["mylib"],
6517 updatable: true,
6518 min_sdk_version: "31"
6519 }
6520
6521 override_apex {
6522 name: "override_myapex",
6523 base: "myapex",
6524 logging_parent: "com.foo.bar",
6525 package_name: "test.overridden.package"
6526 }
6527
6528 apex_key {
6529 name: "myapex.key",
6530 public_key: "testkey.avbpubkey",
6531 private_key: "testkey.pem",
6532 }
6533
6534 cc_library {
6535 name: "mylib",
6536 srcs: ["mylib.cpp"],
6537 runtime_libs: ["libbar"],
6538 system_shared_libs: [],
6539 stl: "none",
6540 apex_available: [ "myapex" ],
6541 min_sdk_version: "apex_inherit"
6542 }
6543
6544 cc_library {
6545 name: "libbar",
6546 srcs: ["mylib.cpp"],
6547 system_shared_libs: [],
6548 stl: "none",
6549 apex_available: [ "myapex" ],
6550 min_sdk_version: "apex_inherit"
6551 }
6552
6553 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6554
6555 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6556 copyCmds := apexRule.Args["copy_commands"]
6557
6558 // Ensure that direct non-stubs dep is always included
6559 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6560
6561 // Ensure that runtime_libs dep in included
6562 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6563
6564 // Ensure libraries target the original min_sdk_version value rather than the overridden
6565 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6566}
6567
Jooyung Han214bf372019-11-12 13:03:50 +09006568func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006569 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006570 apex {
6571 name: "myapex",
6572 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006573 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006574 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006575 }
6576
6577 apex_key {
6578 name: "myapex.key",
6579 public_key: "testkey.avbpubkey",
6580 private_key: "testkey.pem",
6581 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006582
6583 cc_library {
6584 name: "mylib",
6585 srcs: ["mylib.cpp"],
6586 stl: "libc++",
6587 system_shared_libs: [],
6588 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006589 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006590 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006591 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006592
6593 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6594 args := module.Rule("apexRule").Args
6595 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006596 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006597
6598 // The copies of the libraries in the apex should have one more dependency than
6599 // the ones outside the apex, namely the unwinder. Ideally we should check
6600 // the dependency names directly here but for some reason the names are blank in
6601 // this test.
6602 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006603 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006604 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6605 if len(apexImplicits) != len(nonApexImplicits)+1 {
6606 t.Errorf("%q missing unwinder dep", lib)
6607 }
6608 }
Jooyung Han214bf372019-11-12 13:03:50 +09006609}
6610
Paul Duffine05480a2021-03-08 15:07:14 +00006611var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006612 "api/current.txt": nil,
6613 "api/removed.txt": nil,
6614 "api/system-current.txt": nil,
6615 "api/system-removed.txt": nil,
6616 "api/test-current.txt": nil,
6617 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006618
Anton Hanssondff2c782020-12-21 17:10:01 +00006619 "100/public/api/foo.txt": nil,
6620 "100/public/api/foo-removed.txt": nil,
6621 "100/system/api/foo.txt": nil,
6622 "100/system/api/foo-removed.txt": nil,
6623
Paul Duffineedc5d52020-06-12 17:46:39 +01006624 // For java_sdk_library_import
6625 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006626}
6627
Jooyung Han58f26ab2019-12-18 15:34:32 +09006628func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006629 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006630 apex {
6631 name: "myapex",
6632 key: "myapex.key",
6633 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006634 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006635 }
6636
6637 apex_key {
6638 name: "myapex.key",
6639 public_key: "testkey.avbpubkey",
6640 private_key: "testkey.pem",
6641 }
6642
6643 java_sdk_library {
6644 name: "foo",
6645 srcs: ["a.java"],
6646 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006647 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006648 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006649
6650 prebuilt_apis {
6651 name: "sdk",
6652 api_dirs: ["100"],
6653 }
Paul Duffin9b879592020-05-26 13:21:35 +01006654 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006655
6656 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006657 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006658 "javalib/foo.jar",
6659 "etc/permissions/foo.xml",
6660 })
6661 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006662 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006663 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 +09006664}
6665
Paul Duffin9b879592020-05-26 13:21:35 +01006666func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006667 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006668 apex {
6669 name: "myapex",
6670 key: "myapex.key",
6671 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006672 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006673 }
6674
6675 apex_key {
6676 name: "myapex.key",
6677 public_key: "testkey.avbpubkey",
6678 private_key: "testkey.pem",
6679 }
6680
6681 java_sdk_library {
6682 name: "foo",
6683 srcs: ["a.java"],
6684 api_packages: ["foo"],
6685 apex_available: ["myapex"],
6686 sdk_version: "none",
6687 system_modules: "none",
6688 }
6689
6690 java_library {
6691 name: "bar",
6692 srcs: ["a.java"],
6693 libs: ["foo"],
6694 apex_available: ["myapex"],
6695 sdk_version: "none",
6696 system_modules: "none",
6697 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006698
6699 prebuilt_apis {
6700 name: "sdk",
6701 api_dirs: ["100"],
6702 }
Paul Duffin9b879592020-05-26 13:21:35 +01006703 `, withFiles(filesForSdkLibrary))
6704
6705 // java_sdk_library installs both impl jar and permission XML
6706 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6707 "javalib/bar.jar",
6708 "javalib/foo.jar",
6709 "etc/permissions/foo.xml",
6710 })
6711
6712 // The bar library should depend on the implementation jar.
6713 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006714 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006715 t.Errorf("expected %q, found %#q", expected, actual)
6716 }
6717}
6718
6719func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006720 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006721 apex {
6722 name: "myapex",
6723 key: "myapex.key",
6724 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006725 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006726 }
6727
6728 apex_key {
6729 name: "myapex.key",
6730 public_key: "testkey.avbpubkey",
6731 private_key: "testkey.pem",
6732 }
6733
6734 java_sdk_library {
6735 name: "foo",
6736 srcs: ["a.java"],
6737 api_packages: ["foo"],
6738 apex_available: ["myapex"],
6739 sdk_version: "none",
6740 system_modules: "none",
6741 }
6742
6743 java_library {
6744 name: "bar",
6745 srcs: ["a.java"],
6746 libs: ["foo"],
6747 sdk_version: "none",
6748 system_modules: "none",
6749 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006750
6751 prebuilt_apis {
6752 name: "sdk",
6753 api_dirs: ["100"],
6754 }
Paul Duffin9b879592020-05-26 13:21:35 +01006755 `, withFiles(filesForSdkLibrary))
6756
6757 // java_sdk_library installs both impl jar and permission XML
6758 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6759 "javalib/foo.jar",
6760 "etc/permissions/foo.xml",
6761 })
6762
6763 // The bar library should depend on the stubs jar.
6764 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006765 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006766 t.Errorf("expected %q, found %#q", expected, actual)
6767 }
6768}
6769
Paul Duffineedc5d52020-06-12 17:46:39 +01006770func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006771 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006772 prebuilt_apis {
6773 name: "sdk",
6774 api_dirs: ["100"],
6775 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006776 withFiles(map[string][]byte{
6777 "apex/a.java": nil,
6778 "apex/apex_manifest.json": nil,
6779 "apex/Android.bp": []byte(`
6780 package {
6781 default_visibility: ["//visibility:private"],
6782 }
6783
6784 apex {
6785 name: "myapex",
6786 key: "myapex.key",
6787 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006788 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006789 }
6790
6791 apex_key {
6792 name: "myapex.key",
6793 public_key: "testkey.avbpubkey",
6794 private_key: "testkey.pem",
6795 }
6796
6797 java_library {
6798 name: "bar",
6799 srcs: ["a.java"],
6800 libs: ["foo"],
6801 apex_available: ["myapex"],
6802 sdk_version: "none",
6803 system_modules: "none",
6804 }
6805`),
6806 "source/a.java": nil,
6807 "source/api/current.txt": nil,
6808 "source/api/removed.txt": nil,
6809 "source/Android.bp": []byte(`
6810 package {
6811 default_visibility: ["//visibility:private"],
6812 }
6813
6814 java_sdk_library {
6815 name: "foo",
6816 visibility: ["//apex"],
6817 srcs: ["a.java"],
6818 api_packages: ["foo"],
6819 apex_available: ["myapex"],
6820 sdk_version: "none",
6821 system_modules: "none",
6822 public: {
6823 enabled: true,
6824 },
6825 }
6826`),
6827 "prebuilt/a.jar": nil,
6828 "prebuilt/Android.bp": []byte(`
6829 package {
6830 default_visibility: ["//visibility:private"],
6831 }
6832
6833 java_sdk_library_import {
6834 name: "foo",
6835 visibility: ["//apex", "//source"],
6836 apex_available: ["myapex"],
6837 prefer: true,
6838 public: {
6839 jars: ["a.jar"],
6840 },
6841 }
6842`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006843 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006844 )
6845
6846 // java_sdk_library installs both impl jar and permission XML
6847 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6848 "javalib/bar.jar",
6849 "javalib/foo.jar",
6850 "etc/permissions/foo.xml",
6851 })
6852
6853 // The bar library should depend on the implementation jar.
6854 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006855 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006856 t.Errorf("expected %q, found %#q", expected, actual)
6857 }
6858}
6859
6860func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6861 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6862 apex {
6863 name: "myapex",
6864 key: "myapex.key",
6865 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006866 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006867 }
6868
6869 apex_key {
6870 name: "myapex.key",
6871 public_key: "testkey.avbpubkey",
6872 private_key: "testkey.pem",
6873 }
6874
6875 java_sdk_library_import {
6876 name: "foo",
6877 apex_available: ["myapex"],
6878 prefer: true,
6879 public: {
6880 jars: ["a.jar"],
6881 },
6882 }
6883
6884 `, withFiles(filesForSdkLibrary))
6885}
6886
atrost6e126252020-01-27 17:01:16 +00006887func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006888 result := android.GroupFixturePreparers(
6889 prepareForApexTest,
6890 java.PrepareForTestWithPlatformCompatConfig,
6891 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006892 apex {
6893 name: "myapex",
6894 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006895 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006896 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006897 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006898 }
6899
6900 apex_key {
6901 name: "myapex.key",
6902 public_key: "testkey.avbpubkey",
6903 private_key: "testkey.pem",
6904 }
6905
6906 platform_compat_config {
6907 name: "myjar-platform-compat-config",
6908 src: ":myjar",
6909 }
6910
6911 java_library {
6912 name: "myjar",
6913 srcs: ["foo/bar/MyClass.java"],
6914 sdk_version: "none",
6915 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006916 apex_available: [ "myapex" ],
6917 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006918
6919 // Make sure that a preferred prebuilt does not affect the apex contents.
6920 prebuilt_platform_compat_config {
6921 name: "myjar-platform-compat-config",
6922 metadata: "compat-config/metadata.xml",
6923 prefer: true,
6924 }
atrost6e126252020-01-27 17:01:16 +00006925 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006926 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006927 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6928 "etc/compatconfig/myjar-platform-compat-config.xml",
6929 "javalib/myjar.jar",
6930 })
6931}
6932
Jooyung Han862c0d62022-12-21 10:15:37 +09006933func TestNoDupeApexFiles(t *testing.T) {
6934 android.GroupFixturePreparers(
6935 android.PrepareForTestWithAndroidBuildComponents,
6936 PrepareForTestWithApexBuildComponents,
6937 prepareForTestWithMyapex,
6938 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
6939 ).
6940 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
6941 RunTestWithBp(t, `
6942 apex {
6943 name: "myapex",
6944 key: "myapex.key",
6945 prebuilts: ["foo", "bar"],
6946 updatable: false,
6947 }
6948
6949 apex_key {
6950 name: "myapex.key",
6951 public_key: "testkey.avbpubkey",
6952 private_key: "testkey.pem",
6953 }
6954
6955 prebuilt_etc {
6956 name: "foo",
6957 src: "myprebuilt",
6958 filename_from_src: true,
6959 }
6960
6961 prebuilt_etc {
6962 name: "bar",
6963 src: "myprebuilt",
6964 filename_from_src: true,
6965 }
6966 `)
6967}
6968
Jiyong Park479321d2019-12-16 11:47:12 +09006969func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6970 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6971 apex {
6972 name: "myapex",
6973 key: "myapex.key",
6974 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006975 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006976 }
6977
6978 apex_key {
6979 name: "myapex.key",
6980 public_key: "testkey.avbpubkey",
6981 private_key: "testkey.pem",
6982 }
6983
6984 java_library {
6985 name: "myjar",
6986 srcs: ["foo/bar/MyClass.java"],
6987 sdk_version: "none",
6988 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006989 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006990 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006991 }
6992 `)
6993}
6994
Jiyong Park7afd1072019-12-30 16:56:33 +09006995func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006996 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006997 apex {
6998 name: "myapex",
6999 key: "myapex.key",
7000 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007001 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007002 }
7003
7004 apex_key {
7005 name: "myapex.key",
7006 public_key: "testkey.avbpubkey",
7007 private_key: "testkey.pem",
7008 }
7009
7010 cc_library {
7011 name: "mylib",
7012 srcs: ["mylib.cpp"],
7013 system_shared_libs: [],
7014 stl: "none",
7015 required: ["a", "b"],
7016 host_required: ["c", "d"],
7017 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007018 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007019 }
7020 `)
7021
7022 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007023 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007024 name := apexBundle.BaseModuleName()
7025 prefix := "TARGET_"
7026 var builder strings.Builder
7027 data.Custom(&builder, name, prefix, "", data)
7028 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007029 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 -08007030 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7031 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007032}
7033
Jiyong Park7cd10e32020-01-14 09:22:18 +09007034func TestSymlinksFromApexToSystem(t *testing.T) {
7035 bp := `
7036 apex {
7037 name: "myapex",
7038 key: "myapex.key",
7039 native_shared_libs: ["mylib"],
7040 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007041 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007042 }
7043
Jiyong Park9d677202020-02-19 16:29:35 +09007044 apex {
7045 name: "myapex.updatable",
7046 key: "myapex.key",
7047 native_shared_libs: ["mylib"],
7048 java_libs: ["myjar"],
7049 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09007050 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09007051 }
7052
Jiyong Park7cd10e32020-01-14 09:22:18 +09007053 apex_key {
7054 name: "myapex.key",
7055 public_key: "testkey.avbpubkey",
7056 private_key: "testkey.pem",
7057 }
7058
7059 cc_library {
7060 name: "mylib",
7061 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007062 shared_libs: [
7063 "myotherlib",
7064 "myotherlib_ext",
7065 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007066 system_shared_libs: [],
7067 stl: "none",
7068 apex_available: [
7069 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007070 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007071 "//apex_available:platform",
7072 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007073 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007074 }
7075
7076 cc_library {
7077 name: "myotherlib",
7078 srcs: ["mylib.cpp"],
7079 system_shared_libs: [],
7080 stl: "none",
7081 apex_available: [
7082 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007083 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007084 "//apex_available:platform",
7085 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007086 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007087 }
7088
Jiyong Parkce243632023-02-17 18:22:25 +09007089 cc_library {
7090 name: "myotherlib_ext",
7091 srcs: ["mylib.cpp"],
7092 system_shared_libs: [],
7093 system_ext_specific: true,
7094 stl: "none",
7095 apex_available: [
7096 "myapex",
7097 "myapex.updatable",
7098 "//apex_available:platform",
7099 ],
7100 min_sdk_version: "current",
7101 }
7102
Jiyong Park7cd10e32020-01-14 09:22:18 +09007103 java_library {
7104 name: "myjar",
7105 srcs: ["foo/bar/MyClass.java"],
7106 sdk_version: "none",
7107 system_modules: "none",
7108 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007109 apex_available: [
7110 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007111 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007112 "//apex_available:platform",
7113 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007114 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007115 }
7116
7117 java_library {
7118 name: "myotherjar",
7119 srcs: ["foo/bar/MyClass.java"],
7120 sdk_version: "none",
7121 system_modules: "none",
7122 apex_available: [
7123 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007124 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007125 "//apex_available:platform",
7126 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007127 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007128 }
7129 `
7130
7131 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7132 for _, f := range files {
7133 if f.path == file {
7134 if f.isLink {
7135 t.Errorf("%q is not a real file", file)
7136 }
7137 return
7138 }
7139 }
7140 t.Errorf("%q is not found", file)
7141 }
7142
Jiyong Parkce243632023-02-17 18:22:25 +09007143 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007144 for _, f := range files {
7145 if f.path == file {
7146 if !f.isLink {
7147 t.Errorf("%q is not a symlink", file)
7148 }
Jiyong Parkce243632023-02-17 18:22:25 +09007149 if f.src != target {
7150 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7151 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007152 return
7153 }
7154 }
7155 t.Errorf("%q is not found", file)
7156 }
7157
Jiyong Park9d677202020-02-19 16:29:35 +09007158 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7159 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007160 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007161 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007162 ensureRealfileExists(t, files, "javalib/myjar.jar")
7163 ensureRealfileExists(t, files, "lib64/mylib.so")
7164 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007165 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007166
Jiyong Park9d677202020-02-19 16:29:35 +09007167 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7168 ensureRealfileExists(t, files, "javalib/myjar.jar")
7169 ensureRealfileExists(t, files, "lib64/mylib.so")
7170 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007171 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007172
7173 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007174 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007175 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007176 ensureRealfileExists(t, files, "javalib/myjar.jar")
7177 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007178 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7179 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007180
7181 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7182 ensureRealfileExists(t, files, "javalib/myjar.jar")
7183 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007184 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7185 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007186}
7187
Yo Chiange8128052020-07-23 20:09:18 +08007188func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007189 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007190 apex {
7191 name: "myapex",
7192 key: "myapex.key",
7193 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007194 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007195 }
7196
7197 apex_key {
7198 name: "myapex.key",
7199 public_key: "testkey.avbpubkey",
7200 private_key: "testkey.pem",
7201 }
7202
7203 cc_library_shared {
7204 name: "mylib",
7205 srcs: ["mylib.cpp"],
7206 shared_libs: ["myotherlib"],
7207 system_shared_libs: [],
7208 stl: "none",
7209 apex_available: [
7210 "myapex",
7211 "//apex_available:platform",
7212 ],
7213 }
7214
7215 cc_prebuilt_library_shared {
7216 name: "myotherlib",
7217 srcs: ["prebuilt.so"],
7218 system_shared_libs: [],
7219 stl: "none",
7220 apex_available: [
7221 "myapex",
7222 "//apex_available:platform",
7223 ],
7224 }
7225 `)
7226
Prerana Patilb1896c82022-11-09 18:14:34 +00007227 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007228 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007229 var builder strings.Builder
7230 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7231 androidMk := builder.String()
7232 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007233 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007234 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7235 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7236 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007237 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 +08007238}
7239
Jooyung Han643adc42020-02-27 13:50:06 +09007240func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007241 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007242 apex {
7243 name: "myapex",
7244 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007245 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007246 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007247 }
7248
7249 apex_key {
7250 name: "myapex.key",
7251 public_key: "testkey.avbpubkey",
7252 private_key: "testkey.pem",
7253 }
7254
7255 cc_library {
7256 name: "mylib",
7257 srcs: ["mylib.cpp"],
7258 shared_libs: ["mylib2"],
7259 system_shared_libs: [],
7260 stl: "none",
7261 apex_available: [ "myapex" ],
7262 }
7263
7264 cc_library {
7265 name: "mylib2",
7266 srcs: ["mylib.cpp"],
7267 system_shared_libs: [],
7268 stl: "none",
7269 apex_available: [ "myapex" ],
7270 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007271
7272 rust_ffi_shared {
7273 name: "libfoo.rust",
7274 crate_name: "foo",
7275 srcs: ["foo.rs"],
7276 shared_libs: ["libfoo.shared_from_rust"],
7277 prefer_rlib: true,
7278 apex_available: ["myapex"],
7279 }
7280
7281 cc_library_shared {
7282 name: "libfoo.shared_from_rust",
7283 srcs: ["mylib.cpp"],
7284 system_shared_libs: [],
7285 stl: "none",
7286 stubs: {
7287 versions: ["10", "11", "12"],
7288 },
7289 }
7290
Jooyung Han643adc42020-02-27 13:50:06 +09007291 `)
7292
7293 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7294 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007295 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007296 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7297 "lib64/mylib.so",
7298 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007299 "lib64/libfoo.rust.so",
7300 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7301 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007302 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007303
7304 // b/220397949
7305 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007306}
7307
Jooyung Han49f67012020-04-17 13:43:10 +09007308func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007309 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007310 apex {
7311 name: "myapex",
7312 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007313 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007314 }
7315 apex_key {
7316 name: "myapex.key",
7317 public_key: "testkey.avbpubkey",
7318 private_key: "testkey.pem",
7319 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007320 `,
7321 android.FixtureModifyConfig(func(config android.Config) {
7322 delete(config.Targets, android.Android)
7323 config.AndroidCommonTarget = android.Target{}
7324 }),
7325 )
Jooyung Han49f67012020-04-17 13:43:10 +09007326
7327 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7328 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7329 }
7330}
7331
Jiyong Parkbd159612020-02-28 15:22:21 +09007332func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007333 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007334 apex {
7335 name: "myapex",
7336 key: "myapex.key",
7337 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007338 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007339 }
7340
7341 apex_key {
7342 name: "myapex.key",
7343 public_key: "testkey.avbpubkey",
7344 private_key: "testkey.pem",
7345 }
7346
7347 android_app {
7348 name: "AppFoo",
7349 srcs: ["foo/bar/MyClass.java"],
7350 sdk_version: "none",
7351 system_modules: "none",
7352 apex_available: [ "myapex" ],
7353 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007354 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007355
Colin Crosscf371cc2020-11-13 11:48:42 -08007356 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007357 content := bundleConfigRule.Args["content"]
7358
7359 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007360 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 +09007361}
7362
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007363func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007364 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007365 apex {
7366 name: "myapex",
7367 key: "myapex.key",
7368 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007369 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007370 }
7371
7372 apex_key {
7373 name: "myapex.key",
7374 public_key: "testkey.avbpubkey",
7375 private_key: "testkey.pem",
7376 }
7377
7378 android_app_set {
7379 name: "AppSet",
7380 set: "AppSet.apks",
7381 }`)
7382 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007383 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007384 content := bundleConfigRule.Args["content"]
7385 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7386 s := mod.Rule("apexRule").Args["copy_commands"]
7387 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007388 if len(copyCmds) != 4 {
7389 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007390 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007391 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7392 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007393 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7394 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007395
7396 // Ensure that canned_fs_config has an entry for the app set zip file
7397 generateFsRule := mod.Rule("generateFsConfig")
7398 cmd := generateFsRule.RuleParams.Command
7399 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007400}
7401
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007402func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007403 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007404 apex_set {
7405 name: "myapex",
7406 filename: "foo_v2.apex",
7407 sanitized: {
7408 none: { set: "myapex.apks", },
7409 hwaddress: { set: "myapex.hwasan.apks", },
7410 },
Paul Duffin24704672021-04-06 16:09:30 +01007411 }
7412 `
7413 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007414
Paul Duffin24704672021-04-06 16:09:30 +01007415 // Check that the extractor produces the correct output file from the correct input file.
7416 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007417
Paul Duffin24704672021-04-06 16:09:30 +01007418 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7419 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007420
Paul Duffin24704672021-04-06 16:09:30 +01007421 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7422
7423 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007424 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7425 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007426
7427 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007428}
7429
Pranav Guptaeba03b02022-09-27 00:27:08 +00007430func TestApexSetApksModuleAssignment(t *testing.T) {
7431 ctx := testApex(t, `
7432 apex_set {
7433 name: "myapex",
7434 set: ":myapex_apks_file",
7435 }
7436
7437 filegroup {
7438 name: "myapex_apks_file",
7439 srcs: ["myapex.apks"],
7440 }
7441 `)
7442
7443 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7444
7445 // Check that the extractor produces the correct apks file from the input module
7446 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7447 extractedApex := m.Output(extractorOutput)
7448
7449 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7450}
7451
Paul Duffin89f570a2021-06-16 01:42:33 +01007452func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007453 t.Helper()
7454
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007455 bp := `
7456 java_library {
7457 name: "some-updatable-apex-lib",
7458 srcs: ["a.java"],
7459 sdk_version: "current",
7460 apex_available: [
7461 "some-updatable-apex",
7462 ],
satayevabcd5972021-08-06 17:49:46 +01007463 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007464 }
7465
7466 java_library {
7467 name: "some-non-updatable-apex-lib",
7468 srcs: ["a.java"],
7469 apex_available: [
7470 "some-non-updatable-apex",
7471 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007472 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007473 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007474 }
7475
7476 bootclasspath_fragment {
7477 name: "some-non-updatable-fragment",
7478 contents: ["some-non-updatable-apex-lib"],
7479 apex_available: [
7480 "some-non-updatable-apex",
7481 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007482 hidden_api: {
7483 split_packages: ["*"],
7484 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007485 }
7486
7487 java_library {
7488 name: "some-platform-lib",
7489 srcs: ["a.java"],
7490 sdk_version: "current",
7491 installable: true,
7492 }
7493
7494 java_library {
7495 name: "some-art-lib",
7496 srcs: ["a.java"],
7497 sdk_version: "current",
7498 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007499 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007500 ],
7501 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007502 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007503 }
7504
7505 apex {
7506 name: "some-updatable-apex",
7507 key: "some-updatable-apex.key",
7508 java_libs: ["some-updatable-apex-lib"],
7509 updatable: true,
7510 min_sdk_version: "current",
7511 }
7512
7513 apex {
7514 name: "some-non-updatable-apex",
7515 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007516 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007517 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007518 }
7519
7520 apex_key {
7521 name: "some-updatable-apex.key",
7522 }
7523
7524 apex_key {
7525 name: "some-non-updatable-apex.key",
7526 }
7527
7528 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007529 name: "com.android.art.debug",
7530 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007531 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007532 updatable: true,
7533 min_sdk_version: "current",
7534 }
7535
Paul Duffinf23bc472021-04-27 12:42:20 +01007536 bootclasspath_fragment {
7537 name: "art-bootclasspath-fragment",
7538 image_name: "art",
7539 contents: ["some-art-lib"],
7540 apex_available: [
7541 "com.android.art.debug",
7542 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007543 hidden_api: {
7544 split_packages: ["*"],
7545 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007546 }
7547
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007548 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007549 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007550 }
7551
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007552 filegroup {
7553 name: "some-updatable-apex-file_contexts",
7554 srcs: [
7555 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7556 ],
7557 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007558
7559 filegroup {
7560 name: "some-non-updatable-apex-file_contexts",
7561 srcs: [
7562 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7563 ],
7564 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007565 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007566
Paul Duffin89f570a2021-06-16 01:42:33 +01007567 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007568}
7569
Paul Duffin89f570a2021-06-16 01:42:33 +01007570func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007571 t.Helper()
7572
Paul Duffin55607122021-03-30 23:32:51 +01007573 fs := android.MockFS{
7574 "a.java": nil,
7575 "a.jar": nil,
7576 "apex_manifest.json": nil,
7577 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007578 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007579 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7580 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7581 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007582 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007583 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007584
Paul Duffin55607122021-03-30 23:32:51 +01007585 errorHandler := android.FixtureExpectsNoErrors
7586 if errmsg != "" {
7587 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007588 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007589
Paul Duffin55607122021-03-30 23:32:51 +01007590 result := android.GroupFixturePreparers(
7591 cc.PrepareForTestWithCcDefaultModules,
7592 java.PrepareForTestWithHiddenApiBuildComponents,
7593 java.PrepareForTestWithJavaDefaultModules,
7594 java.PrepareForTestWithJavaSdkLibraryFiles,
7595 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007596 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007597 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007598 android.FixtureModifyMockFS(func(fs android.MockFS) {
7599 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7600 insert := ""
7601 for _, fragment := range fragments {
7602 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7603 }
7604 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7605 platform_bootclasspath {
7606 name: "platform-bootclasspath",
7607 fragments: [
7608 %s
7609 ],
7610 }
7611 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007612 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007613 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007614 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007615 ).
7616 ExtendWithErrorHandler(errorHandler).
7617 RunTestWithBp(t, bp)
7618
7619 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007620}
7621
Paul Duffin5556c5f2022-06-09 17:32:21 +00007622func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007623 preparers := android.GroupFixturePreparers(
7624 java.PrepareForTestWithJavaDefaultModules,
7625 PrepareForTestWithApexBuildComponents,
7626 ).
7627 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7628 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7629
7630 bpBase := `
7631 apex_set {
7632 name: "com.android.myapex",
7633 installable: true,
7634 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7635 set: "myapex.apks",
7636 }
7637
7638 apex_set {
7639 name: "com.mycompany.android.myapex",
7640 apex_name: "com.android.myapex",
7641 installable: true,
7642 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7643 set: "company-myapex.apks",
7644 }
7645
7646 prebuilt_bootclasspath_fragment {
7647 name: "my-bootclasspath-fragment",
7648 apex_available: ["com.android.myapex"],
7649 %s
7650 }
7651 `
7652
7653 t.Run("java_import", func(t *testing.T) {
7654 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7655 java_import {
7656 name: "libfoo",
7657 jars: ["libfoo.jar"],
7658 apex_available: ["com.android.myapex"],
7659 }
7660 `)
7661 })
7662
7663 t.Run("java_sdk_library_import", func(t *testing.T) {
7664 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7665 java_sdk_library_import {
7666 name: "libfoo",
7667 public: {
7668 jars: ["libbar.jar"],
7669 },
7670 apex_available: ["com.android.myapex"],
7671 }
7672 `)
7673 })
7674
7675 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7676 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7677 image_name: "art",
7678 contents: ["libfoo"],
7679 `)+`
7680 java_sdk_library_import {
7681 name: "libfoo",
7682 public: {
7683 jars: ["libbar.jar"],
7684 },
7685 apex_available: ["com.android.myapex"],
7686 }
7687 `)
7688 })
7689}
7690
Paul Duffin5556c5f2022-06-09 17:32:21 +00007691func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7692 preparers := android.GroupFixturePreparers(
7693 java.PrepareForTestWithJavaDefaultModules,
7694 PrepareForTestWithApexBuildComponents,
7695 )
7696
7697 bpBase := `
7698 apex_set {
7699 name: "com.android.myapex",
7700 installable: true,
7701 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7702 set: "myapex.apks",
7703 }
7704
7705 apex_set {
7706 name: "com.android.myapex_compressed",
7707 apex_name: "com.android.myapex",
7708 installable: true,
7709 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7710 set: "myapex_compressed.apks",
7711 }
7712
7713 prebuilt_bootclasspath_fragment {
7714 name: "my-bootclasspath-fragment",
7715 apex_available: [
7716 "com.android.myapex",
7717 "com.android.myapex_compressed",
7718 ],
7719 hidden_api: {
7720 annotation_flags: "annotation-flags.csv",
7721 metadata: "metadata.csv",
7722 index: "index.csv",
7723 signature_patterns: "signature_patterns.csv",
7724 },
7725 %s
7726 }
7727 `
7728
7729 t.Run("java_import", func(t *testing.T) {
7730 result := preparers.RunTestWithBp(t,
7731 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7732 java_import {
7733 name: "libfoo",
7734 jars: ["libfoo.jar"],
7735 apex_available: [
7736 "com.android.myapex",
7737 "com.android.myapex_compressed",
7738 ],
7739 }
7740 `)
7741
7742 module := result.Module("libfoo", "android_common_com.android.myapex")
7743 usesLibraryDep := module.(java.UsesLibraryDependency)
7744 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7745 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7746 usesLibraryDep.DexJarBuildPath().Path())
7747 })
7748
7749 t.Run("java_sdk_library_import", func(t *testing.T) {
7750 result := preparers.RunTestWithBp(t,
7751 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7752 java_sdk_library_import {
7753 name: "libfoo",
7754 public: {
7755 jars: ["libbar.jar"],
7756 },
7757 apex_available: [
7758 "com.android.myapex",
7759 "com.android.myapex_compressed",
7760 ],
7761 compile_dex: true,
7762 }
7763 `)
7764
7765 module := result.Module("libfoo", "android_common_com.android.myapex")
7766 usesLibraryDep := module.(java.UsesLibraryDependency)
7767 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7768 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7769 usesLibraryDep.DexJarBuildPath().Path())
7770 })
7771
7772 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7773 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7774 image_name: "art",
7775 contents: ["libfoo"],
7776 `)+`
7777 java_sdk_library_import {
7778 name: "libfoo",
7779 public: {
7780 jars: ["libbar.jar"],
7781 },
7782 apex_available: [
7783 "com.android.myapex",
7784 "com.android.myapex_compressed",
7785 ],
7786 compile_dex: true,
7787 }
7788 `)
7789 })
7790}
7791
Jooyung Han548640b2020-04-27 12:10:30 +09007792func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7793 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7794 apex {
7795 name: "myapex",
7796 key: "myapex.key",
7797 updatable: true,
7798 }
7799
7800 apex_key {
7801 name: "myapex.key",
7802 public_key: "testkey.avbpubkey",
7803 private_key: "testkey.pem",
7804 }
7805 `)
7806}
7807
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007808func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7809 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7810 apex {
7811 name: "myapex",
7812 key: "myapex.key",
7813 }
7814
7815 apex_key {
7816 name: "myapex.key",
7817 public_key: "testkey.avbpubkey",
7818 private_key: "testkey.pem",
7819 }
7820 `)
7821}
7822
Daniel Norman69109112021-12-02 12:52:42 -08007823func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7824 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7825 apex {
7826 name: "myapex",
7827 key: "myapex.key",
7828 updatable: true,
7829 soc_specific: true,
7830 }
7831
7832 apex_key {
7833 name: "myapex.key",
7834 public_key: "testkey.avbpubkey",
7835 private_key: "testkey.pem",
7836 }
7837 `)
7838}
7839
satayevb98371c2021-06-15 16:49:50 +01007840func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7841 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7842 apex {
7843 name: "myapex",
7844 key: "myapex.key",
7845 systemserverclasspath_fragments: [
7846 "mysystemserverclasspathfragment",
7847 ],
7848 min_sdk_version: "29",
7849 updatable: true,
7850 }
7851
7852 apex_key {
7853 name: "myapex.key",
7854 public_key: "testkey.avbpubkey",
7855 private_key: "testkey.pem",
7856 }
7857
7858 java_library {
7859 name: "foo",
7860 srcs: ["b.java"],
7861 min_sdk_version: "29",
7862 installable: true,
7863 apex_available: [
7864 "myapex",
7865 ],
7866 }
7867
7868 systemserverclasspath_fragment {
7869 name: "mysystemserverclasspathfragment",
7870 generate_classpaths_proto: false,
7871 contents: [
7872 "foo",
7873 ],
7874 apex_available: [
7875 "myapex",
7876 ],
7877 }
satayevabcd5972021-08-06 17:49:46 +01007878 `,
7879 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7880 )
satayevb98371c2021-06-15 16:49:50 +01007881}
7882
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007883func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007884 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7885 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7886 // modules to be included in the BootJars.
7887 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7888 return android.GroupFixturePreparers(
7889 dexpreopt.FixtureSetBootJars(bootJars...),
7890 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7891 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7892 }),
7893 )
7894 }
7895
7896 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7897 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7898 // specified in the ArtApexJars configuration.
7899 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7900 return android.GroupFixturePreparers(
7901 dexpreopt.FixtureSetArtBootJars(bootJars...),
7902 dexpreopt.FixtureSetBootJars(bootJars...),
7903 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7904 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7905 }),
7906 )
7907 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007908
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007909 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007910 preparer := android.GroupFixturePreparers(
7911 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7912 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7913 )
7914 fragments := []java.ApexVariantReference{
7915 {
7916 Apex: proptools.StringPtr("com.android.art.debug"),
7917 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7918 },
7919 {
7920 Apex: proptools.StringPtr("some-non-updatable-apex"),
7921 Module: proptools.StringPtr("some-non-updatable-fragment"),
7922 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007923 }
satayevabcd5972021-08-06 17:49:46 +01007924 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007925 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007926
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007927 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007928 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7929 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007930 preparer := android.GroupFixturePreparers(
7931 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7932 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7933 )
Paul Duffin60264a02021-04-12 20:02:36 +01007934 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007935 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007936
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007937 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 +01007938 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 +01007939 // Update the dexpreopt ArtApexJars directly.
7940 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7941 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007942 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007943
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007944 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 +01007945 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 +01007946 // Update the dexpreopt ArtApexJars directly.
7947 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7948 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007949 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007950
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007951 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 +01007952 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 +01007953 preparer := android.GroupFixturePreparers(
7954 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7955 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7956 )
Paul Duffin60264a02021-04-12 20:02:36 +01007957 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007958 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007959
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007960 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 +01007961 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007962 fragment := java.ApexVariantReference{
7963 Apex: proptools.StringPtr("some-non-updatable-apex"),
7964 Module: proptools.StringPtr("some-non-updatable-fragment"),
7965 }
7966 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007967 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007968
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007969 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007970 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007971 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7972 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007973 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007974
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007975 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007976 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007977 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7978 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007979 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007980
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007981 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007982 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007983 // Update the dexpreopt ArtApexJars directly.
7984 preparer := prepareSetArtJars("platform:some-platform-lib")
7985 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007986 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007987
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007988 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007989 preparer := android.GroupFixturePreparers(
7990 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7991 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7992 )
7993 fragments := []java.ApexVariantReference{
7994 {
7995 Apex: proptools.StringPtr("some-non-updatable-apex"),
7996 Module: proptools.StringPtr("some-non-updatable-fragment"),
7997 },
7998 }
7999 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008000 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008001}
8002
8003func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008004 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008005 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008006 fragment := java.ApexVariantReference{
8007 Apex: proptools.StringPtr("myapex"),
8008 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8009 }
8010
Paul Duffin064b70c2020-11-02 17:32:38 +00008011 testDexpreoptWithApexes(t, `
8012 prebuilt_apex {
8013 name: "myapex" ,
8014 arch: {
8015 arm64: {
8016 src: "myapex-arm64.apex",
8017 },
8018 arm: {
8019 src: "myapex-arm.apex",
8020 },
8021 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008022 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8023 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008024
Paul Duffin89f570a2021-06-16 01:42:33 +01008025 prebuilt_bootclasspath_fragment {
8026 name: "my-bootclasspath-fragment",
8027 contents: ["libfoo"],
8028 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008029 hidden_api: {
8030 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8031 metadata: "my-bootclasspath-fragment/metadata.csv",
8032 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008033 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8034 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8035 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008036 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008037 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008038
Paul Duffin89f570a2021-06-16 01:42:33 +01008039 java_import {
8040 name: "libfoo",
8041 jars: ["libfoo.jar"],
8042 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008043 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008044 }
8045 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008046 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008047}
8048
Spandan Dasf14e2542021-11-12 00:01:37 +00008049func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008050 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008051 bp += `
8052 apex_key {
8053 name: "myapex.key",
8054 public_key: "testkey.avbpubkey",
8055 private_key: "testkey.pem",
8056 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008057 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008058 "lib1/src/A.java": nil,
8059 "lib2/src/B.java": nil,
8060 "system/sepolicy/apex/myapex-file_contexts": nil,
8061 }
8062
Paul Duffin45338f02021-03-30 23:07:52 +01008063 errorHandler := android.FixtureExpectsNoErrors
8064 if errmsg != "" {
8065 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008066 }
Colin Crossae8600b2020-10-29 17:09:13 -07008067
Paul Duffin45338f02021-03-30 23:07:52 +01008068 android.GroupFixturePreparers(
8069 android.PrepareForTestWithAndroidBuildComponents,
8070 java.PrepareForTestWithJavaBuildComponents,
8071 PrepareForTestWithApexBuildComponents,
8072 android.PrepareForTestWithNeverallowRules(rules),
8073 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008074 apexBootJars := make([]string, 0, len(bootJars))
8075 for _, apexBootJar := range bootJars {
8076 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008077 }
satayevd604b212021-07-21 14:23:52 +01008078 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008079 }),
8080 fs.AddToFixture(),
8081 ).
8082 ExtendWithErrorHandler(errorHandler).
8083 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008084}
8085
8086func TestApexPermittedPackagesRules(t *testing.T) {
8087 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008088 name string
8089 expectedError string
8090 bp string
8091 bootJars []string
8092 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008093 }{
8094
8095 {
8096 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8097 expectedError: "",
8098 bp: `
8099 java_library {
8100 name: "bcp_lib1",
8101 srcs: ["lib1/src/*.java"],
8102 permitted_packages: ["foo.bar"],
8103 apex_available: ["myapex"],
8104 sdk_version: "none",
8105 system_modules: "none",
8106 }
8107 java_library {
8108 name: "nonbcp_lib2",
8109 srcs: ["lib2/src/*.java"],
8110 apex_available: ["myapex"],
8111 permitted_packages: ["a.b"],
8112 sdk_version: "none",
8113 system_modules: "none",
8114 }
8115 apex {
8116 name: "myapex",
8117 key: "myapex.key",
8118 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008119 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008120 }`,
8121 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008122 bcpPermittedPackages: map[string][]string{
8123 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008124 "foo.bar",
8125 },
8126 },
8127 },
8128 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008129 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008130 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 +01008131 bp: `
8132 java_library {
8133 name: "bcp_lib1",
8134 srcs: ["lib1/src/*.java"],
8135 apex_available: ["myapex"],
8136 permitted_packages: ["foo.bar"],
8137 sdk_version: "none",
8138 system_modules: "none",
8139 }
8140 java_library {
8141 name: "bcp_lib2",
8142 srcs: ["lib2/src/*.java"],
8143 apex_available: ["myapex"],
8144 permitted_packages: ["foo.bar", "bar.baz"],
8145 sdk_version: "none",
8146 system_modules: "none",
8147 }
8148 apex {
8149 name: "myapex",
8150 key: "myapex.key",
8151 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008152 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008153 }
8154 `,
8155 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008156 bcpPermittedPackages: map[string][]string{
8157 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008158 "foo.bar",
8159 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008160 "bcp_lib2": []string{
8161 "foo.bar",
8162 },
8163 },
8164 },
8165 {
8166 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8167 expectedError: "",
8168 bp: `
8169 java_library {
8170 name: "bcp_lib_restricted",
8171 srcs: ["lib1/src/*.java"],
8172 apex_available: ["myapex"],
8173 permitted_packages: ["foo.bar"],
8174 sdk_version: "none",
8175 min_sdk_version: "29",
8176 system_modules: "none",
8177 }
8178 java_library {
8179 name: "bcp_lib_unrestricted",
8180 srcs: ["lib2/src/*.java"],
8181 apex_available: ["myapex"],
8182 permitted_packages: ["foo.bar", "bar.baz"],
8183 sdk_version: "none",
8184 min_sdk_version: "29",
8185 system_modules: "none",
8186 }
8187 apex {
8188 name: "myapex",
8189 key: "myapex.key",
8190 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8191 updatable: true,
8192 min_sdk_version: "29",
8193 }
8194 `,
8195 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8196 bcpPermittedPackages: map[string][]string{
8197 "bcp_lib1_non_updateable": []string{
8198 "foo.bar",
8199 },
8200 // 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 +01008201 },
8202 },
8203 }
8204 for _, tc := range testcases {
8205 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008206 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8207 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008208 })
8209 }
8210}
8211
Jiyong Park62304bb2020-04-13 16:19:48 +09008212func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008213 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008214 apex {
8215 name: "myapex",
8216 key: "myapex.key",
8217 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008218 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008219 }
8220
8221 apex_key {
8222 name: "myapex.key",
8223 public_key: "testkey.avbpubkey",
8224 private_key: "testkey.pem",
8225 }
8226
8227 cc_library {
8228 name: "mylib",
8229 srcs: ["mylib.cpp"],
8230 system_shared_libs: [],
8231 stl: "none",
8232 stubs: {
8233 versions: ["1"],
8234 },
8235 apex_available: ["myapex"],
8236 }
8237
8238 cc_library {
8239 name: "myprivlib",
8240 srcs: ["mylib.cpp"],
8241 system_shared_libs: [],
8242 stl: "none",
8243 apex_available: ["myapex"],
8244 }
8245
8246
8247 cc_test {
8248 name: "mytest",
8249 gtest: false,
8250 srcs: ["mylib.cpp"],
8251 system_shared_libs: [],
8252 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008253 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008254 test_for: ["myapex"]
8255 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008256
8257 cc_library {
8258 name: "mytestlib",
8259 srcs: ["mylib.cpp"],
8260 system_shared_libs: [],
8261 shared_libs: ["mylib", "myprivlib"],
8262 stl: "none",
8263 test_for: ["myapex"],
8264 }
8265
8266 cc_benchmark {
8267 name: "mybench",
8268 srcs: ["mylib.cpp"],
8269 system_shared_libs: [],
8270 shared_libs: ["mylib", "myprivlib"],
8271 stl: "none",
8272 test_for: ["myapex"],
8273 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008274 `)
8275
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008276 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008277 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008278 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8279 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8280 }
8281
8282 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008283 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008284 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8285 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8286 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8287}
Jiyong Park46a512f2020-12-04 18:02:13 +09008288
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008289func TestIndirectTestFor(t *testing.T) {
8290 ctx := testApex(t, `
8291 apex {
8292 name: "myapex",
8293 key: "myapex.key",
8294 native_shared_libs: ["mylib", "myprivlib"],
8295 updatable: false,
8296 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008297
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008298 apex_key {
8299 name: "myapex.key",
8300 public_key: "testkey.avbpubkey",
8301 private_key: "testkey.pem",
8302 }
8303
8304 cc_library {
8305 name: "mylib",
8306 srcs: ["mylib.cpp"],
8307 system_shared_libs: [],
8308 stl: "none",
8309 stubs: {
8310 versions: ["1"],
8311 },
8312 apex_available: ["myapex"],
8313 }
8314
8315 cc_library {
8316 name: "myprivlib",
8317 srcs: ["mylib.cpp"],
8318 system_shared_libs: [],
8319 stl: "none",
8320 shared_libs: ["mylib"],
8321 apex_available: ["myapex"],
8322 }
8323
8324 cc_library {
8325 name: "mytestlib",
8326 srcs: ["mylib.cpp"],
8327 system_shared_libs: [],
8328 shared_libs: ["myprivlib"],
8329 stl: "none",
8330 test_for: ["myapex"],
8331 }
8332 `)
8333
8334 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008335 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008336 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8337 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8338 }
8339
8340 // The platform variant of mytestlib links to the platform variant of the
8341 // internal myprivlib.
8342 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8343
8344 // The platform variant of myprivlib links to the platform variant of mylib
8345 // and bypasses its stubs.
8346 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 +09008347}
8348
Martin Stjernholmec009002021-03-27 15:18:31 +00008349func TestTestForForLibInOtherApex(t *testing.T) {
8350 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8351 _ = testApex(t, `
8352 apex {
8353 name: "com.android.art",
8354 key: "myapex.key",
8355 native_shared_libs: ["mylib"],
8356 updatable: false,
8357 }
8358
8359 apex {
8360 name: "com.android.art.debug",
8361 key: "myapex.key",
8362 native_shared_libs: ["mylib", "mytestlib"],
8363 updatable: false,
8364 }
8365
8366 apex_key {
8367 name: "myapex.key",
8368 public_key: "testkey.avbpubkey",
8369 private_key: "testkey.pem",
8370 }
8371
8372 cc_library {
8373 name: "mylib",
8374 srcs: ["mylib.cpp"],
8375 system_shared_libs: [],
8376 stl: "none",
8377 stubs: {
8378 versions: ["1"],
8379 },
8380 apex_available: ["com.android.art", "com.android.art.debug"],
8381 }
8382
8383 cc_library {
8384 name: "mytestlib",
8385 srcs: ["mylib.cpp"],
8386 system_shared_libs: [],
8387 shared_libs: ["mylib"],
8388 stl: "none",
8389 apex_available: ["com.android.art.debug"],
8390 test_for: ["com.android.art"],
8391 }
8392 `,
8393 android.MockFS{
8394 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8395 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8396 }.AddToFixture())
8397}
8398
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008399// TODO(jungjw): Move this to proptools
8400func intPtr(i int) *int {
8401 return &i
8402}
8403
8404func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008405 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008406 apex_set {
8407 name: "myapex",
8408 set: "myapex.apks",
8409 filename: "foo_v2.apex",
8410 overrides: ["foo"],
8411 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008412 `,
8413 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8414 variables.Platform_sdk_version = intPtr(30)
8415 }),
8416 android.FixtureModifyConfig(func(config android.Config) {
8417 config.Targets[android.Android] = []android.Target{
8418 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8419 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8420 }
8421 }),
8422 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008423
Paul Duffin24704672021-04-06 16:09:30 +01008424 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008425
8426 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008427 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008428 actual := extractedApex.Args["abis"]
8429 expected := "ARMEABI_V7A,ARM64_V8A"
8430 if actual != expected {
8431 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8432 }
8433 actual = extractedApex.Args["sdk-version"]
8434 expected = "30"
8435 if actual != expected {
8436 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8437 }
8438
Paul Duffin6717d882021-06-15 19:09:41 +01008439 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008440 a := m.Module().(*ApexSet)
8441 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008442 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008443 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8444 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8445 }
8446}
8447
Anton Hansson805e0a52022-11-25 14:06:46 +00008448func TestApexSet_NativeBridge(t *testing.T) {
8449 ctx := testApex(t, `
8450 apex_set {
8451 name: "myapex",
8452 set: "myapex.apks",
8453 filename: "foo_v2.apex",
8454 overrides: ["foo"],
8455 }
8456 `,
8457 android.FixtureModifyConfig(func(config android.Config) {
8458 config.Targets[android.Android] = []android.Target{
8459 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8460 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8461 }
8462 }),
8463 )
8464
8465 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8466
8467 // Check extract_apks tool parameters. No native bridge arch expected
8468 extractedApex := m.Output("extracted/myapex.apks")
8469 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8470}
8471
Jiyong Park7d95a512020-05-10 15:16:24 +09008472func TestNoStaticLinkingToStubsLib(t *testing.T) {
8473 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8474 apex {
8475 name: "myapex",
8476 key: "myapex.key",
8477 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008478 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008479 }
8480
8481 apex_key {
8482 name: "myapex.key",
8483 public_key: "testkey.avbpubkey",
8484 private_key: "testkey.pem",
8485 }
8486
8487 cc_library {
8488 name: "mylib",
8489 srcs: ["mylib.cpp"],
8490 static_libs: ["otherlib"],
8491 system_shared_libs: [],
8492 stl: "none",
8493 apex_available: [ "myapex" ],
8494 }
8495
8496 cc_library {
8497 name: "otherlib",
8498 srcs: ["mylib.cpp"],
8499 system_shared_libs: [],
8500 stl: "none",
8501 stubs: {
8502 versions: ["1", "2", "3"],
8503 },
8504 apex_available: [ "myapex" ],
8505 }
8506 `)
8507}
8508
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008509func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008510 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008511 apex {
8512 name: "myapex",
8513 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008514 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008515 custom_sign_tool: "sign_myapex",
8516 }
8517
8518 apex_key {
8519 name: "myapex.key",
8520 public_key: "testkey.avbpubkey",
8521 private_key: "testkey.pem",
8522 }
8523 `)
8524
8525 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8526 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8527 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"`)
8528}
8529
8530func TestApexKeysTxtOverrides(t *testing.T) {
8531 ctx := testApex(t, `
8532 apex {
8533 name: "myapex",
8534 key: "myapex.key",
8535 updatable: false,
8536 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008537 }
8538
8539 apex_key {
8540 name: "myapex.key",
8541 public_key: "testkey.avbpubkey",
8542 private_key: "testkey.pem",
8543 }
8544
8545 prebuilt_apex {
8546 name: "myapex",
8547 prefer: true,
8548 arch: {
8549 arm64: {
8550 src: "myapex-arm64.apex",
8551 },
8552 arm: {
8553 src: "myapex-arm.apex",
8554 },
8555 },
8556 }
8557
8558 apex_set {
8559 name: "myapex_set",
8560 set: "myapex.apks",
8561 filename: "myapex_set.apex",
8562 overrides: ["myapex"],
8563 }
8564 `)
8565
8566 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8567 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8568 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 +09008569 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 +09008570}
8571
Jooyung Han938b5932020-06-20 12:47:47 +09008572func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008573 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008574 apex {
8575 name: "myapex",
8576 key: "myapex.key",
8577 apps: ["app"],
8578 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008579 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008580 }
8581
8582 apex_key {
8583 name: "myapex.key",
8584 public_key: "testkey.avbpubkey",
8585 private_key: "testkey.pem",
8586 }
8587
8588 android_app {
8589 name: "app",
8590 srcs: ["foo/bar/MyClass.java"],
8591 package_name: "foo",
8592 sdk_version: "none",
8593 system_modules: "none",
8594 apex_available: [ "myapex" ],
8595 }
8596 `, withFiles(map[string][]byte{
8597 "sub/Android.bp": []byte(`
8598 override_apex {
8599 name: "override_myapex",
8600 base: "myapex",
8601 apps: ["override_app"],
8602 allowed_files: ":allowed",
8603 }
8604 // Overridable "path" property should be referenced indirectly
8605 filegroup {
8606 name: "allowed",
8607 srcs: ["allowed.txt"],
8608 }
8609 override_android_app {
8610 name: "override_app",
8611 base: "app",
8612 package_name: "bar",
8613 }
8614 `),
8615 }))
8616
8617 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8618 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8619 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8620 }
8621
8622 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8623 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8624 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8625 }
8626}
8627
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008628func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008629 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008630 apex {
8631 name: "myapex",
8632 key: "myapex.key",
8633 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008634 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008635 }
8636
8637 apex_key {
8638 name: "myapex.key",
8639 public_key: "testkey.avbpubkey",
8640 private_key: "testkey.pem",
8641 }
8642
8643 cc_library {
8644 name: "mylib",
8645 srcs: ["mylib.cpp"],
8646 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008647 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008648 },
8649 apex_available: ["myapex"],
8650 }
8651
8652 cc_prebuilt_library_shared {
8653 name: "mylib",
8654 prefer: false,
8655 srcs: ["prebuilt.so"],
8656 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008657 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008658 },
8659 apex_available: ["myapex"],
8660 }
8661 `)
8662}
8663
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008664func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008665 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008666 apex {
8667 name: "myapex",
8668 key: "myapex.key",
8669 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008670 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008671 }
8672 apex_key {
8673 name: "myapex.key",
8674 public_key: "testkey.avbpubkey",
8675 private_key: "testkey.pem",
8676 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008677 `,
8678 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8679 variables.CompressedApex = proptools.BoolPtr(true)
8680 }),
8681 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008682
8683 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8684 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8685
8686 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8687 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8688
8689 // Make sure output of bundle is .capex
8690 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8691 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8692
8693 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008694 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008695 var builder strings.Builder
8696 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8697 androidMk := builder.String()
8698 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8699}
8700
Martin Stjernholm2856c662020-12-02 15:03:42 +00008701func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008702 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008703 apex {
8704 name: "myapex",
8705 key: "myapex.key",
8706 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008707 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008708 }
8709
8710 apex_key {
8711 name: "myapex.key",
8712 public_key: "testkey.avbpubkey",
8713 private_key: "testkey.pem",
8714 }
8715
8716 cc_library {
8717 name: "mylib",
8718 srcs: ["mylib.cpp"],
8719 apex_available: ["myapex"],
8720 shared_libs: ["otherlib"],
8721 system_shared_libs: [],
8722 }
8723
8724 cc_library {
8725 name: "otherlib",
8726 srcs: ["mylib.cpp"],
8727 stubs: {
8728 versions: ["current"],
8729 },
8730 }
8731
8732 cc_prebuilt_library_shared {
8733 name: "otherlib",
8734 prefer: true,
8735 srcs: ["prebuilt.so"],
8736 stubs: {
8737 versions: ["current"],
8738 },
8739 }
8740 `)
8741
8742 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008743 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008744 var builder strings.Builder
8745 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8746 androidMk := builder.String()
8747
8748 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8749 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008750 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 +00008751}
8752
Jiyong Parke3867542020-12-03 17:28:25 +09008753func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008754 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008755 apex {
8756 name: "myapex",
8757 key: "myapex.key",
8758 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008759 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008760 }
8761
8762 apex_key {
8763 name: "myapex.key",
8764 public_key: "testkey.avbpubkey",
8765 private_key: "testkey.pem",
8766 }
8767
8768 cc_library {
8769 name: "mylib",
8770 srcs: ["mylib.cpp"],
8771 system_shared_libs: [],
8772 stl: "none",
8773 apex_available: ["myapex"],
8774 shared_libs: ["mylib2"],
8775 target: {
8776 apex: {
8777 exclude_shared_libs: ["mylib2"],
8778 },
8779 },
8780 }
8781
8782 cc_library {
8783 name: "mylib2",
8784 srcs: ["mylib.cpp"],
8785 system_shared_libs: [],
8786 stl: "none",
8787 }
8788 `)
8789
8790 // Check if mylib is linked to mylib2 for the non-apex target
8791 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8792 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8793
8794 // Make sure that the link doesn't occur for the apex target
8795 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8796 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8797
8798 // It shouldn't appear in the copy cmd as well.
8799 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8800 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8801}
8802
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008803func TestPrebuiltStubLibDep(t *testing.T) {
8804 bpBase := `
8805 apex {
8806 name: "myapex",
8807 key: "myapex.key",
8808 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008809 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008810 }
8811 apex_key {
8812 name: "myapex.key",
8813 public_key: "testkey.avbpubkey",
8814 private_key: "testkey.pem",
8815 }
8816 cc_library {
8817 name: "mylib",
8818 srcs: ["mylib.cpp"],
8819 apex_available: ["myapex"],
8820 shared_libs: ["stublib"],
8821 system_shared_libs: [],
8822 }
8823 apex {
8824 name: "otherapex",
8825 enabled: %s,
8826 key: "myapex.key",
8827 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008828 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008829 }
8830 `
8831
8832 stublibSourceBp := `
8833 cc_library {
8834 name: "stublib",
8835 srcs: ["mylib.cpp"],
8836 apex_available: ["otherapex"],
8837 system_shared_libs: [],
8838 stl: "none",
8839 stubs: {
8840 versions: ["1"],
8841 },
8842 }
8843 `
8844
8845 stublibPrebuiltBp := `
8846 cc_prebuilt_library_shared {
8847 name: "stublib",
8848 srcs: ["prebuilt.so"],
8849 apex_available: ["otherapex"],
8850 stubs: {
8851 versions: ["1"],
8852 },
8853 %s
8854 }
8855 `
8856
8857 tests := []struct {
8858 name string
8859 stublibBp string
8860 usePrebuilt bool
8861 modNames []string // Modules to collect AndroidMkEntries for
8862 otherApexEnabled []string
8863 }{
8864 {
8865 name: "only_source",
8866 stublibBp: stublibSourceBp,
8867 usePrebuilt: false,
8868 modNames: []string{"stublib"},
8869 otherApexEnabled: []string{"true", "false"},
8870 },
8871 {
8872 name: "source_preferred",
8873 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8874 usePrebuilt: false,
8875 modNames: []string{"stublib", "prebuilt_stublib"},
8876 otherApexEnabled: []string{"true", "false"},
8877 },
8878 {
8879 name: "prebuilt_preferred",
8880 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8881 usePrebuilt: true,
8882 modNames: []string{"stublib", "prebuilt_stublib"},
8883 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8884 },
8885 {
8886 name: "only_prebuilt",
8887 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8888 usePrebuilt: true,
8889 modNames: []string{"stublib"},
8890 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8891 },
8892 }
8893
8894 for _, test := range tests {
8895 t.Run(test.name, func(t *testing.T) {
8896 for _, otherApexEnabled := range test.otherApexEnabled {
8897 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008898 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008899
8900 type modAndMkEntries struct {
8901 mod *cc.Module
8902 mkEntries android.AndroidMkEntries
8903 }
8904 entries := []*modAndMkEntries{}
8905
8906 // Gather shared lib modules that are installable
8907 for _, modName := range test.modNames {
8908 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8909 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8910 continue
8911 }
8912 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008913 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008914 continue
8915 }
Colin Crossaa255532020-07-03 13:18:24 -07008916 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008917 if ent.Disabled {
8918 continue
8919 }
8920 entries = append(entries, &modAndMkEntries{
8921 mod: mod,
8922 mkEntries: ent,
8923 })
8924 }
8925 }
8926 }
8927
8928 var entry *modAndMkEntries = nil
8929 for _, ent := range entries {
8930 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8931 if entry != nil {
8932 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8933 } else {
8934 entry = ent
8935 }
8936 }
8937 }
8938
8939 if entry == nil {
8940 t.Errorf("AndroidMk entry for \"stublib\" missing")
8941 } else {
8942 isPrebuilt := entry.mod.Prebuilt() != nil
8943 if isPrebuilt != test.usePrebuilt {
8944 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8945 }
8946 if !entry.mod.IsStubs() {
8947 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8948 }
8949 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8950 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8951 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008952 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008953 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008954 if !android.InList(expected, cflags) {
8955 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8956 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008957 }
8958 })
8959 }
8960 })
8961 }
8962}
8963
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008964func TestHostApexInHostOnlyBuild(t *testing.T) {
8965 testApex(t, `
8966 apex {
8967 name: "myapex",
8968 host_supported: true,
8969 key: "myapex.key",
8970 updatable: false,
8971 payload_type: "zip",
8972 }
8973 apex_key {
8974 name: "myapex.key",
8975 public_key: "testkey.avbpubkey",
8976 private_key: "testkey.pem",
8977 }
8978 `,
8979 android.FixtureModifyConfig(func(config android.Config) {
8980 // We may not have device targets in all builds, e.g. in
8981 // prebuilts/build-tools/build-prebuilts.sh
8982 config.Targets[android.Android] = []android.Target{}
8983 }))
8984}
8985
Colin Crossc33e5212021-05-25 18:16:02 -07008986func TestApexJavaCoverage(t *testing.T) {
8987 bp := `
8988 apex {
8989 name: "myapex",
8990 key: "myapex.key",
8991 java_libs: ["mylib"],
8992 bootclasspath_fragments: ["mybootclasspathfragment"],
8993 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8994 updatable: false,
8995 }
8996
8997 apex_key {
8998 name: "myapex.key",
8999 public_key: "testkey.avbpubkey",
9000 private_key: "testkey.pem",
9001 }
9002
9003 java_library {
9004 name: "mylib",
9005 srcs: ["mylib.java"],
9006 apex_available: ["myapex"],
9007 compile_dex: true,
9008 }
9009
9010 bootclasspath_fragment {
9011 name: "mybootclasspathfragment",
9012 contents: ["mybootclasspathlib"],
9013 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009014 hidden_api: {
9015 split_packages: ["*"],
9016 },
Colin Crossc33e5212021-05-25 18:16:02 -07009017 }
9018
9019 java_library {
9020 name: "mybootclasspathlib",
9021 srcs: ["mybootclasspathlib.java"],
9022 apex_available: ["myapex"],
9023 compile_dex: true,
9024 }
9025
9026 systemserverclasspath_fragment {
9027 name: "mysystemserverclasspathfragment",
9028 contents: ["mysystemserverclasspathlib"],
9029 apex_available: ["myapex"],
9030 }
9031
9032 java_library {
9033 name: "mysystemserverclasspathlib",
9034 srcs: ["mysystemserverclasspathlib.java"],
9035 apex_available: ["myapex"],
9036 compile_dex: true,
9037 }
9038 `
9039
9040 result := android.GroupFixturePreparers(
9041 PrepareForTestWithApexBuildComponents,
9042 prepareForTestWithMyapex,
9043 java.PrepareForTestWithJavaDefaultModules,
9044 android.PrepareForTestWithAndroidBuildComponents,
9045 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009046 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9047 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009048 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009049 ).RunTest(t)
9050
9051 // Make sure jacoco ran on both mylib and mybootclasspathlib
9052 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9053 t.Errorf("Failed to find jacoco rule for mylib")
9054 }
9055 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9056 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9057 }
9058 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9059 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9060 }
9061}
9062
Jiyong Park192600a2021-08-03 07:52:17 +00009063func TestProhibitStaticExecutable(t *testing.T) {
9064 testApexError(t, `executable mybin is static`, `
9065 apex {
9066 name: "myapex",
9067 key: "myapex.key",
9068 binaries: ["mybin"],
9069 min_sdk_version: "29",
9070 }
9071
9072 apex_key {
9073 name: "myapex.key",
9074 public_key: "testkey.avbpubkey",
9075 private_key: "testkey.pem",
9076 }
9077
9078 cc_binary {
9079 name: "mybin",
9080 srcs: ["mylib.cpp"],
9081 relative_install_path: "foo/bar",
9082 static_executable: true,
9083 system_shared_libs: [],
9084 stl: "none",
9085 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009086 min_sdk_version: "29",
9087 }
9088 `)
9089
9090 testApexError(t, `executable mybin.rust is static`, `
9091 apex {
9092 name: "myapex",
9093 key: "myapex.key",
9094 binaries: ["mybin.rust"],
9095 min_sdk_version: "29",
9096 }
9097
9098 apex_key {
9099 name: "myapex.key",
9100 public_key: "testkey.avbpubkey",
9101 private_key: "testkey.pem",
9102 }
9103
9104 rust_binary {
9105 name: "mybin.rust",
9106 srcs: ["foo.rs"],
9107 static_executable: true,
9108 apex_available: ["myapex"],
9109 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009110 }
9111 `)
9112}
9113
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009114func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9115 ctx := testApex(t, `
9116 apex {
9117 name: "myapex",
9118 key: "myapex.key",
9119 updatable: false,
9120 java_libs: ["foo"],
9121 }
9122
9123 apex_key {
9124 name: "myapex.key",
9125 public_key: "testkey.avbpubkey",
9126 private_key: "testkey.pem",
9127 }
9128
9129 java_library {
9130 name: "foo",
9131 srcs: ["foo.java"],
9132 apex_available: ["myapex"],
9133 installable: true,
9134 }
9135 `,
9136 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9137 )
9138
9139 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9140 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9141 var builder strings.Builder
9142 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9143 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009144 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 +00009145}
9146
9147func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9148 ctx := testApex(t, `
9149 prebuilt_apex {
9150 name: "myapex",
9151 arch: {
9152 arm64: {
9153 src: "myapex-arm64.apex",
9154 },
9155 arm: {
9156 src: "myapex-arm.apex",
9157 },
9158 },
9159 exported_java_libs: ["foo"],
9160 }
9161
9162 java_import {
9163 name: "foo",
9164 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009165 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009166 }
9167 `,
9168 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9169 )
9170
9171 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9172 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9173 mainModuleEntries := entriesList[0]
9174 android.AssertArrayString(t,
9175 "LOCAL_REQUIRED_MODULES",
9176 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9177 []string{
9178 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9179 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9180 })
9181}
9182
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009183func TestAndroidMk_RequiredModules(t *testing.T) {
9184 ctx := testApex(t, `
9185 apex {
9186 name: "myapex",
9187 key: "myapex.key",
9188 updatable: false,
9189 java_libs: ["foo"],
9190 required: ["otherapex"],
9191 }
9192
9193 apex {
9194 name: "otherapex",
9195 key: "myapex.key",
9196 updatable: false,
9197 java_libs: ["foo"],
9198 required: ["otherapex"],
9199 }
9200
9201 apex_key {
9202 name: "myapex.key",
9203 public_key: "testkey.avbpubkey",
9204 private_key: "testkey.pem",
9205 }
9206
9207 java_library {
9208 name: "foo",
9209 srcs: ["foo.java"],
9210 apex_available: ["myapex", "otherapex"],
9211 installable: true,
9212 }
9213 `)
9214
9215 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9216 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9217 var builder strings.Builder
9218 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9219 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009220 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009221}
9222
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009223func TestAndroidMk_RequiredDeps(t *testing.T) {
9224 ctx := testApex(t, `
9225 apex {
9226 name: "myapex",
9227 key: "myapex.key",
9228 updatable: false,
9229 }
9230
9231 apex_key {
9232 name: "myapex.key",
9233 public_key: "testkey.avbpubkey",
9234 private_key: "testkey.pem",
9235 }
9236 `)
9237
9238 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009239 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009240 data := android.AndroidMkDataForTest(t, ctx, bundle)
9241 var builder strings.Builder
9242 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9243 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009244 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009245
9246 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009247 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009248 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9249 var flattenedBuilder strings.Builder
9250 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9251 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009252 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009253}
9254
Jooyung Hana6d36672022-02-24 13:58:07 +09009255func TestApexOutputFileProducer(t *testing.T) {
9256 for _, tc := range []struct {
9257 name string
9258 ref string
9259 expected_data []string
9260 }{
9261 {
9262 name: "test_using_output",
9263 ref: ":myapex",
9264 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9265 },
9266 {
9267 name: "test_using_apex",
9268 ref: ":myapex{.apex}",
9269 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9270 },
9271 } {
9272 t.Run(tc.name, func(t *testing.T) {
9273 ctx := testApex(t, `
9274 apex {
9275 name: "myapex",
9276 key: "myapex.key",
9277 compressible: true,
9278 updatable: false,
9279 }
9280
9281 apex_key {
9282 name: "myapex.key",
9283 public_key: "testkey.avbpubkey",
9284 private_key: "testkey.pem",
9285 }
9286
9287 java_test {
9288 name: "`+tc.name+`",
9289 srcs: ["a.java"],
9290 data: ["`+tc.ref+`"],
9291 }
9292 `,
9293 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9294 variables.CompressedApex = proptools.BoolPtr(true)
9295 }))
9296 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9297 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9298 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9299 })
9300 }
9301}
9302
satayev758968a2021-12-06 11:42:40 +00009303func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9304 preparer := android.GroupFixturePreparers(
9305 PrepareForTestWithApexBuildComponents,
9306 prepareForTestWithMyapex,
9307 java.PrepareForTestWithJavaSdkLibraryFiles,
9308 java.PrepareForTestWithJavaDefaultModules,
9309 android.PrepareForTestWithAndroidBuildComponents,
9310 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9311 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9312 )
9313
9314 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9315 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9316 preparer.RunTestWithBp(t, `
9317 apex {
9318 name: "myapex",
9319 key: "myapex.key",
9320 bootclasspath_fragments: ["mybootclasspathfragment"],
9321 min_sdk_version: "30",
9322 updatable: false,
9323 }
9324
9325 apex_key {
9326 name: "myapex.key",
9327 public_key: "testkey.avbpubkey",
9328 private_key: "testkey.pem",
9329 }
9330
9331 bootclasspath_fragment {
9332 name: "mybootclasspathfragment",
9333 contents: ["mybootclasspathlib"],
9334 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009335 hidden_api: {
9336 split_packages: ["*"],
9337 },
satayev758968a2021-12-06 11:42:40 +00009338 }
9339
9340 java_sdk_library {
9341 name: "mybootclasspathlib",
9342 srcs: ["mybootclasspathlib.java"],
9343 apex_available: ["myapex"],
9344 compile_dex: true,
9345 unsafe_ignore_missing_latest_api: true,
9346 min_sdk_version: "31",
9347 static_libs: ["util"],
9348 }
9349
9350 java_library {
9351 name: "util",
9352 srcs: ["a.java"],
9353 apex_available: ["myapex"],
9354 min_sdk_version: "31",
9355 static_libs: ["another_util"],
9356 }
9357
9358 java_library {
9359 name: "another_util",
9360 srcs: ["a.java"],
9361 min_sdk_version: "31",
9362 apex_available: ["myapex"],
9363 }
9364 `)
9365 })
9366
9367 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9368 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9369 preparer.RunTestWithBp(t, `
9370 apex {
9371 name: "myapex",
9372 key: "myapex.key",
9373 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9374 min_sdk_version: "30",
9375 updatable: false,
9376 }
9377
9378 apex_key {
9379 name: "myapex.key",
9380 public_key: "testkey.avbpubkey",
9381 private_key: "testkey.pem",
9382 }
9383
9384 systemserverclasspath_fragment {
9385 name: "mysystemserverclasspathfragment",
9386 contents: ["mysystemserverclasspathlib"],
9387 apex_available: ["myapex"],
9388 }
9389
9390 java_sdk_library {
9391 name: "mysystemserverclasspathlib",
9392 srcs: ["mysystemserverclasspathlib.java"],
9393 apex_available: ["myapex"],
9394 compile_dex: true,
9395 min_sdk_version: "32",
9396 unsafe_ignore_missing_latest_api: true,
9397 static_libs: ["util"],
9398 }
9399
9400 java_library {
9401 name: "util",
9402 srcs: ["a.java"],
9403 apex_available: ["myapex"],
9404 min_sdk_version: "31",
9405 static_libs: ["another_util"],
9406 }
9407
9408 java_library {
9409 name: "another_util",
9410 srcs: ["a.java"],
9411 min_sdk_version: "31",
9412 apex_available: ["myapex"],
9413 }
9414 `)
9415 })
9416
9417 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9418 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9419 RunTestWithBp(t, `
9420 apex {
9421 name: "myapex",
9422 key: "myapex.key",
9423 bootclasspath_fragments: ["mybootclasspathfragment"],
9424 min_sdk_version: "30",
9425 updatable: false,
9426 }
9427
9428 apex_key {
9429 name: "myapex.key",
9430 public_key: "testkey.avbpubkey",
9431 private_key: "testkey.pem",
9432 }
9433
9434 bootclasspath_fragment {
9435 name: "mybootclasspathfragment",
9436 contents: ["mybootclasspathlib"],
9437 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009438 hidden_api: {
9439 split_packages: ["*"],
9440 },
satayev758968a2021-12-06 11:42:40 +00009441 }
9442
9443 java_sdk_library {
9444 name: "mybootclasspathlib",
9445 srcs: ["mybootclasspathlib.java"],
9446 apex_available: ["myapex"],
9447 compile_dex: true,
9448 unsafe_ignore_missing_latest_api: true,
9449 }
9450 `)
9451 })
9452
9453 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9454 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9455 RunTestWithBp(t, `
9456 apex {
9457 name: "myapex",
9458 key: "myapex.key",
9459 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9460 min_sdk_version: "30",
9461 updatable: false,
9462 }
9463
9464 apex_key {
9465 name: "myapex.key",
9466 public_key: "testkey.avbpubkey",
9467 private_key: "testkey.pem",
9468 }
9469
9470 systemserverclasspath_fragment {
9471 name: "mysystemserverclasspathfragment",
9472 contents: ["mysystemserverclasspathlib"],
9473 apex_available: ["myapex"],
9474 }
9475
9476 java_sdk_library {
9477 name: "mysystemserverclasspathlib",
9478 srcs: ["mysystemserverclasspathlib.java"],
9479 apex_available: ["myapex"],
9480 compile_dex: true,
9481 unsafe_ignore_missing_latest_api: true,
9482 }
9483 `)
9484 })
9485}
9486
Jiakai Zhang6decef92022-01-12 17:56:19 +00009487// Verifies that the APEX depends on all the Make modules in the list.
9488func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9489 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9490 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009491 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009492 }
9493}
9494
9495// Verifies that the APEX does not depend on any of the Make modules in the list.
9496func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9497 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9498 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009499 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009500 }
9501}
9502
Cole Faust1021ccd2023-02-26 21:15:25 -08009503// TODO(b/193460475): Re-enable this test
9504//func TestApexStrictUpdtabilityLint(t *testing.T) {
9505// bpTemplate := `
9506// apex {
9507// name: "myapex",
9508// key: "myapex.key",
9509// java_libs: ["myjavalib"],
9510// updatable: %v,
9511// min_sdk_version: "29",
9512// }
9513// apex_key {
9514// name: "myapex.key",
9515// }
9516// java_library {
9517// name: "myjavalib",
9518// srcs: ["MyClass.java"],
9519// apex_available: [ "myapex" ],
9520// lint: {
9521// strict_updatability_linting: %v,
9522// },
9523// sdk_version: "current",
9524// min_sdk_version: "29",
9525// }
9526// `
9527// fs := android.MockFS{
9528// "lint-baseline.xml": nil,
9529// }
9530//
9531// testCases := []struct {
9532// testCaseName string
9533// apexUpdatable bool
9534// javaStrictUpdtabilityLint bool
9535// lintFileExists bool
9536// disallowedFlagExpected bool
9537// }{
9538// {
9539// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9540// apexUpdatable: true,
9541// javaStrictUpdtabilityLint: true,
9542// lintFileExists: false,
9543// disallowedFlagExpected: false,
9544// },
9545// {
9546// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9547// apexUpdatable: false,
9548// javaStrictUpdtabilityLint: false,
9549// lintFileExists: true,
9550// disallowedFlagExpected: false,
9551// },
9552// {
9553// testCaseName: "non-updatable apex respects strict updatability of javalib",
9554// apexUpdatable: false,
9555// javaStrictUpdtabilityLint: true,
9556// lintFileExists: true,
9557// disallowedFlagExpected: true,
9558// },
9559// {
9560// testCaseName: "updatable apex sets strict updatability of javalib to true",
9561// apexUpdatable: true,
9562// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9563// lintFileExists: true,
9564// disallowedFlagExpected: true,
9565// },
9566// }
9567//
9568// for _, testCase := range testCases {
9569// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9570// fixtures := []android.FixturePreparer{}
9571// if testCase.lintFileExists {
9572// fixtures = append(fixtures, fs.AddToFixture())
9573// }
9574//
9575// result := testApex(t, bp, fixtures...)
9576// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9577// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9578// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9579//
9580// if disallowedFlagActual != testCase.disallowedFlagExpected {
9581// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9582// }
9583// }
9584//}
9585//
9586//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9587// bp := `
9588// apex {
9589// name: "myapex",
9590// key: "myapex.key",
9591// java_libs: ["myjavalib"],
9592// updatable: true,
9593// min_sdk_version: "29",
9594// }
9595// apex_key {
9596// name: "myapex.key",
9597// }
9598// java_library {
9599// name: "myjavalib",
9600// srcs: ["MyClass.java"],
9601// apex_available: [ "myapex" ],
9602// sdk_version: "current",
9603// min_sdk_version: "29",
9604// }
9605// `
9606//
9607// testCases := []struct {
9608// testCaseName string
9609// moduleDirectory string
9610// disallowedFlagExpected bool
9611// }{
9612// {
9613// testCaseName: "lintable module defined outside libcore",
9614// moduleDirectory: "",
9615// disallowedFlagExpected: true,
9616// },
9617// {
9618// testCaseName: "lintable module defined in libcore root directory",
9619// moduleDirectory: "libcore/",
9620// disallowedFlagExpected: false,
9621// },
9622// {
9623// testCaseName: "lintable module defined in libcore child directory",
9624// moduleDirectory: "libcore/childdir/",
9625// disallowedFlagExpected: true,
9626// },
9627// }
9628//
9629// for _, testCase := range testCases {
9630// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9631// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9632// result := testApex(t, "", lintFileCreator, bpFileCreator)
9633// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9634// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9635// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9636// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9637//
9638// if disallowedFlagActual != testCase.disallowedFlagExpected {
9639// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9640// }
9641// }
9642//}
9643//
9644//// checks transtive deps of an apex coming from bootclasspath_fragment
9645//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9646// bp := `
9647// apex {
9648// name: "myapex",
9649// key: "myapex.key",
9650// bootclasspath_fragments: ["mybootclasspathfragment"],
9651// updatable: true,
9652// min_sdk_version: "29",
9653// }
9654// apex_key {
9655// name: "myapex.key",
9656// }
9657// bootclasspath_fragment {
9658// name: "mybootclasspathfragment",
9659// contents: ["myjavalib"],
9660// apex_available: ["myapex"],
9661// hidden_api: {
9662// split_packages: ["*"],
9663// },
9664// }
9665// java_library {
9666// name: "myjavalib",
9667// srcs: ["MyClass.java"],
9668// apex_available: [ "myapex" ],
9669// sdk_version: "current",
9670// min_sdk_version: "29",
9671// compile_dex: true,
9672// }
9673// `
9674// fs := android.MockFS{
9675// "lint-baseline.xml": nil,
9676// }
9677//
9678// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9679// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9680// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9681// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9682// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9683// }
9684//}
Spandan Das66773252022-01-15 00:23:18 +00009685
Spandan Das42e89502022-05-06 22:12:55 +00009686// updatable apexes should propagate updatable=true to its apps
9687func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9688 bp := `
9689 apex {
9690 name: "myapex",
9691 key: "myapex.key",
9692 updatable: %v,
9693 apps: [
9694 "myapp",
9695 ],
9696 min_sdk_version: "30",
9697 }
9698 apex_key {
9699 name: "myapex.key",
9700 }
9701 android_app {
9702 name: "myapp",
9703 updatable: %v,
9704 apex_available: [
9705 "myapex",
9706 ],
9707 sdk_version: "current",
9708 min_sdk_version: "30",
9709 }
9710 `
9711 testCases := []struct {
9712 name string
9713 apex_is_updatable_bp bool
9714 app_is_updatable_bp bool
9715 app_is_updatable_expected bool
9716 }{
9717 {
9718 name: "Non-updatable apex respects updatable property of non-updatable app",
9719 apex_is_updatable_bp: false,
9720 app_is_updatable_bp: false,
9721 app_is_updatable_expected: false,
9722 },
9723 {
9724 name: "Non-updatable apex respects updatable property of updatable app",
9725 apex_is_updatable_bp: false,
9726 app_is_updatable_bp: true,
9727 app_is_updatable_expected: true,
9728 },
9729 {
9730 name: "Updatable apex respects updatable property of updatable app",
9731 apex_is_updatable_bp: true,
9732 app_is_updatable_bp: true,
9733 app_is_updatable_expected: true,
9734 },
9735 {
9736 name: "Updatable apex sets updatable=true on non-updatable app",
9737 apex_is_updatable_bp: true,
9738 app_is_updatable_bp: false,
9739 app_is_updatable_expected: true,
9740 },
9741 }
9742 for _, testCase := range testCases {
9743 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9744 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9745 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9746 }
9747}
9748
Kiyoung Kim487689e2022-07-26 09:48:22 +09009749func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9750 bp := `
9751 apex {
9752 name: "myapex",
9753 key: "myapex.key",
9754 native_shared_libs: ["libfoo"],
9755 min_sdk_version: "29",
9756 }
9757 apex_key {
9758 name: "myapex.key",
9759 }
9760 cc_library {
9761 name: "libfoo",
9762 shared_libs: ["libc"],
9763 apex_available: ["myapex"],
9764 min_sdk_version: "29",
9765 }
9766 cc_api_library {
9767 name: "libc",
9768 src: "libc.so",
9769 min_sdk_version: "29",
9770 recovery_available: true,
9771 }
9772 api_imports {
9773 name: "api_imports",
9774 shared_libs: [
9775 "libc",
9776 ],
9777 header_libs: [],
9778 }
9779 `
9780 result := testApex(t, bp)
9781
9782 hasDep := func(m android.Module, wantDep android.Module) bool {
9783 t.Helper()
9784 var found bool
9785 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9786 if dep == wantDep {
9787 found = true
9788 }
9789 })
9790 return found
9791 }
9792
9793 libfooApexVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex29").Module()
9794 libcApexVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared_apex29").Module()
9795
9796 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(libfooApexVariant, libcApexVariant))
9797
9798 // libfoo core variant should be buildable in the same inner tree since
9799 // certain mcombo files might build system and apexes in the same inner tree
9800 // libfoo core variant should link against source libc
9801 libfooCoreVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
9802 libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
9803 android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
9804}
Dennis Shend4f5d932023-01-31 20:27:21 +00009805
9806func TestTrimmedApex(t *testing.T) {
9807 bp := `
9808 apex {
9809 name: "myapex",
9810 key: "myapex.key",
9811 native_shared_libs: ["libfoo","libbaz"],
9812 min_sdk_version: "29",
9813 trim_against: "mydcla",
9814 }
9815 apex {
9816 name: "mydcla",
9817 key: "myapex.key",
9818 native_shared_libs: ["libfoo","libbar"],
9819 min_sdk_version: "29",
9820 file_contexts: ":myapex-file_contexts",
9821 dynamic_common_lib_apex: true,
9822 }
9823 apex_key {
9824 name: "myapex.key",
9825 }
9826 cc_library {
9827 name: "libfoo",
9828 shared_libs: ["libc"],
9829 apex_available: ["myapex","mydcla"],
9830 min_sdk_version: "29",
9831 }
9832 cc_library {
9833 name: "libbar",
9834 shared_libs: ["libc"],
9835 apex_available: ["myapex","mydcla"],
9836 min_sdk_version: "29",
9837 }
9838 cc_library {
9839 name: "libbaz",
9840 shared_libs: ["libc"],
9841 apex_available: ["myapex","mydcla"],
9842 min_sdk_version: "29",
9843 }
9844 cc_api_library {
9845 name: "libc",
9846 src: "libc.so",
9847 min_sdk_version: "29",
9848 recovery_available: true,
9849 }
9850 api_imports {
9851 name: "api_imports",
9852 shared_libs: [
9853 "libc",
9854 ],
9855 header_libs: [],
9856 }
9857 `
9858 ctx := testApex(t, bp)
9859 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
9860 apexRule := module.MaybeRule("apexRule")
9861 if apexRule.Rule == nil {
9862 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
9863 }
9864
9865 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
9866 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
9867 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
9868 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
9869 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
9870 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
9871}