blob: 0dd01176a05987dc882887f7b4b95a2837a477f4 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Jooyung Hanaf730952023-02-28 14:13:38 +0900787func TestFileContexts(t *testing.T) {
788 for _, useFileContextsAsIs := range []bool{true, false} {
789 prop := ""
790 if useFileContextsAsIs {
791 prop = "use_file_contexts_as_is: true,\n"
792 }
793 ctx := testApex(t, `
794 apex {
795 name: "myapex",
796 key: "myapex.key",
797 file_contexts: "file_contexts",
798 updatable: false,
799 vendor: true,
800 `+prop+`
801 }
802
803 apex_key {
804 name: "myapex.key",
805 public_key: "testkey.avbpubkey",
806 private_key: "testkey.pem",
807 }
808 `, withFiles(map[string][]byte{
809 "file_contexts": nil,
810 }))
811
812 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("file_contexts")
813 forceLabellingCommand := "apex_manifest\\\\.pb u:object_r:system_file:s0"
814 if useFileContextsAsIs {
815 android.AssertStringDoesNotContain(t, "should force-label",
816 rule.RuleParams.Command, forceLabellingCommand)
817 } else {
818 android.AssertStringDoesContain(t, "shouldn't force-label",
819 rule.RuleParams.Command, forceLabellingCommand)
820 }
821 }
822}
823
Alex Light5098a612018-11-29 17:12:15 -0800824func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800825 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800826 apex {
827 name: "myapex",
828 key: "myapex.key",
829 payload_type: "zip",
830 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000831 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800832 }
833
834 apex_key {
835 name: "myapex.key",
836 public_key: "testkey.avbpubkey",
837 private_key: "testkey.pem",
838 }
839
840 cc_library {
841 name: "mylib",
842 srcs: ["mylib.cpp"],
843 shared_libs: ["mylib2"],
844 system_shared_libs: [],
845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000846 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800847 }
848
849 cc_library {
850 name: "mylib2",
851 srcs: ["mylib.cpp"],
852 system_shared_libs: [],
853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000854 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800855 }
856 `)
857
Sundong Ahnabb64432019-10-22 13:58:29 +0900858 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800859 copyCmds := zipApexRule.Args["copy_commands"]
860
861 // Ensure that main rule creates an output
862 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
863
864 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700865 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800866
867 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800869
870 // Ensure that both direct and indirect deps are copied into apex
871 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
872 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873}
874
875func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800876 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900881 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000882 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 }
884
885 apex_key {
886 name: "myapex.key",
887 public_key: "testkey.avbpubkey",
888 private_key: "testkey.pem",
889 }
890
891 cc_library {
892 name: "mylib",
893 srcs: ["mylib.cpp"],
894 shared_libs: ["mylib2", "mylib3"],
895 system_shared_libs: [],
896 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000897 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898 }
899
900 cc_library {
901 name: "mylib2",
902 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900903 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 system_shared_libs: [],
905 stl: "none",
906 stubs: {
907 versions: ["1", "2", "3"],
908 },
909 }
910
911 cc_library {
912 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900913 srcs: ["mylib.cpp"],
914 shared_libs: ["mylib4"],
915 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 stl: "none",
917 stubs: {
918 versions: ["10", "11", "12"],
919 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900922
923 cc_library {
924 name: "mylib4",
925 srcs: ["mylib.cpp"],
926 system_shared_libs: [],
927 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000928 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900929 }
Jiyong Park105dc322021-06-11 17:22:09 +0900930
931 rust_binary {
932 name: "foo.rust",
933 srcs: ["foo.rs"],
934 shared_libs: ["libfoo.shared_from_rust"],
935 prefer_rlib: true,
936 apex_available: ["myapex"],
937 }
938
939 cc_library_shared {
940 name: "libfoo.shared_from_rust",
941 srcs: ["mylib.cpp"],
942 system_shared_libs: [],
943 stl: "none",
944 stubs: {
945 versions: ["10", "11", "12"],
946 },
947 }
948
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 `)
950
Sundong Ahnabb64432019-10-22 13:58:29 +0900951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 copyCmds := apexRule.Args["copy_commands"]
953
954 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956
957 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800958 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959
960 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800961 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962
Colin Crossaede88c2020-08-11 12:17:01 -0700963 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964
965 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900966 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900968 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969
970 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700971 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700973 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900974
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700975 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
976 // is replaced by sharing of "cFlags" in cc/builder.go.
977 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
978 // module variable representing "cflags". So it was not detected by ensureNotContains.
979 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
980 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
981 // including the original cflags's "-include mylib.h".
982 //
Jiyong Park64379952018-12-13 18:37:29 +0900983 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700984 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
985 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900986
Jiyong Park85cc35a2022-07-17 11:30:47 +0900987 // Ensure that genstub for platform-provided lib is invoked with --systemapi
988 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
989 // Ensure that genstub for apex-provided lib is invoked with --apex
990 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900991
Jooyung Hana57af4a2020-01-23 05:36:59 +0000992 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900993 "lib64/mylib.so",
994 "lib64/mylib3.so",
995 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900996 "bin/foo.rust",
997 "lib64/libc++.so", // by the implicit dependency from foo.rust
998 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900999 })
Jiyong Park105dc322021-06-11 17:22:09 +09001000
1001 // Ensure that stub dependency from a rust module is not included
1002 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1003 // The rust module is linked to the stub cc library
1004 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
1005 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1006 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001007
1008 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1009 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010}
1011
Jiyong Park1bc84122021-06-22 20:23:05 +09001012func TestApexCanUsePrivateApis(t *testing.T) {
1013 ctx := testApex(t, `
1014 apex {
1015 name: "myapex",
1016 key: "myapex.key",
1017 native_shared_libs: ["mylib"],
1018 binaries: ["foo.rust"],
1019 updatable: false,
1020 platform_apis: true,
1021 }
1022
1023 apex_key {
1024 name: "myapex.key",
1025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 cc_library {
1030 name: "mylib",
1031 srcs: ["mylib.cpp"],
1032 shared_libs: ["mylib2"],
1033 system_shared_libs: [],
1034 stl: "none",
1035 apex_available: [ "myapex" ],
1036 }
1037
1038 cc_library {
1039 name: "mylib2",
1040 srcs: ["mylib.cpp"],
1041 cflags: ["-include mylib.h"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 stubs: {
1045 versions: ["1", "2", "3"],
1046 },
1047 }
1048
1049 rust_binary {
1050 name: "foo.rust",
1051 srcs: ["foo.rs"],
1052 shared_libs: ["libfoo.shared_from_rust"],
1053 prefer_rlib: true,
1054 apex_available: ["myapex"],
1055 }
1056
1057 cc_library_shared {
1058 name: "libfoo.shared_from_rust",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["10", "11", "12"],
1064 },
1065 }
1066 `)
1067
1068 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1069 copyCmds := apexRule.Args["copy_commands"]
1070
1071 // Ensure that indirect stubs dep is not included
1072 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1073 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1074
1075 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1076 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001077 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001078 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1079 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Parkd4a00632022-04-12 12:23:20 +09001080 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001081 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1082 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1083}
1084
Colin Cross7812fd32020-09-25 12:35:10 -07001085func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1086 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001087 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001088 apex {
1089 name: "myapex",
1090 key: "myapex.key",
1091 native_shared_libs: ["mylib", "mylib3"],
1092 min_sdk_version: "29",
1093 }
1094
1095 apex_key {
1096 name: "myapex.key",
1097 public_key: "testkey.avbpubkey",
1098 private_key: "testkey.pem",
1099 }
1100
1101 cc_library {
1102 name: "mylib",
1103 srcs: ["mylib.cpp"],
1104 shared_libs: ["mylib2", "mylib3"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 min_sdk_version: "28",
1109 }
1110
1111 cc_library {
1112 name: "mylib2",
1113 srcs: ["mylib.cpp"],
1114 cflags: ["-include mylib.h"],
1115 system_shared_libs: [],
1116 stl: "none",
1117 stubs: {
1118 versions: ["28", "29", "30", "current"],
1119 },
1120 min_sdk_version: "28",
1121 }
1122
1123 cc_library {
1124 name: "mylib3",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib4"],
1127 system_shared_libs: [],
1128 stl: "none",
1129 stubs: {
1130 versions: ["28", "29", "30", "current"],
1131 },
1132 apex_available: [ "myapex" ],
1133 min_sdk_version: "28",
1134 }
1135
1136 cc_library {
1137 name: "mylib4",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 stl: "none",
1141 apex_available: [ "myapex" ],
1142 min_sdk_version: "28",
1143 }
1144 `)
1145
1146 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1147 copyCmds := apexRule.Args["copy_commands"]
1148
1149 // Ensure that direct non-stubs dep is always included
1150 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1151
1152 // Ensure that indirect stubs dep is not included
1153 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1154
1155 // Ensure that direct stubs dep is included
1156 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1157
1158 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1159
Jiyong Park55549df2021-02-26 23:57:23 +09001160 // Ensure that mylib is linking with the latest version of stub for mylib2
1161 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001162 // ... and not linking to the non-stub (impl) variant of mylib2
1163 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1164
1165 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1166 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1167 // .. and not linking to the stubs variant of mylib3
1168 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1169
1170 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001171 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001172 ensureNotContains(t, mylib2Cflags, "-include ")
1173
Jiyong Park85cc35a2022-07-17 11:30:47 +09001174 // Ensure that genstub is invoked with --systemapi
1175 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001176
1177 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1178 "lib64/mylib.so",
1179 "lib64/mylib3.so",
1180 "lib64/mylib4.so",
1181 })
1182}
1183
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001184func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1185 t.Parallel()
1186 // myapex (Z)
1187 // mylib -----------------.
1188 // |
1189 // otherapex (29) |
1190 // libstub's versions: 29 Z current
1191 // |
1192 // <platform> |
1193 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001194 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 min_sdk_version: "Z", // non-final
1200 }
1201
1202 cc_library {
1203 name: "mylib",
1204 srcs: ["mylib.cpp"],
1205 shared_libs: ["libstub"],
1206 apex_available: ["myapex"],
1207 min_sdk_version: "Z",
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 apex {
1217 name: "otherapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libstub"],
1220 min_sdk_version: "29",
1221 }
1222
1223 cc_library {
1224 name: "libstub",
1225 srcs: ["mylib.cpp"],
1226 stubs: {
1227 versions: ["29", "Z", "current"],
1228 },
1229 apex_available: ["otherapex"],
1230 min_sdk_version: "29",
1231 }
1232
1233 // platform module depending on libstub from otherapex should use the latest stub("current")
1234 cc_library {
1235 name: "libplatform",
1236 srcs: ["mylib.cpp"],
1237 shared_libs: ["libstub"],
1238 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001239 `,
1240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1241 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1242 variables.Platform_sdk_final = proptools.BoolPtr(false)
1243 variables.Platform_version_active_codenames = []string{"Z"}
1244 }),
1245 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001246
Jiyong Park55549df2021-02-26 23:57:23 +09001247 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001248 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001249 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001250 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001251 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001252
1253 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1254 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1255 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1256 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1257 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1258}
1259
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001260func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001261 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001262 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001263 name: "myapex2",
1264 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001265 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001266 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001267 }
1268
1269 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001270 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001271 public_key: "testkey.avbpubkey",
1272 private_key: "testkey.pem",
1273 }
1274
1275 cc_library {
1276 name: "mylib",
1277 srcs: ["mylib.cpp"],
1278 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001279 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280 system_shared_libs: [],
1281 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001282 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001283 }
1284
1285 cc_library {
1286 name: "libfoo",
1287 srcs: ["mylib.cpp"],
1288 shared_libs: ["libbar"],
1289 system_shared_libs: [],
1290 stl: "none",
1291 stubs: {
1292 versions: ["10", "20", "30"],
1293 },
1294 }
1295
1296 cc_library {
1297 name: "libbar",
1298 srcs: ["mylib.cpp"],
1299 system_shared_libs: [],
1300 stl: "none",
1301 }
1302
Jiyong Park678c8812020-02-07 17:25:49 +09001303 cc_library_static {
1304 name: "libbaz",
1305 srcs: ["mylib.cpp"],
1306 system_shared_libs: [],
1307 stl: "none",
1308 apex_available: [ "myapex2" ],
1309 }
1310
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 `)
1312
Jiyong Park83dc74b2020-01-14 18:38:44 +09001313 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 copyCmds := apexRule.Args["copy_commands"]
1315
1316 // Ensure that direct non-stubs dep is always included
1317 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1318
1319 // Ensure that indirect stubs dep is not included
1320 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1321
1322 // Ensure that dependency of stubs is not included
1323 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1324
Colin Crossaede88c2020-08-11 12:17:01 -07001325 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001326
1327 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001328 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001330 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001331
Jiyong Park3ff16992019-12-27 14:11:47 +09001332 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001333
1334 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1335 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001336
Artur Satayeva8bd1132020-04-27 18:07:06 +01001337 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001338 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001339
Artur Satayeva8bd1132020-04-27 18:07:06 +01001340 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001341 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001342}
1343
Jooyung Hand3639552019-08-09 12:57:43 +09001344func TestApexWithRuntimeLibsDependency(t *testing.T) {
1345 /*
1346 myapex
1347 |
1348 v (runtime_libs)
1349 mylib ------+------> libfoo [provides stub]
1350 |
1351 `------> libbar
1352 */
Colin Cross1c460562021-02-16 17:55:47 -08001353 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001358 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366
1367 cc_library {
1368 name: "mylib",
1369 srcs: ["mylib.cpp"],
1370 runtime_libs: ["libfoo", "libbar"],
1371 system_shared_libs: [],
1372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001373 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001374 }
1375
1376 cc_library {
1377 name: "libfoo",
1378 srcs: ["mylib.cpp"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 stubs: {
1382 versions: ["10", "20", "30"],
1383 },
1384 }
1385
1386 cc_library {
1387 name: "libbar",
1388 srcs: ["mylib.cpp"],
1389 system_shared_libs: [],
1390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001391 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001392 }
1393
1394 `)
1395
Sundong Ahnabb64432019-10-22 13:58:29 +09001396 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001397 copyCmds := apexRule.Args["copy_commands"]
1398
1399 // Ensure that direct non-stubs dep is always included
1400 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1401
1402 // Ensure that indirect stubs dep is not included
1403 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1404
1405 // Ensure that runtime_libs dep in included
1406 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1407
Sundong Ahnabb64432019-10-22 13:58:29 +09001408 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001409 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1410 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001411
1412}
1413
Paul Duffina02cae32021-03-09 01:44:06 +00001414var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1415 cc.PrepareForTestWithCcBuildComponents,
1416 PrepareForTestWithApexBuildComponents,
1417 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001418 apex {
1419 name: "com.android.runtime",
1420 key: "com.android.runtime.key",
1421 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001422 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001423 }
1424
1425 apex_key {
1426 name: "com.android.runtime.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
Paul Duffina02cae32021-03-09 01:44:06 +00001430 `),
1431 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1432)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001433
Paul Duffina02cae32021-03-09 01:44:06 +00001434func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001435 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436 cc_library {
1437 name: "libc",
1438 no_libcrt: true,
1439 nocrt: true,
1440 stl: "none",
1441 system_shared_libs: [],
1442 stubs: { versions: ["1"] },
1443 apex_available: ["com.android.runtime"],
1444
1445 sanitize: {
1446 hwaddress: true,
1447 }
1448 }
1449
1450 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001451 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001452 no_libcrt: true,
1453 nocrt: true,
1454 stl: "none",
1455 system_shared_libs: [],
1456 srcs: [""],
1457 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001458 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 sanitize: {
1461 never: true,
1462 },
Paul Duffina02cae32021-03-09 01:44:06 +00001463 } `)
1464 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001465
1466 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1467 "lib64/bionic/libc.so",
1468 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1469 })
1470
Colin Cross4c4c1be2022-02-10 11:41:18 -08001471 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001472
1473 installed := hwasan.Description("install libclang_rt.hwasan")
1474 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1475
1476 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1477 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1478 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1479}
1480
1481func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001482 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001483 prepareForTestOfRuntimeApexWithHwasan,
1484 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1485 variables.SanitizeDevice = []string{"hwaddress"}
1486 }),
1487 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001488 cc_library {
1489 name: "libc",
1490 no_libcrt: true,
1491 nocrt: true,
1492 stl: "none",
1493 system_shared_libs: [],
1494 stubs: { versions: ["1"] },
1495 apex_available: ["com.android.runtime"],
1496 }
1497
1498 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001499 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001500 no_libcrt: true,
1501 nocrt: true,
1502 stl: "none",
1503 system_shared_libs: [],
1504 srcs: [""],
1505 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001506 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507
1508 sanitize: {
1509 never: true,
1510 },
1511 }
Paul Duffina02cae32021-03-09 01:44:06 +00001512 `)
1513 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001514
1515 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1516 "lib64/bionic/libc.so",
1517 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1518 })
1519
Colin Cross4c4c1be2022-02-10 11:41:18 -08001520 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001521
1522 installed := hwasan.Description("install libclang_rt.hwasan")
1523 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1524
1525 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1526 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1527 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1528}
1529
Jooyung Han61b66e92020-03-21 14:21:46 +00001530func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1531 testcases := []struct {
1532 name string
1533 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001534 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001535 shouldLink string
1536 shouldNotLink []string
1537 }{
1538 {
Jiyong Park55549df2021-02-26 23:57:23 +09001539 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001540 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001541 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001542 shouldLink: "current",
1543 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 },
1545 {
Jiyong Park55549df2021-02-26 23:57:23 +09001546 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001547 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001548 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001549 shouldLink: "current",
1550 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001551 },
1552 }
1553 for _, tc := range testcases {
1554 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001555 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 apex {
1557 name: "myapex",
1558 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001559 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001560 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001561 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001563
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001569
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 cc_library {
1571 name: "mylib",
1572 srcs: ["mylib.cpp"],
1573 vendor_available: true,
1574 shared_libs: ["libbar"],
1575 system_shared_libs: [],
1576 stl: "none",
1577 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001578 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001579 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001580
Jooyung Han61b66e92020-03-21 14:21:46 +00001581 cc_library {
1582 name: "libbar",
1583 srcs: ["mylib.cpp"],
1584 system_shared_libs: [],
1585 stl: "none",
1586 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001587 llndk: {
1588 symbol_file: "libbar.map.txt",
1589 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001590 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001591 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001592 withUnbundledBuild,
1593 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001594
Jooyung Han61b66e92020-03-21 14:21:46 +00001595 // Ensure that LLNDK dep is not included
1596 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1597 "lib64/mylib.so",
1598 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001599
Jooyung Han61b66e92020-03-21 14:21:46 +00001600 // Ensure that LLNDK dep is required
1601 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1602 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1603 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001604
Steven Moreland2c4000c2021-04-27 02:08:49 +00001605 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1606 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001607 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001608 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001609 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001610
Steven Moreland2c4000c2021-04-27 02:08:49 +00001611 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001612 ver := tc.shouldLink
1613 if tc.shouldLink == "current" {
1614 ver = strconv.Itoa(android.FutureApiLevelInt)
1615 }
1616 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001617 })
1618 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001619}
1620
Jiyong Park25fc6a92018-11-18 18:02:45 +09001621func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001622 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623 apex {
1624 name: "myapex",
1625 key: "myapex.key",
1626 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001627 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_library {
1637 name: "mylib",
1638 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001639 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001640 shared_libs: ["libdl#27"],
1641 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001642 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 }
1644
1645 cc_library_shared {
1646 name: "mylib_shared",
1647 srcs: ["mylib.cpp"],
1648 shared_libs: ["libdl#27"],
1649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001650 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 }
1652
1653 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001654 name: "libBootstrap",
1655 srcs: ["mylib.cpp"],
1656 stl: "none",
1657 bootstrap: true,
1658 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001659 `)
1660
Sundong Ahnabb64432019-10-22 13:58:29 +09001661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001662 copyCmds := apexRule.Args["copy_commands"]
1663
1664 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001666 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1667 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001668
1669 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001670 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001671
Colin Crossaede88c2020-08-11 12:17:01 -07001672 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1673 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1674 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001675
1676 // For dependency to libc
1677 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001678 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001679 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001680 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001681 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001682 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1683 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001684
1685 // For dependency to libm
1686 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001687 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001688 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001689 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001690 // ... and is not compiling with the stub
1691 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1692 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1693
1694 // For dependency to libdl
1695 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001696 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001697 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001698 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1699 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001700 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001701 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001702 // ... Cflags from stub is correctly exported to mylib
1703 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1704 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001705
1706 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001707 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1708 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1709 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1710 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001711}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001712
Jooyung Han749dc692020-04-15 11:03:39 +09001713func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001714 // there are three links between liba --> libz.
1715 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001716 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001717 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001718 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001719 apex {
1720 name: "myapex",
1721 key: "myapex.key",
1722 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001723 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001724 }
1725
1726 apex {
1727 name: "otherapex",
1728 key: "myapex.key",
1729 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001730 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
1739 cc_library {
1740 name: "libx",
1741 shared_libs: ["liba"],
1742 system_shared_libs: [],
1743 stl: "none",
1744 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001745 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001746 }
1747
1748 cc_library {
1749 name: "liby",
1750 shared_libs: ["liba"],
1751 system_shared_libs: [],
1752 stl: "none",
1753 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001754 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001755 }
1756
1757 cc_library {
1758 name: "liba",
1759 shared_libs: ["libz"],
1760 system_shared_libs: [],
1761 stl: "none",
1762 apex_available: [
1763 "//apex_available:anyapex",
1764 "//apex_available:platform",
1765 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001766 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001767 }
1768
1769 cc_library {
1770 name: "libz",
1771 system_shared_libs: [],
1772 stl: "none",
1773 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001774 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001775 },
1776 }
Jooyung Han749dc692020-04-15 11:03:39 +09001777 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001778
1779 expectLink := func(from, from_variant, to, to_variant string) {
1780 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1781 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1782 }
1783 expectNoLink := func(from, from_variant, to, to_variant string) {
1784 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1785 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1786 }
1787 // platform liba is linked to non-stub version
1788 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001789 // liba in myapex is linked to current
1790 expectLink("liba", "shared_apex29", "libz", "shared_current")
1791 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001792 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001793 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 // liba in otherapex is linked to current
1795 expectLink("liba", "shared_apex30", "libz", "shared_current")
1796 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001797 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1798 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001802 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libx"],
1807 min_sdk_version: "R",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 cc_library {
1817 name: "libx",
1818 shared_libs: ["libz"],
1819 system_shared_libs: [],
1820 stl: "none",
1821 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001822 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001823 }
1824
1825 cc_library {
1826 name: "libz",
1827 system_shared_libs: [],
1828 stl: "none",
1829 stubs: {
1830 versions: ["29", "R"],
1831 },
1832 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001833 `,
1834 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1835 variables.Platform_version_active_codenames = []string{"R"}
1836 }),
1837 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001838
1839 expectLink := func(from, from_variant, to, to_variant string) {
1840 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1841 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1842 }
1843 expectNoLink := func(from, from_variant, to, to_variant string) {
1844 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1845 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1846 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001847 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1848 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001849 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1850 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001851}
1852
Jooyung Han4c4da062021-06-23 10:23:16 +09001853func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1854 testApex(t, `
1855 apex {
1856 name: "myapex",
1857 key: "myapex.key",
1858 java_libs: ["libx"],
1859 min_sdk_version: "S",
1860 }
1861
1862 apex_key {
1863 name: "myapex.key",
1864 public_key: "testkey.avbpubkey",
1865 private_key: "testkey.pem",
1866 }
1867
1868 java_library {
1869 name: "libx",
1870 srcs: ["a.java"],
1871 apex_available: [ "myapex" ],
1872 sdk_version: "current",
1873 min_sdk_version: "S", // should be okay
1874 }
1875 `,
1876 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1877 variables.Platform_version_active_codenames = []string{"S"}
1878 variables.Platform_sdk_codename = proptools.StringPtr("S")
1879 }),
1880 )
1881}
1882
Jooyung Han749dc692020-04-15 11:03:39 +09001883func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001884 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001885 apex {
1886 name: "myapex",
1887 key: "myapex.key",
1888 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001889 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library {
1899 name: "libx",
1900 shared_libs: ["libz"],
1901 system_shared_libs: [],
1902 stl: "none",
1903 apex_available: [ "myapex" ],
1904 }
1905
1906 cc_library {
1907 name: "libz",
1908 system_shared_libs: [],
1909 stl: "none",
1910 stubs: {
1911 versions: ["1", "2"],
1912 },
1913 }
1914 `)
1915
1916 expectLink := func(from, from_variant, to, to_variant string) {
1917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
1920 expectNoLink := func(from, from_variant, to, to_variant string) {
1921 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1922 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1923 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001924 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001925 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001926 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001927 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001928}
1929
Jiyong Park5df7bd32021-08-25 16:18:46 +09001930func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1931 ctx := testApex(t, `
1932 apex {
1933 name: "myapex",
1934 key: "myapex.key",
1935 native_shared_libs: ["mylib"],
1936 updatable: false,
1937 vendor: true,
1938 min_sdk_version: "29",
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "mylib",
1949 vendor_available: true,
1950 system_shared_libs: [],
1951 stl: "none",
1952 apex_available: [ "myapex" ],
1953 min_sdk_version: "29",
1954 }
1955 `)
1956
1957 vendorVariant := "android_vendor.29_arm64_armv8-a"
1958
1959 // First check that the correct variant of crtbegin_so is used.
1960 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1961 crtBegin := names(ldRule.Args["crtBegin"])
1962 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1963
1964 // Ensure that the crtbegin_so used by the APEX is targeting 29
1965 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1966 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1967}
1968
Jooyung Han03b51852020-02-26 22:45:42 +09001969func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001970 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001971 apex {
1972 name: "myapex",
1973 key: "myapex.key",
1974 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001975 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001976 }
1977
1978 apex_key {
1979 name: "myapex.key",
1980 public_key: "testkey.avbpubkey",
1981 private_key: "testkey.pem",
1982 }
1983
1984 cc_library {
1985 name: "libx",
1986 system_shared_libs: [],
1987 stl: "none",
1988 apex_available: [ "myapex" ],
1989 stubs: {
1990 versions: ["1", "2"],
1991 },
1992 }
1993
1994 cc_library {
1995 name: "libz",
1996 shared_libs: ["libx"],
1997 system_shared_libs: [],
1998 stl: "none",
1999 }
2000 `)
2001
2002 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002003 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002004 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2005 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2006 }
2007 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002008 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002009 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2010 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2011 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002012 expectLink("libz", "shared", "libx", "shared_current")
2013 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002014 expectNoLink("libz", "shared", "libz", "shared_1")
2015 expectNoLink("libz", "shared", "libz", "shared")
2016}
2017
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002018var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2019 func(variables android.FixtureProductVariables) {
2020 variables.SanitizeDevice = []string{"hwaddress"}
2021 },
2022)
2023
Jooyung Han75568392020-03-20 04:29:24 +09002024func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002025 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002026 apex {
2027 name: "myapex",
2028 key: "myapex.key",
2029 native_shared_libs: ["libx"],
2030 min_sdk_version: "29",
2031 }
2032
2033 apex_key {
2034 name: "myapex.key",
2035 public_key: "testkey.avbpubkey",
2036 private_key: "testkey.pem",
2037 }
2038
2039 cc_library {
2040 name: "libx",
2041 shared_libs: ["libbar"],
2042 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002043 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002044 }
2045
2046 cc_library {
2047 name: "libbar",
2048 stubs: {
2049 versions: ["29", "30"],
2050 },
2051 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002052 `,
2053 prepareForTestWithSantitizeHwaddress,
2054 )
Jooyung Han03b51852020-02-26 22:45:42 +09002055 expectLink := func(from, from_variant, to, to_variant string) {
2056 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2057 libFlags := ld.Args["libFlags"]
2058 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2059 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002060 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002061}
2062
Jooyung Han75568392020-03-20 04:29:24 +09002063func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002064 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002065 apex {
2066 name: "myapex",
2067 key: "myapex.key",
2068 native_shared_libs: ["libx"],
2069 min_sdk_version: "29",
2070 }
2071
2072 apex_key {
2073 name: "myapex.key",
2074 public_key: "testkey.avbpubkey",
2075 private_key: "testkey.pem",
2076 }
2077
2078 cc_library {
2079 name: "libx",
2080 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002081 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002082 }
Jooyung Han75568392020-03-20 04:29:24 +09002083 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002084
2085 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002086 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002087 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002088 // note that platform variant is not.
2089 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002090 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002091}
2092
Jooyung Han749dc692020-04-15 11:03:39 +09002093func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2094 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002095 apex {
2096 name: "myapex",
2097 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002098 native_shared_libs: ["mylib"],
2099 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002100 }
2101
2102 apex_key {
2103 name: "myapex.key",
2104 public_key: "testkey.avbpubkey",
2105 private_key: "testkey.pem",
2106 }
Jooyung Han749dc692020-04-15 11:03:39 +09002107
2108 cc_library {
2109 name: "mylib",
2110 srcs: ["mylib.cpp"],
2111 system_shared_libs: [],
2112 stl: "none",
2113 apex_available: [
2114 "myapex",
2115 ],
2116 min_sdk_version: "30",
2117 }
2118 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002119
2120 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2121 apex {
2122 name: "myapex",
2123 key: "myapex.key",
2124 native_shared_libs: ["libfoo.ffi"],
2125 min_sdk_version: "29",
2126 }
2127
2128 apex_key {
2129 name: "myapex.key",
2130 public_key: "testkey.avbpubkey",
2131 private_key: "testkey.pem",
2132 }
2133
2134 rust_ffi_shared {
2135 name: "libfoo.ffi",
2136 srcs: ["foo.rs"],
2137 crate_name: "foo",
2138 apex_available: [
2139 "myapex",
2140 ],
2141 min_sdk_version: "30",
2142 }
2143 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002144
2145 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2146 apex {
2147 name: "myapex",
2148 key: "myapex.key",
2149 java_libs: ["libfoo"],
2150 min_sdk_version: "29",
2151 }
2152
2153 apex_key {
2154 name: "myapex.key",
2155 public_key: "testkey.avbpubkey",
2156 private_key: "testkey.pem",
2157 }
2158
2159 java_import {
2160 name: "libfoo",
2161 jars: ["libfoo.jar"],
2162 apex_available: [
2163 "myapex",
2164 ],
2165 min_sdk_version: "30",
2166 }
2167 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002168}
2169
2170func TestApexMinSdkVersion_Okay(t *testing.T) {
2171 testApex(t, `
2172 apex {
2173 name: "myapex",
2174 key: "myapex.key",
2175 native_shared_libs: ["libfoo"],
2176 java_libs: ["libbar"],
2177 min_sdk_version: "29",
2178 }
2179
2180 apex_key {
2181 name: "myapex.key",
2182 public_key: "testkey.avbpubkey",
2183 private_key: "testkey.pem",
2184 }
2185
2186 cc_library {
2187 name: "libfoo",
2188 srcs: ["mylib.cpp"],
2189 shared_libs: ["libfoo_dep"],
2190 apex_available: ["myapex"],
2191 min_sdk_version: "29",
2192 }
2193
2194 cc_library {
2195 name: "libfoo_dep",
2196 srcs: ["mylib.cpp"],
2197 apex_available: ["myapex"],
2198 min_sdk_version: "29",
2199 }
2200
2201 java_library {
2202 name: "libbar",
2203 sdk_version: "current",
2204 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002205 static_libs: [
2206 "libbar_dep",
2207 "libbar_import_dep",
2208 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002209 apex_available: ["myapex"],
2210 min_sdk_version: "29",
2211 }
2212
2213 java_library {
2214 name: "libbar_dep",
2215 sdk_version: "current",
2216 srcs: ["a.java"],
2217 apex_available: ["myapex"],
2218 min_sdk_version: "29",
2219 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002220
2221 java_import {
2222 name: "libbar_import_dep",
2223 jars: ["libbar.jar"],
2224 apex_available: ["myapex"],
2225 min_sdk_version: "29",
2226 }
Jooyung Han03b51852020-02-26 22:45:42 +09002227 `)
2228}
2229
Colin Cross8ca61c12022-10-06 21:00:14 -07002230func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2231 // Tests that an apex dependency with min_sdk_version higher than the
2232 // min_sdk_version of the apex is allowed as long as the dependency's
2233 // min_sdk_version is less than or equal to the api level that the
2234 // architecture was introduced in. In this case, arm64 didn't exist
2235 // until api level 21, so the arm64 code will never need to run on
2236 // an api level 20 device, even if other architectures of the apex
2237 // will.
2238 testApex(t, `
2239 apex {
2240 name: "myapex",
2241 key: "myapex.key",
2242 native_shared_libs: ["libfoo"],
2243 min_sdk_version: "20",
2244 }
2245
2246 apex_key {
2247 name: "myapex.key",
2248 public_key: "testkey.avbpubkey",
2249 private_key: "testkey.pem",
2250 }
2251
2252 cc_library {
2253 name: "libfoo",
2254 srcs: ["mylib.cpp"],
2255 apex_available: ["myapex"],
2256 min_sdk_version: "21",
2257 stl: "none",
2258 }
2259 `)
2260}
2261
Artur Satayev8cf899a2020-04-15 17:29:42 +01002262func TestJavaStableSdkVersion(t *testing.T) {
2263 testCases := []struct {
2264 name string
2265 expectedError string
2266 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002267 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002268 }{
2269 {
2270 name: "Non-updatable apex with non-stable dep",
2271 bp: `
2272 apex {
2273 name: "myapex",
2274 java_libs: ["myjar"],
2275 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002276 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002277 }
2278 apex_key {
2279 name: "myapex.key",
2280 public_key: "testkey.avbpubkey",
2281 private_key: "testkey.pem",
2282 }
2283 java_library {
2284 name: "myjar",
2285 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002286 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002287 apex_available: ["myapex"],
2288 }
2289 `,
2290 },
2291 {
2292 name: "Updatable apex with stable dep",
2293 bp: `
2294 apex {
2295 name: "myapex",
2296 java_libs: ["myjar"],
2297 key: "myapex.key",
2298 updatable: true,
2299 min_sdk_version: "29",
2300 }
2301 apex_key {
2302 name: "myapex.key",
2303 public_key: "testkey.avbpubkey",
2304 private_key: "testkey.pem",
2305 }
2306 java_library {
2307 name: "myjar",
2308 srcs: ["foo/bar/MyClass.java"],
2309 sdk_version: "current",
2310 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002311 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002312 }
2313 `,
2314 },
2315 {
2316 name: "Updatable apex with non-stable dep",
2317 expectedError: "cannot depend on \"myjar\"",
2318 bp: `
2319 apex {
2320 name: "myapex",
2321 java_libs: ["myjar"],
2322 key: "myapex.key",
2323 updatable: true,
2324 }
2325 apex_key {
2326 name: "myapex.key",
2327 public_key: "testkey.avbpubkey",
2328 private_key: "testkey.pem",
2329 }
2330 java_library {
2331 name: "myjar",
2332 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002333 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002334 apex_available: ["myapex"],
2335 }
2336 `,
2337 },
2338 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002339 name: "Updatable apex with non-stable legacy core platform dep",
2340 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2341 bp: `
2342 apex {
2343 name: "myapex",
2344 java_libs: ["myjar-uses-legacy"],
2345 key: "myapex.key",
2346 updatable: true,
2347 }
2348 apex_key {
2349 name: "myapex.key",
2350 public_key: "testkey.avbpubkey",
2351 private_key: "testkey.pem",
2352 }
2353 java_library {
2354 name: "myjar-uses-legacy",
2355 srcs: ["foo/bar/MyClass.java"],
2356 sdk_version: "core_platform",
2357 apex_available: ["myapex"],
2358 }
2359 `,
2360 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2361 },
2362 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002363 name: "Updatable apex with non-stable transitive dep",
2364 // This is not actually detecting that the transitive dependency is unstable, rather it is
2365 // detecting that the transitive dependency is building against a wider API surface than the
2366 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002367 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002368 bp: `
2369 apex {
2370 name: "myapex",
2371 java_libs: ["myjar"],
2372 key: "myapex.key",
2373 updatable: true,
2374 }
2375 apex_key {
2376 name: "myapex.key",
2377 public_key: "testkey.avbpubkey",
2378 private_key: "testkey.pem",
2379 }
2380 java_library {
2381 name: "myjar",
2382 srcs: ["foo/bar/MyClass.java"],
2383 sdk_version: "current",
2384 apex_available: ["myapex"],
2385 static_libs: ["transitive-jar"],
2386 }
2387 java_library {
2388 name: "transitive-jar",
2389 srcs: ["foo/bar/MyClass.java"],
2390 sdk_version: "core_platform",
2391 apex_available: ["myapex"],
2392 }
2393 `,
2394 },
2395 }
2396
2397 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002398 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2399 continue
2400 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002401 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002402 errorHandler := android.FixtureExpectsNoErrors
2403 if test.expectedError != "" {
2404 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002405 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002406 android.GroupFixturePreparers(
2407 java.PrepareForTestWithJavaDefaultModules,
2408 PrepareForTestWithApexBuildComponents,
2409 prepareForTestWithMyapex,
2410 android.OptionalFixturePreparer(test.preparer),
2411 ).
2412 ExtendWithErrorHandler(errorHandler).
2413 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002414 })
2415 }
2416}
2417
Jooyung Han749dc692020-04-15 11:03:39 +09002418func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2419 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2420 apex {
2421 name: "myapex",
2422 key: "myapex.key",
2423 native_shared_libs: ["mylib"],
2424 min_sdk_version: "29",
2425 }
2426
2427 apex_key {
2428 name: "myapex.key",
2429 public_key: "testkey.avbpubkey",
2430 private_key: "testkey.pem",
2431 }
2432
2433 cc_library {
2434 name: "mylib",
2435 srcs: ["mylib.cpp"],
2436 shared_libs: ["mylib2"],
2437 system_shared_libs: [],
2438 stl: "none",
2439 apex_available: [
2440 "myapex",
2441 ],
2442 min_sdk_version: "29",
2443 }
2444
2445 // indirect part of the apex
2446 cc_library {
2447 name: "mylib2",
2448 srcs: ["mylib.cpp"],
2449 system_shared_libs: [],
2450 stl: "none",
2451 apex_available: [
2452 "myapex",
2453 ],
2454 min_sdk_version: "30",
2455 }
2456 `)
2457}
2458
2459func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2460 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2461 apex {
2462 name: "myapex",
2463 key: "myapex.key",
2464 apps: ["AppFoo"],
2465 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002466 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002467 }
2468
2469 apex_key {
2470 name: "myapex.key",
2471 public_key: "testkey.avbpubkey",
2472 private_key: "testkey.pem",
2473 }
2474
2475 android_app {
2476 name: "AppFoo",
2477 srcs: ["foo/bar/MyClass.java"],
2478 sdk_version: "current",
2479 min_sdk_version: "29",
2480 system_modules: "none",
2481 stl: "none",
2482 static_libs: ["bar"],
2483 apex_available: [ "myapex" ],
2484 }
2485
2486 java_library {
2487 name: "bar",
2488 sdk_version: "current",
2489 srcs: ["a.java"],
2490 apex_available: [ "myapex" ],
2491 }
2492 `)
2493}
2494
2495func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002496 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002497 apex {
2498 name: "myapex",
2499 key: "myapex.key",
2500 native_shared_libs: ["mylib"],
2501 min_sdk_version: "29",
2502 }
2503
2504 apex_key {
2505 name: "myapex.key",
2506 public_key: "testkey.avbpubkey",
2507 private_key: "testkey.pem",
2508 }
2509
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002510 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002511 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2512 cc_library {
2513 name: "mylib",
2514 srcs: ["mylib.cpp"],
2515 shared_libs: ["mylib2"],
2516 system_shared_libs: [],
2517 stl: "none",
2518 apex_available: ["myapex", "otherapex"],
2519 min_sdk_version: "29",
2520 }
2521
2522 cc_library {
2523 name: "mylib2",
2524 srcs: ["mylib.cpp"],
2525 system_shared_libs: [],
2526 stl: "none",
2527 apex_available: ["otherapex"],
2528 stubs: { versions: ["29", "30"] },
2529 min_sdk_version: "30",
2530 }
2531
2532 apex {
2533 name: "otherapex",
2534 key: "myapex.key",
2535 native_shared_libs: ["mylib", "mylib2"],
2536 min_sdk_version: "30",
2537 }
2538 `)
2539 expectLink := func(from, from_variant, to, to_variant string) {
2540 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2541 libFlags := ld.Args["libFlags"]
2542 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2543 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002544 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002545 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002546}
2547
Jooyung Haned124c32021-01-26 11:43:46 +09002548func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002549 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2550 func(variables android.FixtureProductVariables) {
2551 variables.Platform_sdk_codename = proptools.StringPtr("S")
2552 variables.Platform_version_active_codenames = []string{"S"}
2553 },
2554 )
Jooyung Haned124c32021-01-26 11:43:46 +09002555 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2556 apex {
2557 name: "myapex",
2558 key: "myapex.key",
2559 native_shared_libs: ["libfoo"],
2560 min_sdk_version: "S",
2561 }
2562 apex_key {
2563 name: "myapex.key",
2564 public_key: "testkey.avbpubkey",
2565 private_key: "testkey.pem",
2566 }
2567 cc_library {
2568 name: "libfoo",
2569 shared_libs: ["libbar"],
2570 apex_available: ["myapex"],
2571 min_sdk_version: "29",
2572 }
2573 cc_library {
2574 name: "libbar",
2575 apex_available: ["myapex"],
2576 }
2577 `, withSAsActiveCodeNames)
2578}
2579
2580func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002581 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2582 variables.Platform_sdk_codename = proptools.StringPtr("S")
2583 variables.Platform_version_active_codenames = []string{"S", "T"}
2584 })
Colin Cross1c460562021-02-16 17:55:47 -08002585 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002586 apex {
2587 name: "myapex",
2588 key: "myapex.key",
2589 native_shared_libs: ["libfoo"],
2590 min_sdk_version: "S",
2591 }
2592 apex_key {
2593 name: "myapex.key",
2594 public_key: "testkey.avbpubkey",
2595 private_key: "testkey.pem",
2596 }
2597 cc_library {
2598 name: "libfoo",
2599 shared_libs: ["libbar"],
2600 apex_available: ["myapex"],
2601 min_sdk_version: "S",
2602 }
2603 cc_library {
2604 name: "libbar",
2605 stubs: {
2606 symbol_file: "libbar.map.txt",
2607 versions: ["30", "S", "T"],
2608 },
2609 }
2610 `, withSAsActiveCodeNames)
2611
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002612 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002613 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2614 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002615 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002616}
2617
Jiyong Park7c2ee712018-12-07 00:42:25 +09002618func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002619 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002620 apex {
2621 name: "myapex",
2622 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002623 native_shared_libs: ["mylib"],
2624 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002625 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002626 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002627 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002628 }
2629
2630 apex_key {
2631 name: "myapex.key",
2632 public_key: "testkey.avbpubkey",
2633 private_key: "testkey.pem",
2634 }
2635
2636 prebuilt_etc {
2637 name: "myetc",
2638 src: "myprebuilt",
2639 sub_dir: "foo/bar",
2640 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002641
2642 cc_library {
2643 name: "mylib",
2644 srcs: ["mylib.cpp"],
2645 relative_install_path: "foo/bar",
2646 system_shared_libs: [],
2647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002648 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002649 }
2650
2651 cc_binary {
2652 name: "mybin",
2653 srcs: ["mylib.cpp"],
2654 relative_install_path: "foo/bar",
2655 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002656 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002657 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002658 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002659 `)
2660
Sundong Ahnabb64432019-10-22 13:58:29 +09002661 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002662 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002663
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002664 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002665 ensureContains(t, cmd, "/etc ")
2666 ensureContains(t, cmd, "/etc/foo ")
2667 ensureContains(t, cmd, "/etc/foo/bar ")
2668 ensureContains(t, cmd, "/lib64 ")
2669 ensureContains(t, cmd, "/lib64/foo ")
2670 ensureContains(t, cmd, "/lib64/foo/bar ")
2671 ensureContains(t, cmd, "/lib ")
2672 ensureContains(t, cmd, "/lib/foo ")
2673 ensureContains(t, cmd, "/lib/foo/bar ")
2674 ensureContains(t, cmd, "/bin ")
2675 ensureContains(t, cmd, "/bin/foo ")
2676 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002677}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002678
Jooyung Han35155c42020-02-06 17:33:20 +09002679func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002680 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002681 apex {
2682 name: "myapex",
2683 key: "myapex.key",
2684 multilib: {
2685 both: {
2686 native_shared_libs: ["mylib"],
2687 binaries: ["mybin"],
2688 },
2689 },
2690 compile_multilib: "both",
2691 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002692 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002693 }
2694
2695 apex_key {
2696 name: "myapex.key",
2697 public_key: "testkey.avbpubkey",
2698 private_key: "testkey.pem",
2699 }
2700
2701 cc_library {
2702 name: "mylib",
2703 relative_install_path: "foo/bar",
2704 system_shared_libs: [],
2705 stl: "none",
2706 apex_available: [ "myapex" ],
2707 native_bridge_supported: true,
2708 }
2709
2710 cc_binary {
2711 name: "mybin",
2712 relative_install_path: "foo/bar",
2713 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002714 stl: "none",
2715 apex_available: [ "myapex" ],
2716 native_bridge_supported: true,
2717 compile_multilib: "both", // default is "first" for binary
2718 multilib: {
2719 lib64: {
2720 suffix: "64",
2721 },
2722 },
2723 }
2724 `, withNativeBridgeEnabled)
2725 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2726 "bin/foo/bar/mybin",
2727 "bin/foo/bar/mybin64",
2728 "bin/arm/foo/bar/mybin",
2729 "bin/arm64/foo/bar/mybin64",
2730 "lib/foo/bar/mylib.so",
2731 "lib/arm/foo/bar/mylib.so",
2732 "lib64/foo/bar/mylib.so",
2733 "lib64/arm64/foo/bar/mylib.so",
2734 })
2735}
2736
Jooyung Han85d61762020-06-24 23:50:26 +09002737func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002738 result := android.GroupFixturePreparers(
2739 prepareForApexTest,
2740 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2741 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002742 apex {
2743 name: "myapex",
2744 key: "myapex.key",
2745 binaries: ["mybin"],
2746 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002747 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002748 }
2749 apex_key {
2750 name: "myapex.key",
2751 public_key: "testkey.avbpubkey",
2752 private_key: "testkey.pem",
2753 }
2754 cc_binary {
2755 name: "mybin",
2756 vendor: true,
2757 shared_libs: ["libfoo"],
2758 }
2759 cc_library {
2760 name: "libfoo",
2761 proprietary: true,
2762 }
2763 `)
2764
Colin Crossc68db4b2021-11-11 18:59:15 -08002765 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002766 "bin/mybin",
2767 "lib64/libfoo.so",
2768 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2769 "lib64/libc++.so",
2770 })
2771
Colin Crossc68db4b2021-11-11 18:59:15 -08002772 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2773 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002774 name := apexBundle.BaseModuleName()
2775 prefix := "TARGET_"
2776 var builder strings.Builder
2777 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002778 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002779 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002780 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002781
Colin Crossc68db4b2021-11-11 18:59:15 -08002782 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002783 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2784 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002785}
2786
Jooyung Hanc5a96762022-02-04 11:54:50 +09002787func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2788 testApexError(t, `Trying to include a VNDK library`, `
2789 apex {
2790 name: "myapex",
2791 key: "myapex.key",
2792 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2793 vendor: true,
2794 use_vndk_as_stable: true,
2795 updatable: false,
2796 }
2797 apex_key {
2798 name: "myapex.key",
2799 public_key: "testkey.avbpubkey",
2800 private_key: "testkey.pem",
2801 }`)
2802}
2803
Jooyung Handf78e212020-07-22 15:54:47 +09002804func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002805 // myapex myapex2
2806 // | |
2807 // mybin ------. mybin2
2808 // \ \ / |
2809 // (stable) .---\--------` |
2810 // \ / \ |
2811 // \ / \ /
2812 // libvndk libvendor
2813 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002814 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002815 apex {
2816 name: "myapex",
2817 key: "myapex.key",
2818 binaries: ["mybin"],
2819 vendor: true,
2820 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002821 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002822 }
2823 apex_key {
2824 name: "myapex.key",
2825 public_key: "testkey.avbpubkey",
2826 private_key: "testkey.pem",
2827 }
2828 cc_binary {
2829 name: "mybin",
2830 vendor: true,
2831 shared_libs: ["libvndk", "libvendor"],
2832 }
2833 cc_library {
2834 name: "libvndk",
2835 vndk: {
2836 enabled: true,
2837 },
2838 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002839 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002840 }
2841 cc_library {
2842 name: "libvendor",
2843 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002844 stl: "none",
2845 }
2846 apex {
2847 name: "myapex2",
2848 key: "myapex.key",
2849 binaries: ["mybin2"],
2850 vendor: true,
2851 use_vndk_as_stable: false,
2852 updatable: false,
2853 }
2854 cc_binary {
2855 name: "mybin2",
2856 vendor: true,
2857 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002858 }
2859 `)
2860
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002861 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002862
Jooyung Han91f92032022-02-04 12:36:33 +09002863 for _, tc := range []struct {
2864 name string
2865 apexName string
2866 moduleName string
2867 moduleVariant string
2868 libs []string
2869 contents []string
2870 requireVndkNamespace bool
2871 }{
2872 {
2873 name: "use_vndk_as_stable",
2874 apexName: "myapex",
2875 moduleName: "mybin",
2876 moduleVariant: vendorVariant + "_apex10000",
2877 libs: []string{
2878 // should link with vendor variants of VNDK libs(libvndk/libc++)
2879 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2880 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2881 // unstable Vendor libs as APEX variant
2882 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2883 },
2884 contents: []string{
2885 "bin/mybin",
2886 "lib64/libvendor.so",
2887 // VNDK libs (libvndk/libc++) are not included
2888 },
2889 requireVndkNamespace: true,
2890 },
2891 {
2892 name: "!use_vndk_as_stable",
2893 apexName: "myapex2",
2894 moduleName: "mybin2",
2895 moduleVariant: vendorVariant + "_myapex2",
2896 libs: []string{
2897 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2898 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2899 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2900 // unstable vendor libs have "merged" APEX variants
2901 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2902 },
2903 contents: []string{
2904 "bin/mybin2",
2905 "lib64/libvendor.so",
2906 // VNDK libs are included as well
2907 "lib64/libvndk.so",
2908 "lib64/libc++.so",
2909 },
2910 requireVndkNamespace: false,
2911 },
2912 } {
2913 t.Run(tc.name, func(t *testing.T) {
2914 // Check linked libs
2915 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2916 libs := names(ldRule.Args["libFlags"])
2917 for _, lib := range tc.libs {
2918 ensureListContains(t, libs, lib)
2919 }
2920 // Check apex contents
2921 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002922
Jooyung Han91f92032022-02-04 12:36:33 +09002923 // Check "requireNativeLibs"
2924 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2925 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2926 if tc.requireVndkNamespace {
2927 ensureListContains(t, requireNativeLibs, ":vndk")
2928 } else {
2929 ensureListNotContains(t, requireNativeLibs, ":vndk")
2930 }
2931 })
2932 }
Jooyung Handf78e212020-07-22 15:54:47 +09002933}
2934
Justin Yun13decfb2021-03-08 19:25:55 +09002935func TestProductVariant(t *testing.T) {
2936 ctx := testApex(t, `
2937 apex {
2938 name: "myapex",
2939 key: "myapex.key",
2940 updatable: false,
2941 product_specific: true,
2942 binaries: ["foo"],
2943 }
2944
2945 apex_key {
2946 name: "myapex.key",
2947 public_key: "testkey.avbpubkey",
2948 private_key: "testkey.pem",
2949 }
2950
2951 cc_binary {
2952 name: "foo",
2953 product_available: true,
2954 apex_available: ["myapex"],
2955 srcs: ["foo.cpp"],
2956 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002957 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2958 variables.ProductVndkVersion = proptools.StringPtr("current")
2959 }),
2960 )
Justin Yun13decfb2021-03-08 19:25:55 +09002961
2962 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002963 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002964 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2965 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2966 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2967 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2968}
2969
Jooyung Han8e5685d2020-09-21 11:02:57 +09002970func TestApex_withPrebuiltFirmware(t *testing.T) {
2971 testCases := []struct {
2972 name string
2973 additionalProp string
2974 }{
2975 {"system apex with prebuilt_firmware", ""},
2976 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2977 }
2978 for _, tc := range testCases {
2979 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002980 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002981 apex {
2982 name: "myapex",
2983 key: "myapex.key",
2984 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002985 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002986 `+tc.additionalProp+`
2987 }
2988 apex_key {
2989 name: "myapex.key",
2990 public_key: "testkey.avbpubkey",
2991 private_key: "testkey.pem",
2992 }
2993 prebuilt_firmware {
2994 name: "myfirmware",
2995 src: "myfirmware.bin",
2996 filename_from_src: true,
2997 `+tc.additionalProp+`
2998 }
2999 `)
3000 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3001 "etc/firmware/myfirmware.bin",
3002 })
3003 })
3004 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003005}
3006
Jooyung Hanefb184e2020-06-25 17:14:25 +09003007func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003008 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003009 apex {
3010 name: "myapex",
3011 key: "myapex.key",
3012 vendor: true,
3013 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003014 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003015 }
3016
3017 apex_key {
3018 name: "myapex.key",
3019 public_key: "testkey.avbpubkey",
3020 private_key: "testkey.pem",
3021 }
3022
3023 cc_library {
3024 name: "mylib",
3025 vendor_available: true,
3026 }
3027 `)
3028
3029 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003030 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003031 name := apexBundle.BaseModuleName()
3032 prefix := "TARGET_"
3033 var builder strings.Builder
3034 data.Custom(&builder, name, prefix, "", data)
3035 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00003036 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 +09003037}
3038
Jooyung Han2ed99d02020-06-24 23:26:26 +09003039func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003040 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003041 apex {
3042 name: "myapex",
3043 key: "myapex.key",
3044 vintf_fragments: ["fragment.xml"],
3045 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003046 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003047 }
3048 apex_key {
3049 name: "myapex.key",
3050 public_key: "testkey.avbpubkey",
3051 private_key: "testkey.pem",
3052 }
3053 cc_binary {
3054 name: "mybin",
3055 }
3056 `)
3057
3058 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003059 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003060 name := apexBundle.BaseModuleName()
3061 prefix := "TARGET_"
3062 var builder strings.Builder
3063 data.Custom(&builder, name, prefix, "", data)
3064 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003065 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003066 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003067}
3068
Jiyong Park16e91a02018-12-20 18:18:08 +09003069func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003070 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003071 apex {
3072 name: "myapex",
3073 key: "myapex.key",
3074 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003075 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003076 }
3077
3078 apex_key {
3079 name: "myapex.key",
3080 public_key: "testkey.avbpubkey",
3081 private_key: "testkey.pem",
3082 }
3083
3084 cc_library {
3085 name: "mylib",
3086 srcs: ["mylib.cpp"],
3087 system_shared_libs: [],
3088 stl: "none",
3089 stubs: {
3090 versions: ["1", "2", "3"],
3091 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003092 apex_available: [
3093 "//apex_available:platform",
3094 "myapex",
3095 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003096 }
3097
3098 cc_binary {
3099 name: "not_in_apex",
3100 srcs: ["mylib.cpp"],
3101 static_libs: ["mylib"],
3102 static_executable: true,
3103 system_shared_libs: [],
3104 stl: "none",
3105 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003106 `)
3107
Colin Cross7113d202019-11-20 16:39:12 -08003108 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003109
3110 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003111 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003112}
Jiyong Park9335a262018-12-24 11:31:58 +09003113
3114func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003115 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003116 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003117 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003118 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003119 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003120 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003121 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003122 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003123 }
3124
3125 cc_library {
3126 name: "mylib",
3127 srcs: ["mylib.cpp"],
3128 system_shared_libs: [],
3129 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003130 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003131 }
3132
3133 apex_key {
3134 name: "myapex.key",
3135 public_key: "testkey.avbpubkey",
3136 private_key: "testkey.pem",
3137 }
3138
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003139 android_app_certificate {
3140 name: "myapex.certificate",
3141 certificate: "testkey",
3142 }
3143
3144 android_app_certificate {
3145 name: "myapex.certificate.override",
3146 certificate: "testkey.override",
3147 }
3148
Jiyong Park9335a262018-12-24 11:31:58 +09003149 `)
3150
3151 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003152 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003153
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003154 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3155 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003156 "vendor/foo/devkeys/testkey.avbpubkey")
3157 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003158 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3159 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003160 "vendor/foo/devkeys/testkey.pem")
3161 }
3162
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003163 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003164 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003165 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003166 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003167 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003168 }
3169}
Jiyong Park58e364a2019-01-19 19:24:06 +09003170
Jooyung Hanf121a652019-12-17 14:30:11 +09003171func TestCertificate(t *testing.T) {
3172 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003173 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003174 apex {
3175 name: "myapex",
3176 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003177 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003178 }
3179 apex_key {
3180 name: "myapex.key",
3181 public_key: "testkey.avbpubkey",
3182 private_key: "testkey.pem",
3183 }`)
3184 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3185 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3186 if actual := rule.Args["certificates"]; actual != expected {
3187 t.Errorf("certificates should be %q, not %q", expected, actual)
3188 }
3189 })
3190 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003191 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003192 apex {
3193 name: "myapex_keytest",
3194 key: "myapex.key",
3195 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003196 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003197 }
3198 apex_key {
3199 name: "myapex.key",
3200 public_key: "testkey.avbpubkey",
3201 private_key: "testkey.pem",
3202 }
3203 android_app_certificate {
3204 name: "myapex.certificate.override",
3205 certificate: "testkey.override",
3206 }`)
3207 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3208 expected := "testkey.override.x509.pem testkey.override.pk8"
3209 if actual := rule.Args["certificates"]; actual != expected {
3210 t.Errorf("certificates should be %q, not %q", expected, actual)
3211 }
3212 })
3213 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003214 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003215 apex {
3216 name: "myapex",
3217 key: "myapex.key",
3218 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003219 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003220 }
3221 apex_key {
3222 name: "myapex.key",
3223 public_key: "testkey.avbpubkey",
3224 private_key: "testkey.pem",
3225 }
3226 android_app_certificate {
3227 name: "myapex.certificate",
3228 certificate: "testkey",
3229 }`)
3230 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3231 expected := "testkey.x509.pem testkey.pk8"
3232 if actual := rule.Args["certificates"]; actual != expected {
3233 t.Errorf("certificates should be %q, not %q", expected, actual)
3234 }
3235 })
3236 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003237 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003238 apex {
3239 name: "myapex_keytest",
3240 key: "myapex.key",
3241 file_contexts: ":myapex-file_contexts",
3242 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003243 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003244 }
3245 apex_key {
3246 name: "myapex.key",
3247 public_key: "testkey.avbpubkey",
3248 private_key: "testkey.pem",
3249 }
3250 android_app_certificate {
3251 name: "myapex.certificate.override",
3252 certificate: "testkey.override",
3253 }`)
3254 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3255 expected := "testkey.override.x509.pem testkey.override.pk8"
3256 if actual := rule.Args["certificates"]; actual != expected {
3257 t.Errorf("certificates should be %q, not %q", expected, actual)
3258 }
3259 })
3260 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003261 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003262 apex {
3263 name: "myapex",
3264 key: "myapex.key",
3265 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003266 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003267 }
3268 apex_key {
3269 name: "myapex.key",
3270 public_key: "testkey.avbpubkey",
3271 private_key: "testkey.pem",
3272 }`)
3273 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3274 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3275 if actual := rule.Args["certificates"]; actual != expected {
3276 t.Errorf("certificates should be %q, not %q", expected, actual)
3277 }
3278 })
3279 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003280 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003281 apex {
3282 name: "myapex_keytest",
3283 key: "myapex.key",
3284 file_contexts: ":myapex-file_contexts",
3285 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003286 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003287 }
3288 apex_key {
3289 name: "myapex.key",
3290 public_key: "testkey.avbpubkey",
3291 private_key: "testkey.pem",
3292 }
3293 android_app_certificate {
3294 name: "myapex.certificate.override",
3295 certificate: "testkey.override",
3296 }`)
3297 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3298 expected := "testkey.override.x509.pem testkey.override.pk8"
3299 if actual := rule.Args["certificates"]; actual != expected {
3300 t.Errorf("certificates should be %q, not %q", expected, actual)
3301 }
3302 })
3303}
3304
Jiyong Park58e364a2019-01-19 19:24:06 +09003305func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003306 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003307 apex {
3308 name: "myapex",
3309 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003310 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003311 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003312 }
3313
3314 apex {
3315 name: "otherapex",
3316 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003317 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003318 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003319 }
3320
3321 apex_key {
3322 name: "myapex.key",
3323 public_key: "testkey.avbpubkey",
3324 private_key: "testkey.pem",
3325 }
3326
3327 cc_library {
3328 name: "mylib",
3329 srcs: ["mylib.cpp"],
3330 system_shared_libs: [],
3331 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003332 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003333 "myapex",
3334 "otherapex",
3335 ],
Jooyung Han24282772020-03-21 23:20:55 +09003336 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003337 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003338 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003339 cc_library {
3340 name: "mylib2",
3341 srcs: ["mylib.cpp"],
3342 system_shared_libs: [],
3343 stl: "none",
3344 apex_available: [
3345 "myapex",
3346 "otherapex",
3347 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003348 static_libs: ["mylib3"],
3349 recovery_available: true,
3350 min_sdk_version: "29",
3351 }
3352 cc_library {
3353 name: "mylib3",
3354 srcs: ["mylib.cpp"],
3355 system_shared_libs: [],
3356 stl: "none",
3357 apex_available: [
3358 "myapex",
3359 "otherapex",
3360 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003361 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003362 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003363 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003364 `)
3365
Jooyung Hanc87a0592020-03-02 17:44:33 +09003366 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003367 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003368 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003369
Vinh Tranf9754732023-01-19 22:41:46 -05003370 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003371 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003372 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003373
Vinh Tranf9754732023-01-19 22:41:46 -05003374 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003375 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003376 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003377
Colin Crossaede88c2020-08-11 12:17:01 -07003378 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3379 // each variant defines additional macros to distinguish which apex variant it is built for
3380
3381 // non-APEX variant does not have __ANDROID_APEX__ defined
3382 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3383 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3384
Vinh Tranf9754732023-01-19 22:41:46 -05003385 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003386 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3387 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003388
Jooyung Hanc87a0592020-03-02 17:44:33 +09003389 // non-APEX variant does not have __ANDROID_APEX__ defined
3390 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3391 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3392
Vinh Tranf9754732023-01-19 22:41:46 -05003393 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003394 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003395 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003396}
Jiyong Park7e636d02019-01-28 16:16:54 +09003397
3398func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003399 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003400 apex {
3401 name: "myapex",
3402 key: "myapex.key",
3403 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003404 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003405 }
3406
3407 apex_key {
3408 name: "myapex.key",
3409 public_key: "testkey.avbpubkey",
3410 private_key: "testkey.pem",
3411 }
3412
3413 cc_library_headers {
3414 name: "mylib_headers",
3415 export_include_dirs: ["my_include"],
3416 system_shared_libs: [],
3417 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003418 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003419 }
3420
3421 cc_library {
3422 name: "mylib",
3423 srcs: ["mylib.cpp"],
3424 system_shared_libs: [],
3425 stl: "none",
3426 header_libs: ["mylib_headers"],
3427 export_header_lib_headers: ["mylib_headers"],
3428 stubs: {
3429 versions: ["1", "2", "3"],
3430 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003431 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003432 }
3433
3434 cc_library {
3435 name: "otherlib",
3436 srcs: ["mylib.cpp"],
3437 system_shared_libs: [],
3438 stl: "none",
3439 shared_libs: ["mylib"],
3440 }
3441 `)
3442
Colin Cross7113d202019-11-20 16:39:12 -08003443 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003444
3445 // Ensure that the include path of the header lib is exported to 'otherlib'
3446 ensureContains(t, cFlags, "-Imy_include")
3447}
Alex Light9670d332019-01-29 18:07:33 -08003448
Jiyong Park7cd10e32020-01-14 09:22:18 +09003449type fileInApex struct {
3450 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003451 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003452 isLink bool
3453}
3454
Jooyung Han1724d582022-12-21 10:17:44 +09003455func (f fileInApex) String() string {
3456 return f.src + ":" + f.path
3457}
3458
3459func (f fileInApex) match(expectation string) bool {
3460 parts := strings.Split(expectation, ":")
3461 if len(parts) == 1 {
3462 match, _ := path.Match(parts[0], f.path)
3463 return match
3464 }
3465 if len(parts) == 2 {
3466 matchSrc, _ := path.Match(parts[0], f.src)
3467 matchDst, _ := path.Match(parts[1], f.path)
3468 return matchSrc && matchDst
3469 }
3470 panic("invalid expected file specification: " + expectation)
3471}
3472
Jooyung Hana57af4a2020-01-23 05:36:59 +00003473func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003474 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003475 module := ctx.ModuleForTests(moduleName, variant)
3476 apexRule := module.MaybeRule("apexRule")
3477 apexDir := "/image.apex/"
3478 if apexRule.Rule == nil {
3479 apexRule = module.Rule("zipApexRule")
3480 apexDir = "/image.zipapex/"
3481 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003482 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003483 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003484 for _, cmd := range strings.Split(copyCmds, "&&") {
3485 cmd = strings.TrimSpace(cmd)
3486 if cmd == "" {
3487 continue
3488 }
3489 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003490 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003491 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003492 switch terms[0] {
3493 case "mkdir":
3494 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003495 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003496 t.Fatal("copyCmds contains invalid cp command", cmd)
3497 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003498 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003499 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003500 isLink = false
3501 case "ln":
3502 if len(terms) != 3 && len(terms) != 4 {
3503 // ln LINK TARGET or ln -s LINK TARGET
3504 t.Fatal("copyCmds contains invalid ln command", cmd)
3505 }
3506 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003507 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003508 isLink = true
3509 default:
3510 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3511 }
3512 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003513 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003514 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003515 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003516 }
Jooyung Han1724d582022-12-21 10:17:44 +09003517 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003518 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003519 }
3520 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003521 return ret
3522}
3523
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003524func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003525 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003526 var failed bool
3527 var surplus []string
3528 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003529 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003530 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003531 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003532 if file.match(expected) {
3533 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003534 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003535 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003536 }
3537 }
Jooyung Han1724d582022-12-21 10:17:44 +09003538 if !matchFound {
3539 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003540 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003541 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003542
Jooyung Han31c470b2019-10-18 16:26:59 +09003543 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003544 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003545 t.Log("surplus files", surplus)
3546 failed = true
3547 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003548
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003549 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003550 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003551 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003552 if !filesMatched[expected] {
3553 missing = append(missing, expected)
3554 }
3555 }
3556 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003557 t.Log("missing files", missing)
3558 failed = true
3559 }
3560 if failed {
3561 t.Fail()
3562 }
3563}
3564
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003565func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3566 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3567}
3568
3569func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3570 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3571 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3572 if deapexer.Output != nil {
3573 outputs = append(outputs, deapexer.Output.String())
3574 }
3575 for _, output := range deapexer.ImplicitOutputs {
3576 outputs = append(outputs, output.String())
3577 }
3578 actualFiles := make([]fileInApex, 0, len(outputs))
3579 for _, output := range outputs {
3580 dir := "/deapexer/"
3581 pos := strings.LastIndex(output, dir)
3582 if pos == -1 {
3583 t.Fatal("Unknown deapexer output ", output)
3584 }
3585 path := output[pos+len(dir):]
3586 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3587 }
3588 assertFileListEquals(t, files, actualFiles)
3589}
3590
Jooyung Han344d5432019-08-23 11:17:39 +09003591func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003592 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003593 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003594 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003595 "etc/llndk.libraries.29.txt",
3596 "etc/vndkcore.libraries.29.txt",
3597 "etc/vndksp.libraries.29.txt",
3598 "etc/vndkprivate.libraries.29.txt",
3599 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003600 }
3601 testCases := []struct {
3602 vndkVersion string
3603 expectedFiles []string
3604 }{
3605 {
3606 vndkVersion: "current",
3607 expectedFiles: append(commonFiles,
3608 "lib/libvndk.so",
3609 "lib/libvndksp.so",
3610 "lib64/libvndk.so",
3611 "lib64/libvndksp.so"),
3612 },
3613 {
3614 vndkVersion: "",
3615 expectedFiles: append(commonFiles,
3616 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3617 "lib/libvndksp.so",
3618 "lib64/libvndksp.so"),
3619 },
3620 }
3621 for _, tc := range testCases {
3622 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3623 ctx := testApex(t, `
3624 apex_vndk {
3625 name: "com.android.vndk.current",
3626 key: "com.android.vndk.current.key",
3627 updatable: false,
3628 }
3629
3630 apex_key {
3631 name: "com.android.vndk.current.key",
3632 public_key: "testkey.avbpubkey",
3633 private_key: "testkey.pem",
3634 }
3635
3636 cc_library {
3637 name: "libvndk",
3638 srcs: ["mylib.cpp"],
3639 vendor_available: true,
3640 product_available: true,
3641 vndk: {
3642 enabled: true,
3643 },
3644 system_shared_libs: [],
3645 stl: "none",
3646 apex_available: [ "com.android.vndk.current" ],
3647 }
3648
3649 cc_library {
3650 name: "libvndksp",
3651 srcs: ["mylib.cpp"],
3652 vendor_available: true,
3653 product_available: true,
3654 vndk: {
3655 enabled: true,
3656 support_system_process: true,
3657 },
3658 system_shared_libs: [],
3659 stl: "none",
3660 apex_available: [ "com.android.vndk.current" ],
3661 }
3662
3663 // VNDK-Ext should not cause any problems
3664
3665 cc_library {
3666 name: "libvndk.ext",
3667 srcs: ["mylib2.cpp"],
3668 vendor: true,
3669 vndk: {
3670 enabled: true,
3671 extends: "libvndk",
3672 },
3673 system_shared_libs: [],
3674 stl: "none",
3675 }
3676
3677 cc_library {
3678 name: "libvndksp.ext",
3679 srcs: ["mylib2.cpp"],
3680 vendor: true,
3681 vndk: {
3682 enabled: true,
3683 support_system_process: true,
3684 extends: "libvndksp",
3685 },
3686 system_shared_libs: [],
3687 stl: "none",
3688 }
3689 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3690 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3691 }))
3692 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3693 })
3694 }
Jooyung Han344d5432019-08-23 11:17:39 +09003695}
3696
3697func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003698 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003699 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003700 name: "com.android.vndk.current",
3701 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003702 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003703 }
3704
3705 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003706 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003707 public_key: "testkey.avbpubkey",
3708 private_key: "testkey.pem",
3709 }
3710
3711 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003712 name: "libvndk",
3713 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003714 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003715 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003716 vndk: {
3717 enabled: true,
3718 },
3719 system_shared_libs: [],
3720 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003721 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003722 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003723
3724 cc_prebuilt_library_shared {
3725 name: "libvndk.arm",
3726 srcs: ["libvndk.arm.so"],
3727 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003728 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003729 vndk: {
3730 enabled: true,
3731 },
3732 enabled: false,
3733 arch: {
3734 arm: {
3735 enabled: true,
3736 },
3737 },
3738 system_shared_libs: [],
3739 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003740 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003741 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003742 `+vndkLibrariesTxtFiles("current"),
3743 withFiles(map[string][]byte{
3744 "libvndk.so": nil,
3745 "libvndk.arm.so": nil,
3746 }))
Colin Cross2807f002021-03-02 10:15:29 -08003747 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003748 "lib/libvndk.so",
3749 "lib/libvndk.arm.so",
3750 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003751 "lib/libc++.so",
3752 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003753 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003754 })
Jooyung Han344d5432019-08-23 11:17:39 +09003755}
3756
Jooyung Han39edb6c2019-11-06 16:53:07 +09003757func vndkLibrariesTxtFiles(vers ...string) (result string) {
3758 for _, v := range vers {
3759 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003760 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003761 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003762 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003763 name: "` + txt + `.libraries.txt",
3764 }
3765 `
3766 }
3767 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003768 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003769 result += `
3770 prebuilt_etc {
3771 name: "` + txt + `.libraries.` + v + `.txt",
3772 src: "dummy.txt",
3773 }
3774 `
3775 }
3776 }
3777 }
3778 return
3779}
3780
Jooyung Han344d5432019-08-23 11:17:39 +09003781func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003782 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003783 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003784 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003785 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003786 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003787 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003788 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003789 }
3790
3791 apex_key {
3792 name: "myapex.key",
3793 public_key: "testkey.avbpubkey",
3794 private_key: "testkey.pem",
3795 }
3796
Jooyung Han31c470b2019-10-18 16:26:59 +09003797 vndk_prebuilt_shared {
3798 name: "libvndk27",
3799 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003800 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003801 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003802 vndk: {
3803 enabled: true,
3804 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003805 target_arch: "arm64",
3806 arch: {
3807 arm: {
3808 srcs: ["libvndk27_arm.so"],
3809 },
3810 arm64: {
3811 srcs: ["libvndk27_arm64.so"],
3812 },
3813 },
Colin Cross2807f002021-03-02 10:15:29 -08003814 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003815 }
3816
3817 vndk_prebuilt_shared {
3818 name: "libvndk27",
3819 version: "27",
3820 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003821 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003822 vndk: {
3823 enabled: true,
3824 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003825 target_arch: "x86_64",
3826 arch: {
3827 x86: {
3828 srcs: ["libvndk27_x86.so"],
3829 },
3830 x86_64: {
3831 srcs: ["libvndk27_x86_64.so"],
3832 },
3833 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003834 }
3835 `+vndkLibrariesTxtFiles("27"),
3836 withFiles(map[string][]byte{
3837 "libvndk27_arm.so": nil,
3838 "libvndk27_arm64.so": nil,
3839 "libvndk27_x86.so": nil,
3840 "libvndk27_x86_64.so": nil,
3841 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003842
Colin Cross2807f002021-03-02 10:15:29 -08003843 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003844 "lib/libvndk27_arm.so",
3845 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003846 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 })
Jooyung Han344d5432019-08-23 11:17:39 +09003848}
3849
Jooyung Han90eee022019-10-01 20:02:42 +09003850func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003851 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003852 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003853 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003854 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003855 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003856 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003857 }
3858 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003859 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003860 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003861 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003862 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003863 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003864 }
3865 apex_key {
3866 name: "myapex.key",
3867 public_key: "testkey.avbpubkey",
3868 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003869 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003870
3871 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003872 module := ctx.ModuleForTests(moduleName, "android_common_image")
3873 apexManifestRule := module.Rule("apexManifestRule")
3874 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003875 }
3876
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003877 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003878 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003879}
3880
Jooyung Han344d5432019-08-23 11:17:39 +09003881func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003882 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003883 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003884 name: "com.android.vndk.current",
3885 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003886 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003887 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003888 }
3889
3890 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003891 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003892 public_key: "testkey.avbpubkey",
3893 private_key: "testkey.pem",
3894 }
3895
3896 cc_library {
3897 name: "libvndk",
3898 srcs: ["mylib.cpp"],
3899 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003900 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003901 native_bridge_supported: true,
3902 host_supported: true,
3903 vndk: {
3904 enabled: true,
3905 },
3906 system_shared_libs: [],
3907 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003908 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003909 }
Colin Cross2807f002021-03-02 10:15:29 -08003910 `+vndkLibrariesTxtFiles("current"),
3911 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003912
Colin Cross2807f002021-03-02 10:15:29 -08003913 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003914 "lib/libvndk.so",
3915 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003916 "lib/libc++.so",
3917 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003918 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003919 })
Jooyung Han344d5432019-08-23 11:17:39 +09003920}
3921
3922func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003923 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003924 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003925 name: "com.android.vndk.current",
3926 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003927 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003928 native_bridge_supported: true,
3929 }
3930
3931 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003932 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003933 public_key: "testkey.avbpubkey",
3934 private_key: "testkey.pem",
3935 }
3936
3937 cc_library {
3938 name: "libvndk",
3939 srcs: ["mylib.cpp"],
3940 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003941 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003942 native_bridge_supported: true,
3943 host_supported: true,
3944 vndk: {
3945 enabled: true,
3946 },
3947 system_shared_libs: [],
3948 stl: "none",
3949 }
3950 `)
3951}
3952
Jooyung Han31c470b2019-10-18 16:26:59 +09003953func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003954 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003955 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003956 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003957 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003958 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003959 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003960 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003961 }
3962
3963 apex_key {
3964 name: "myapex.key",
3965 public_key: "testkey.avbpubkey",
3966 private_key: "testkey.pem",
3967 }
3968
3969 vndk_prebuilt_shared {
3970 name: "libvndk27",
3971 version: "27",
3972 target_arch: "arm",
3973 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003974 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003975 vndk: {
3976 enabled: true,
3977 },
3978 arch: {
3979 arm: {
3980 srcs: ["libvndk27.so"],
3981 }
3982 },
3983 }
3984
3985 vndk_prebuilt_shared {
3986 name: "libvndk27",
3987 version: "27",
3988 target_arch: "arm",
3989 binder32bit: true,
3990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003991 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003992 vndk: {
3993 enabled: true,
3994 },
3995 arch: {
3996 arm: {
3997 srcs: ["libvndk27binder32.so"],
3998 }
3999 },
Colin Cross2807f002021-03-02 10:15:29 -08004000 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004001 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004002 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 withFiles(map[string][]byte{
4004 "libvndk27.so": nil,
4005 "libvndk27binder32.so": nil,
4006 }),
4007 withBinder32bit,
4008 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004009 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004010 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4011 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004012 },
4013 }),
4014 )
4015
Colin Cross2807f002021-03-02 10:15:29 -08004016 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004017 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004018 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004019 })
4020}
4021
Jooyung Han45a96772020-06-15 14:59:42 +09004022func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004023 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004024 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004025 name: "com.android.vndk.current",
4026 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004027 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004028 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004029 }
4030
4031 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004032 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004033 public_key: "testkey.avbpubkey",
4034 private_key: "testkey.pem",
4035 }
4036
4037 cc_library {
4038 name: "libz",
4039 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004040 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004041 vndk: {
4042 enabled: true,
4043 },
4044 stubs: {
4045 symbol_file: "libz.map.txt",
4046 versions: ["30"],
4047 }
4048 }
4049 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4050 "libz.map.txt": nil,
4051 }))
4052
Colin Cross2807f002021-03-02 10:15:29 -08004053 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004054 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4055 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004056 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4057 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4058 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4059 "*/*",
4060 })
Jooyung Han45a96772020-06-15 14:59:42 +09004061}
4062
Jooyung Hane1633032019-08-01 17:41:43 +09004063func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004064 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004065 apex {
4066 name: "myapex_nodep",
4067 key: "myapex.key",
4068 native_shared_libs: ["lib_nodep"],
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_dep",
4076 key: "myapex.key",
4077 native_shared_libs: ["lib_dep"],
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_provider",
4085 key: "myapex.key",
4086 native_shared_libs: ["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 {
4093 name: "myapex_selfcontained",
4094 key: "myapex.key",
4095 native_shared_libs: ["lib_dep", "libfoo"],
4096 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004097 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004098 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004099 }
4100
4101 apex_key {
4102 name: "myapex.key",
4103 public_key: "testkey.avbpubkey",
4104 private_key: "testkey.pem",
4105 }
4106
4107 cc_library {
4108 name: "lib_nodep",
4109 srcs: ["mylib.cpp"],
4110 system_shared_libs: [],
4111 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004112 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004113 }
4114
4115 cc_library {
4116 name: "lib_dep",
4117 srcs: ["mylib.cpp"],
4118 shared_libs: ["libfoo"],
4119 system_shared_libs: [],
4120 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004121 apex_available: [
4122 "myapex_dep",
4123 "myapex_provider",
4124 "myapex_selfcontained",
4125 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004126 }
4127
4128 cc_library {
4129 name: "libfoo",
4130 srcs: ["mytest.cpp"],
4131 stubs: {
4132 versions: ["1"],
4133 },
4134 system_shared_libs: [],
4135 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004136 apex_available: [
4137 "myapex_provider",
4138 "myapex_selfcontained",
4139 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004140 }
4141 `)
4142
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004143 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004144 var provideNativeLibs, requireNativeLibs []string
4145
Sundong Ahnabb64432019-10-22 13:58:29 +09004146 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004147 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4148 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004149 ensureListEmpty(t, provideNativeLibs)
4150 ensureListEmpty(t, requireNativeLibs)
4151
Sundong Ahnabb64432019-10-22 13:58:29 +09004152 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004153 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4154 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004155 ensureListEmpty(t, provideNativeLibs)
4156 ensureListContains(t, requireNativeLibs, "libfoo.so")
4157
Sundong Ahnabb64432019-10-22 13:58:29 +09004158 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004159 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4160 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004161 ensureListContains(t, provideNativeLibs, "libfoo.so")
4162 ensureListEmpty(t, requireNativeLibs)
4163
Sundong Ahnabb64432019-10-22 13:58:29 +09004164 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004165 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4166 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004167 ensureListContains(t, provideNativeLibs, "libfoo.so")
4168 ensureListEmpty(t, requireNativeLibs)
4169}
4170
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004171func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4172 ctx := testApex(t, `
4173 apex {
4174 name: "myapex",
4175 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004176 native_shared_libs: ["mylib"],
4177 updatable: false,
4178 }
4179
4180 apex_key {
4181 name: "myapex.key",
4182 public_key: "testkey.avbpubkey",
4183 private_key: "testkey.pem",
4184 }
4185
4186 cc_library {
4187 name: "mylib",
4188 srcs: ["mylib.cpp"],
4189 system_shared_libs: [],
4190 stl: "none",
4191 apex_available: [
4192 "//apex_available:platform",
4193 "myapex",
4194 ],
4195 }
4196 `, android.FixtureMergeEnv(map[string]string{
4197 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4198 }))
4199
Jooyung Han63dff462023-02-09 00:11:27 +00004200 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004201 apexManifestRule := module.Rule("apexManifestRule")
4202 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4203}
4204
Vinh Tran8f5310f2022-10-07 18:16:47 -04004205func TestCompileMultilibProp(t *testing.T) {
4206 testCases := []struct {
4207 compileMultiLibProp string
4208 containedLibs []string
4209 notContainedLibs []string
4210 }{
4211 {
4212 containedLibs: []string{
4213 "image.apex/lib64/mylib.so",
4214 "image.apex/lib/mylib.so",
4215 },
4216 compileMultiLibProp: `compile_multilib: "both",`,
4217 },
4218 {
4219 containedLibs: []string{"image.apex/lib64/mylib.so"},
4220 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4221 compileMultiLibProp: `compile_multilib: "first",`,
4222 },
4223 {
4224 containedLibs: []string{"image.apex/lib64/mylib.so"},
4225 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4226 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4227 },
4228 {
4229 containedLibs: []string{"image.apex/lib64/mylib.so"},
4230 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4231 compileMultiLibProp: `compile_multilib: "64",`,
4232 },
4233 {
4234 containedLibs: []string{"image.apex/lib/mylib.so"},
4235 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4236 compileMultiLibProp: `compile_multilib: "32",`,
4237 },
4238 }
4239 for _, testCase := range testCases {
4240 ctx := testApex(t, fmt.Sprintf(`
4241 apex {
4242 name: "myapex",
4243 key: "myapex.key",
4244 %s
4245 native_shared_libs: ["mylib"],
4246 updatable: false,
4247 }
4248 apex_key {
4249 name: "myapex.key",
4250 public_key: "testkey.avbpubkey",
4251 private_key: "testkey.pem",
4252 }
4253 cc_library {
4254 name: "mylib",
4255 srcs: ["mylib.cpp"],
4256 apex_available: [
4257 "//apex_available:platform",
4258 "myapex",
4259 ],
4260 }
4261 `, testCase.compileMultiLibProp),
4262 )
4263 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4264 apexRule := module.Rule("apexRule")
4265 copyCmds := apexRule.Args["copy_commands"]
4266 for _, containedLib := range testCase.containedLibs {
4267 ensureContains(t, copyCmds, containedLib)
4268 }
4269 for _, notContainedLib := range testCase.notContainedLibs {
4270 ensureNotContains(t, copyCmds, notContainedLib)
4271 }
4272 }
4273}
4274
Alex Light0851b882019-02-07 13:20:53 -08004275func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004276 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004277 apex {
4278 name: "myapex",
4279 key: "myapex.key",
4280 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004281 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004282 }
4283
4284 apex_key {
4285 name: "myapex.key",
4286 public_key: "testkey.avbpubkey",
4287 private_key: "testkey.pem",
4288 }
4289
4290 cc_library {
4291 name: "mylib_common",
4292 srcs: ["mylib.cpp"],
4293 system_shared_libs: [],
4294 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004295 apex_available: [
4296 "//apex_available:platform",
4297 "myapex",
4298 ],
Alex Light0851b882019-02-07 13:20:53 -08004299 }
4300 `)
4301
Sundong Ahnabb64432019-10-22 13:58:29 +09004302 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004303 apexRule := module.Rule("apexRule")
4304 copyCmds := apexRule.Args["copy_commands"]
4305
4306 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4307 t.Log("Apex was a test apex!")
4308 t.Fail()
4309 }
4310 // Ensure that main rule creates an output
4311 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4312
4313 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004314 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004315
4316 // Ensure that both direct and indirect deps are copied into apex
4317 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4318
Colin Cross7113d202019-11-20 16:39:12 -08004319 // Ensure that the platform variant ends with _shared
4320 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004321
Colin Cross56a83212020-09-15 18:30:11 -07004322 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004323 t.Log("Found mylib_common not in any apex!")
4324 t.Fail()
4325 }
4326}
4327
4328func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004329 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004330 apex_test {
4331 name: "myapex",
4332 key: "myapex.key",
4333 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004334 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004335 }
4336
4337 apex_key {
4338 name: "myapex.key",
4339 public_key: "testkey.avbpubkey",
4340 private_key: "testkey.pem",
4341 }
4342
4343 cc_library {
4344 name: "mylib_common_test",
4345 srcs: ["mylib.cpp"],
4346 system_shared_libs: [],
4347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004348 // TODO: remove //apex_available:platform
4349 apex_available: [
4350 "//apex_available:platform",
4351 "myapex",
4352 ],
Alex Light0851b882019-02-07 13:20:53 -08004353 }
4354 `)
4355
Sundong Ahnabb64432019-10-22 13:58:29 +09004356 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004357 apexRule := module.Rule("apexRule")
4358 copyCmds := apexRule.Args["copy_commands"]
4359
4360 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4361 t.Log("Apex was not a test apex!")
4362 t.Fail()
4363 }
4364 // Ensure that main rule creates an output
4365 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4366
4367 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004368 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004369
4370 // Ensure that both direct and indirect deps are copied into apex
4371 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4372
Colin Cross7113d202019-11-20 16:39:12 -08004373 // Ensure that the platform variant ends with _shared
4374 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004375}
4376
Alex Light9670d332019-01-29 18:07:33 -08004377func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004378 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004379 apex {
4380 name: "myapex",
4381 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004382 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004383 multilib: {
4384 first: {
4385 native_shared_libs: ["mylib_common"],
4386 }
4387 },
4388 target: {
4389 android: {
4390 multilib: {
4391 first: {
4392 native_shared_libs: ["mylib"],
4393 }
4394 }
4395 },
4396 host: {
4397 multilib: {
4398 first: {
4399 native_shared_libs: ["mylib2"],
4400 }
4401 }
4402 }
4403 }
4404 }
4405
4406 apex_key {
4407 name: "myapex.key",
4408 public_key: "testkey.avbpubkey",
4409 private_key: "testkey.pem",
4410 }
4411
4412 cc_library {
4413 name: "mylib",
4414 srcs: ["mylib.cpp"],
4415 system_shared_libs: [],
4416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004417 // TODO: remove //apex_available:platform
4418 apex_available: [
4419 "//apex_available:platform",
4420 "myapex",
4421 ],
Alex Light9670d332019-01-29 18:07:33 -08004422 }
4423
4424 cc_library {
4425 name: "mylib_common",
4426 srcs: ["mylib.cpp"],
4427 system_shared_libs: [],
4428 stl: "none",
4429 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004430 // TODO: remove //apex_available:platform
4431 apex_available: [
4432 "//apex_available:platform",
4433 "myapex",
4434 ],
Alex Light9670d332019-01-29 18:07:33 -08004435 }
4436
4437 cc_library {
4438 name: "mylib2",
4439 srcs: ["mylib.cpp"],
4440 system_shared_libs: [],
4441 stl: "none",
4442 compile_multilib: "first",
4443 }
4444 `)
4445
Sundong Ahnabb64432019-10-22 13:58:29 +09004446 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004447 copyCmds := apexRule.Args["copy_commands"]
4448
4449 // Ensure that main rule creates an output
4450 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4451
4452 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004453 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4454 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4455 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004456
4457 // Ensure that both direct and indirect deps are copied into apex
4458 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4459 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4460 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4461
Colin Cross7113d202019-11-20 16:39:12 -08004462 // Ensure that the platform variant ends with _shared
4463 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4464 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4465 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004466}
Jiyong Park04480cf2019-02-06 00:16:29 +09004467
Jiyong Park59140302020-12-14 18:44:04 +09004468func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004469 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004470 apex {
4471 name: "myapex",
4472 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004473 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004474 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004475 arch: {
4476 arm64: {
4477 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004478 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004479 },
4480 x86_64: {
4481 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004482 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004483 },
4484 }
4485 }
4486
4487 apex_key {
4488 name: "myapex.key",
4489 public_key: "testkey.avbpubkey",
4490 private_key: "testkey.pem",
4491 }
4492
4493 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004494 name: "mylib.generic",
4495 srcs: ["mylib.cpp"],
4496 system_shared_libs: [],
4497 stl: "none",
4498 // TODO: remove //apex_available:platform
4499 apex_available: [
4500 "//apex_available:platform",
4501 "myapex",
4502 ],
4503 }
4504
4505 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004506 name: "mylib.arm64",
4507 srcs: ["mylib.cpp"],
4508 system_shared_libs: [],
4509 stl: "none",
4510 // TODO: remove //apex_available:platform
4511 apex_available: [
4512 "//apex_available:platform",
4513 "myapex",
4514 ],
4515 }
4516
4517 cc_library {
4518 name: "mylib.x64",
4519 srcs: ["mylib.cpp"],
4520 system_shared_libs: [],
4521 stl: "none",
4522 // TODO: remove //apex_available:platform
4523 apex_available: [
4524 "//apex_available:platform",
4525 "myapex",
4526 ],
4527 }
4528 `)
4529
4530 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4531 copyCmds := apexRule.Args["copy_commands"]
4532
4533 // Ensure that apex variant is created for the direct dep
4534 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004535 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004536 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4537
4538 // Ensure that both direct and indirect deps are copied into apex
4539 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4540 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4541}
4542
Jiyong Park04480cf2019-02-06 00:16:29 +09004543func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004544 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004545 apex {
4546 name: "myapex",
4547 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004548 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004549 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004550 }
4551
4552 apex_key {
4553 name: "myapex.key",
4554 public_key: "testkey.avbpubkey",
4555 private_key: "testkey.pem",
4556 }
4557
4558 sh_binary {
4559 name: "myscript",
4560 src: "mylib.cpp",
4561 filename: "myscript.sh",
4562 sub_dir: "script",
4563 }
4564 `)
4565
Sundong Ahnabb64432019-10-22 13:58:29 +09004566 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004567 copyCmds := apexRule.Args["copy_commands"]
4568
4569 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4570}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004571
Jooyung Han91df2082019-11-20 01:49:42 +09004572func TestApexInVariousPartition(t *testing.T) {
4573 testcases := []struct {
4574 propName, parition, flattenedPartition string
4575 }{
4576 {"", "system", "system_ext"},
4577 {"product_specific: true", "product", "product"},
4578 {"soc_specific: true", "vendor", "vendor"},
4579 {"proprietary: true", "vendor", "vendor"},
4580 {"vendor: true", "vendor", "vendor"},
4581 {"system_ext_specific: true", "system_ext", "system_ext"},
4582 }
4583 for _, tc := range testcases {
4584 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004585 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004586 apex {
4587 name: "myapex",
4588 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004589 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004590 `+tc.propName+`
4591 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004592
Jooyung Han91df2082019-11-20 01:49:42 +09004593 apex_key {
4594 name: "myapex.key",
4595 public_key: "testkey.avbpubkey",
4596 private_key: "testkey.pem",
4597 }
4598 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004599
Jooyung Han91df2082019-11-20 01:49:42 +09004600 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004601 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4602 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004603 if actual != expected {
4604 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4605 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004606
Jooyung Han91df2082019-11-20 01:49:42 +09004607 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004608 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4609 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004610 if actual != expected {
4611 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4612 }
4613 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004614 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004615}
Jiyong Park67882562019-03-21 01:11:21 +09004616
Jooyung Han580eb4f2020-06-24 19:33:06 +09004617func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004618 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004619 apex {
4620 name: "myapex",
4621 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004622 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004623 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004624
Jooyung Han580eb4f2020-06-24 19:33:06 +09004625 apex_key {
4626 name: "myapex.key",
4627 public_key: "testkey.avbpubkey",
4628 private_key: "testkey.pem",
4629 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004630 `)
4631 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004632 rule := module.Output("file_contexts")
4633 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4634}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004635
Jooyung Han580eb4f2020-06-24 19:33:06 +09004636func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004637 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004638 apex {
4639 name: "myapex",
4640 key: "myapex.key",
4641 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004642 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004643 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004644
Jooyung Han580eb4f2020-06-24 19:33:06 +09004645 apex_key {
4646 name: "myapex.key",
4647 public_key: "testkey.avbpubkey",
4648 private_key: "testkey.pem",
4649 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004650 `, withFiles(map[string][]byte{
4651 "my_own_file_contexts": nil,
4652 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004653}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004654
Jooyung Han580eb4f2020-06-24 19:33:06 +09004655func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004656 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004657 apex {
4658 name: "myapex",
4659 key: "myapex.key",
4660 product_specific: true,
4661 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004662 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004663 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004664
Jooyung Han580eb4f2020-06-24 19:33:06 +09004665 apex_key {
4666 name: "myapex.key",
4667 public_key: "testkey.avbpubkey",
4668 private_key: "testkey.pem",
4669 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004670 `)
4671
Colin Cross1c460562021-02-16 17:55:47 -08004672 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004673 apex {
4674 name: "myapex",
4675 key: "myapex.key",
4676 product_specific: true,
4677 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004678 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004679 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004680
Jooyung Han580eb4f2020-06-24 19:33:06 +09004681 apex_key {
4682 name: "myapex.key",
4683 public_key: "testkey.avbpubkey",
4684 private_key: "testkey.pem",
4685 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004686 `, withFiles(map[string][]byte{
4687 "product_specific_file_contexts": nil,
4688 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004689 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4690 rule := module.Output("file_contexts")
4691 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4692}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004693
Jooyung Han580eb4f2020-06-24 19:33:06 +09004694func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004695 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004696 apex {
4697 name: "myapex",
4698 key: "myapex.key",
4699 product_specific: true,
4700 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004701 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004702 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004703
Jooyung Han580eb4f2020-06-24 19:33:06 +09004704 apex_key {
4705 name: "myapex.key",
4706 public_key: "testkey.avbpubkey",
4707 private_key: "testkey.pem",
4708 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004709
Jooyung Han580eb4f2020-06-24 19:33:06 +09004710 filegroup {
4711 name: "my-file-contexts",
4712 srcs: ["product_specific_file_contexts"],
4713 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004714 `, withFiles(map[string][]byte{
4715 "product_specific_file_contexts": nil,
4716 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004717 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4718 rule := module.Output("file_contexts")
4719 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004720}
4721
Jiyong Park67882562019-03-21 01:11:21 +09004722func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004723 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004724 apex_key {
4725 name: "myapex.key",
4726 public_key: ":my.avbpubkey",
4727 private_key: ":my.pem",
4728 product_specific: true,
4729 }
4730
4731 filegroup {
4732 name: "my.avbpubkey",
4733 srcs: ["testkey2.avbpubkey"],
4734 }
4735
4736 filegroup {
4737 name: "my.pem",
4738 srcs: ["testkey2.pem"],
4739 }
4740 `)
4741
4742 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4743 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004744 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004745 if actual_pubkey != expected_pubkey {
4746 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4747 }
4748 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004749 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004750 if actual_privkey != expected_privkey {
4751 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4752 }
4753}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004754
4755func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004756 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004757 prebuilt_apex {
4758 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004759 arch: {
4760 arm64: {
4761 src: "myapex-arm64.apex",
4762 },
4763 arm: {
4764 src: "myapex-arm.apex",
4765 },
4766 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004767 }
4768 `)
4769
Wei Li340ee8e2022-03-18 17:33:24 -07004770 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4771 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004772
Jiyong Parkc95714e2019-03-29 14:23:10 +09004773 expectedInput := "myapex-arm64.apex"
4774 if prebuilt.inputApex.String() != expectedInput {
4775 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4776 }
Wei Li340ee8e2022-03-18 17:33:24 -07004777 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4778 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4779 rule := testingModule.Rule("genProvenanceMetaData")
4780 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4781 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4782 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4783 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004784}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004785
Paul Duffinc0609c62021-03-01 17:27:16 +00004786func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004787 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004788 prebuilt_apex {
4789 name: "myapex",
4790 }
4791 `)
4792}
4793
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004794func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004795 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004796 prebuilt_apex {
4797 name: "myapex",
4798 src: "myapex-arm.apex",
4799 filename: "notmyapex.apex",
4800 }
4801 `)
4802
Wei Li340ee8e2022-03-18 17:33:24 -07004803 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4804 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004805
4806 expected := "notmyapex.apex"
4807 if p.installFilename != expected {
4808 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4809 }
Wei Li340ee8e2022-03-18 17:33:24 -07004810 rule := testingModule.Rule("genProvenanceMetaData")
4811 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4812 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4813 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4814 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004815}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004816
Samiul Islam7c02e262021-09-08 17:48:28 +01004817func TestApexSetFilenameOverride(t *testing.T) {
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.apex"
4824 }
4825 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4826
4827 testApex(t, `
4828 apex_set {
4829 name: "com.company.android.myapex",
4830 apex_name: "com.android.myapex",
4831 set: "company-myapex.apks",
4832 filename: "com.company.android.myapex.capex"
4833 }
4834 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4835
4836 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4837 apex_set {
4838 name: "com.company.android.myapex",
4839 apex_name: "com.android.myapex",
4840 set: "company-myapex.apks",
4841 filename: "some-random-suffix"
4842 }
4843 `)
4844}
4845
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004846func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004847 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004848 prebuilt_apex {
4849 name: "myapex.prebuilt",
4850 src: "myapex-arm.apex",
4851 overrides: [
4852 "myapex",
4853 ],
4854 }
4855 `)
4856
Wei Li340ee8e2022-03-18 17:33:24 -07004857 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4858 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004859
4860 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004861 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004862 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004863 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004864 }
Wei Li340ee8e2022-03-18 17:33:24 -07004865 rule := testingModule.Rule("genProvenanceMetaData")
4866 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4867 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4868 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4869 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004870}
4871
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004872func TestPrebuiltApexName(t *testing.T) {
4873 testApex(t, `
4874 prebuilt_apex {
4875 name: "com.company.android.myapex",
4876 apex_name: "com.android.myapex",
4877 src: "company-myapex-arm.apex",
4878 }
4879 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4880
4881 testApex(t, `
4882 apex_set {
4883 name: "com.company.android.myapex",
4884 apex_name: "com.android.myapex",
4885 set: "company-myapex.apks",
4886 }
4887 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4888}
4889
4890func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4891 _ = android.GroupFixturePreparers(
4892 java.PrepareForTestWithJavaDefaultModules,
4893 PrepareForTestWithApexBuildComponents,
4894 android.FixtureWithRootAndroidBp(`
4895 platform_bootclasspath {
4896 name: "platform-bootclasspath",
4897 fragments: [
4898 {
4899 apex: "com.android.art",
4900 module: "art-bootclasspath-fragment",
4901 },
4902 ],
4903 }
4904
4905 prebuilt_apex {
4906 name: "com.company.android.art",
4907 apex_name: "com.android.art",
4908 src: "com.company.android.art-arm.apex",
4909 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4910 }
4911
4912 prebuilt_bootclasspath_fragment {
4913 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004914 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004915 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004916 hidden_api: {
4917 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4918 metadata: "my-bootclasspath-fragment/metadata.csv",
4919 index: "my-bootclasspath-fragment/index.csv",
4920 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4921 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4922 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004923 }
4924
4925 java_import {
4926 name: "core-oj",
4927 jars: ["prebuilt.jar"],
4928 }
4929 `),
4930 ).RunTest(t)
4931}
4932
Paul Duffin092153d2021-01-26 11:42:39 +00004933// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4934// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004935func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004936 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004937
Paul Duffin89886cb2021-02-05 16:44:03 +00004938 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004939 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004940 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004941 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004942 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004943 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004944 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4945 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4946 android.NormalizePathForTesting(dexJarBuildPath))
4947 }
4948
4949 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004950 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004951 // Make sure the import has been given the correct path to the dex jar.
4952 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4953 dexJarBuildPath := p.DexJarInstallPath()
4954 stem := android.RemoveOptionalPrebuiltPrefix(name)
4955 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4956 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4957 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004958 }
4959
Paul Duffin39853512021-02-26 11:09:39 +00004960 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004961 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004962 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004963 android.AssertArrayString(t, "Check if there is no source variant",
4964 []string{"android_common"},
4965 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004966 }
4967
4968 t.Run("prebuilt only", func(t *testing.T) {
4969 bp := `
4970 prebuilt_apex {
4971 name: "myapex",
4972 arch: {
4973 arm64: {
4974 src: "myapex-arm64.apex",
4975 },
4976 arm: {
4977 src: "myapex-arm.apex",
4978 },
4979 },
Paul Duffin39853512021-02-26 11:09:39 +00004980 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004981 }
4982
4983 java_import {
4984 name: "libfoo",
4985 jars: ["libfoo.jar"],
4986 }
Paul Duffin39853512021-02-26 11:09:39 +00004987
4988 java_sdk_library_import {
4989 name: "libbar",
4990 public: {
4991 jars: ["libbar.jar"],
4992 },
4993 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004994 `
4995
4996 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4997 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4998
Martin Stjernholm44825602021-09-17 01:44:12 +01004999 deapexerName := deapexerModuleName("myapex")
5000 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5001
Paul Duffinf6932af2021-02-26 18:21:56 +00005002 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005003 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005004 rule := deapexer.Rule("deapexer")
5005 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5006 t.Errorf("expected: %q, found: %q", expected, actual)
5007 }
5008
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005009 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005010 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005011 rule = prebuiltApex.Rule("android/soong/android.Cp")
5012 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5013 t.Errorf("expected: %q, found: %q", expected, actual)
5014 }
5015
Paul Duffin89886cb2021-02-05 16:44:03 +00005016 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005017 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005018
5019 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005020 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005021 })
5022
5023 t.Run("prebuilt with source preferred", func(t *testing.T) {
5024
5025 bp := `
5026 prebuilt_apex {
5027 name: "myapex",
5028 arch: {
5029 arm64: {
5030 src: "myapex-arm64.apex",
5031 },
5032 arm: {
5033 src: "myapex-arm.apex",
5034 },
5035 },
Paul Duffin39853512021-02-26 11:09:39 +00005036 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005037 }
5038
5039 java_import {
5040 name: "libfoo",
5041 jars: ["libfoo.jar"],
5042 }
5043
5044 java_library {
5045 name: "libfoo",
5046 }
Paul Duffin39853512021-02-26 11:09:39 +00005047
5048 java_sdk_library_import {
5049 name: "libbar",
5050 public: {
5051 jars: ["libbar.jar"],
5052 },
5053 }
5054
5055 java_sdk_library {
5056 name: "libbar",
5057 srcs: ["foo/bar/MyClass.java"],
5058 unsafe_ignore_missing_latest_api: true,
5059 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005060 `
5061
5062 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5063 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5064
Paul Duffin89886cb2021-02-05 16:44:03 +00005065 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005066 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005067 ensureNoSourceVariant(t, ctx, "libfoo")
5068
5069 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005070 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005071 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005072 })
5073
5074 t.Run("prebuilt preferred with source", func(t *testing.T) {
5075 bp := `
5076 prebuilt_apex {
5077 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005078 arch: {
5079 arm64: {
5080 src: "myapex-arm64.apex",
5081 },
5082 arm: {
5083 src: "myapex-arm.apex",
5084 },
5085 },
Paul Duffin39853512021-02-26 11:09:39 +00005086 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005087 }
5088
5089 java_import {
5090 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005091 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005092 jars: ["libfoo.jar"],
5093 }
5094
5095 java_library {
5096 name: "libfoo",
5097 }
Paul Duffin39853512021-02-26 11:09:39 +00005098
5099 java_sdk_library_import {
5100 name: "libbar",
5101 prefer: true,
5102 public: {
5103 jars: ["libbar.jar"],
5104 },
5105 }
5106
5107 java_sdk_library {
5108 name: "libbar",
5109 srcs: ["foo/bar/MyClass.java"],
5110 unsafe_ignore_missing_latest_api: true,
5111 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005112 `
5113
5114 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5115 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5116
Paul Duffin89886cb2021-02-05 16:44:03 +00005117 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005118 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005119 ensureNoSourceVariant(t, ctx, "libfoo")
5120
5121 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005122 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005123 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005124 })
5125}
5126
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005127func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005128 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005129 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005130 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5131 // is disabled.
5132 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5133 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005134
Paul Duffin37856732021-02-26 14:24:15 +00005135 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5136 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005137 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005138 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005139 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005140 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005141 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005142 foundLibfooJar = true
5143 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005144 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005145 }
5146 }
5147 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005148 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 +00005149 }
5150 }
5151
Paul Duffin40a3f652021-07-19 13:11:24 +01005152 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005153 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005154 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005155 var rule android.TestingBuildParams
5156
5157 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5158 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005159 }
5160
Paul Duffin40a3f652021-07-19 13:11:24 +01005161 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5162 t.Helper()
5163 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5164 var rule android.TestingBuildParams
5165
5166 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5167 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5168 }
5169
Paul Duffin89f570a2021-06-16 01:42:33 +01005170 fragment := java.ApexVariantReference{
5171 Apex: proptools.StringPtr("myapex"),
5172 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5173 }
5174
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005175 t.Run("prebuilt only", func(t *testing.T) {
5176 bp := `
5177 prebuilt_apex {
5178 name: "myapex",
5179 arch: {
5180 arm64: {
5181 src: "myapex-arm64.apex",
5182 },
5183 arm: {
5184 src: "myapex-arm.apex",
5185 },
5186 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005187 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5188 }
5189
5190 prebuilt_bootclasspath_fragment {
5191 name: "my-bootclasspath-fragment",
5192 contents: ["libfoo", "libbar"],
5193 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005194 hidden_api: {
5195 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5196 metadata: "my-bootclasspath-fragment/metadata.csv",
5197 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005198 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5199 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5200 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005201 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005202 }
5203
5204 java_import {
5205 name: "libfoo",
5206 jars: ["libfoo.jar"],
5207 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005208 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005209 }
Paul Duffin37856732021-02-26 14:24:15 +00005210
5211 java_sdk_library_import {
5212 name: "libbar",
5213 public: {
5214 jars: ["libbar.jar"],
5215 },
5216 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005217 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005218 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005219 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005220 `
5221
Paul Duffin89f570a2021-06-16 01:42:33 +01005222 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005223 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5224 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005225
Paul Duffin537ea3d2021-05-14 10:38:00 +01005226 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005227 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005228 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005229 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005230 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5231 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005232 })
5233
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005234 t.Run("apex_set only", func(t *testing.T) {
5235 bp := `
5236 apex_set {
5237 name: "myapex",
5238 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005239 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5240 }
5241
5242 prebuilt_bootclasspath_fragment {
5243 name: "my-bootclasspath-fragment",
5244 contents: ["libfoo", "libbar"],
5245 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005246 hidden_api: {
5247 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5248 metadata: "my-bootclasspath-fragment/metadata.csv",
5249 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005250 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5251 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5252 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005253 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005254 }
5255
5256 java_import {
5257 name: "libfoo",
5258 jars: ["libfoo.jar"],
5259 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005260 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005261 }
5262
5263 java_sdk_library_import {
5264 name: "libbar",
5265 public: {
5266 jars: ["libbar.jar"],
5267 },
5268 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005269 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005270 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005271 }
5272 `
5273
Paul Duffin89f570a2021-06-16 01:42:33 +01005274 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005275 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5276 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5277
Paul Duffin537ea3d2021-05-14 10:38:00 +01005278 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005279 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005280 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005281 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005282 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5283 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005284 })
5285
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005286 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5287 bp := `
5288 prebuilt_apex {
5289 name: "myapex",
5290 arch: {
5291 arm64: {
5292 src: "myapex-arm64.apex",
5293 },
5294 arm: {
5295 src: "myapex-arm.apex",
5296 },
5297 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005298 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5299 }
5300
5301 prebuilt_bootclasspath_fragment {
5302 name: "my-bootclasspath-fragment",
5303 contents: ["libfoo", "libbar"],
5304 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005305 hidden_api: {
5306 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5307 metadata: "my-bootclasspath-fragment/metadata.csv",
5308 index: "my-bootclasspath-fragment/index.csv",
5309 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5310 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5311 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005312 }
5313
5314 java_import {
5315 name: "libfoo",
5316 jars: ["libfoo.jar"],
5317 apex_available: ["myapex"],
5318 }
5319
5320 java_library {
5321 name: "libfoo",
5322 srcs: ["foo/bar/MyClass.java"],
5323 apex_available: ["myapex"],
5324 }
Paul Duffin37856732021-02-26 14:24:15 +00005325
5326 java_sdk_library_import {
5327 name: "libbar",
5328 public: {
5329 jars: ["libbar.jar"],
5330 },
5331 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005332 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005333 }
5334
5335 java_sdk_library {
5336 name: "libbar",
5337 srcs: ["foo/bar/MyClass.java"],
5338 unsafe_ignore_missing_latest_api: true,
5339 apex_available: ["myapex"],
5340 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005341 `
5342
5343 // In this test the source (java_library) libfoo is active since the
5344 // prebuilt (java_import) defaults to prefer:false. However the
5345 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5346 // find the dex boot jar in it. We either need to disable the source libfoo
5347 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005348 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005349 // dexbootjar check is skipped if AllowMissingDependencies is true
5350 preparerAllowMissingDeps := android.GroupFixturePreparers(
5351 preparer,
5352 android.PrepareForTestWithAllowMissingDependencies,
5353 )
5354 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005355 })
5356
5357 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5358 bp := `
5359 prebuilt_apex {
5360 name: "myapex",
5361 arch: {
5362 arm64: {
5363 src: "myapex-arm64.apex",
5364 },
5365 arm: {
5366 src: "myapex-arm.apex",
5367 },
5368 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005369 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5370 }
5371
5372 prebuilt_bootclasspath_fragment {
5373 name: "my-bootclasspath-fragment",
5374 contents: ["libfoo", "libbar"],
5375 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005376 hidden_api: {
5377 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5378 metadata: "my-bootclasspath-fragment/metadata.csv",
5379 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005380 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5381 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5382 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005383 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005384 }
5385
5386 java_import {
5387 name: "libfoo",
5388 prefer: true,
5389 jars: ["libfoo.jar"],
5390 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005391 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005392 }
5393
5394 java_library {
5395 name: "libfoo",
5396 srcs: ["foo/bar/MyClass.java"],
5397 apex_available: ["myapex"],
5398 }
Paul Duffin37856732021-02-26 14:24:15 +00005399
5400 java_sdk_library_import {
5401 name: "libbar",
5402 prefer: true,
5403 public: {
5404 jars: ["libbar.jar"],
5405 },
5406 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005407 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005408 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005409 }
5410
5411 java_sdk_library {
5412 name: "libbar",
5413 srcs: ["foo/bar/MyClass.java"],
5414 unsafe_ignore_missing_latest_api: true,
5415 apex_available: ["myapex"],
5416 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005417 `
5418
Paul Duffin89f570a2021-06-16 01:42:33 +01005419 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005420 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5421 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005422
Paul Duffin537ea3d2021-05-14 10:38:00 +01005423 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005424 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005425 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005426 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005427 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5428 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 })
5430
5431 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5432 bp := `
5433 apex {
5434 name: "myapex",
5435 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005436 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005437 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005438 }
5439
5440 apex_key {
5441 name: "myapex.key",
5442 public_key: "testkey.avbpubkey",
5443 private_key: "testkey.pem",
5444 }
5445
5446 prebuilt_apex {
5447 name: "myapex",
5448 arch: {
5449 arm64: {
5450 src: "myapex-arm64.apex",
5451 },
5452 arm: {
5453 src: "myapex-arm.apex",
5454 },
5455 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005456 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5457 }
5458
5459 prebuilt_bootclasspath_fragment {
5460 name: "my-bootclasspath-fragment",
5461 contents: ["libfoo", "libbar"],
5462 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005463 hidden_api: {
5464 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5465 metadata: "my-bootclasspath-fragment/metadata.csv",
5466 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005467 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5468 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5469 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005470 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005471 }
5472
5473 java_import {
5474 name: "libfoo",
5475 jars: ["libfoo.jar"],
5476 apex_available: ["myapex"],
5477 }
5478
5479 java_library {
5480 name: "libfoo",
5481 srcs: ["foo/bar/MyClass.java"],
5482 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005483 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005484 }
Paul Duffin37856732021-02-26 14:24:15 +00005485
5486 java_sdk_library_import {
5487 name: "libbar",
5488 public: {
5489 jars: ["libbar.jar"],
5490 },
5491 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005492 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005493 }
5494
5495 java_sdk_library {
5496 name: "libbar",
5497 srcs: ["foo/bar/MyClass.java"],
5498 unsafe_ignore_missing_latest_api: true,
5499 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005500 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005501 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005502 `
5503
Paul Duffin89f570a2021-06-16 01:42:33 +01005504 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005505 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5506 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005507
Paul Duffin537ea3d2021-05-14 10:38:00 +01005508 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005509 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005510 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005511 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005512 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5513 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005514 })
5515
5516 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5517 bp := `
5518 apex {
5519 name: "myapex",
5520 enabled: false,
5521 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005522 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005523 }
5524
5525 apex_key {
5526 name: "myapex.key",
5527 public_key: "testkey.avbpubkey",
5528 private_key: "testkey.pem",
5529 }
5530
5531 prebuilt_apex {
5532 name: "myapex",
5533 arch: {
5534 arm64: {
5535 src: "myapex-arm64.apex",
5536 },
5537 arm: {
5538 src: "myapex-arm.apex",
5539 },
5540 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005541 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5542 }
5543
5544 prebuilt_bootclasspath_fragment {
5545 name: "my-bootclasspath-fragment",
5546 contents: ["libfoo", "libbar"],
5547 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005548 hidden_api: {
5549 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5550 metadata: "my-bootclasspath-fragment/metadata.csv",
5551 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005552 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5553 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5554 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005555 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005556 }
5557
5558 java_import {
5559 name: "libfoo",
5560 prefer: true,
5561 jars: ["libfoo.jar"],
5562 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005563 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005564 }
5565
5566 java_library {
5567 name: "libfoo",
5568 srcs: ["foo/bar/MyClass.java"],
5569 apex_available: ["myapex"],
5570 }
Paul Duffin37856732021-02-26 14:24:15 +00005571
5572 java_sdk_library_import {
5573 name: "libbar",
5574 prefer: true,
5575 public: {
5576 jars: ["libbar.jar"],
5577 },
5578 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005579 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005580 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005581 }
5582
5583 java_sdk_library {
5584 name: "libbar",
5585 srcs: ["foo/bar/MyClass.java"],
5586 unsafe_ignore_missing_latest_api: true,
5587 apex_available: ["myapex"],
5588 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005589 `
5590
Paul Duffin89f570a2021-06-16 01:42:33 +01005591 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005592 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5593 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005594
Paul Duffin537ea3d2021-05-14 10:38:00 +01005595 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005596 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005597 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005598 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005599 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5600 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601 })
5602}
5603
Roland Levillain630846d2019-06-26 12:48:34 +01005604func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005605 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005606 apex_test {
5607 name: "myapex",
5608 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005609 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005610 tests: [
5611 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005612 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005613 ],
5614 }
5615
5616 apex_key {
5617 name: "myapex.key",
5618 public_key: "testkey.avbpubkey",
5619 private_key: "testkey.pem",
5620 }
5621
Liz Kammer1c14a212020-05-12 15:26:55 -07005622 filegroup {
5623 name: "fg",
5624 srcs: [
5625 "baz",
5626 "bar/baz"
5627 ],
5628 }
5629
Roland Levillain630846d2019-06-26 12:48:34 +01005630 cc_test {
5631 name: "mytest",
5632 gtest: false,
5633 srcs: ["mytest.cpp"],
5634 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005635 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005636 system_shared_libs: [],
5637 static_executable: true,
5638 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005639 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005640 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005641
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005642 cc_library {
5643 name: "mylib",
5644 srcs: ["mylib.cpp"],
5645 system_shared_libs: [],
5646 stl: "none",
5647 }
5648
Liz Kammer5bd365f2020-05-27 15:15:11 -07005649 filegroup {
5650 name: "fg2",
5651 srcs: [
5652 "testdata/baz"
5653 ],
5654 }
5655
Roland Levillain9b5fde92019-06-28 15:41:19 +01005656 cc_test {
5657 name: "mytests",
5658 gtest: false,
5659 srcs: [
5660 "mytest1.cpp",
5661 "mytest2.cpp",
5662 "mytest3.cpp",
5663 ],
5664 test_per_src: true,
5665 relative_install_path: "test",
5666 system_shared_libs: [],
5667 static_executable: true,
5668 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005669 data: [
5670 ":fg",
5671 ":fg2",
5672 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005673 }
Roland Levillain630846d2019-06-26 12:48:34 +01005674 `)
5675
Sundong Ahnabb64432019-10-22 13:58:29 +09005676 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005677 copyCmds := apexRule.Args["copy_commands"]
5678
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005679 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005680 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005681 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005682
Liz Kammer1c14a212020-05-12 15:26:55 -07005683 //Ensure that test data are copied into apex.
5684 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5685 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5686
Roland Levillain9b5fde92019-06-28 15:41:19 +01005687 // Ensure that test deps built with `test_per_src` are copied into apex.
5688 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5689 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5690 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005691
5692 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005693 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005694 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005695 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005696 prefix := "TARGET_"
5697 var builder strings.Builder
5698 data.Custom(&builder, name, prefix, "", data)
5699 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005700 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5701 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5702 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5703 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5704 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5705 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005706 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005707
5708 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005709 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005710 data.Custom(&builder, name, prefix, "", data)
5711 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005712 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5713 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005714}
5715
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005716func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005717 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005718 apex {
5719 name: "myapex",
5720 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005721 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005722 }
5723 apex_key {
5724 name: "myapex.key",
5725 public_key: "testkey.avbpubkey",
5726 private_key: "testkey.pem",
5727 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005728 `,
5729 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5730 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5731 }),
5732 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005733 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005734 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005735 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005736 var builder strings.Builder
5737 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5738 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005739 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005740}
5741
Jooyung Hand48f3c32019-08-23 11:18:57 +09005742func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5743 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5744 apex {
5745 name: "myapex",
5746 key: "myapex.key",
5747 native_shared_libs: ["libfoo"],
5748 }
5749
5750 apex_key {
5751 name: "myapex.key",
5752 public_key: "testkey.avbpubkey",
5753 private_key: "testkey.pem",
5754 }
5755
5756 cc_library {
5757 name: "libfoo",
5758 stl: "none",
5759 system_shared_libs: [],
5760 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005761 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005762 }
5763 `)
5764 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5765 apex {
5766 name: "myapex",
5767 key: "myapex.key",
5768 java_libs: ["myjar"],
5769 }
5770
5771 apex_key {
5772 name: "myapex.key",
5773 public_key: "testkey.avbpubkey",
5774 private_key: "testkey.pem",
5775 }
5776
5777 java_library {
5778 name: "myjar",
5779 srcs: ["foo/bar/MyClass.java"],
5780 sdk_version: "none",
5781 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005782 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005783 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005784 }
5785 `)
5786}
5787
Bill Peckhama41a6962021-01-11 10:58:54 -08005788func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005789 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005790 apex {
5791 name: "myapex",
5792 key: "myapex.key",
5793 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005794 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005795 }
5796
5797 apex_key {
5798 name: "myapex.key",
5799 public_key: "testkey.avbpubkey",
5800 private_key: "testkey.pem",
5801 }
5802
5803 java_import {
5804 name: "myjavaimport",
5805 apex_available: ["myapex"],
5806 jars: ["my.jar"],
5807 compile_dex: true,
5808 }
5809 `)
5810
5811 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5812 apexRule := module.Rule("apexRule")
5813 copyCmds := apexRule.Args["copy_commands"]
5814 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5815}
5816
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005817func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005818 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005819 apex {
5820 name: "myapex",
5821 key: "myapex.key",
5822 apps: [
5823 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005824 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005825 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005826 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005827 }
5828
5829 apex_key {
5830 name: "myapex.key",
5831 public_key: "testkey.avbpubkey",
5832 private_key: "testkey.pem",
5833 }
5834
5835 android_app {
5836 name: "AppFoo",
5837 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005838 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005839 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005840 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005841 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005842 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005843 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005844
5845 android_app {
5846 name: "AppFooPriv",
5847 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005848 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005849 system_modules: "none",
5850 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005851 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005852 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005853 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005854
5855 cc_library_shared {
5856 name: "libjni",
5857 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005858 shared_libs: ["libfoo"],
5859 stl: "none",
5860 system_shared_libs: [],
5861 apex_available: [ "myapex" ],
5862 sdk_version: "current",
5863 }
5864
5865 cc_library_shared {
5866 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005867 stl: "none",
5868 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005869 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005870 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005871 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005872 `)
5873
Sundong Ahnabb64432019-10-22 13:58:29 +09005874 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005875 apexRule := module.Rule("apexRule")
5876 copyCmds := apexRule.Args["copy_commands"]
5877
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005878 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5879 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005880
Colin Crossaede88c2020-08-11 12:17:01 -07005881 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005882 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005883 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005884 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005885 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005886 // JNI libraries including transitive deps are
5887 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005888 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005889 // ... embedded inside APK (jnilibs.zip)
5890 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5891 // ... and not directly inside the APEX
5892 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5893 }
Dario Frenicde2a032019-10-27 00:29:22 +01005894}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005895
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005896func TestApexWithAppImportBuildId(t *testing.T) {
5897 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5898 for _, id := range invalidBuildIds {
5899 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5900 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5901 variables.BuildId = proptools.StringPtr(id)
5902 })
5903 testApexError(t, message, `apex {
5904 name: "myapex",
5905 key: "myapex.key",
5906 apps: ["AppFooPrebuilt"],
5907 updatable: false,
5908 }
5909
5910 apex_key {
5911 name: "myapex.key",
5912 public_key: "testkey.avbpubkey",
5913 private_key: "testkey.pem",
5914 }
5915
5916 android_app_import {
5917 name: "AppFooPrebuilt",
5918 apk: "PrebuiltAppFoo.apk",
5919 presigned: true,
5920 apex_available: ["myapex"],
5921 }
5922 `, fixture)
5923 }
5924}
5925
Dario Frenicde2a032019-10-27 00:29:22 +01005926func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005927 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005928 apex {
5929 name: "myapex",
5930 key: "myapex.key",
5931 apps: [
5932 "AppFooPrebuilt",
5933 "AppFooPrivPrebuilt",
5934 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005935 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005936 }
5937
5938 apex_key {
5939 name: "myapex.key",
5940 public_key: "testkey.avbpubkey",
5941 private_key: "testkey.pem",
5942 }
5943
5944 android_app_import {
5945 name: "AppFooPrebuilt",
5946 apk: "PrebuiltAppFoo.apk",
5947 presigned: true,
5948 dex_preopt: {
5949 enabled: false,
5950 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005951 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005952 }
5953
5954 android_app_import {
5955 name: "AppFooPrivPrebuilt",
5956 apk: "PrebuiltAppFooPriv.apk",
5957 privileged: true,
5958 presigned: true,
5959 dex_preopt: {
5960 enabled: false,
5961 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005962 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005963 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005964 }
5965 `)
5966
Sundong Ahnabb64432019-10-22 13:58:29 +09005967 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005968 apexRule := module.Rule("apexRule")
5969 copyCmds := apexRule.Args["copy_commands"]
5970
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005971 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5972 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005973}
5974
5975func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005976 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005977 apex {
5978 name: "myapex",
5979 key: "myapex.key",
5980 apps: [
5981 "AppFoo",
5982 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005983 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005984 }
5985
5986 apex_key {
5987 name: "myapex.key",
5988 public_key: "testkey.avbpubkey",
5989 private_key: "testkey.pem",
5990 }
5991
5992 android_app {
5993 name: "AppFoo",
5994 srcs: ["foo/bar/MyClass.java"],
5995 sdk_version: "none",
5996 system_modules: "none",
5997 apex_available: [ "myapex" ],
5998 }
5999
6000 android_app_import {
6001 name: "AppFoo",
6002 apk: "AppFooPrebuilt.apk",
6003 filename: "AppFooPrebuilt.apk",
6004 presigned: true,
6005 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006006 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006007 }
6008 `, withFiles(map[string][]byte{
6009 "AppFooPrebuilt.apk": nil,
6010 }))
6011
6012 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006013 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006014 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006015}
6016
Dario Freni6f3937c2019-12-20 22:58:03 +00006017func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006018 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006019 apex {
6020 name: "myapex",
6021 key: "myapex.key",
6022 apps: [
6023 "TesterHelpAppFoo",
6024 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006025 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006026 }
6027
6028 apex_key {
6029 name: "myapex.key",
6030 public_key: "testkey.avbpubkey",
6031 private_key: "testkey.pem",
6032 }
6033
6034 android_test_helper_app {
6035 name: "TesterHelpAppFoo",
6036 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006037 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006038 }
6039
6040 `)
6041
6042 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6043 apexRule := module.Rule("apexRule")
6044 copyCmds := apexRule.Args["copy_commands"]
6045
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006046 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006047}
6048
Jooyung Han18020ea2019-11-13 10:50:48 +09006049func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6050 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006051 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006052 apex {
6053 name: "myapex",
6054 key: "myapex.key",
6055 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006056 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006057 }
6058
6059 apex_key {
6060 name: "myapex.key",
6061 public_key: "testkey.avbpubkey",
6062 private_key: "testkey.pem",
6063 }
6064
6065 apex {
6066 name: "otherapex",
6067 key: "myapex.key",
6068 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006069 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006070 }
6071
6072 cc_defaults {
6073 name: "libfoo-defaults",
6074 apex_available: ["otherapex"],
6075 }
6076
6077 cc_library {
6078 name: "libfoo",
6079 defaults: ["libfoo-defaults"],
6080 stl: "none",
6081 system_shared_libs: [],
6082 }`)
6083}
6084
Paul Duffine52e66f2020-03-30 17:54:29 +01006085func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006086 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006087 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006088 apex {
6089 name: "myapex",
6090 key: "myapex.key",
6091 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006092 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006093 }
6094
6095 apex_key {
6096 name: "myapex.key",
6097 public_key: "testkey.avbpubkey",
6098 private_key: "testkey.pem",
6099 }
6100
6101 apex {
6102 name: "otherapex",
6103 key: "otherapex.key",
6104 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006105 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006106 }
6107
6108 apex_key {
6109 name: "otherapex.key",
6110 public_key: "testkey.avbpubkey",
6111 private_key: "testkey.pem",
6112 }
6113
6114 cc_library {
6115 name: "libfoo",
6116 stl: "none",
6117 system_shared_libs: [],
6118 apex_available: ["otherapex"],
6119 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006120}
Jiyong Park127b40b2019-09-30 16:04:35 +09006121
Paul Duffine52e66f2020-03-30 17:54:29 +01006122func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006123 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006124 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006125.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006126.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006127.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006128.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006129.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006130.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006131 apex {
6132 name: "myapex",
6133 key: "myapex.key",
6134 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006135 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006136 }
6137
6138 apex_key {
6139 name: "myapex.key",
6140 public_key: "testkey.avbpubkey",
6141 private_key: "testkey.pem",
6142 }
6143
Jiyong Park127b40b2019-09-30 16:04:35 +09006144 cc_library {
6145 name: "libfoo",
6146 stl: "none",
6147 shared_libs: ["libbar"],
6148 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006149 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006150 }
6151
6152 cc_library {
6153 name: "libbar",
6154 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006155 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006156 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006157 apex_available: ["myapex"],
6158 }
6159
6160 cc_library {
6161 name: "libbaz",
6162 stl: "none",
6163 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006164 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006165}
Jiyong Park127b40b2019-09-30 16:04:35 +09006166
Paul Duffine52e66f2020-03-30 17:54:29 +01006167func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006168 testApexError(t, "\"otherapex\" is not a valid module name", `
6169 apex {
6170 name: "myapex",
6171 key: "myapex.key",
6172 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006173 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006174 }
6175
6176 apex_key {
6177 name: "myapex.key",
6178 public_key: "testkey.avbpubkey",
6179 private_key: "testkey.pem",
6180 }
6181
6182 cc_library {
6183 name: "libfoo",
6184 stl: "none",
6185 system_shared_libs: [],
6186 apex_available: ["otherapex"],
6187 }`)
6188
Paul Duffine52e66f2020-03-30 17:54:29 +01006189 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006190 apex {
6191 name: "myapex",
6192 key: "myapex.key",
6193 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006194 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006195 }
6196
6197 apex_key {
6198 name: "myapex.key",
6199 public_key: "testkey.avbpubkey",
6200 private_key: "testkey.pem",
6201 }
6202
6203 cc_library {
6204 name: "libfoo",
6205 stl: "none",
6206 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006207 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006208 apex_available: ["myapex"],
6209 }
6210
6211 cc_library {
6212 name: "libbar",
6213 stl: "none",
6214 system_shared_libs: [],
6215 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006216 }
6217
6218 cc_library {
6219 name: "libbaz",
6220 stl: "none",
6221 system_shared_libs: [],
6222 stubs: {
6223 versions: ["10", "20", "30"],
6224 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006225 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006226}
Jiyong Park127b40b2019-09-30 16:04:35 +09006227
Jiyong Park89e850a2020-04-07 16:37:39 +09006228func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006229 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006230 apex {
6231 name: "myapex",
6232 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006233 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006234 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006235 }
6236
6237 apex_key {
6238 name: "myapex.key",
6239 public_key: "testkey.avbpubkey",
6240 private_key: "testkey.pem",
6241 }
6242
6243 cc_library {
6244 name: "libfoo",
6245 stl: "none",
6246 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006247 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006248 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006249 }
6250
6251 cc_library {
6252 name: "libfoo2",
6253 stl: "none",
6254 system_shared_libs: [],
6255 shared_libs: ["libbaz"],
6256 apex_available: ["//apex_available:platform"],
6257 }
6258
6259 cc_library {
6260 name: "libbar",
6261 stl: "none",
6262 system_shared_libs: [],
6263 apex_available: ["myapex"],
6264 }
6265
6266 cc_library {
6267 name: "libbaz",
6268 stl: "none",
6269 system_shared_libs: [],
6270 apex_available: ["myapex"],
6271 stubs: {
6272 versions: ["1"],
6273 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006274 }`)
6275
Jiyong Park89e850a2020-04-07 16:37:39 +09006276 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6277 // because it depends on libbar which isn't available to platform
6278 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6279 if libfoo.NotAvailableForPlatform() != true {
6280 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6281 }
6282
6283 // libfoo2 however can be available to platform because it depends on libbaz which provides
6284 // stubs
6285 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6286 if libfoo2.NotAvailableForPlatform() == true {
6287 t.Errorf("%q should be available to platform", libfoo2.String())
6288 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006289}
Jiyong Parka90ca002019-10-07 15:47:24 +09006290
Paul Duffine52e66f2020-03-30 17:54:29 +01006291func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006292 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006293 apex {
6294 name: "myapex",
6295 key: "myapex.key",
6296 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006297 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006298 }
6299
6300 apex_key {
6301 name: "myapex.key",
6302 public_key: "testkey.avbpubkey",
6303 private_key: "testkey.pem",
6304 }
6305
6306 cc_library {
6307 name: "libfoo",
6308 stl: "none",
6309 system_shared_libs: [],
6310 apex_available: ["myapex"],
6311 static: {
6312 apex_available: ["//apex_available:platform"],
6313 },
6314 }`)
6315
Jiyong Park89e850a2020-04-07 16:37:39 +09006316 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6317 if libfooShared.NotAvailableForPlatform() != true {
6318 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6319 }
6320 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6321 if libfooStatic.NotAvailableForPlatform() != false {
6322 t.Errorf("%q should be available to platform", libfooStatic.String())
6323 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006324}
6325
Jiyong Park5d790c32019-11-15 18:40:32 +09006326func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006327 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006328 apex {
6329 name: "myapex",
6330 key: "myapex.key",
6331 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006332 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006333 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006334 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006335 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006336 }
6337
6338 override_apex {
6339 name: "override_myapex",
6340 base: "myapex",
6341 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006342 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006343 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006344 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006345 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006346 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006347 key: "mynewapex.key",
6348 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006349 }
6350
6351 apex_key {
6352 name: "myapex.key",
6353 public_key: "testkey.avbpubkey",
6354 private_key: "testkey.pem",
6355 }
6356
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006357 apex_key {
6358 name: "mynewapex.key",
6359 public_key: "testkey2.avbpubkey",
6360 private_key: "testkey2.pem",
6361 }
6362
6363 android_app_certificate {
6364 name: "myapex.certificate",
6365 certificate: "testkey",
6366 }
6367
Jiyong Park5d790c32019-11-15 18:40:32 +09006368 android_app {
6369 name: "app",
6370 srcs: ["foo/bar/MyClass.java"],
6371 package_name: "foo",
6372 sdk_version: "none",
6373 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006374 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006375 }
6376
6377 override_android_app {
6378 name: "override_app",
6379 base: "app",
6380 package_name: "bar",
6381 }
markchien7c803b82021-08-26 22:10:06 +08006382
6383 bpf {
6384 name: "bpf",
6385 srcs: ["bpf.c"],
6386 }
6387
6388 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006389 name: "overrideBpf",
6390 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006391 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006392
6393 prebuilt_etc {
6394 name: "myetc",
6395 src: "myprebuilt",
6396 }
6397
6398 prebuilt_etc {
6399 name: "override_myetc",
6400 src: "override_myprebuilt",
6401 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006402 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006403
Jiyong Park317645e2019-12-05 13:20:58 +09006404 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6405 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6406 if originalVariant.GetOverriddenBy() != "" {
6407 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6408 }
6409 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6410 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6411 }
6412
Jiyong Park5d790c32019-11-15 18:40:32 +09006413 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6414 apexRule := module.Rule("apexRule")
6415 copyCmds := apexRule.Args["copy_commands"]
6416
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006417 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6418 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006419
markchien7c803b82021-08-26 22:10:06 +08006420 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006421 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006422
Daniel Norman5a3ce132021-08-26 15:44:43 -07006423 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6424 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6425
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006426 apexBundle := module.Module().(*apexBundle)
6427 name := apexBundle.Name()
6428 if name != "override_myapex" {
6429 t.Errorf("name should be \"override_myapex\", but was %q", name)
6430 }
6431
Baligh Uddin004d7172020-02-19 21:29:28 -08006432 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6433 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6434 }
6435
Jiyong Park20bacab2020-03-03 11:45:41 +09006436 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006437 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006438 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6439
6440 signApkRule := module.Rule("signapk")
6441 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006442
Colin Crossaa255532020-07-03 13:18:24 -07006443 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006444 var builder strings.Builder
6445 data.Custom(&builder, name, "TARGET_", "", data)
6446 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006447 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6448 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6449 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006450 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006451 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006452 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006453 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006454 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006455 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6456 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006457}
6458
Albert Martineefabcf2022-03-21 20:11:16 +00006459func TestMinSdkVersionOverride(t *testing.T) {
6460 // Override from 29 to 31
6461 minSdkOverride31 := "31"
6462 ctx := testApex(t, `
6463 apex {
6464 name: "myapex",
6465 key: "myapex.key",
6466 native_shared_libs: ["mylib"],
6467 updatable: true,
6468 min_sdk_version: "29"
6469 }
6470
6471 override_apex {
6472 name: "override_myapex",
6473 base: "myapex",
6474 logging_parent: "com.foo.bar",
6475 package_name: "test.overridden.package"
6476 }
6477
6478 apex_key {
6479 name: "myapex.key",
6480 public_key: "testkey.avbpubkey",
6481 private_key: "testkey.pem",
6482 }
6483
6484 cc_library {
6485 name: "mylib",
6486 srcs: ["mylib.cpp"],
6487 runtime_libs: ["libbar"],
6488 system_shared_libs: [],
6489 stl: "none",
6490 apex_available: [ "myapex" ],
6491 min_sdk_version: "apex_inherit"
6492 }
6493
6494 cc_library {
6495 name: "libbar",
6496 srcs: ["mylib.cpp"],
6497 system_shared_libs: [],
6498 stl: "none",
6499 apex_available: [ "myapex" ],
6500 min_sdk_version: "apex_inherit"
6501 }
6502
6503 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6504
6505 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6506 copyCmds := apexRule.Args["copy_commands"]
6507
6508 // Ensure that direct non-stubs dep is always included
6509 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6510
6511 // Ensure that runtime_libs dep in included
6512 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6513
6514 // Ensure libraries target overridden min_sdk_version value
6515 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6516}
6517
6518func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6519 // Attempt to override from 31 to 29, should be a NOOP
6520 minSdkOverride29 := "29"
6521 ctx := testApex(t, `
6522 apex {
6523 name: "myapex",
6524 key: "myapex.key",
6525 native_shared_libs: ["mylib"],
6526 updatable: true,
6527 min_sdk_version: "31"
6528 }
6529
6530 override_apex {
6531 name: "override_myapex",
6532 base: "myapex",
6533 logging_parent: "com.foo.bar",
6534 package_name: "test.overridden.package"
6535 }
6536
6537 apex_key {
6538 name: "myapex.key",
6539 public_key: "testkey.avbpubkey",
6540 private_key: "testkey.pem",
6541 }
6542
6543 cc_library {
6544 name: "mylib",
6545 srcs: ["mylib.cpp"],
6546 runtime_libs: ["libbar"],
6547 system_shared_libs: [],
6548 stl: "none",
6549 apex_available: [ "myapex" ],
6550 min_sdk_version: "apex_inherit"
6551 }
6552
6553 cc_library {
6554 name: "libbar",
6555 srcs: ["mylib.cpp"],
6556 system_shared_libs: [],
6557 stl: "none",
6558 apex_available: [ "myapex" ],
6559 min_sdk_version: "apex_inherit"
6560 }
6561
6562 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6563
6564 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6565 copyCmds := apexRule.Args["copy_commands"]
6566
6567 // Ensure that direct non-stubs dep is always included
6568 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6569
6570 // Ensure that runtime_libs dep in included
6571 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6572
6573 // Ensure libraries target the original min_sdk_version value rather than the overridden
6574 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6575}
6576
Jooyung Han214bf372019-11-12 13:03:50 +09006577func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006578 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006579 apex {
6580 name: "myapex",
6581 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006582 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006583 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006584 }
6585
6586 apex_key {
6587 name: "myapex.key",
6588 public_key: "testkey.avbpubkey",
6589 private_key: "testkey.pem",
6590 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006591
6592 cc_library {
6593 name: "mylib",
6594 srcs: ["mylib.cpp"],
6595 stl: "libc++",
6596 system_shared_libs: [],
6597 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006598 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006599 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006600 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006601
6602 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6603 args := module.Rule("apexRule").Args
6604 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006605 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006606
6607 // The copies of the libraries in the apex should have one more dependency than
6608 // the ones outside the apex, namely the unwinder. Ideally we should check
6609 // the dependency names directly here but for some reason the names are blank in
6610 // this test.
6611 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006612 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006613 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6614 if len(apexImplicits) != len(nonApexImplicits)+1 {
6615 t.Errorf("%q missing unwinder dep", lib)
6616 }
6617 }
Jooyung Han214bf372019-11-12 13:03:50 +09006618}
6619
Paul Duffine05480a2021-03-08 15:07:14 +00006620var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006621 "api/current.txt": nil,
6622 "api/removed.txt": nil,
6623 "api/system-current.txt": nil,
6624 "api/system-removed.txt": nil,
6625 "api/test-current.txt": nil,
6626 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006627
Anton Hanssondff2c782020-12-21 17:10:01 +00006628 "100/public/api/foo.txt": nil,
6629 "100/public/api/foo-removed.txt": nil,
6630 "100/system/api/foo.txt": nil,
6631 "100/system/api/foo-removed.txt": nil,
6632
Paul Duffineedc5d52020-06-12 17:46:39 +01006633 // For java_sdk_library_import
6634 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006635}
6636
Jooyung Han58f26ab2019-12-18 15:34:32 +09006637func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006638 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006639 apex {
6640 name: "myapex",
6641 key: "myapex.key",
6642 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006643 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006644 }
6645
6646 apex_key {
6647 name: "myapex.key",
6648 public_key: "testkey.avbpubkey",
6649 private_key: "testkey.pem",
6650 }
6651
6652 java_sdk_library {
6653 name: "foo",
6654 srcs: ["a.java"],
6655 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006656 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006657 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006658
6659 prebuilt_apis {
6660 name: "sdk",
6661 api_dirs: ["100"],
6662 }
Paul Duffin9b879592020-05-26 13:21:35 +01006663 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006664
6665 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006666 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006667 "javalib/foo.jar",
6668 "etc/permissions/foo.xml",
6669 })
6670 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006671 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006672 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 +09006673}
6674
Paul Duffin9b879592020-05-26 13:21:35 +01006675func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006676 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006677 apex {
6678 name: "myapex",
6679 key: "myapex.key",
6680 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006681 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006682 }
6683
6684 apex_key {
6685 name: "myapex.key",
6686 public_key: "testkey.avbpubkey",
6687 private_key: "testkey.pem",
6688 }
6689
6690 java_sdk_library {
6691 name: "foo",
6692 srcs: ["a.java"],
6693 api_packages: ["foo"],
6694 apex_available: ["myapex"],
6695 sdk_version: "none",
6696 system_modules: "none",
6697 }
6698
6699 java_library {
6700 name: "bar",
6701 srcs: ["a.java"],
6702 libs: ["foo"],
6703 apex_available: ["myapex"],
6704 sdk_version: "none",
6705 system_modules: "none",
6706 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006707
6708 prebuilt_apis {
6709 name: "sdk",
6710 api_dirs: ["100"],
6711 }
Paul Duffin9b879592020-05-26 13:21:35 +01006712 `, withFiles(filesForSdkLibrary))
6713
6714 // java_sdk_library installs both impl jar and permission XML
6715 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6716 "javalib/bar.jar",
6717 "javalib/foo.jar",
6718 "etc/permissions/foo.xml",
6719 })
6720
6721 // The bar library should depend on the implementation jar.
6722 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006723 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006724 t.Errorf("expected %q, found %#q", expected, actual)
6725 }
6726}
6727
6728func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006729 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006730 apex {
6731 name: "myapex",
6732 key: "myapex.key",
6733 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006734 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006735 }
6736
6737 apex_key {
6738 name: "myapex.key",
6739 public_key: "testkey.avbpubkey",
6740 private_key: "testkey.pem",
6741 }
6742
6743 java_sdk_library {
6744 name: "foo",
6745 srcs: ["a.java"],
6746 api_packages: ["foo"],
6747 apex_available: ["myapex"],
6748 sdk_version: "none",
6749 system_modules: "none",
6750 }
6751
6752 java_library {
6753 name: "bar",
6754 srcs: ["a.java"],
6755 libs: ["foo"],
6756 sdk_version: "none",
6757 system_modules: "none",
6758 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006759
6760 prebuilt_apis {
6761 name: "sdk",
6762 api_dirs: ["100"],
6763 }
Paul Duffin9b879592020-05-26 13:21:35 +01006764 `, withFiles(filesForSdkLibrary))
6765
6766 // java_sdk_library installs both impl jar and permission XML
6767 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6768 "javalib/foo.jar",
6769 "etc/permissions/foo.xml",
6770 })
6771
6772 // The bar library should depend on the stubs jar.
6773 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006774 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006775 t.Errorf("expected %q, found %#q", expected, actual)
6776 }
6777}
6778
Paul Duffineedc5d52020-06-12 17:46:39 +01006779func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006780 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006781 prebuilt_apis {
6782 name: "sdk",
6783 api_dirs: ["100"],
6784 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006785 withFiles(map[string][]byte{
6786 "apex/a.java": nil,
6787 "apex/apex_manifest.json": nil,
6788 "apex/Android.bp": []byte(`
6789 package {
6790 default_visibility: ["//visibility:private"],
6791 }
6792
6793 apex {
6794 name: "myapex",
6795 key: "myapex.key",
6796 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006797 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006798 }
6799
6800 apex_key {
6801 name: "myapex.key",
6802 public_key: "testkey.avbpubkey",
6803 private_key: "testkey.pem",
6804 }
6805
6806 java_library {
6807 name: "bar",
6808 srcs: ["a.java"],
6809 libs: ["foo"],
6810 apex_available: ["myapex"],
6811 sdk_version: "none",
6812 system_modules: "none",
6813 }
6814`),
6815 "source/a.java": nil,
6816 "source/api/current.txt": nil,
6817 "source/api/removed.txt": nil,
6818 "source/Android.bp": []byte(`
6819 package {
6820 default_visibility: ["//visibility:private"],
6821 }
6822
6823 java_sdk_library {
6824 name: "foo",
6825 visibility: ["//apex"],
6826 srcs: ["a.java"],
6827 api_packages: ["foo"],
6828 apex_available: ["myapex"],
6829 sdk_version: "none",
6830 system_modules: "none",
6831 public: {
6832 enabled: true,
6833 },
6834 }
6835`),
6836 "prebuilt/a.jar": nil,
6837 "prebuilt/Android.bp": []byte(`
6838 package {
6839 default_visibility: ["//visibility:private"],
6840 }
6841
6842 java_sdk_library_import {
6843 name: "foo",
6844 visibility: ["//apex", "//source"],
6845 apex_available: ["myapex"],
6846 prefer: true,
6847 public: {
6848 jars: ["a.jar"],
6849 },
6850 }
6851`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006852 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006853 )
6854
6855 // java_sdk_library installs both impl jar and permission XML
6856 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6857 "javalib/bar.jar",
6858 "javalib/foo.jar",
6859 "etc/permissions/foo.xml",
6860 })
6861
6862 // The bar library should depend on the implementation jar.
6863 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006864 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006865 t.Errorf("expected %q, found %#q", expected, actual)
6866 }
6867}
6868
6869func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6870 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6871 apex {
6872 name: "myapex",
6873 key: "myapex.key",
6874 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006875 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006876 }
6877
6878 apex_key {
6879 name: "myapex.key",
6880 public_key: "testkey.avbpubkey",
6881 private_key: "testkey.pem",
6882 }
6883
6884 java_sdk_library_import {
6885 name: "foo",
6886 apex_available: ["myapex"],
6887 prefer: true,
6888 public: {
6889 jars: ["a.jar"],
6890 },
6891 }
6892
6893 `, withFiles(filesForSdkLibrary))
6894}
6895
atrost6e126252020-01-27 17:01:16 +00006896func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006897 result := android.GroupFixturePreparers(
6898 prepareForApexTest,
6899 java.PrepareForTestWithPlatformCompatConfig,
6900 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006901 apex {
6902 name: "myapex",
6903 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006904 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006905 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006906 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006907 }
6908
6909 apex_key {
6910 name: "myapex.key",
6911 public_key: "testkey.avbpubkey",
6912 private_key: "testkey.pem",
6913 }
6914
6915 platform_compat_config {
6916 name: "myjar-platform-compat-config",
6917 src: ":myjar",
6918 }
6919
6920 java_library {
6921 name: "myjar",
6922 srcs: ["foo/bar/MyClass.java"],
6923 sdk_version: "none",
6924 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006925 apex_available: [ "myapex" ],
6926 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006927
6928 // Make sure that a preferred prebuilt does not affect the apex contents.
6929 prebuilt_platform_compat_config {
6930 name: "myjar-platform-compat-config",
6931 metadata: "compat-config/metadata.xml",
6932 prefer: true,
6933 }
atrost6e126252020-01-27 17:01:16 +00006934 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006935 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006936 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6937 "etc/compatconfig/myjar-platform-compat-config.xml",
6938 "javalib/myjar.jar",
6939 })
6940}
6941
Jooyung Han862c0d62022-12-21 10:15:37 +09006942func TestNoDupeApexFiles(t *testing.T) {
6943 android.GroupFixturePreparers(
6944 android.PrepareForTestWithAndroidBuildComponents,
6945 PrepareForTestWithApexBuildComponents,
6946 prepareForTestWithMyapex,
6947 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
6948 ).
6949 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
6950 RunTestWithBp(t, `
6951 apex {
6952 name: "myapex",
6953 key: "myapex.key",
6954 prebuilts: ["foo", "bar"],
6955 updatable: false,
6956 }
6957
6958 apex_key {
6959 name: "myapex.key",
6960 public_key: "testkey.avbpubkey",
6961 private_key: "testkey.pem",
6962 }
6963
6964 prebuilt_etc {
6965 name: "foo",
6966 src: "myprebuilt",
6967 filename_from_src: true,
6968 }
6969
6970 prebuilt_etc {
6971 name: "bar",
6972 src: "myprebuilt",
6973 filename_from_src: true,
6974 }
6975 `)
6976}
6977
Jiyong Park479321d2019-12-16 11:47:12 +09006978func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6979 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6980 apex {
6981 name: "myapex",
6982 key: "myapex.key",
6983 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006984 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006985 }
6986
6987 apex_key {
6988 name: "myapex.key",
6989 public_key: "testkey.avbpubkey",
6990 private_key: "testkey.pem",
6991 }
6992
6993 java_library {
6994 name: "myjar",
6995 srcs: ["foo/bar/MyClass.java"],
6996 sdk_version: "none",
6997 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006998 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006999 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007000 }
7001 `)
7002}
7003
Jiyong Park7afd1072019-12-30 16:56:33 +09007004func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007005 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007006 apex {
7007 name: "myapex",
7008 key: "myapex.key",
7009 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007010 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007011 }
7012
7013 apex_key {
7014 name: "myapex.key",
7015 public_key: "testkey.avbpubkey",
7016 private_key: "testkey.pem",
7017 }
7018
7019 cc_library {
7020 name: "mylib",
7021 srcs: ["mylib.cpp"],
7022 system_shared_libs: [],
7023 stl: "none",
7024 required: ["a", "b"],
7025 host_required: ["c", "d"],
7026 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007027 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007028 }
7029 `)
7030
7031 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007032 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007033 name := apexBundle.BaseModuleName()
7034 prefix := "TARGET_"
7035 var builder strings.Builder
7036 data.Custom(&builder, name, prefix, "", data)
7037 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007038 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 -08007039 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7040 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007041}
7042
Jiyong Park7cd10e32020-01-14 09:22:18 +09007043func TestSymlinksFromApexToSystem(t *testing.T) {
7044 bp := `
7045 apex {
7046 name: "myapex",
7047 key: "myapex.key",
7048 native_shared_libs: ["mylib"],
7049 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007050 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007051 }
7052
Jiyong Park9d677202020-02-19 16:29:35 +09007053 apex {
7054 name: "myapex.updatable",
7055 key: "myapex.key",
7056 native_shared_libs: ["mylib"],
7057 java_libs: ["myjar"],
7058 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09007059 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09007060 }
7061
Jiyong Park7cd10e32020-01-14 09:22:18 +09007062 apex_key {
7063 name: "myapex.key",
7064 public_key: "testkey.avbpubkey",
7065 private_key: "testkey.pem",
7066 }
7067
7068 cc_library {
7069 name: "mylib",
7070 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007071 shared_libs: [
7072 "myotherlib",
7073 "myotherlib_ext",
7074 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007075 system_shared_libs: [],
7076 stl: "none",
7077 apex_available: [
7078 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007079 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007080 "//apex_available:platform",
7081 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007082 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007083 }
7084
7085 cc_library {
7086 name: "myotherlib",
7087 srcs: ["mylib.cpp"],
7088 system_shared_libs: [],
7089 stl: "none",
7090 apex_available: [
7091 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007092 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007093 "//apex_available:platform",
7094 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007095 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007096 }
7097
Jiyong Parkce243632023-02-17 18:22:25 +09007098 cc_library {
7099 name: "myotherlib_ext",
7100 srcs: ["mylib.cpp"],
7101 system_shared_libs: [],
7102 system_ext_specific: true,
7103 stl: "none",
7104 apex_available: [
7105 "myapex",
7106 "myapex.updatable",
7107 "//apex_available:platform",
7108 ],
7109 min_sdk_version: "current",
7110 }
7111
Jiyong Park7cd10e32020-01-14 09:22:18 +09007112 java_library {
7113 name: "myjar",
7114 srcs: ["foo/bar/MyClass.java"],
7115 sdk_version: "none",
7116 system_modules: "none",
7117 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007118 apex_available: [
7119 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007120 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007121 "//apex_available:platform",
7122 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007123 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007124 }
7125
7126 java_library {
7127 name: "myotherjar",
7128 srcs: ["foo/bar/MyClass.java"],
7129 sdk_version: "none",
7130 system_modules: "none",
7131 apex_available: [
7132 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007133 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007134 "//apex_available:platform",
7135 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007136 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007137 }
7138 `
7139
7140 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7141 for _, f := range files {
7142 if f.path == file {
7143 if f.isLink {
7144 t.Errorf("%q is not a real file", file)
7145 }
7146 return
7147 }
7148 }
7149 t.Errorf("%q is not found", file)
7150 }
7151
Jiyong Parkce243632023-02-17 18:22:25 +09007152 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007153 for _, f := range files {
7154 if f.path == file {
7155 if !f.isLink {
7156 t.Errorf("%q is not a symlink", file)
7157 }
Jiyong Parkce243632023-02-17 18:22:25 +09007158 if f.src != target {
7159 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7160 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007161 return
7162 }
7163 }
7164 t.Errorf("%q is not found", file)
7165 }
7166
Jiyong Park9d677202020-02-19 16:29:35 +09007167 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7168 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007169 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007170 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007171 ensureRealfileExists(t, files, "javalib/myjar.jar")
7172 ensureRealfileExists(t, files, "lib64/mylib.so")
7173 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007174 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007175
Jiyong Park9d677202020-02-19 16:29:35 +09007176 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7177 ensureRealfileExists(t, files, "javalib/myjar.jar")
7178 ensureRealfileExists(t, files, "lib64/mylib.so")
7179 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007180 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007181
7182 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007183 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007184 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007185 ensureRealfileExists(t, files, "javalib/myjar.jar")
7186 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007187 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7188 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007189
7190 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7191 ensureRealfileExists(t, files, "javalib/myjar.jar")
7192 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007193 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7194 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007195}
7196
Yo Chiange8128052020-07-23 20:09:18 +08007197func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007198 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007199 apex {
7200 name: "myapex",
7201 key: "myapex.key",
7202 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007203 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007204 }
7205
7206 apex_key {
7207 name: "myapex.key",
7208 public_key: "testkey.avbpubkey",
7209 private_key: "testkey.pem",
7210 }
7211
7212 cc_library_shared {
7213 name: "mylib",
7214 srcs: ["mylib.cpp"],
7215 shared_libs: ["myotherlib"],
7216 system_shared_libs: [],
7217 stl: "none",
7218 apex_available: [
7219 "myapex",
7220 "//apex_available:platform",
7221 ],
7222 }
7223
7224 cc_prebuilt_library_shared {
7225 name: "myotherlib",
7226 srcs: ["prebuilt.so"],
7227 system_shared_libs: [],
7228 stl: "none",
7229 apex_available: [
7230 "myapex",
7231 "//apex_available:platform",
7232 ],
7233 }
7234 `)
7235
Prerana Patilb1896c82022-11-09 18:14:34 +00007236 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007237 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007238 var builder strings.Builder
7239 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7240 androidMk := builder.String()
7241 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007242 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007243 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7244 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7245 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007246 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 +08007247}
7248
Jooyung Han643adc42020-02-27 13:50:06 +09007249func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007250 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007251 apex {
7252 name: "myapex",
7253 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007254 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007255 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007256 }
7257
7258 apex_key {
7259 name: "myapex.key",
7260 public_key: "testkey.avbpubkey",
7261 private_key: "testkey.pem",
7262 }
7263
7264 cc_library {
7265 name: "mylib",
7266 srcs: ["mylib.cpp"],
7267 shared_libs: ["mylib2"],
7268 system_shared_libs: [],
7269 stl: "none",
7270 apex_available: [ "myapex" ],
7271 }
7272
7273 cc_library {
7274 name: "mylib2",
7275 srcs: ["mylib.cpp"],
7276 system_shared_libs: [],
7277 stl: "none",
7278 apex_available: [ "myapex" ],
7279 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007280
7281 rust_ffi_shared {
7282 name: "libfoo.rust",
7283 crate_name: "foo",
7284 srcs: ["foo.rs"],
7285 shared_libs: ["libfoo.shared_from_rust"],
7286 prefer_rlib: true,
7287 apex_available: ["myapex"],
7288 }
7289
7290 cc_library_shared {
7291 name: "libfoo.shared_from_rust",
7292 srcs: ["mylib.cpp"],
7293 system_shared_libs: [],
7294 stl: "none",
7295 stubs: {
7296 versions: ["10", "11", "12"],
7297 },
7298 }
7299
Jooyung Han643adc42020-02-27 13:50:06 +09007300 `)
7301
7302 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7303 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007304 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007305 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7306 "lib64/mylib.so",
7307 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007308 "lib64/libfoo.rust.so",
7309 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7310 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007311 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007312
7313 // b/220397949
7314 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007315}
7316
Jooyung Han49f67012020-04-17 13:43:10 +09007317func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007318 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007319 apex {
7320 name: "myapex",
7321 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007322 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007323 }
7324 apex_key {
7325 name: "myapex.key",
7326 public_key: "testkey.avbpubkey",
7327 private_key: "testkey.pem",
7328 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007329 `,
7330 android.FixtureModifyConfig(func(config android.Config) {
7331 delete(config.Targets, android.Android)
7332 config.AndroidCommonTarget = android.Target{}
7333 }),
7334 )
Jooyung Han49f67012020-04-17 13:43:10 +09007335
7336 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7337 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7338 }
7339}
7340
Jiyong Parkbd159612020-02-28 15:22:21 +09007341func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007342 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007343 apex {
7344 name: "myapex",
7345 key: "myapex.key",
7346 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007347 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007348 }
7349
7350 apex_key {
7351 name: "myapex.key",
7352 public_key: "testkey.avbpubkey",
7353 private_key: "testkey.pem",
7354 }
7355
7356 android_app {
7357 name: "AppFoo",
7358 srcs: ["foo/bar/MyClass.java"],
7359 sdk_version: "none",
7360 system_modules: "none",
7361 apex_available: [ "myapex" ],
7362 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007363 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007364
Colin Crosscf371cc2020-11-13 11:48:42 -08007365 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007366 content := bundleConfigRule.Args["content"]
7367
7368 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007369 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 +09007370}
7371
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007372func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007373 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007374 apex {
7375 name: "myapex",
7376 key: "myapex.key",
7377 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007378 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007379 }
7380
7381 apex_key {
7382 name: "myapex.key",
7383 public_key: "testkey.avbpubkey",
7384 private_key: "testkey.pem",
7385 }
7386
7387 android_app_set {
7388 name: "AppSet",
7389 set: "AppSet.apks",
7390 }`)
7391 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007392 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007393 content := bundleConfigRule.Args["content"]
7394 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7395 s := mod.Rule("apexRule").Args["copy_commands"]
7396 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007397 if len(copyCmds) != 4 {
7398 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007399 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007400 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7401 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007402 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7403 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007404
7405 // Ensure that canned_fs_config has an entry for the app set zip file
7406 generateFsRule := mod.Rule("generateFsConfig")
7407 cmd := generateFsRule.RuleParams.Command
7408 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007409}
7410
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007411func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007412 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007413 apex_set {
7414 name: "myapex",
7415 filename: "foo_v2.apex",
7416 sanitized: {
7417 none: { set: "myapex.apks", },
7418 hwaddress: { set: "myapex.hwasan.apks", },
7419 },
Paul Duffin24704672021-04-06 16:09:30 +01007420 }
7421 `
7422 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007423
Paul Duffin24704672021-04-06 16:09:30 +01007424 // Check that the extractor produces the correct output file from the correct input file.
7425 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007426
Paul Duffin24704672021-04-06 16:09:30 +01007427 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7428 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007429
Paul Duffin24704672021-04-06 16:09:30 +01007430 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7431
7432 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007433 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7434 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007435
7436 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007437}
7438
Pranav Guptaeba03b02022-09-27 00:27:08 +00007439func TestApexSetApksModuleAssignment(t *testing.T) {
7440 ctx := testApex(t, `
7441 apex_set {
7442 name: "myapex",
7443 set: ":myapex_apks_file",
7444 }
7445
7446 filegroup {
7447 name: "myapex_apks_file",
7448 srcs: ["myapex.apks"],
7449 }
7450 `)
7451
7452 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7453
7454 // Check that the extractor produces the correct apks file from the input module
7455 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7456 extractedApex := m.Output(extractorOutput)
7457
7458 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7459}
7460
Paul Duffin89f570a2021-06-16 01:42:33 +01007461func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007462 t.Helper()
7463
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007464 bp := `
7465 java_library {
7466 name: "some-updatable-apex-lib",
7467 srcs: ["a.java"],
7468 sdk_version: "current",
7469 apex_available: [
7470 "some-updatable-apex",
7471 ],
satayevabcd5972021-08-06 17:49:46 +01007472 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007473 }
7474
7475 java_library {
7476 name: "some-non-updatable-apex-lib",
7477 srcs: ["a.java"],
7478 apex_available: [
7479 "some-non-updatable-apex",
7480 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007481 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007482 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007483 }
7484
7485 bootclasspath_fragment {
7486 name: "some-non-updatable-fragment",
7487 contents: ["some-non-updatable-apex-lib"],
7488 apex_available: [
7489 "some-non-updatable-apex",
7490 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007491 hidden_api: {
7492 split_packages: ["*"],
7493 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007494 }
7495
7496 java_library {
7497 name: "some-platform-lib",
7498 srcs: ["a.java"],
7499 sdk_version: "current",
7500 installable: true,
7501 }
7502
7503 java_library {
7504 name: "some-art-lib",
7505 srcs: ["a.java"],
7506 sdk_version: "current",
7507 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007508 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007509 ],
7510 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007511 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007512 }
7513
7514 apex {
7515 name: "some-updatable-apex",
7516 key: "some-updatable-apex.key",
7517 java_libs: ["some-updatable-apex-lib"],
7518 updatable: true,
7519 min_sdk_version: "current",
7520 }
7521
7522 apex {
7523 name: "some-non-updatable-apex",
7524 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007525 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007526 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007527 }
7528
7529 apex_key {
7530 name: "some-updatable-apex.key",
7531 }
7532
7533 apex_key {
7534 name: "some-non-updatable-apex.key",
7535 }
7536
7537 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007538 name: "com.android.art.debug",
7539 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007540 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007541 updatable: true,
7542 min_sdk_version: "current",
7543 }
7544
Paul Duffinf23bc472021-04-27 12:42:20 +01007545 bootclasspath_fragment {
7546 name: "art-bootclasspath-fragment",
7547 image_name: "art",
7548 contents: ["some-art-lib"],
7549 apex_available: [
7550 "com.android.art.debug",
7551 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007552 hidden_api: {
7553 split_packages: ["*"],
7554 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007555 }
7556
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007557 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007558 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007559 }
7560
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007561 filegroup {
7562 name: "some-updatable-apex-file_contexts",
7563 srcs: [
7564 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7565 ],
7566 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007567
7568 filegroup {
7569 name: "some-non-updatable-apex-file_contexts",
7570 srcs: [
7571 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7572 ],
7573 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007574 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007575
Paul Duffin89f570a2021-06-16 01:42:33 +01007576 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007577}
7578
Paul Duffin89f570a2021-06-16 01:42:33 +01007579func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007580 t.Helper()
7581
Paul Duffin55607122021-03-30 23:32:51 +01007582 fs := android.MockFS{
7583 "a.java": nil,
7584 "a.jar": nil,
7585 "apex_manifest.json": nil,
7586 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007587 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007588 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7589 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7590 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007591 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007592 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007593
Paul Duffin55607122021-03-30 23:32:51 +01007594 errorHandler := android.FixtureExpectsNoErrors
7595 if errmsg != "" {
7596 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007597 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007598
Paul Duffin55607122021-03-30 23:32:51 +01007599 result := android.GroupFixturePreparers(
7600 cc.PrepareForTestWithCcDefaultModules,
7601 java.PrepareForTestWithHiddenApiBuildComponents,
7602 java.PrepareForTestWithJavaDefaultModules,
7603 java.PrepareForTestWithJavaSdkLibraryFiles,
7604 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007605 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007606 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007607 android.FixtureModifyMockFS(func(fs android.MockFS) {
7608 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7609 insert := ""
7610 for _, fragment := range fragments {
7611 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7612 }
7613 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7614 platform_bootclasspath {
7615 name: "platform-bootclasspath",
7616 fragments: [
7617 %s
7618 ],
7619 }
7620 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007621 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007622 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007623 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007624 ).
7625 ExtendWithErrorHandler(errorHandler).
7626 RunTestWithBp(t, bp)
7627
7628 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007629}
7630
Paul Duffin5556c5f2022-06-09 17:32:21 +00007631func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007632 preparers := android.GroupFixturePreparers(
7633 java.PrepareForTestWithJavaDefaultModules,
7634 PrepareForTestWithApexBuildComponents,
7635 ).
7636 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7637 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7638
7639 bpBase := `
7640 apex_set {
7641 name: "com.android.myapex",
7642 installable: true,
7643 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7644 set: "myapex.apks",
7645 }
7646
7647 apex_set {
7648 name: "com.mycompany.android.myapex",
7649 apex_name: "com.android.myapex",
7650 installable: true,
7651 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7652 set: "company-myapex.apks",
7653 }
7654
7655 prebuilt_bootclasspath_fragment {
7656 name: "my-bootclasspath-fragment",
7657 apex_available: ["com.android.myapex"],
7658 %s
7659 }
7660 `
7661
7662 t.Run("java_import", func(t *testing.T) {
7663 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7664 java_import {
7665 name: "libfoo",
7666 jars: ["libfoo.jar"],
7667 apex_available: ["com.android.myapex"],
7668 }
7669 `)
7670 })
7671
7672 t.Run("java_sdk_library_import", func(t *testing.T) {
7673 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7674 java_sdk_library_import {
7675 name: "libfoo",
7676 public: {
7677 jars: ["libbar.jar"],
7678 },
7679 apex_available: ["com.android.myapex"],
7680 }
7681 `)
7682 })
7683
7684 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7685 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7686 image_name: "art",
7687 contents: ["libfoo"],
7688 `)+`
7689 java_sdk_library_import {
7690 name: "libfoo",
7691 public: {
7692 jars: ["libbar.jar"],
7693 },
7694 apex_available: ["com.android.myapex"],
7695 }
7696 `)
7697 })
7698}
7699
Paul Duffin5556c5f2022-06-09 17:32:21 +00007700func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7701 preparers := android.GroupFixturePreparers(
7702 java.PrepareForTestWithJavaDefaultModules,
7703 PrepareForTestWithApexBuildComponents,
7704 )
7705
7706 bpBase := `
7707 apex_set {
7708 name: "com.android.myapex",
7709 installable: true,
7710 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7711 set: "myapex.apks",
7712 }
7713
7714 apex_set {
7715 name: "com.android.myapex_compressed",
7716 apex_name: "com.android.myapex",
7717 installable: true,
7718 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7719 set: "myapex_compressed.apks",
7720 }
7721
7722 prebuilt_bootclasspath_fragment {
7723 name: "my-bootclasspath-fragment",
7724 apex_available: [
7725 "com.android.myapex",
7726 "com.android.myapex_compressed",
7727 ],
7728 hidden_api: {
7729 annotation_flags: "annotation-flags.csv",
7730 metadata: "metadata.csv",
7731 index: "index.csv",
7732 signature_patterns: "signature_patterns.csv",
7733 },
7734 %s
7735 }
7736 `
7737
7738 t.Run("java_import", func(t *testing.T) {
7739 result := preparers.RunTestWithBp(t,
7740 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7741 java_import {
7742 name: "libfoo",
7743 jars: ["libfoo.jar"],
7744 apex_available: [
7745 "com.android.myapex",
7746 "com.android.myapex_compressed",
7747 ],
7748 }
7749 `)
7750
7751 module := result.Module("libfoo", "android_common_com.android.myapex")
7752 usesLibraryDep := module.(java.UsesLibraryDependency)
7753 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7754 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7755 usesLibraryDep.DexJarBuildPath().Path())
7756 })
7757
7758 t.Run("java_sdk_library_import", func(t *testing.T) {
7759 result := preparers.RunTestWithBp(t,
7760 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7761 java_sdk_library_import {
7762 name: "libfoo",
7763 public: {
7764 jars: ["libbar.jar"],
7765 },
7766 apex_available: [
7767 "com.android.myapex",
7768 "com.android.myapex_compressed",
7769 ],
7770 compile_dex: true,
7771 }
7772 `)
7773
7774 module := result.Module("libfoo", "android_common_com.android.myapex")
7775 usesLibraryDep := module.(java.UsesLibraryDependency)
7776 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7777 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7778 usesLibraryDep.DexJarBuildPath().Path())
7779 })
7780
7781 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7782 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7783 image_name: "art",
7784 contents: ["libfoo"],
7785 `)+`
7786 java_sdk_library_import {
7787 name: "libfoo",
7788 public: {
7789 jars: ["libbar.jar"],
7790 },
7791 apex_available: [
7792 "com.android.myapex",
7793 "com.android.myapex_compressed",
7794 ],
7795 compile_dex: true,
7796 }
7797 `)
7798 })
7799}
7800
Jooyung Han548640b2020-04-27 12:10:30 +09007801func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7802 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7803 apex {
7804 name: "myapex",
7805 key: "myapex.key",
7806 updatable: true,
7807 }
7808
7809 apex_key {
7810 name: "myapex.key",
7811 public_key: "testkey.avbpubkey",
7812 private_key: "testkey.pem",
7813 }
7814 `)
7815}
7816
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007817func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7818 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7819 apex {
7820 name: "myapex",
7821 key: "myapex.key",
7822 }
7823
7824 apex_key {
7825 name: "myapex.key",
7826 public_key: "testkey.avbpubkey",
7827 private_key: "testkey.pem",
7828 }
7829 `)
7830}
7831
Daniel Norman69109112021-12-02 12:52:42 -08007832func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7833 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7834 apex {
7835 name: "myapex",
7836 key: "myapex.key",
7837 updatable: true,
7838 soc_specific: true,
7839 }
7840
7841 apex_key {
7842 name: "myapex.key",
7843 public_key: "testkey.avbpubkey",
7844 private_key: "testkey.pem",
7845 }
7846 `)
7847}
7848
satayevb98371c2021-06-15 16:49:50 +01007849func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7850 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7851 apex {
7852 name: "myapex",
7853 key: "myapex.key",
7854 systemserverclasspath_fragments: [
7855 "mysystemserverclasspathfragment",
7856 ],
7857 min_sdk_version: "29",
7858 updatable: true,
7859 }
7860
7861 apex_key {
7862 name: "myapex.key",
7863 public_key: "testkey.avbpubkey",
7864 private_key: "testkey.pem",
7865 }
7866
7867 java_library {
7868 name: "foo",
7869 srcs: ["b.java"],
7870 min_sdk_version: "29",
7871 installable: true,
7872 apex_available: [
7873 "myapex",
7874 ],
7875 }
7876
7877 systemserverclasspath_fragment {
7878 name: "mysystemserverclasspathfragment",
7879 generate_classpaths_proto: false,
7880 contents: [
7881 "foo",
7882 ],
7883 apex_available: [
7884 "myapex",
7885 ],
7886 }
satayevabcd5972021-08-06 17:49:46 +01007887 `,
7888 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7889 )
satayevb98371c2021-06-15 16:49:50 +01007890}
7891
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007892func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007893 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7894 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7895 // modules to be included in the BootJars.
7896 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7897 return android.GroupFixturePreparers(
7898 dexpreopt.FixtureSetBootJars(bootJars...),
7899 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7900 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7901 }),
7902 )
7903 }
7904
7905 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7906 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7907 // specified in the ArtApexJars configuration.
7908 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7909 return android.GroupFixturePreparers(
7910 dexpreopt.FixtureSetArtBootJars(bootJars...),
7911 dexpreopt.FixtureSetBootJars(bootJars...),
7912 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7913 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7914 }),
7915 )
7916 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007917
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007918 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007919 preparer := android.GroupFixturePreparers(
7920 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7921 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7922 )
7923 fragments := []java.ApexVariantReference{
7924 {
7925 Apex: proptools.StringPtr("com.android.art.debug"),
7926 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7927 },
7928 {
7929 Apex: proptools.StringPtr("some-non-updatable-apex"),
7930 Module: proptools.StringPtr("some-non-updatable-fragment"),
7931 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007932 }
satayevabcd5972021-08-06 17:49:46 +01007933 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007934 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007935
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007936 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007937 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7938 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007939 preparer := android.GroupFixturePreparers(
7940 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7941 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7942 )
Paul Duffin60264a02021-04-12 20:02:36 +01007943 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007944 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007945
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007946 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 +01007947 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 +01007948 // Update the dexpreopt ArtApexJars directly.
7949 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7950 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007951 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007952
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007953 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 +01007954 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 +01007955 // Update the dexpreopt ArtApexJars directly.
7956 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7957 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007958 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007959
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007960 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 +01007961 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 +01007962 preparer := android.GroupFixturePreparers(
7963 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7964 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7965 )
Paul Duffin60264a02021-04-12 20:02:36 +01007966 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007967 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007968
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007969 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 +01007970 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007971 fragment := java.ApexVariantReference{
7972 Apex: proptools.StringPtr("some-non-updatable-apex"),
7973 Module: proptools.StringPtr("some-non-updatable-fragment"),
7974 }
7975 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007976 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007977
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007978 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007979 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007980 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7981 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007982 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007983
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007984 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007985 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007986 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7987 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007988 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007989
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007990 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007991 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007992 // Update the dexpreopt ArtApexJars directly.
7993 preparer := prepareSetArtJars("platform:some-platform-lib")
7994 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007995 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007996
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007997 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007998 preparer := android.GroupFixturePreparers(
7999 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8000 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8001 )
8002 fragments := []java.ApexVariantReference{
8003 {
8004 Apex: proptools.StringPtr("some-non-updatable-apex"),
8005 Module: proptools.StringPtr("some-non-updatable-fragment"),
8006 },
8007 }
8008 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008009 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008010}
8011
8012func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008013 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008014 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008015 fragment := java.ApexVariantReference{
8016 Apex: proptools.StringPtr("myapex"),
8017 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8018 }
8019
Paul Duffin064b70c2020-11-02 17:32:38 +00008020 testDexpreoptWithApexes(t, `
8021 prebuilt_apex {
8022 name: "myapex" ,
8023 arch: {
8024 arm64: {
8025 src: "myapex-arm64.apex",
8026 },
8027 arm: {
8028 src: "myapex-arm.apex",
8029 },
8030 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008031 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8032 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008033
Paul Duffin89f570a2021-06-16 01:42:33 +01008034 prebuilt_bootclasspath_fragment {
8035 name: "my-bootclasspath-fragment",
8036 contents: ["libfoo"],
8037 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008038 hidden_api: {
8039 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8040 metadata: "my-bootclasspath-fragment/metadata.csv",
8041 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008042 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8043 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8044 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008045 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008046 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008047
Paul Duffin89f570a2021-06-16 01:42:33 +01008048 java_import {
8049 name: "libfoo",
8050 jars: ["libfoo.jar"],
8051 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008052 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008053 }
8054 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008055 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008056}
8057
Spandan Dasf14e2542021-11-12 00:01:37 +00008058func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008059 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008060 bp += `
8061 apex_key {
8062 name: "myapex.key",
8063 public_key: "testkey.avbpubkey",
8064 private_key: "testkey.pem",
8065 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008066 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008067 "lib1/src/A.java": nil,
8068 "lib2/src/B.java": nil,
8069 "system/sepolicy/apex/myapex-file_contexts": nil,
8070 }
8071
Paul Duffin45338f02021-03-30 23:07:52 +01008072 errorHandler := android.FixtureExpectsNoErrors
8073 if errmsg != "" {
8074 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008075 }
Colin Crossae8600b2020-10-29 17:09:13 -07008076
Paul Duffin45338f02021-03-30 23:07:52 +01008077 android.GroupFixturePreparers(
8078 android.PrepareForTestWithAndroidBuildComponents,
8079 java.PrepareForTestWithJavaBuildComponents,
8080 PrepareForTestWithApexBuildComponents,
8081 android.PrepareForTestWithNeverallowRules(rules),
8082 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008083 apexBootJars := make([]string, 0, len(bootJars))
8084 for _, apexBootJar := range bootJars {
8085 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008086 }
satayevd604b212021-07-21 14:23:52 +01008087 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008088 }),
8089 fs.AddToFixture(),
8090 ).
8091 ExtendWithErrorHandler(errorHandler).
8092 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008093}
8094
8095func TestApexPermittedPackagesRules(t *testing.T) {
8096 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008097 name string
8098 expectedError string
8099 bp string
8100 bootJars []string
8101 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008102 }{
8103
8104 {
8105 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8106 expectedError: "",
8107 bp: `
8108 java_library {
8109 name: "bcp_lib1",
8110 srcs: ["lib1/src/*.java"],
8111 permitted_packages: ["foo.bar"],
8112 apex_available: ["myapex"],
8113 sdk_version: "none",
8114 system_modules: "none",
8115 }
8116 java_library {
8117 name: "nonbcp_lib2",
8118 srcs: ["lib2/src/*.java"],
8119 apex_available: ["myapex"],
8120 permitted_packages: ["a.b"],
8121 sdk_version: "none",
8122 system_modules: "none",
8123 }
8124 apex {
8125 name: "myapex",
8126 key: "myapex.key",
8127 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008128 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008129 }`,
8130 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008131 bcpPermittedPackages: map[string][]string{
8132 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008133 "foo.bar",
8134 },
8135 },
8136 },
8137 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008138 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008139 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 +01008140 bp: `
8141 java_library {
8142 name: "bcp_lib1",
8143 srcs: ["lib1/src/*.java"],
8144 apex_available: ["myapex"],
8145 permitted_packages: ["foo.bar"],
8146 sdk_version: "none",
8147 system_modules: "none",
8148 }
8149 java_library {
8150 name: "bcp_lib2",
8151 srcs: ["lib2/src/*.java"],
8152 apex_available: ["myapex"],
8153 permitted_packages: ["foo.bar", "bar.baz"],
8154 sdk_version: "none",
8155 system_modules: "none",
8156 }
8157 apex {
8158 name: "myapex",
8159 key: "myapex.key",
8160 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008161 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008162 }
8163 `,
8164 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008165 bcpPermittedPackages: map[string][]string{
8166 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008167 "foo.bar",
8168 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008169 "bcp_lib2": []string{
8170 "foo.bar",
8171 },
8172 },
8173 },
8174 {
8175 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8176 expectedError: "",
8177 bp: `
8178 java_library {
8179 name: "bcp_lib_restricted",
8180 srcs: ["lib1/src/*.java"],
8181 apex_available: ["myapex"],
8182 permitted_packages: ["foo.bar"],
8183 sdk_version: "none",
8184 min_sdk_version: "29",
8185 system_modules: "none",
8186 }
8187 java_library {
8188 name: "bcp_lib_unrestricted",
8189 srcs: ["lib2/src/*.java"],
8190 apex_available: ["myapex"],
8191 permitted_packages: ["foo.bar", "bar.baz"],
8192 sdk_version: "none",
8193 min_sdk_version: "29",
8194 system_modules: "none",
8195 }
8196 apex {
8197 name: "myapex",
8198 key: "myapex.key",
8199 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8200 updatable: true,
8201 min_sdk_version: "29",
8202 }
8203 `,
8204 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8205 bcpPermittedPackages: map[string][]string{
8206 "bcp_lib1_non_updateable": []string{
8207 "foo.bar",
8208 },
8209 // 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 +01008210 },
8211 },
8212 }
8213 for _, tc := range testcases {
8214 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008215 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8216 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008217 })
8218 }
8219}
8220
Jiyong Park62304bb2020-04-13 16:19:48 +09008221func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008222 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008223 apex {
8224 name: "myapex",
8225 key: "myapex.key",
8226 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008227 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008228 }
8229
8230 apex_key {
8231 name: "myapex.key",
8232 public_key: "testkey.avbpubkey",
8233 private_key: "testkey.pem",
8234 }
8235
8236 cc_library {
8237 name: "mylib",
8238 srcs: ["mylib.cpp"],
8239 system_shared_libs: [],
8240 stl: "none",
8241 stubs: {
8242 versions: ["1"],
8243 },
8244 apex_available: ["myapex"],
8245 }
8246
8247 cc_library {
8248 name: "myprivlib",
8249 srcs: ["mylib.cpp"],
8250 system_shared_libs: [],
8251 stl: "none",
8252 apex_available: ["myapex"],
8253 }
8254
8255
8256 cc_test {
8257 name: "mytest",
8258 gtest: false,
8259 srcs: ["mylib.cpp"],
8260 system_shared_libs: [],
8261 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008262 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008263 test_for: ["myapex"]
8264 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008265
8266 cc_library {
8267 name: "mytestlib",
8268 srcs: ["mylib.cpp"],
8269 system_shared_libs: [],
8270 shared_libs: ["mylib", "myprivlib"],
8271 stl: "none",
8272 test_for: ["myapex"],
8273 }
8274
8275 cc_benchmark {
8276 name: "mybench",
8277 srcs: ["mylib.cpp"],
8278 system_shared_libs: [],
8279 shared_libs: ["mylib", "myprivlib"],
8280 stl: "none",
8281 test_for: ["myapex"],
8282 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008283 `)
8284
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008285 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008286 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008287 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8288 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8289 }
8290
8291 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008292 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008293 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8294 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8295 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8296}
Jiyong Park46a512f2020-12-04 18:02:13 +09008297
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008298func TestIndirectTestFor(t *testing.T) {
8299 ctx := testApex(t, `
8300 apex {
8301 name: "myapex",
8302 key: "myapex.key",
8303 native_shared_libs: ["mylib", "myprivlib"],
8304 updatable: false,
8305 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008306
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008307 apex_key {
8308 name: "myapex.key",
8309 public_key: "testkey.avbpubkey",
8310 private_key: "testkey.pem",
8311 }
8312
8313 cc_library {
8314 name: "mylib",
8315 srcs: ["mylib.cpp"],
8316 system_shared_libs: [],
8317 stl: "none",
8318 stubs: {
8319 versions: ["1"],
8320 },
8321 apex_available: ["myapex"],
8322 }
8323
8324 cc_library {
8325 name: "myprivlib",
8326 srcs: ["mylib.cpp"],
8327 system_shared_libs: [],
8328 stl: "none",
8329 shared_libs: ["mylib"],
8330 apex_available: ["myapex"],
8331 }
8332
8333 cc_library {
8334 name: "mytestlib",
8335 srcs: ["mylib.cpp"],
8336 system_shared_libs: [],
8337 shared_libs: ["myprivlib"],
8338 stl: "none",
8339 test_for: ["myapex"],
8340 }
8341 `)
8342
8343 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008344 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008345 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8346 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8347 }
8348
8349 // The platform variant of mytestlib links to the platform variant of the
8350 // internal myprivlib.
8351 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8352
8353 // The platform variant of myprivlib links to the platform variant of mylib
8354 // and bypasses its stubs.
8355 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 +09008356}
8357
Martin Stjernholmec009002021-03-27 15:18:31 +00008358func TestTestForForLibInOtherApex(t *testing.T) {
8359 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8360 _ = testApex(t, `
8361 apex {
8362 name: "com.android.art",
8363 key: "myapex.key",
8364 native_shared_libs: ["mylib"],
8365 updatable: false,
8366 }
8367
8368 apex {
8369 name: "com.android.art.debug",
8370 key: "myapex.key",
8371 native_shared_libs: ["mylib", "mytestlib"],
8372 updatable: false,
8373 }
8374
8375 apex_key {
8376 name: "myapex.key",
8377 public_key: "testkey.avbpubkey",
8378 private_key: "testkey.pem",
8379 }
8380
8381 cc_library {
8382 name: "mylib",
8383 srcs: ["mylib.cpp"],
8384 system_shared_libs: [],
8385 stl: "none",
8386 stubs: {
8387 versions: ["1"],
8388 },
8389 apex_available: ["com.android.art", "com.android.art.debug"],
8390 }
8391
8392 cc_library {
8393 name: "mytestlib",
8394 srcs: ["mylib.cpp"],
8395 system_shared_libs: [],
8396 shared_libs: ["mylib"],
8397 stl: "none",
8398 apex_available: ["com.android.art.debug"],
8399 test_for: ["com.android.art"],
8400 }
8401 `,
8402 android.MockFS{
8403 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8404 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8405 }.AddToFixture())
8406}
8407
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008408// TODO(jungjw): Move this to proptools
8409func intPtr(i int) *int {
8410 return &i
8411}
8412
8413func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008414 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008415 apex_set {
8416 name: "myapex",
8417 set: "myapex.apks",
8418 filename: "foo_v2.apex",
8419 overrides: ["foo"],
8420 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008421 `,
8422 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8423 variables.Platform_sdk_version = intPtr(30)
8424 }),
8425 android.FixtureModifyConfig(func(config android.Config) {
8426 config.Targets[android.Android] = []android.Target{
8427 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8428 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8429 }
8430 }),
8431 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008432
Paul Duffin24704672021-04-06 16:09:30 +01008433 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008434
8435 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008436 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008437 actual := extractedApex.Args["abis"]
8438 expected := "ARMEABI_V7A,ARM64_V8A"
8439 if actual != expected {
8440 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8441 }
8442 actual = extractedApex.Args["sdk-version"]
8443 expected = "30"
8444 if actual != expected {
8445 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8446 }
8447
Paul Duffin6717d882021-06-15 19:09:41 +01008448 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008449 a := m.Module().(*ApexSet)
8450 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008451 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008452 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8453 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8454 }
8455}
8456
Anton Hansson805e0a52022-11-25 14:06:46 +00008457func TestApexSet_NativeBridge(t *testing.T) {
8458 ctx := testApex(t, `
8459 apex_set {
8460 name: "myapex",
8461 set: "myapex.apks",
8462 filename: "foo_v2.apex",
8463 overrides: ["foo"],
8464 }
8465 `,
8466 android.FixtureModifyConfig(func(config android.Config) {
8467 config.Targets[android.Android] = []android.Target{
8468 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8469 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8470 }
8471 }),
8472 )
8473
8474 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8475
8476 // Check extract_apks tool parameters. No native bridge arch expected
8477 extractedApex := m.Output("extracted/myapex.apks")
8478 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8479}
8480
Jiyong Park7d95a512020-05-10 15:16:24 +09008481func TestNoStaticLinkingToStubsLib(t *testing.T) {
8482 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8483 apex {
8484 name: "myapex",
8485 key: "myapex.key",
8486 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008487 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008488 }
8489
8490 apex_key {
8491 name: "myapex.key",
8492 public_key: "testkey.avbpubkey",
8493 private_key: "testkey.pem",
8494 }
8495
8496 cc_library {
8497 name: "mylib",
8498 srcs: ["mylib.cpp"],
8499 static_libs: ["otherlib"],
8500 system_shared_libs: [],
8501 stl: "none",
8502 apex_available: [ "myapex" ],
8503 }
8504
8505 cc_library {
8506 name: "otherlib",
8507 srcs: ["mylib.cpp"],
8508 system_shared_libs: [],
8509 stl: "none",
8510 stubs: {
8511 versions: ["1", "2", "3"],
8512 },
8513 apex_available: [ "myapex" ],
8514 }
8515 `)
8516}
8517
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008518func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008519 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008520 apex {
8521 name: "myapex",
8522 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008523 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008524 custom_sign_tool: "sign_myapex",
8525 }
8526
8527 apex_key {
8528 name: "myapex.key",
8529 public_key: "testkey.avbpubkey",
8530 private_key: "testkey.pem",
8531 }
8532 `)
8533
8534 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8535 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8536 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"`)
8537}
8538
8539func TestApexKeysTxtOverrides(t *testing.T) {
8540 ctx := testApex(t, `
8541 apex {
8542 name: "myapex",
8543 key: "myapex.key",
8544 updatable: false,
8545 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008546 }
8547
8548 apex_key {
8549 name: "myapex.key",
8550 public_key: "testkey.avbpubkey",
8551 private_key: "testkey.pem",
8552 }
8553
8554 prebuilt_apex {
8555 name: "myapex",
8556 prefer: true,
8557 arch: {
8558 arm64: {
8559 src: "myapex-arm64.apex",
8560 },
8561 arm: {
8562 src: "myapex-arm.apex",
8563 },
8564 },
8565 }
8566
8567 apex_set {
8568 name: "myapex_set",
8569 set: "myapex.apks",
8570 filename: "myapex_set.apex",
8571 overrides: ["myapex"],
8572 }
8573 `)
8574
8575 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8576 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8577 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 +09008578 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 +09008579}
8580
Jooyung Han938b5932020-06-20 12:47:47 +09008581func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008582 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008583 apex {
8584 name: "myapex",
8585 key: "myapex.key",
8586 apps: ["app"],
8587 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008588 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008589 }
8590
8591 apex_key {
8592 name: "myapex.key",
8593 public_key: "testkey.avbpubkey",
8594 private_key: "testkey.pem",
8595 }
8596
8597 android_app {
8598 name: "app",
8599 srcs: ["foo/bar/MyClass.java"],
8600 package_name: "foo",
8601 sdk_version: "none",
8602 system_modules: "none",
8603 apex_available: [ "myapex" ],
8604 }
8605 `, withFiles(map[string][]byte{
8606 "sub/Android.bp": []byte(`
8607 override_apex {
8608 name: "override_myapex",
8609 base: "myapex",
8610 apps: ["override_app"],
8611 allowed_files: ":allowed",
8612 }
8613 // Overridable "path" property should be referenced indirectly
8614 filegroup {
8615 name: "allowed",
8616 srcs: ["allowed.txt"],
8617 }
8618 override_android_app {
8619 name: "override_app",
8620 base: "app",
8621 package_name: "bar",
8622 }
8623 `),
8624 }))
8625
8626 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8627 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8628 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8629 }
8630
8631 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8632 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8633 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8634 }
8635}
8636
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008637func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008638 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008639 apex {
8640 name: "myapex",
8641 key: "myapex.key",
8642 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008643 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008644 }
8645
8646 apex_key {
8647 name: "myapex.key",
8648 public_key: "testkey.avbpubkey",
8649 private_key: "testkey.pem",
8650 }
8651
8652 cc_library {
8653 name: "mylib",
8654 srcs: ["mylib.cpp"],
8655 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008656 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008657 },
8658 apex_available: ["myapex"],
8659 }
8660
8661 cc_prebuilt_library_shared {
8662 name: "mylib",
8663 prefer: false,
8664 srcs: ["prebuilt.so"],
8665 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008666 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008667 },
8668 apex_available: ["myapex"],
8669 }
8670 `)
8671}
8672
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008673func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008674 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008675 apex {
8676 name: "myapex",
8677 key: "myapex.key",
8678 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008679 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008680 }
8681 apex_key {
8682 name: "myapex.key",
8683 public_key: "testkey.avbpubkey",
8684 private_key: "testkey.pem",
8685 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008686 `,
8687 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8688 variables.CompressedApex = proptools.BoolPtr(true)
8689 }),
8690 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008691
8692 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8693 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8694
8695 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8696 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8697
8698 // Make sure output of bundle is .capex
8699 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8700 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8701
8702 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008703 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008704 var builder strings.Builder
8705 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8706 androidMk := builder.String()
8707 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8708}
8709
Martin Stjernholm2856c662020-12-02 15:03:42 +00008710func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008711 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008712 apex {
8713 name: "myapex",
8714 key: "myapex.key",
8715 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008716 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008717 }
8718
8719 apex_key {
8720 name: "myapex.key",
8721 public_key: "testkey.avbpubkey",
8722 private_key: "testkey.pem",
8723 }
8724
8725 cc_library {
8726 name: "mylib",
8727 srcs: ["mylib.cpp"],
8728 apex_available: ["myapex"],
8729 shared_libs: ["otherlib"],
8730 system_shared_libs: [],
8731 }
8732
8733 cc_library {
8734 name: "otherlib",
8735 srcs: ["mylib.cpp"],
8736 stubs: {
8737 versions: ["current"],
8738 },
8739 }
8740
8741 cc_prebuilt_library_shared {
8742 name: "otherlib",
8743 prefer: true,
8744 srcs: ["prebuilt.so"],
8745 stubs: {
8746 versions: ["current"],
8747 },
8748 }
8749 `)
8750
8751 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008752 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008753 var builder strings.Builder
8754 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8755 androidMk := builder.String()
8756
8757 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8758 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008759 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 +00008760}
8761
Jiyong Parke3867542020-12-03 17:28:25 +09008762func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008763 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008764 apex {
8765 name: "myapex",
8766 key: "myapex.key",
8767 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008768 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008769 }
8770
8771 apex_key {
8772 name: "myapex.key",
8773 public_key: "testkey.avbpubkey",
8774 private_key: "testkey.pem",
8775 }
8776
8777 cc_library {
8778 name: "mylib",
8779 srcs: ["mylib.cpp"],
8780 system_shared_libs: [],
8781 stl: "none",
8782 apex_available: ["myapex"],
8783 shared_libs: ["mylib2"],
8784 target: {
8785 apex: {
8786 exclude_shared_libs: ["mylib2"],
8787 },
8788 },
8789 }
8790
8791 cc_library {
8792 name: "mylib2",
8793 srcs: ["mylib.cpp"],
8794 system_shared_libs: [],
8795 stl: "none",
8796 }
8797 `)
8798
8799 // Check if mylib is linked to mylib2 for the non-apex target
8800 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8801 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8802
8803 // Make sure that the link doesn't occur for the apex target
8804 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8805 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8806
8807 // It shouldn't appear in the copy cmd as well.
8808 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8809 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8810}
8811
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008812func TestPrebuiltStubLibDep(t *testing.T) {
8813 bpBase := `
8814 apex {
8815 name: "myapex",
8816 key: "myapex.key",
8817 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008818 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008819 }
8820 apex_key {
8821 name: "myapex.key",
8822 public_key: "testkey.avbpubkey",
8823 private_key: "testkey.pem",
8824 }
8825 cc_library {
8826 name: "mylib",
8827 srcs: ["mylib.cpp"],
8828 apex_available: ["myapex"],
8829 shared_libs: ["stublib"],
8830 system_shared_libs: [],
8831 }
8832 apex {
8833 name: "otherapex",
8834 enabled: %s,
8835 key: "myapex.key",
8836 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008837 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008838 }
8839 `
8840
8841 stublibSourceBp := `
8842 cc_library {
8843 name: "stublib",
8844 srcs: ["mylib.cpp"],
8845 apex_available: ["otherapex"],
8846 system_shared_libs: [],
8847 stl: "none",
8848 stubs: {
8849 versions: ["1"],
8850 },
8851 }
8852 `
8853
8854 stublibPrebuiltBp := `
8855 cc_prebuilt_library_shared {
8856 name: "stublib",
8857 srcs: ["prebuilt.so"],
8858 apex_available: ["otherapex"],
8859 stubs: {
8860 versions: ["1"],
8861 },
8862 %s
8863 }
8864 `
8865
8866 tests := []struct {
8867 name string
8868 stublibBp string
8869 usePrebuilt bool
8870 modNames []string // Modules to collect AndroidMkEntries for
8871 otherApexEnabled []string
8872 }{
8873 {
8874 name: "only_source",
8875 stublibBp: stublibSourceBp,
8876 usePrebuilt: false,
8877 modNames: []string{"stublib"},
8878 otherApexEnabled: []string{"true", "false"},
8879 },
8880 {
8881 name: "source_preferred",
8882 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8883 usePrebuilt: false,
8884 modNames: []string{"stublib", "prebuilt_stublib"},
8885 otherApexEnabled: []string{"true", "false"},
8886 },
8887 {
8888 name: "prebuilt_preferred",
8889 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8890 usePrebuilt: true,
8891 modNames: []string{"stublib", "prebuilt_stublib"},
8892 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8893 },
8894 {
8895 name: "only_prebuilt",
8896 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8897 usePrebuilt: true,
8898 modNames: []string{"stublib"},
8899 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8900 },
8901 }
8902
8903 for _, test := range tests {
8904 t.Run(test.name, func(t *testing.T) {
8905 for _, otherApexEnabled := range test.otherApexEnabled {
8906 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008907 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008908
8909 type modAndMkEntries struct {
8910 mod *cc.Module
8911 mkEntries android.AndroidMkEntries
8912 }
8913 entries := []*modAndMkEntries{}
8914
8915 // Gather shared lib modules that are installable
8916 for _, modName := range test.modNames {
8917 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8918 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8919 continue
8920 }
8921 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008922 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008923 continue
8924 }
Colin Crossaa255532020-07-03 13:18:24 -07008925 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008926 if ent.Disabled {
8927 continue
8928 }
8929 entries = append(entries, &modAndMkEntries{
8930 mod: mod,
8931 mkEntries: ent,
8932 })
8933 }
8934 }
8935 }
8936
8937 var entry *modAndMkEntries = nil
8938 for _, ent := range entries {
8939 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8940 if entry != nil {
8941 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8942 } else {
8943 entry = ent
8944 }
8945 }
8946 }
8947
8948 if entry == nil {
8949 t.Errorf("AndroidMk entry for \"stublib\" missing")
8950 } else {
8951 isPrebuilt := entry.mod.Prebuilt() != nil
8952 if isPrebuilt != test.usePrebuilt {
8953 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8954 }
8955 if !entry.mod.IsStubs() {
8956 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8957 }
8958 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8959 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8960 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008961 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008962 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008963 if !android.InList(expected, cflags) {
8964 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8965 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008966 }
8967 })
8968 }
8969 })
8970 }
8971}
8972
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008973func TestHostApexInHostOnlyBuild(t *testing.T) {
8974 testApex(t, `
8975 apex {
8976 name: "myapex",
8977 host_supported: true,
8978 key: "myapex.key",
8979 updatable: false,
8980 payload_type: "zip",
8981 }
8982 apex_key {
8983 name: "myapex.key",
8984 public_key: "testkey.avbpubkey",
8985 private_key: "testkey.pem",
8986 }
8987 `,
8988 android.FixtureModifyConfig(func(config android.Config) {
8989 // We may not have device targets in all builds, e.g. in
8990 // prebuilts/build-tools/build-prebuilts.sh
8991 config.Targets[android.Android] = []android.Target{}
8992 }))
8993}
8994
Colin Crossc33e5212021-05-25 18:16:02 -07008995func TestApexJavaCoverage(t *testing.T) {
8996 bp := `
8997 apex {
8998 name: "myapex",
8999 key: "myapex.key",
9000 java_libs: ["mylib"],
9001 bootclasspath_fragments: ["mybootclasspathfragment"],
9002 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9003 updatable: false,
9004 }
9005
9006 apex_key {
9007 name: "myapex.key",
9008 public_key: "testkey.avbpubkey",
9009 private_key: "testkey.pem",
9010 }
9011
9012 java_library {
9013 name: "mylib",
9014 srcs: ["mylib.java"],
9015 apex_available: ["myapex"],
9016 compile_dex: true,
9017 }
9018
9019 bootclasspath_fragment {
9020 name: "mybootclasspathfragment",
9021 contents: ["mybootclasspathlib"],
9022 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009023 hidden_api: {
9024 split_packages: ["*"],
9025 },
Colin Crossc33e5212021-05-25 18:16:02 -07009026 }
9027
9028 java_library {
9029 name: "mybootclasspathlib",
9030 srcs: ["mybootclasspathlib.java"],
9031 apex_available: ["myapex"],
9032 compile_dex: true,
9033 }
9034
9035 systemserverclasspath_fragment {
9036 name: "mysystemserverclasspathfragment",
9037 contents: ["mysystemserverclasspathlib"],
9038 apex_available: ["myapex"],
9039 }
9040
9041 java_library {
9042 name: "mysystemserverclasspathlib",
9043 srcs: ["mysystemserverclasspathlib.java"],
9044 apex_available: ["myapex"],
9045 compile_dex: true,
9046 }
9047 `
9048
9049 result := android.GroupFixturePreparers(
9050 PrepareForTestWithApexBuildComponents,
9051 prepareForTestWithMyapex,
9052 java.PrepareForTestWithJavaDefaultModules,
9053 android.PrepareForTestWithAndroidBuildComponents,
9054 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009055 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9056 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009057 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009058 ).RunTest(t)
9059
9060 // Make sure jacoco ran on both mylib and mybootclasspathlib
9061 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9062 t.Errorf("Failed to find jacoco rule for mylib")
9063 }
9064 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9065 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9066 }
9067 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9068 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9069 }
9070}
9071
Jiyong Park192600a2021-08-03 07:52:17 +00009072func TestProhibitStaticExecutable(t *testing.T) {
9073 testApexError(t, `executable mybin is static`, `
9074 apex {
9075 name: "myapex",
9076 key: "myapex.key",
9077 binaries: ["mybin"],
9078 min_sdk_version: "29",
9079 }
9080
9081 apex_key {
9082 name: "myapex.key",
9083 public_key: "testkey.avbpubkey",
9084 private_key: "testkey.pem",
9085 }
9086
9087 cc_binary {
9088 name: "mybin",
9089 srcs: ["mylib.cpp"],
9090 relative_install_path: "foo/bar",
9091 static_executable: true,
9092 system_shared_libs: [],
9093 stl: "none",
9094 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009095 min_sdk_version: "29",
9096 }
9097 `)
9098
9099 testApexError(t, `executable mybin.rust is static`, `
9100 apex {
9101 name: "myapex",
9102 key: "myapex.key",
9103 binaries: ["mybin.rust"],
9104 min_sdk_version: "29",
9105 }
9106
9107 apex_key {
9108 name: "myapex.key",
9109 public_key: "testkey.avbpubkey",
9110 private_key: "testkey.pem",
9111 }
9112
9113 rust_binary {
9114 name: "mybin.rust",
9115 srcs: ["foo.rs"],
9116 static_executable: true,
9117 apex_available: ["myapex"],
9118 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009119 }
9120 `)
9121}
9122
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009123func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9124 ctx := testApex(t, `
9125 apex {
9126 name: "myapex",
9127 key: "myapex.key",
9128 updatable: false,
9129 java_libs: ["foo"],
9130 }
9131
9132 apex_key {
9133 name: "myapex.key",
9134 public_key: "testkey.avbpubkey",
9135 private_key: "testkey.pem",
9136 }
9137
9138 java_library {
9139 name: "foo",
9140 srcs: ["foo.java"],
9141 apex_available: ["myapex"],
9142 installable: true,
9143 }
9144 `,
9145 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9146 )
9147
9148 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9149 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9150 var builder strings.Builder
9151 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9152 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009153 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 +00009154}
9155
9156func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9157 ctx := testApex(t, `
9158 prebuilt_apex {
9159 name: "myapex",
9160 arch: {
9161 arm64: {
9162 src: "myapex-arm64.apex",
9163 },
9164 arm: {
9165 src: "myapex-arm.apex",
9166 },
9167 },
9168 exported_java_libs: ["foo"],
9169 }
9170
9171 java_import {
9172 name: "foo",
9173 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009174 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009175 }
9176 `,
9177 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9178 )
9179
9180 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9181 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9182 mainModuleEntries := entriesList[0]
9183 android.AssertArrayString(t,
9184 "LOCAL_REQUIRED_MODULES",
9185 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9186 []string{
9187 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9188 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9189 })
9190}
9191
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009192func TestAndroidMk_RequiredModules(t *testing.T) {
9193 ctx := testApex(t, `
9194 apex {
9195 name: "myapex",
9196 key: "myapex.key",
9197 updatable: false,
9198 java_libs: ["foo"],
9199 required: ["otherapex"],
9200 }
9201
9202 apex {
9203 name: "otherapex",
9204 key: "myapex.key",
9205 updatable: false,
9206 java_libs: ["foo"],
9207 required: ["otherapex"],
9208 }
9209
9210 apex_key {
9211 name: "myapex.key",
9212 public_key: "testkey.avbpubkey",
9213 private_key: "testkey.pem",
9214 }
9215
9216 java_library {
9217 name: "foo",
9218 srcs: ["foo.java"],
9219 apex_available: ["myapex", "otherapex"],
9220 installable: true,
9221 }
9222 `)
9223
9224 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9225 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9226 var builder strings.Builder
9227 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9228 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009229 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009230}
9231
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009232func TestAndroidMk_RequiredDeps(t *testing.T) {
9233 ctx := testApex(t, `
9234 apex {
9235 name: "myapex",
9236 key: "myapex.key",
9237 updatable: false,
9238 }
9239
9240 apex_key {
9241 name: "myapex.key",
9242 public_key: "testkey.avbpubkey",
9243 private_key: "testkey.pem",
9244 }
9245 `)
9246
9247 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009248 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009249 data := android.AndroidMkDataForTest(t, ctx, bundle)
9250 var builder strings.Builder
9251 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9252 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009253 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009254
9255 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009256 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009257 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9258 var flattenedBuilder strings.Builder
9259 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9260 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009261 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009262}
9263
Jooyung Hana6d36672022-02-24 13:58:07 +09009264func TestApexOutputFileProducer(t *testing.T) {
9265 for _, tc := range []struct {
9266 name string
9267 ref string
9268 expected_data []string
9269 }{
9270 {
9271 name: "test_using_output",
9272 ref: ":myapex",
9273 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9274 },
9275 {
9276 name: "test_using_apex",
9277 ref: ":myapex{.apex}",
9278 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9279 },
9280 } {
9281 t.Run(tc.name, func(t *testing.T) {
9282 ctx := testApex(t, `
9283 apex {
9284 name: "myapex",
9285 key: "myapex.key",
9286 compressible: true,
9287 updatable: false,
9288 }
9289
9290 apex_key {
9291 name: "myapex.key",
9292 public_key: "testkey.avbpubkey",
9293 private_key: "testkey.pem",
9294 }
9295
9296 java_test {
9297 name: "`+tc.name+`",
9298 srcs: ["a.java"],
9299 data: ["`+tc.ref+`"],
9300 }
9301 `,
9302 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9303 variables.CompressedApex = proptools.BoolPtr(true)
9304 }))
9305 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9306 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9307 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9308 })
9309 }
9310}
9311
satayev758968a2021-12-06 11:42:40 +00009312func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9313 preparer := android.GroupFixturePreparers(
9314 PrepareForTestWithApexBuildComponents,
9315 prepareForTestWithMyapex,
9316 java.PrepareForTestWithJavaSdkLibraryFiles,
9317 java.PrepareForTestWithJavaDefaultModules,
9318 android.PrepareForTestWithAndroidBuildComponents,
9319 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9320 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9321 )
9322
9323 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9324 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9325 preparer.RunTestWithBp(t, `
9326 apex {
9327 name: "myapex",
9328 key: "myapex.key",
9329 bootclasspath_fragments: ["mybootclasspathfragment"],
9330 min_sdk_version: "30",
9331 updatable: false,
9332 }
9333
9334 apex_key {
9335 name: "myapex.key",
9336 public_key: "testkey.avbpubkey",
9337 private_key: "testkey.pem",
9338 }
9339
9340 bootclasspath_fragment {
9341 name: "mybootclasspathfragment",
9342 contents: ["mybootclasspathlib"],
9343 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009344 hidden_api: {
9345 split_packages: ["*"],
9346 },
satayev758968a2021-12-06 11:42:40 +00009347 }
9348
9349 java_sdk_library {
9350 name: "mybootclasspathlib",
9351 srcs: ["mybootclasspathlib.java"],
9352 apex_available: ["myapex"],
9353 compile_dex: true,
9354 unsafe_ignore_missing_latest_api: true,
9355 min_sdk_version: "31",
9356 static_libs: ["util"],
9357 }
9358
9359 java_library {
9360 name: "util",
9361 srcs: ["a.java"],
9362 apex_available: ["myapex"],
9363 min_sdk_version: "31",
9364 static_libs: ["another_util"],
9365 }
9366
9367 java_library {
9368 name: "another_util",
9369 srcs: ["a.java"],
9370 min_sdk_version: "31",
9371 apex_available: ["myapex"],
9372 }
9373 `)
9374 })
9375
9376 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9377 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9378 preparer.RunTestWithBp(t, `
9379 apex {
9380 name: "myapex",
9381 key: "myapex.key",
9382 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9383 min_sdk_version: "30",
9384 updatable: false,
9385 }
9386
9387 apex_key {
9388 name: "myapex.key",
9389 public_key: "testkey.avbpubkey",
9390 private_key: "testkey.pem",
9391 }
9392
9393 systemserverclasspath_fragment {
9394 name: "mysystemserverclasspathfragment",
9395 contents: ["mysystemserverclasspathlib"],
9396 apex_available: ["myapex"],
9397 }
9398
9399 java_sdk_library {
9400 name: "mysystemserverclasspathlib",
9401 srcs: ["mysystemserverclasspathlib.java"],
9402 apex_available: ["myapex"],
9403 compile_dex: true,
9404 min_sdk_version: "32",
9405 unsafe_ignore_missing_latest_api: true,
9406 static_libs: ["util"],
9407 }
9408
9409 java_library {
9410 name: "util",
9411 srcs: ["a.java"],
9412 apex_available: ["myapex"],
9413 min_sdk_version: "31",
9414 static_libs: ["another_util"],
9415 }
9416
9417 java_library {
9418 name: "another_util",
9419 srcs: ["a.java"],
9420 min_sdk_version: "31",
9421 apex_available: ["myapex"],
9422 }
9423 `)
9424 })
9425
9426 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9427 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9428 RunTestWithBp(t, `
9429 apex {
9430 name: "myapex",
9431 key: "myapex.key",
9432 bootclasspath_fragments: ["mybootclasspathfragment"],
9433 min_sdk_version: "30",
9434 updatable: false,
9435 }
9436
9437 apex_key {
9438 name: "myapex.key",
9439 public_key: "testkey.avbpubkey",
9440 private_key: "testkey.pem",
9441 }
9442
9443 bootclasspath_fragment {
9444 name: "mybootclasspathfragment",
9445 contents: ["mybootclasspathlib"],
9446 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009447 hidden_api: {
9448 split_packages: ["*"],
9449 },
satayev758968a2021-12-06 11:42:40 +00009450 }
9451
9452 java_sdk_library {
9453 name: "mybootclasspathlib",
9454 srcs: ["mybootclasspathlib.java"],
9455 apex_available: ["myapex"],
9456 compile_dex: true,
9457 unsafe_ignore_missing_latest_api: true,
9458 }
9459 `)
9460 })
9461
9462 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9463 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9464 RunTestWithBp(t, `
9465 apex {
9466 name: "myapex",
9467 key: "myapex.key",
9468 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9469 min_sdk_version: "30",
9470 updatable: false,
9471 }
9472
9473 apex_key {
9474 name: "myapex.key",
9475 public_key: "testkey.avbpubkey",
9476 private_key: "testkey.pem",
9477 }
9478
9479 systemserverclasspath_fragment {
9480 name: "mysystemserverclasspathfragment",
9481 contents: ["mysystemserverclasspathlib"],
9482 apex_available: ["myapex"],
9483 }
9484
9485 java_sdk_library {
9486 name: "mysystemserverclasspathlib",
9487 srcs: ["mysystemserverclasspathlib.java"],
9488 apex_available: ["myapex"],
9489 compile_dex: true,
9490 unsafe_ignore_missing_latest_api: true,
9491 }
9492 `)
9493 })
9494}
9495
Jiakai Zhang6decef92022-01-12 17:56:19 +00009496// Verifies that the APEX depends on all the Make modules in the list.
9497func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9498 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9499 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009500 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009501 }
9502}
9503
9504// Verifies that the APEX does not depend on any of the Make modules in the list.
9505func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9506 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9507 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009508 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009509 }
9510}
9511
Spandan Das66773252022-01-15 00:23:18 +00009512func TestApexStrictUpdtabilityLint(t *testing.T) {
9513 bpTemplate := `
9514 apex {
9515 name: "myapex",
9516 key: "myapex.key",
9517 java_libs: ["myjavalib"],
9518 updatable: %v,
9519 min_sdk_version: "29",
9520 }
9521 apex_key {
9522 name: "myapex.key",
9523 }
9524 java_library {
9525 name: "myjavalib",
9526 srcs: ["MyClass.java"],
9527 apex_available: [ "myapex" ],
9528 lint: {
9529 strict_updatability_linting: %v,
9530 },
9531 sdk_version: "current",
9532 min_sdk_version: "29",
9533 }
9534 `
9535 fs := android.MockFS{
9536 "lint-baseline.xml": nil,
9537 }
9538
9539 testCases := []struct {
9540 testCaseName string
9541 apexUpdatable bool
9542 javaStrictUpdtabilityLint bool
9543 lintFileExists bool
9544 disallowedFlagExpected bool
9545 }{
9546 {
9547 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9548 apexUpdatable: true,
9549 javaStrictUpdtabilityLint: true,
9550 lintFileExists: false,
9551 disallowedFlagExpected: false,
9552 },
9553 {
9554 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9555 apexUpdatable: false,
9556 javaStrictUpdtabilityLint: false,
9557 lintFileExists: true,
9558 disallowedFlagExpected: false,
9559 },
9560 {
9561 testCaseName: "non-updatable apex respects strict updatability of javalib",
9562 apexUpdatable: false,
9563 javaStrictUpdtabilityLint: true,
9564 lintFileExists: true,
9565 disallowedFlagExpected: true,
9566 },
9567 {
9568 testCaseName: "updatable apex sets strict updatability of javalib to true",
9569 apexUpdatable: true,
9570 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9571 lintFileExists: true,
9572 disallowedFlagExpected: true,
9573 },
9574 }
9575
9576 for _, testCase := range testCases {
9577 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9578 fixtures := []android.FixturePreparer{}
9579 if testCase.lintFileExists {
9580 fixtures = append(fixtures, fs.AddToFixture())
9581 }
9582
9583 result := testApex(t, bp, fixtures...)
9584 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9585 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9586 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9587
9588 if disallowedFlagActual != testCase.disallowedFlagExpected {
9589 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9590 }
9591 }
9592}
9593
Spandan Dasd9c23ab2022-02-10 02:34:13 +00009594func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9595 bp := `
9596 apex {
9597 name: "myapex",
9598 key: "myapex.key",
9599 java_libs: ["myjavalib"],
9600 updatable: true,
9601 min_sdk_version: "29",
9602 }
9603 apex_key {
9604 name: "myapex.key",
9605 }
9606 java_library {
9607 name: "myjavalib",
9608 srcs: ["MyClass.java"],
9609 apex_available: [ "myapex" ],
9610 sdk_version: "current",
9611 min_sdk_version: "29",
9612 }
9613 `
9614
9615 testCases := []struct {
9616 testCaseName string
9617 moduleDirectory string
9618 disallowedFlagExpected bool
9619 }{
9620 {
9621 testCaseName: "lintable module defined outside libcore",
9622 moduleDirectory: "",
9623 disallowedFlagExpected: true,
9624 },
9625 {
9626 testCaseName: "lintable module defined in libcore root directory",
9627 moduleDirectory: "libcore/",
9628 disallowedFlagExpected: false,
9629 },
9630 {
9631 testCaseName: "lintable module defined in libcore child directory",
9632 moduleDirectory: "libcore/childdir/",
9633 disallowedFlagExpected: true,
9634 },
9635 }
9636
9637 for _, testCase := range testCases {
9638 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9639 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9640 result := testApex(t, "", lintFileCreator, bpFileCreator)
9641 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9642 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9643 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9644 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9645
9646 if disallowedFlagActual != testCase.disallowedFlagExpected {
9647 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9648 }
9649 }
9650}
9651
Spandan Das66773252022-01-15 00:23:18 +00009652// checks transtive deps of an apex coming from bootclasspath_fragment
9653func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9654 bp := `
9655 apex {
9656 name: "myapex",
9657 key: "myapex.key",
9658 bootclasspath_fragments: ["mybootclasspathfragment"],
9659 updatable: true,
9660 min_sdk_version: "29",
9661 }
9662 apex_key {
9663 name: "myapex.key",
9664 }
9665 bootclasspath_fragment {
9666 name: "mybootclasspathfragment",
9667 contents: ["myjavalib"],
9668 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009669 hidden_api: {
9670 split_packages: ["*"],
9671 },
Spandan Das66773252022-01-15 00:23:18 +00009672 }
9673 java_library {
9674 name: "myjavalib",
9675 srcs: ["MyClass.java"],
9676 apex_available: [ "myapex" ],
9677 sdk_version: "current",
9678 min_sdk_version: "29",
9679 compile_dex: true,
9680 }
9681 `
9682 fs := android.MockFS{
9683 "lint-baseline.xml": nil,
9684 }
9685
9686 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9687 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9688 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9689 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9690 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9691 }
9692}
9693
Spandan Das42e89502022-05-06 22:12:55 +00009694// updatable apexes should propagate updatable=true to its apps
9695func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9696 bp := `
9697 apex {
9698 name: "myapex",
9699 key: "myapex.key",
9700 updatable: %v,
9701 apps: [
9702 "myapp",
9703 ],
9704 min_sdk_version: "30",
9705 }
9706 apex_key {
9707 name: "myapex.key",
9708 }
9709 android_app {
9710 name: "myapp",
9711 updatable: %v,
9712 apex_available: [
9713 "myapex",
9714 ],
9715 sdk_version: "current",
9716 min_sdk_version: "30",
9717 }
9718 `
9719 testCases := []struct {
9720 name string
9721 apex_is_updatable_bp bool
9722 app_is_updatable_bp bool
9723 app_is_updatable_expected bool
9724 }{
9725 {
9726 name: "Non-updatable apex respects updatable property of non-updatable app",
9727 apex_is_updatable_bp: false,
9728 app_is_updatable_bp: false,
9729 app_is_updatable_expected: false,
9730 },
9731 {
9732 name: "Non-updatable apex respects updatable property of updatable app",
9733 apex_is_updatable_bp: false,
9734 app_is_updatable_bp: true,
9735 app_is_updatable_expected: true,
9736 },
9737 {
9738 name: "Updatable apex respects updatable property of updatable app",
9739 apex_is_updatable_bp: true,
9740 app_is_updatable_bp: true,
9741 app_is_updatable_expected: true,
9742 },
9743 {
9744 name: "Updatable apex sets updatable=true on non-updatable app",
9745 apex_is_updatable_bp: true,
9746 app_is_updatable_bp: false,
9747 app_is_updatable_expected: true,
9748 },
9749 }
9750 for _, testCase := range testCases {
9751 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9752 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9753 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9754 }
9755}
9756
Kiyoung Kim487689e2022-07-26 09:48:22 +09009757func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9758 bp := `
9759 apex {
9760 name: "myapex",
9761 key: "myapex.key",
9762 native_shared_libs: ["libfoo"],
9763 min_sdk_version: "29",
9764 }
9765 apex_key {
9766 name: "myapex.key",
9767 }
9768 cc_library {
9769 name: "libfoo",
9770 shared_libs: ["libc"],
9771 apex_available: ["myapex"],
9772 min_sdk_version: "29",
9773 }
9774 cc_api_library {
9775 name: "libc",
9776 src: "libc.so",
9777 min_sdk_version: "29",
9778 recovery_available: true,
9779 }
9780 api_imports {
9781 name: "api_imports",
9782 shared_libs: [
9783 "libc",
9784 ],
9785 header_libs: [],
9786 }
9787 `
9788 result := testApex(t, bp)
9789
9790 hasDep := func(m android.Module, wantDep android.Module) bool {
9791 t.Helper()
9792 var found bool
9793 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9794 if dep == wantDep {
9795 found = true
9796 }
9797 })
9798 return found
9799 }
9800
9801 libfooApexVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex29").Module()
9802 libcApexVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared_apex29").Module()
9803
9804 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(libfooApexVariant, libcApexVariant))
9805
9806 // libfoo core variant should be buildable in the same inner tree since
9807 // certain mcombo files might build system and apexes in the same inner tree
9808 // libfoo core variant should link against source libc
9809 libfooCoreVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
9810 libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
9811 android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
9812}
Dennis Shend4f5d932023-01-31 20:27:21 +00009813
9814func TestTrimmedApex(t *testing.T) {
9815 bp := `
9816 apex {
9817 name: "myapex",
9818 key: "myapex.key",
9819 native_shared_libs: ["libfoo","libbaz"],
9820 min_sdk_version: "29",
9821 trim_against: "mydcla",
9822 }
9823 apex {
9824 name: "mydcla",
9825 key: "myapex.key",
9826 native_shared_libs: ["libfoo","libbar"],
9827 min_sdk_version: "29",
9828 file_contexts: ":myapex-file_contexts",
9829 dynamic_common_lib_apex: true,
9830 }
9831 apex_key {
9832 name: "myapex.key",
9833 }
9834 cc_library {
9835 name: "libfoo",
9836 shared_libs: ["libc"],
9837 apex_available: ["myapex","mydcla"],
9838 min_sdk_version: "29",
9839 }
9840 cc_library {
9841 name: "libbar",
9842 shared_libs: ["libc"],
9843 apex_available: ["myapex","mydcla"],
9844 min_sdk_version: "29",
9845 }
9846 cc_library {
9847 name: "libbaz",
9848 shared_libs: ["libc"],
9849 apex_available: ["myapex","mydcla"],
9850 min_sdk_version: "29",
9851 }
9852 cc_api_library {
9853 name: "libc",
9854 src: "libc.so",
9855 min_sdk_version: "29",
9856 recovery_available: true,
9857 }
9858 api_imports {
9859 name: "api_imports",
9860 shared_libs: [
9861 "libc",
9862 ],
9863 header_libs: [],
9864 }
9865 `
9866 ctx := testApex(t, bp)
9867 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
9868 apexRule := module.MaybeRule("apexRule")
9869 if apexRule.Rule == nil {
9870 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
9871 }
9872
9873 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
9874 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
9875 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
9876 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
9877 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
9878 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
9879}