blob: e558fee037dfddbd95b347984a1282ad0746c939 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Alex Light5098a612018-11-29 17:12:15 -0800787func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800788 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800789 apex {
790 name: "myapex",
791 key: "myapex.key",
792 payload_type: "zip",
793 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000794 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800795 }
796
797 apex_key {
798 name: "myapex.key",
799 public_key: "testkey.avbpubkey",
800 private_key: "testkey.pem",
801 }
802
803 cc_library {
804 name: "mylib",
805 srcs: ["mylib.cpp"],
806 shared_libs: ["mylib2"],
807 system_shared_libs: [],
808 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000809 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800810 }
811
812 cc_library {
813 name: "mylib2",
814 srcs: ["mylib.cpp"],
815 system_shared_libs: [],
816 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000817 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800818 }
819 `)
820
Sundong Ahnabb64432019-10-22 13:58:29 +0900821 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800822 copyCmds := zipApexRule.Args["copy_commands"]
823
824 // Ensure that main rule creates an output
825 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
826
827 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700828 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800829
830 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700831 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800832
833 // Ensure that both direct and indirect deps are copied into apex
834 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
835 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900836}
837
838func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800839 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 apex {
841 name: "myapex",
842 key: "myapex.key",
843 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900844 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000845 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900846 }
847
848 apex_key {
849 name: "myapex.key",
850 public_key: "testkey.avbpubkey",
851 private_key: "testkey.pem",
852 }
853
854 cc_library {
855 name: "mylib",
856 srcs: ["mylib.cpp"],
857 shared_libs: ["mylib2", "mylib3"],
858 system_shared_libs: [],
859 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000860 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900861 }
862
863 cc_library {
864 name: "mylib2",
865 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900866 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900867 system_shared_libs: [],
868 stl: "none",
869 stubs: {
870 versions: ["1", "2", "3"],
871 },
872 }
873
874 cc_library {
875 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900876 srcs: ["mylib.cpp"],
877 shared_libs: ["mylib4"],
878 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900879 stl: "none",
880 stubs: {
881 versions: ["10", "11", "12"],
882 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000883 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900884 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900885
886 cc_library {
887 name: "mylib4",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000891 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900892 }
Jiyong Park105dc322021-06-11 17:22:09 +0900893
894 rust_binary {
895 name: "foo.rust",
896 srcs: ["foo.rs"],
897 shared_libs: ["libfoo.shared_from_rust"],
898 prefer_rlib: true,
899 apex_available: ["myapex"],
900 }
901
902 cc_library_shared {
903 name: "libfoo.shared_from_rust",
904 srcs: ["mylib.cpp"],
905 system_shared_libs: [],
906 stl: "none",
907 stubs: {
908 versions: ["10", "11", "12"],
909 },
910 }
911
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 `)
913
Sundong Ahnabb64432019-10-22 13:58:29 +0900914 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915 copyCmds := apexRule.Args["copy_commands"]
916
917 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800918 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919
920 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800921 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922
923 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800924 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925
Colin Crossaede88c2020-08-11 12:17:01 -0700926 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927
928 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900929 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900930 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900931 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932
933 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700934 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700936 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900937
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700938 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
939 // is replaced by sharing of "cFlags" in cc/builder.go.
940 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
941 // module variable representing "cflags". So it was not detected by ensureNotContains.
942 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
943 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
944 // including the original cflags's "-include mylib.h".
945 //
Jiyong Park64379952018-12-13 18:37:29 +0900946 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700947 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
948 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900949
Jiyong Park85cc35a2022-07-17 11:30:47 +0900950 // Ensure that genstub for platform-provided lib is invoked with --systemapi
951 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
952 // Ensure that genstub for apex-provided lib is invoked with --apex
953 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900954
Jooyung Hana57af4a2020-01-23 05:36:59 +0000955 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900956 "lib64/mylib.so",
957 "lib64/mylib3.so",
958 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900959 "bin/foo.rust",
960 "lib64/libc++.so", // by the implicit dependency from foo.rust
961 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900962 })
Jiyong Park105dc322021-06-11 17:22:09 +0900963
964 // Ensure that stub dependency from a rust module is not included
965 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
966 // The rust module is linked to the stub cc library
967 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
968 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
969 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900970
971 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
972 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973}
974
Jiyong Park1bc84122021-06-22 20:23:05 +0900975func TestApexCanUsePrivateApis(t *testing.T) {
976 ctx := testApex(t, `
977 apex {
978 name: "myapex",
979 key: "myapex.key",
980 native_shared_libs: ["mylib"],
981 binaries: ["foo.rust"],
982 updatable: false,
983 platform_apis: true,
984 }
985
986 apex_key {
987 name: "myapex.key",
988 public_key: "testkey.avbpubkey",
989 private_key: "testkey.pem",
990 }
991
992 cc_library {
993 name: "mylib",
994 srcs: ["mylib.cpp"],
995 shared_libs: ["mylib2"],
996 system_shared_libs: [],
997 stl: "none",
998 apex_available: [ "myapex" ],
999 }
1000
1001 cc_library {
1002 name: "mylib2",
1003 srcs: ["mylib.cpp"],
1004 cflags: ["-include mylib.h"],
1005 system_shared_libs: [],
1006 stl: "none",
1007 stubs: {
1008 versions: ["1", "2", "3"],
1009 },
1010 }
1011
1012 rust_binary {
1013 name: "foo.rust",
1014 srcs: ["foo.rs"],
1015 shared_libs: ["libfoo.shared_from_rust"],
1016 prefer_rlib: true,
1017 apex_available: ["myapex"],
1018 }
1019
1020 cc_library_shared {
1021 name: "libfoo.shared_from_rust",
1022 srcs: ["mylib.cpp"],
1023 system_shared_libs: [],
1024 stl: "none",
1025 stubs: {
1026 versions: ["10", "11", "12"],
1027 },
1028 }
1029 `)
1030
1031 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1032 copyCmds := apexRule.Args["copy_commands"]
1033
1034 // Ensure that indirect stubs dep is not included
1035 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1036 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1037
1038 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1039 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001040 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001041 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1042 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Parkd4a00632022-04-12 12:23:20 +09001043 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001044 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1045 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1046}
1047
Colin Cross7812fd32020-09-25 12:35:10 -07001048func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1049 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001050 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
1054 native_shared_libs: ["mylib", "mylib3"],
1055 min_sdk_version: "29",
1056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 cc_library {
1065 name: "mylib",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["mylib2", "mylib3"],
1068 system_shared_libs: [],
1069 stl: "none",
1070 apex_available: [ "myapex" ],
1071 min_sdk_version: "28",
1072 }
1073
1074 cc_library {
1075 name: "mylib2",
1076 srcs: ["mylib.cpp"],
1077 cflags: ["-include mylib.h"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 stubs: {
1081 versions: ["28", "29", "30", "current"],
1082 },
1083 min_sdk_version: "28",
1084 }
1085
1086 cc_library {
1087 name: "mylib3",
1088 srcs: ["mylib.cpp"],
1089 shared_libs: ["mylib4"],
1090 system_shared_libs: [],
1091 stl: "none",
1092 stubs: {
1093 versions: ["28", "29", "30", "current"],
1094 },
1095 apex_available: [ "myapex" ],
1096 min_sdk_version: "28",
1097 }
1098
1099 cc_library {
1100 name: "mylib4",
1101 srcs: ["mylib.cpp"],
1102 system_shared_libs: [],
1103 stl: "none",
1104 apex_available: [ "myapex" ],
1105 min_sdk_version: "28",
1106 }
1107 `)
1108
1109 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1110 copyCmds := apexRule.Args["copy_commands"]
1111
1112 // Ensure that direct non-stubs dep is always included
1113 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1114
1115 // Ensure that indirect stubs dep is not included
1116 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1117
1118 // Ensure that direct stubs dep is included
1119 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1120
1121 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1122
Jiyong Park55549df2021-02-26 23:57:23 +09001123 // Ensure that mylib is linking with the latest version of stub for mylib2
1124 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001125 // ... and not linking to the non-stub (impl) variant of mylib2
1126 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1127
1128 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1129 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1130 // .. and not linking to the stubs variant of mylib3
1131 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1132
1133 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001134 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001135 ensureNotContains(t, mylib2Cflags, "-include ")
1136
Jiyong Park85cc35a2022-07-17 11:30:47 +09001137 // Ensure that genstub is invoked with --systemapi
1138 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001139
1140 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1141 "lib64/mylib.so",
1142 "lib64/mylib3.so",
1143 "lib64/mylib4.so",
1144 })
1145}
1146
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001147func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1148 t.Parallel()
1149 // myapex (Z)
1150 // mylib -----------------.
1151 // |
1152 // otherapex (29) |
1153 // libstub's versions: 29 Z current
1154 // |
1155 // <platform> |
1156 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001157 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001158 apex {
1159 name: "myapex",
1160 key: "myapex.key",
1161 native_shared_libs: ["mylib"],
1162 min_sdk_version: "Z", // non-final
1163 }
1164
1165 cc_library {
1166 name: "mylib",
1167 srcs: ["mylib.cpp"],
1168 shared_libs: ["libstub"],
1169 apex_available: ["myapex"],
1170 min_sdk_version: "Z",
1171 }
1172
1173 apex_key {
1174 name: "myapex.key",
1175 public_key: "testkey.avbpubkey",
1176 private_key: "testkey.pem",
1177 }
1178
1179 apex {
1180 name: "otherapex",
1181 key: "myapex.key",
1182 native_shared_libs: ["libstub"],
1183 min_sdk_version: "29",
1184 }
1185
1186 cc_library {
1187 name: "libstub",
1188 srcs: ["mylib.cpp"],
1189 stubs: {
1190 versions: ["29", "Z", "current"],
1191 },
1192 apex_available: ["otherapex"],
1193 min_sdk_version: "29",
1194 }
1195
1196 // platform module depending on libstub from otherapex should use the latest stub("current")
1197 cc_library {
1198 name: "libplatform",
1199 srcs: ["mylib.cpp"],
1200 shared_libs: ["libstub"],
1201 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001202 `,
1203 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1204 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1205 variables.Platform_sdk_final = proptools.BoolPtr(false)
1206 variables.Platform_version_active_codenames = []string{"Z"}
1207 }),
1208 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001209
Jiyong Park55549df2021-02-26 23:57:23 +09001210 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001211 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001212 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001213 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001214 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001215
1216 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1217 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1218 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1219 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1220 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1221}
1222
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001223func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001224 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001225 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001226 name: "myapex2",
1227 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001228 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001229 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001230 }
1231
1232 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001233 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "mylib",
1240 srcs: ["mylib.cpp"],
1241 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001242 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001243 system_shared_libs: [],
1244 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001245 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001246 }
1247
1248 cc_library {
1249 name: "libfoo",
1250 srcs: ["mylib.cpp"],
1251 shared_libs: ["libbar"],
1252 system_shared_libs: [],
1253 stl: "none",
1254 stubs: {
1255 versions: ["10", "20", "30"],
1256 },
1257 }
1258
1259 cc_library {
1260 name: "libbar",
1261 srcs: ["mylib.cpp"],
1262 system_shared_libs: [],
1263 stl: "none",
1264 }
1265
Jiyong Park678c8812020-02-07 17:25:49 +09001266 cc_library_static {
1267 name: "libbaz",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 apex_available: [ "myapex2" ],
1272 }
1273
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001274 `)
1275
Jiyong Park83dc74b2020-01-14 18:38:44 +09001276 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001277 copyCmds := apexRule.Args["copy_commands"]
1278
1279 // Ensure that direct non-stubs dep is always included
1280 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1281
1282 // Ensure that indirect stubs dep is not included
1283 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1284
1285 // Ensure that dependency of stubs is not included
1286 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1287
Colin Crossaede88c2020-08-11 12:17:01 -07001288 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289
1290 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001291 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001292 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001293 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001294
Jiyong Park3ff16992019-12-27 14:11:47 +09001295 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001296
1297 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1298 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001299
Artur Satayeva8bd1132020-04-27 18:07:06 +01001300 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001301 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001302
Artur Satayeva8bd1132020-04-27 18:07:06 +01001303 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001304 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001305}
1306
Jooyung Hand3639552019-08-09 12:57:43 +09001307func TestApexWithRuntimeLibsDependency(t *testing.T) {
1308 /*
1309 myapex
1310 |
1311 v (runtime_libs)
1312 mylib ------+------> libfoo [provides stub]
1313 |
1314 `------> libbar
1315 */
Colin Cross1c460562021-02-16 17:55:47 -08001316 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001317 apex {
1318 name: "myapex",
1319 key: "myapex.key",
1320 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001321 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001322 }
1323
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329
1330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 runtime_libs: ["libfoo", "libbar"],
1334 system_shared_libs: [],
1335 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001336 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001337 }
1338
1339 cc_library {
1340 name: "libfoo",
1341 srcs: ["mylib.cpp"],
1342 system_shared_libs: [],
1343 stl: "none",
1344 stubs: {
1345 versions: ["10", "20", "30"],
1346 },
1347 }
1348
1349 cc_library {
1350 name: "libbar",
1351 srcs: ["mylib.cpp"],
1352 system_shared_libs: [],
1353 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001354 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001355 }
1356
1357 `)
1358
Sundong Ahnabb64432019-10-22 13:58:29 +09001359 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001360 copyCmds := apexRule.Args["copy_commands"]
1361
1362 // Ensure that direct non-stubs dep is always included
1363 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1364
1365 // Ensure that indirect stubs dep is not included
1366 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1367
1368 // Ensure that runtime_libs dep in included
1369 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1370
Sundong Ahnabb64432019-10-22 13:58:29 +09001371 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001372 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1373 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001374
1375}
1376
Paul Duffina02cae32021-03-09 01:44:06 +00001377var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1378 cc.PrepareForTestWithCcBuildComponents,
1379 PrepareForTestWithApexBuildComponents,
1380 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001381 apex {
1382 name: "com.android.runtime",
1383 key: "com.android.runtime.key",
1384 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001385 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001386 }
1387
1388 apex_key {
1389 name: "com.android.runtime.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
Paul Duffina02cae32021-03-09 01:44:06 +00001393 `),
1394 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1395)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001396
Paul Duffina02cae32021-03-09 01:44:06 +00001397func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001398 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001399 cc_library {
1400 name: "libc",
1401 no_libcrt: true,
1402 nocrt: true,
1403 stl: "none",
1404 system_shared_libs: [],
1405 stubs: { versions: ["1"] },
1406 apex_available: ["com.android.runtime"],
1407
1408 sanitize: {
1409 hwaddress: true,
1410 }
1411 }
1412
1413 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001414 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001415 no_libcrt: true,
1416 nocrt: true,
1417 stl: "none",
1418 system_shared_libs: [],
1419 srcs: [""],
1420 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001421 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001422
1423 sanitize: {
1424 never: true,
1425 },
Paul Duffina02cae32021-03-09 01:44:06 +00001426 } `)
1427 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001428
1429 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1430 "lib64/bionic/libc.so",
1431 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1432 })
1433
Colin Cross4c4c1be2022-02-10 11:41:18 -08001434 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001435
1436 installed := hwasan.Description("install libclang_rt.hwasan")
1437 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1438
1439 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1440 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1441 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1442}
1443
1444func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001445 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001446 prepareForTestOfRuntimeApexWithHwasan,
1447 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1448 variables.SanitizeDevice = []string{"hwaddress"}
1449 }),
1450 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001451 cc_library {
1452 name: "libc",
1453 no_libcrt: true,
1454 nocrt: true,
1455 stl: "none",
1456 system_shared_libs: [],
1457 stubs: { versions: ["1"] },
1458 apex_available: ["com.android.runtime"],
1459 }
1460
1461 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001462 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001463 no_libcrt: true,
1464 nocrt: true,
1465 stl: "none",
1466 system_shared_libs: [],
1467 srcs: [""],
1468 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001469 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001470
1471 sanitize: {
1472 never: true,
1473 },
1474 }
Paul Duffina02cae32021-03-09 01:44:06 +00001475 `)
1476 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001477
1478 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1479 "lib64/bionic/libc.so",
1480 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1481 })
1482
Colin Cross4c4c1be2022-02-10 11:41:18 -08001483 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001484
1485 installed := hwasan.Description("install libclang_rt.hwasan")
1486 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1487
1488 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1489 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1490 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1491}
1492
Jooyung Han61b66e92020-03-21 14:21:46 +00001493func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1494 testcases := []struct {
1495 name string
1496 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001497 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001498 shouldLink string
1499 shouldNotLink []string
1500 }{
1501 {
Jiyong Park55549df2021-02-26 23:57:23 +09001502 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001503 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001504 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001505 shouldLink: "current",
1506 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001507 },
1508 {
Jiyong Park55549df2021-02-26 23:57:23 +09001509 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001510 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001511 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001512 shouldLink: "current",
1513 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001514 },
1515 }
1516 for _, tc := range testcases {
1517 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001518 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001519 apex {
1520 name: "myapex",
1521 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001522 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001523 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001524 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001526
Jooyung Han61b66e92020-03-21 14:21:46 +00001527 apex_key {
1528 name: "myapex.key",
1529 public_key: "testkey.avbpubkey",
1530 private_key: "testkey.pem",
1531 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001532
Jooyung Han61b66e92020-03-21 14:21:46 +00001533 cc_library {
1534 name: "mylib",
1535 srcs: ["mylib.cpp"],
1536 vendor_available: true,
1537 shared_libs: ["libbar"],
1538 system_shared_libs: [],
1539 stl: "none",
1540 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001541 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001542 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001543
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 cc_library {
1545 name: "libbar",
1546 srcs: ["mylib.cpp"],
1547 system_shared_libs: [],
1548 stl: "none",
1549 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001550 llndk: {
1551 symbol_file: "libbar.map.txt",
1552 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001553 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001554 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001555 withUnbundledBuild,
1556 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001557
Jooyung Han61b66e92020-03-21 14:21:46 +00001558 // Ensure that LLNDK dep is not included
1559 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1560 "lib64/mylib.so",
1561 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001562
Jooyung Han61b66e92020-03-21 14:21:46 +00001563 // Ensure that LLNDK dep is required
1564 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1565 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1566 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001567
Steven Moreland2c4000c2021-04-27 02:08:49 +00001568 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1569 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001571 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001572 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001573
Steven Moreland2c4000c2021-04-27 02:08:49 +00001574 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001575 ver := tc.shouldLink
1576 if tc.shouldLink == "current" {
1577 ver = strconv.Itoa(android.FutureApiLevelInt)
1578 }
1579 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001580 })
1581 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001582}
1583
Jiyong Park25fc6a92018-11-18 18:02:45 +09001584func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001585 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001586 apex {
1587 name: "myapex",
1588 key: "myapex.key",
1589 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001590 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001591 }
1592
1593 apex_key {
1594 name: "myapex.key",
1595 public_key: "testkey.avbpubkey",
1596 private_key: "testkey.pem",
1597 }
1598
1599 cc_library {
1600 name: "mylib",
1601 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001602 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001603 shared_libs: ["libdl#27"],
1604 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001605 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001606 }
1607
1608 cc_library_shared {
1609 name: "mylib_shared",
1610 srcs: ["mylib.cpp"],
1611 shared_libs: ["libdl#27"],
1612 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001613 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001614 }
1615
1616 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001617 name: "libBootstrap",
1618 srcs: ["mylib.cpp"],
1619 stl: "none",
1620 bootstrap: true,
1621 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001622 `)
1623
Sundong Ahnabb64432019-10-22 13:58:29 +09001624 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001625 copyCmds := apexRule.Args["copy_commands"]
1626
1627 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001628 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001629 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1630 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001631
1632 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001633 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001634
Colin Crossaede88c2020-08-11 12:17:01 -07001635 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1636 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1637 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001638
1639 // For dependency to libc
1640 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001641 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001642 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001643 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001644 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001645 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1646 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647
1648 // For dependency to libm
1649 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001650 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001652 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001653 // ... and is not compiling with the stub
1654 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1655 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1656
1657 // For dependency to libdl
1658 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001659 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001660 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001661 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1662 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001663 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001664 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001665 // ... Cflags from stub is correctly exported to mylib
1666 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1667 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001668
1669 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001670 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1671 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1672 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1673 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001674}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001675
Jooyung Han749dc692020-04-15 11:03:39 +09001676func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001677 // there are three links between liba --> libz.
1678 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001679 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001680 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001681 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001686 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001687 }
1688
1689 apex {
1690 name: "otherapex",
1691 key: "myapex.key",
1692 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001693 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001694 }
1695
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701
1702 cc_library {
1703 name: "libx",
1704 shared_libs: ["liba"],
1705 system_shared_libs: [],
1706 stl: "none",
1707 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001708 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001709 }
1710
1711 cc_library {
1712 name: "liby",
1713 shared_libs: ["liba"],
1714 system_shared_libs: [],
1715 stl: "none",
1716 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001717 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001718 }
1719
1720 cc_library {
1721 name: "liba",
1722 shared_libs: ["libz"],
1723 system_shared_libs: [],
1724 stl: "none",
1725 apex_available: [
1726 "//apex_available:anyapex",
1727 "//apex_available:platform",
1728 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001729 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001730 }
1731
1732 cc_library {
1733 name: "libz",
1734 system_shared_libs: [],
1735 stl: "none",
1736 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001737 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001738 },
1739 }
Jooyung Han749dc692020-04-15 11:03:39 +09001740 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001741
1742 expectLink := func(from, from_variant, to, to_variant string) {
1743 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1744 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1745 }
1746 expectNoLink := func(from, from_variant, to, to_variant string) {
1747 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1748 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1749 }
1750 // platform liba is linked to non-stub version
1751 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001752 // liba in myapex is linked to current
1753 expectLink("liba", "shared_apex29", "libz", "shared_current")
1754 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001755 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001756 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001757 // liba in otherapex is linked to current
1758 expectLink("liba", "shared_apex30", "libz", "shared_current")
1759 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001760 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1761 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001762}
1763
Jooyung Hanaed150d2020-04-02 01:41:41 +09001764func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001765 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001766 apex {
1767 name: "myapex",
1768 key: "myapex.key",
1769 native_shared_libs: ["libx"],
1770 min_sdk_version: "R",
1771 }
1772
1773 apex_key {
1774 name: "myapex.key",
1775 public_key: "testkey.avbpubkey",
1776 private_key: "testkey.pem",
1777 }
1778
1779 cc_library {
1780 name: "libx",
1781 shared_libs: ["libz"],
1782 system_shared_libs: [],
1783 stl: "none",
1784 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001785 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001786 }
1787
1788 cc_library {
1789 name: "libz",
1790 system_shared_libs: [],
1791 stl: "none",
1792 stubs: {
1793 versions: ["29", "R"],
1794 },
1795 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001796 `,
1797 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1798 variables.Platform_version_active_codenames = []string{"R"}
1799 }),
1800 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801
1802 expectLink := func(from, from_variant, to, to_variant string) {
1803 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1804 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1805 }
1806 expectNoLink := func(from, from_variant, to, to_variant string) {
1807 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1808 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1809 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001810 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1811 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001812 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1813 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001814}
1815
Jooyung Han4c4da062021-06-23 10:23:16 +09001816func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1817 testApex(t, `
1818 apex {
1819 name: "myapex",
1820 key: "myapex.key",
1821 java_libs: ["libx"],
1822 min_sdk_version: "S",
1823 }
1824
1825 apex_key {
1826 name: "myapex.key",
1827 public_key: "testkey.avbpubkey",
1828 private_key: "testkey.pem",
1829 }
1830
1831 java_library {
1832 name: "libx",
1833 srcs: ["a.java"],
1834 apex_available: [ "myapex" ],
1835 sdk_version: "current",
1836 min_sdk_version: "S", // should be okay
1837 }
1838 `,
1839 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1840 variables.Platform_version_active_codenames = []string{"S"}
1841 variables.Platform_sdk_codename = proptools.StringPtr("S")
1842 }),
1843 )
1844}
1845
Jooyung Han749dc692020-04-15 11:03:39 +09001846func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001847 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001848 apex {
1849 name: "myapex",
1850 key: "myapex.key",
1851 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001852 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001853 }
1854
1855 apex_key {
1856 name: "myapex.key",
1857 public_key: "testkey.avbpubkey",
1858 private_key: "testkey.pem",
1859 }
1860
1861 cc_library {
1862 name: "libx",
1863 shared_libs: ["libz"],
1864 system_shared_libs: [],
1865 stl: "none",
1866 apex_available: [ "myapex" ],
1867 }
1868
1869 cc_library {
1870 name: "libz",
1871 system_shared_libs: [],
1872 stl: "none",
1873 stubs: {
1874 versions: ["1", "2"],
1875 },
1876 }
1877 `)
1878
1879 expectLink := func(from, from_variant, to, to_variant string) {
1880 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1881 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1882 }
1883 expectNoLink := func(from, from_variant, to, to_variant string) {
1884 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1885 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1886 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001887 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001888 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001889 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001890 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001891}
1892
Jiyong Park5df7bd32021-08-25 16:18:46 +09001893func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1894 ctx := testApex(t, `
1895 apex {
1896 name: "myapex",
1897 key: "myapex.key",
1898 native_shared_libs: ["mylib"],
1899 updatable: false,
1900 vendor: true,
1901 min_sdk_version: "29",
1902 }
1903
1904 apex_key {
1905 name: "myapex.key",
1906 public_key: "testkey.avbpubkey",
1907 private_key: "testkey.pem",
1908 }
1909
1910 cc_library {
1911 name: "mylib",
1912 vendor_available: true,
1913 system_shared_libs: [],
1914 stl: "none",
1915 apex_available: [ "myapex" ],
1916 min_sdk_version: "29",
1917 }
1918 `)
1919
1920 vendorVariant := "android_vendor.29_arm64_armv8-a"
1921
1922 // First check that the correct variant of crtbegin_so is used.
1923 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1924 crtBegin := names(ldRule.Args["crtBegin"])
1925 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1926
1927 // Ensure that the crtbegin_so used by the APEX is targeting 29
1928 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1929 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1930}
1931
Jooyung Han03b51852020-02-26 22:45:42 +09001932func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001933 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001934 apex {
1935 name: "myapex",
1936 key: "myapex.key",
1937 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001938 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "libx",
1949 system_shared_libs: [],
1950 stl: "none",
1951 apex_available: [ "myapex" ],
1952 stubs: {
1953 versions: ["1", "2"],
1954 },
1955 }
1956
1957 cc_library {
1958 name: "libz",
1959 shared_libs: ["libx"],
1960 system_shared_libs: [],
1961 stl: "none",
1962 }
1963 `)
1964
1965 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001966 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001967 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1968 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1969 }
1970 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001971 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001972 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1973 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1974 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001975 expectLink("libz", "shared", "libx", "shared_current")
1976 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001977 expectNoLink("libz", "shared", "libz", "shared_1")
1978 expectNoLink("libz", "shared", "libz", "shared")
1979}
1980
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001981var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1982 func(variables android.FixtureProductVariables) {
1983 variables.SanitizeDevice = []string{"hwaddress"}
1984 },
1985)
1986
Jooyung Han75568392020-03-20 04:29:24 +09001987func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001988 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001989 apex {
1990 name: "myapex",
1991 key: "myapex.key",
1992 native_shared_libs: ["libx"],
1993 min_sdk_version: "29",
1994 }
1995
1996 apex_key {
1997 name: "myapex.key",
1998 public_key: "testkey.avbpubkey",
1999 private_key: "testkey.pem",
2000 }
2001
2002 cc_library {
2003 name: "libx",
2004 shared_libs: ["libbar"],
2005 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002006 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002007 }
2008
2009 cc_library {
2010 name: "libbar",
2011 stubs: {
2012 versions: ["29", "30"],
2013 },
2014 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002015 `,
2016 prepareForTestWithSantitizeHwaddress,
2017 )
Jooyung Han03b51852020-02-26 22:45:42 +09002018 expectLink := func(from, from_variant, to, to_variant string) {
2019 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2020 libFlags := ld.Args["libFlags"]
2021 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2022 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002023 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002024}
2025
Jooyung Han75568392020-03-20 04:29:24 +09002026func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002027 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002028 apex {
2029 name: "myapex",
2030 key: "myapex.key",
2031 native_shared_libs: ["libx"],
2032 min_sdk_version: "29",
2033 }
2034
2035 apex_key {
2036 name: "myapex.key",
2037 public_key: "testkey.avbpubkey",
2038 private_key: "testkey.pem",
2039 }
2040
2041 cc_library {
2042 name: "libx",
2043 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002044 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002045 }
Jooyung Han75568392020-03-20 04:29:24 +09002046 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002047
2048 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002049 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002050 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002051 // note that platform variant is not.
2052 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002053 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002054}
2055
Jooyung Han749dc692020-04-15 11:03:39 +09002056func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2057 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002058 apex {
2059 name: "myapex",
2060 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002061 native_shared_libs: ["mylib"],
2062 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002063 }
2064
2065 apex_key {
2066 name: "myapex.key",
2067 public_key: "testkey.avbpubkey",
2068 private_key: "testkey.pem",
2069 }
Jooyung Han749dc692020-04-15 11:03:39 +09002070
2071 cc_library {
2072 name: "mylib",
2073 srcs: ["mylib.cpp"],
2074 system_shared_libs: [],
2075 stl: "none",
2076 apex_available: [
2077 "myapex",
2078 ],
2079 min_sdk_version: "30",
2080 }
2081 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002082
2083 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2084 apex {
2085 name: "myapex",
2086 key: "myapex.key",
2087 native_shared_libs: ["libfoo.ffi"],
2088 min_sdk_version: "29",
2089 }
2090
2091 apex_key {
2092 name: "myapex.key",
2093 public_key: "testkey.avbpubkey",
2094 private_key: "testkey.pem",
2095 }
2096
2097 rust_ffi_shared {
2098 name: "libfoo.ffi",
2099 srcs: ["foo.rs"],
2100 crate_name: "foo",
2101 apex_available: [
2102 "myapex",
2103 ],
2104 min_sdk_version: "30",
2105 }
2106 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002107
2108 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
2112 java_libs: ["libfoo"],
2113 min_sdk_version: "29",
2114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
2121
2122 java_import {
2123 name: "libfoo",
2124 jars: ["libfoo.jar"],
2125 apex_available: [
2126 "myapex",
2127 ],
2128 min_sdk_version: "30",
2129 }
2130 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002131}
2132
2133func TestApexMinSdkVersion_Okay(t *testing.T) {
2134 testApex(t, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo"],
2139 java_libs: ["libbar"],
2140 min_sdk_version: "29",
2141 }
2142
2143 apex_key {
2144 name: "myapex.key",
2145 public_key: "testkey.avbpubkey",
2146 private_key: "testkey.pem",
2147 }
2148
2149 cc_library {
2150 name: "libfoo",
2151 srcs: ["mylib.cpp"],
2152 shared_libs: ["libfoo_dep"],
2153 apex_available: ["myapex"],
2154 min_sdk_version: "29",
2155 }
2156
2157 cc_library {
2158 name: "libfoo_dep",
2159 srcs: ["mylib.cpp"],
2160 apex_available: ["myapex"],
2161 min_sdk_version: "29",
2162 }
2163
2164 java_library {
2165 name: "libbar",
2166 sdk_version: "current",
2167 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002168 static_libs: [
2169 "libbar_dep",
2170 "libbar_import_dep",
2171 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002172 apex_available: ["myapex"],
2173 min_sdk_version: "29",
2174 }
2175
2176 java_library {
2177 name: "libbar_dep",
2178 sdk_version: "current",
2179 srcs: ["a.java"],
2180 apex_available: ["myapex"],
2181 min_sdk_version: "29",
2182 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002183
2184 java_import {
2185 name: "libbar_import_dep",
2186 jars: ["libbar.jar"],
2187 apex_available: ["myapex"],
2188 min_sdk_version: "29",
2189 }
Jooyung Han03b51852020-02-26 22:45:42 +09002190 `)
2191}
2192
Colin Cross8ca61c12022-10-06 21:00:14 -07002193func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2194 // Tests that an apex dependency with min_sdk_version higher than the
2195 // min_sdk_version of the apex is allowed as long as the dependency's
2196 // min_sdk_version is less than or equal to the api level that the
2197 // architecture was introduced in. In this case, arm64 didn't exist
2198 // until api level 21, so the arm64 code will never need to run on
2199 // an api level 20 device, even if other architectures of the apex
2200 // will.
2201 testApex(t, `
2202 apex {
2203 name: "myapex",
2204 key: "myapex.key",
2205 native_shared_libs: ["libfoo"],
2206 min_sdk_version: "20",
2207 }
2208
2209 apex_key {
2210 name: "myapex.key",
2211 public_key: "testkey.avbpubkey",
2212 private_key: "testkey.pem",
2213 }
2214
2215 cc_library {
2216 name: "libfoo",
2217 srcs: ["mylib.cpp"],
2218 apex_available: ["myapex"],
2219 min_sdk_version: "21",
2220 stl: "none",
2221 }
2222 `)
2223}
2224
Artur Satayev8cf899a2020-04-15 17:29:42 +01002225func TestJavaStableSdkVersion(t *testing.T) {
2226 testCases := []struct {
2227 name string
2228 expectedError string
2229 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002230 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002231 }{
2232 {
2233 name: "Non-updatable apex with non-stable dep",
2234 bp: `
2235 apex {
2236 name: "myapex",
2237 java_libs: ["myjar"],
2238 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002239 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002240 }
2241 apex_key {
2242 name: "myapex.key",
2243 public_key: "testkey.avbpubkey",
2244 private_key: "testkey.pem",
2245 }
2246 java_library {
2247 name: "myjar",
2248 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002249 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002250 apex_available: ["myapex"],
2251 }
2252 `,
2253 },
2254 {
2255 name: "Updatable apex with stable dep",
2256 bp: `
2257 apex {
2258 name: "myapex",
2259 java_libs: ["myjar"],
2260 key: "myapex.key",
2261 updatable: true,
2262 min_sdk_version: "29",
2263 }
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269 java_library {
2270 name: "myjar",
2271 srcs: ["foo/bar/MyClass.java"],
2272 sdk_version: "current",
2273 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002274 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002275 }
2276 `,
2277 },
2278 {
2279 name: "Updatable apex with non-stable dep",
2280 expectedError: "cannot depend on \"myjar\"",
2281 bp: `
2282 apex {
2283 name: "myapex",
2284 java_libs: ["myjar"],
2285 key: "myapex.key",
2286 updatable: true,
2287 }
2288 apex_key {
2289 name: "myapex.key",
2290 public_key: "testkey.avbpubkey",
2291 private_key: "testkey.pem",
2292 }
2293 java_library {
2294 name: "myjar",
2295 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002296 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002297 apex_available: ["myapex"],
2298 }
2299 `,
2300 },
2301 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002302 name: "Updatable apex with non-stable legacy core platform dep",
2303 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2304 bp: `
2305 apex {
2306 name: "myapex",
2307 java_libs: ["myjar-uses-legacy"],
2308 key: "myapex.key",
2309 updatable: true,
2310 }
2311 apex_key {
2312 name: "myapex.key",
2313 public_key: "testkey.avbpubkey",
2314 private_key: "testkey.pem",
2315 }
2316 java_library {
2317 name: "myjar-uses-legacy",
2318 srcs: ["foo/bar/MyClass.java"],
2319 sdk_version: "core_platform",
2320 apex_available: ["myapex"],
2321 }
2322 `,
2323 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2324 },
2325 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002326 name: "Updatable apex with non-stable transitive dep",
2327 // This is not actually detecting that the transitive dependency is unstable, rather it is
2328 // detecting that the transitive dependency is building against a wider API surface than the
2329 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002330 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002331 bp: `
2332 apex {
2333 name: "myapex",
2334 java_libs: ["myjar"],
2335 key: "myapex.key",
2336 updatable: true,
2337 }
2338 apex_key {
2339 name: "myapex.key",
2340 public_key: "testkey.avbpubkey",
2341 private_key: "testkey.pem",
2342 }
2343 java_library {
2344 name: "myjar",
2345 srcs: ["foo/bar/MyClass.java"],
2346 sdk_version: "current",
2347 apex_available: ["myapex"],
2348 static_libs: ["transitive-jar"],
2349 }
2350 java_library {
2351 name: "transitive-jar",
2352 srcs: ["foo/bar/MyClass.java"],
2353 sdk_version: "core_platform",
2354 apex_available: ["myapex"],
2355 }
2356 `,
2357 },
2358 }
2359
2360 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002361 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2362 continue
2363 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002364 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002365 errorHandler := android.FixtureExpectsNoErrors
2366 if test.expectedError != "" {
2367 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002368 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002369 android.GroupFixturePreparers(
2370 java.PrepareForTestWithJavaDefaultModules,
2371 PrepareForTestWithApexBuildComponents,
2372 prepareForTestWithMyapex,
2373 android.OptionalFixturePreparer(test.preparer),
2374 ).
2375 ExtendWithErrorHandler(errorHandler).
2376 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002377 })
2378 }
2379}
2380
Jooyung Han749dc692020-04-15 11:03:39 +09002381func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2382 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2383 apex {
2384 name: "myapex",
2385 key: "myapex.key",
2386 native_shared_libs: ["mylib"],
2387 min_sdk_version: "29",
2388 }
2389
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395
2396 cc_library {
2397 name: "mylib",
2398 srcs: ["mylib.cpp"],
2399 shared_libs: ["mylib2"],
2400 system_shared_libs: [],
2401 stl: "none",
2402 apex_available: [
2403 "myapex",
2404 ],
2405 min_sdk_version: "29",
2406 }
2407
2408 // indirect part of the apex
2409 cc_library {
2410 name: "mylib2",
2411 srcs: ["mylib.cpp"],
2412 system_shared_libs: [],
2413 stl: "none",
2414 apex_available: [
2415 "myapex",
2416 ],
2417 min_sdk_version: "30",
2418 }
2419 `)
2420}
2421
2422func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2423 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 apps: ["AppFoo"],
2428 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002429 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002430 }
2431
2432 apex_key {
2433 name: "myapex.key",
2434 public_key: "testkey.avbpubkey",
2435 private_key: "testkey.pem",
2436 }
2437
2438 android_app {
2439 name: "AppFoo",
2440 srcs: ["foo/bar/MyClass.java"],
2441 sdk_version: "current",
2442 min_sdk_version: "29",
2443 system_modules: "none",
2444 stl: "none",
2445 static_libs: ["bar"],
2446 apex_available: [ "myapex" ],
2447 }
2448
2449 java_library {
2450 name: "bar",
2451 sdk_version: "current",
2452 srcs: ["a.java"],
2453 apex_available: [ "myapex" ],
2454 }
2455 `)
2456}
2457
2458func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002459 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002460 apex {
2461 name: "myapex",
2462 key: "myapex.key",
2463 native_shared_libs: ["mylib"],
2464 min_sdk_version: "29",
2465 }
2466
2467 apex_key {
2468 name: "myapex.key",
2469 public_key: "testkey.avbpubkey",
2470 private_key: "testkey.pem",
2471 }
2472
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002473 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002474 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2475 cc_library {
2476 name: "mylib",
2477 srcs: ["mylib.cpp"],
2478 shared_libs: ["mylib2"],
2479 system_shared_libs: [],
2480 stl: "none",
2481 apex_available: ["myapex", "otherapex"],
2482 min_sdk_version: "29",
2483 }
2484
2485 cc_library {
2486 name: "mylib2",
2487 srcs: ["mylib.cpp"],
2488 system_shared_libs: [],
2489 stl: "none",
2490 apex_available: ["otherapex"],
2491 stubs: { versions: ["29", "30"] },
2492 min_sdk_version: "30",
2493 }
2494
2495 apex {
2496 name: "otherapex",
2497 key: "myapex.key",
2498 native_shared_libs: ["mylib", "mylib2"],
2499 min_sdk_version: "30",
2500 }
2501 `)
2502 expectLink := func(from, from_variant, to, to_variant string) {
2503 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2504 libFlags := ld.Args["libFlags"]
2505 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2506 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002507 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002508 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002509}
2510
Jooyung Haned124c32021-01-26 11:43:46 +09002511func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002512 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2513 func(variables android.FixtureProductVariables) {
2514 variables.Platform_sdk_codename = proptools.StringPtr("S")
2515 variables.Platform_version_active_codenames = []string{"S"}
2516 },
2517 )
Jooyung Haned124c32021-01-26 11:43:46 +09002518 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2519 apex {
2520 name: "myapex",
2521 key: "myapex.key",
2522 native_shared_libs: ["libfoo"],
2523 min_sdk_version: "S",
2524 }
2525 apex_key {
2526 name: "myapex.key",
2527 public_key: "testkey.avbpubkey",
2528 private_key: "testkey.pem",
2529 }
2530 cc_library {
2531 name: "libfoo",
2532 shared_libs: ["libbar"],
2533 apex_available: ["myapex"],
2534 min_sdk_version: "29",
2535 }
2536 cc_library {
2537 name: "libbar",
2538 apex_available: ["myapex"],
2539 }
2540 `, withSAsActiveCodeNames)
2541}
2542
2543func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002544 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2545 variables.Platform_sdk_codename = proptools.StringPtr("S")
2546 variables.Platform_version_active_codenames = []string{"S", "T"}
2547 })
Colin Cross1c460562021-02-16 17:55:47 -08002548 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002549 apex {
2550 name: "myapex",
2551 key: "myapex.key",
2552 native_shared_libs: ["libfoo"],
2553 min_sdk_version: "S",
2554 }
2555 apex_key {
2556 name: "myapex.key",
2557 public_key: "testkey.avbpubkey",
2558 private_key: "testkey.pem",
2559 }
2560 cc_library {
2561 name: "libfoo",
2562 shared_libs: ["libbar"],
2563 apex_available: ["myapex"],
2564 min_sdk_version: "S",
2565 }
2566 cc_library {
2567 name: "libbar",
2568 stubs: {
2569 symbol_file: "libbar.map.txt",
2570 versions: ["30", "S", "T"],
2571 },
2572 }
2573 `, withSAsActiveCodeNames)
2574
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002575 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002576 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2577 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002578 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002579}
2580
Jiyong Park7c2ee712018-12-07 00:42:25 +09002581func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002582 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002583 apex {
2584 name: "myapex",
2585 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002586 native_shared_libs: ["mylib"],
2587 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002588 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002589 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002590 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002591 }
2592
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598
2599 prebuilt_etc {
2600 name: "myetc",
2601 src: "myprebuilt",
2602 sub_dir: "foo/bar",
2603 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002604
2605 cc_library {
2606 name: "mylib",
2607 srcs: ["mylib.cpp"],
2608 relative_install_path: "foo/bar",
2609 system_shared_libs: [],
2610 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002611 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002612 }
2613
2614 cc_binary {
2615 name: "mybin",
2616 srcs: ["mylib.cpp"],
2617 relative_install_path: "foo/bar",
2618 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002619 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002620 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002621 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002622 `)
2623
Sundong Ahnabb64432019-10-22 13:58:29 +09002624 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002625 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002626
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002627 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002628 ensureContains(t, cmd, "/etc ")
2629 ensureContains(t, cmd, "/etc/foo ")
2630 ensureContains(t, cmd, "/etc/foo/bar ")
2631 ensureContains(t, cmd, "/lib64 ")
2632 ensureContains(t, cmd, "/lib64/foo ")
2633 ensureContains(t, cmd, "/lib64/foo/bar ")
2634 ensureContains(t, cmd, "/lib ")
2635 ensureContains(t, cmd, "/lib/foo ")
2636 ensureContains(t, cmd, "/lib/foo/bar ")
2637 ensureContains(t, cmd, "/bin ")
2638 ensureContains(t, cmd, "/bin/foo ")
2639 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002640}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002641
Jooyung Han35155c42020-02-06 17:33:20 +09002642func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002643 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002644 apex {
2645 name: "myapex",
2646 key: "myapex.key",
2647 multilib: {
2648 both: {
2649 native_shared_libs: ["mylib"],
2650 binaries: ["mybin"],
2651 },
2652 },
2653 compile_multilib: "both",
2654 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002655 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002656 }
2657
2658 apex_key {
2659 name: "myapex.key",
2660 public_key: "testkey.avbpubkey",
2661 private_key: "testkey.pem",
2662 }
2663
2664 cc_library {
2665 name: "mylib",
2666 relative_install_path: "foo/bar",
2667 system_shared_libs: [],
2668 stl: "none",
2669 apex_available: [ "myapex" ],
2670 native_bridge_supported: true,
2671 }
2672
2673 cc_binary {
2674 name: "mybin",
2675 relative_install_path: "foo/bar",
2676 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002677 stl: "none",
2678 apex_available: [ "myapex" ],
2679 native_bridge_supported: true,
2680 compile_multilib: "both", // default is "first" for binary
2681 multilib: {
2682 lib64: {
2683 suffix: "64",
2684 },
2685 },
2686 }
2687 `, withNativeBridgeEnabled)
2688 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2689 "bin/foo/bar/mybin",
2690 "bin/foo/bar/mybin64",
2691 "bin/arm/foo/bar/mybin",
2692 "bin/arm64/foo/bar/mybin64",
2693 "lib/foo/bar/mylib.so",
2694 "lib/arm/foo/bar/mylib.so",
2695 "lib64/foo/bar/mylib.so",
2696 "lib64/arm64/foo/bar/mylib.so",
2697 })
2698}
2699
Jooyung Han85d61762020-06-24 23:50:26 +09002700func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002701 result := android.GroupFixturePreparers(
2702 prepareForApexTest,
2703 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2704 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 binaries: ["mybin"],
2709 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002710 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002711 }
2712 apex_key {
2713 name: "myapex.key",
2714 public_key: "testkey.avbpubkey",
2715 private_key: "testkey.pem",
2716 }
2717 cc_binary {
2718 name: "mybin",
2719 vendor: true,
2720 shared_libs: ["libfoo"],
2721 }
2722 cc_library {
2723 name: "libfoo",
2724 proprietary: true,
2725 }
2726 `)
2727
Colin Crossc68db4b2021-11-11 18:59:15 -08002728 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002729 "bin/mybin",
2730 "lib64/libfoo.so",
2731 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2732 "lib64/libc++.so",
2733 })
2734
Colin Crossc68db4b2021-11-11 18:59:15 -08002735 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2736 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002737 name := apexBundle.BaseModuleName()
2738 prefix := "TARGET_"
2739 var builder strings.Builder
2740 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002741 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002742 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002743 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002744
Colin Crossc68db4b2021-11-11 18:59:15 -08002745 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002746 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2747 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002748}
2749
Jooyung Hanc5a96762022-02-04 11:54:50 +09002750func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2751 testApexError(t, `Trying to include a VNDK library`, `
2752 apex {
2753 name: "myapex",
2754 key: "myapex.key",
2755 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2756 vendor: true,
2757 use_vndk_as_stable: true,
2758 updatable: false,
2759 }
2760 apex_key {
2761 name: "myapex.key",
2762 public_key: "testkey.avbpubkey",
2763 private_key: "testkey.pem",
2764 }`)
2765}
2766
Jooyung Handf78e212020-07-22 15:54:47 +09002767func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002768 // myapex myapex2
2769 // | |
2770 // mybin ------. mybin2
2771 // \ \ / |
2772 // (stable) .---\--------` |
2773 // \ / \ |
2774 // \ / \ /
2775 // libvndk libvendor
2776 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002777 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002778 apex {
2779 name: "myapex",
2780 key: "myapex.key",
2781 binaries: ["mybin"],
2782 vendor: true,
2783 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002784 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002785 }
2786 apex_key {
2787 name: "myapex.key",
2788 public_key: "testkey.avbpubkey",
2789 private_key: "testkey.pem",
2790 }
2791 cc_binary {
2792 name: "mybin",
2793 vendor: true,
2794 shared_libs: ["libvndk", "libvendor"],
2795 }
2796 cc_library {
2797 name: "libvndk",
2798 vndk: {
2799 enabled: true,
2800 },
2801 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002802 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002803 }
2804 cc_library {
2805 name: "libvendor",
2806 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002807 stl: "none",
2808 }
2809 apex {
2810 name: "myapex2",
2811 key: "myapex.key",
2812 binaries: ["mybin2"],
2813 vendor: true,
2814 use_vndk_as_stable: false,
2815 updatable: false,
2816 }
2817 cc_binary {
2818 name: "mybin2",
2819 vendor: true,
2820 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002821 }
2822 `)
2823
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002824 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002825
Jooyung Han91f92032022-02-04 12:36:33 +09002826 for _, tc := range []struct {
2827 name string
2828 apexName string
2829 moduleName string
2830 moduleVariant string
2831 libs []string
2832 contents []string
2833 requireVndkNamespace bool
2834 }{
2835 {
2836 name: "use_vndk_as_stable",
2837 apexName: "myapex",
2838 moduleName: "mybin",
2839 moduleVariant: vendorVariant + "_apex10000",
2840 libs: []string{
2841 // should link with vendor variants of VNDK libs(libvndk/libc++)
2842 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2843 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2844 // unstable Vendor libs as APEX variant
2845 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2846 },
2847 contents: []string{
2848 "bin/mybin",
2849 "lib64/libvendor.so",
2850 // VNDK libs (libvndk/libc++) are not included
2851 },
2852 requireVndkNamespace: true,
2853 },
2854 {
2855 name: "!use_vndk_as_stable",
2856 apexName: "myapex2",
2857 moduleName: "mybin2",
2858 moduleVariant: vendorVariant + "_myapex2",
2859 libs: []string{
2860 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2861 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2862 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2863 // unstable vendor libs have "merged" APEX variants
2864 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2865 },
2866 contents: []string{
2867 "bin/mybin2",
2868 "lib64/libvendor.so",
2869 // VNDK libs are included as well
2870 "lib64/libvndk.so",
2871 "lib64/libc++.so",
2872 },
2873 requireVndkNamespace: false,
2874 },
2875 } {
2876 t.Run(tc.name, func(t *testing.T) {
2877 // Check linked libs
2878 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2879 libs := names(ldRule.Args["libFlags"])
2880 for _, lib := range tc.libs {
2881 ensureListContains(t, libs, lib)
2882 }
2883 // Check apex contents
2884 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002885
Jooyung Han91f92032022-02-04 12:36:33 +09002886 // Check "requireNativeLibs"
2887 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2888 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2889 if tc.requireVndkNamespace {
2890 ensureListContains(t, requireNativeLibs, ":vndk")
2891 } else {
2892 ensureListNotContains(t, requireNativeLibs, ":vndk")
2893 }
2894 })
2895 }
Jooyung Handf78e212020-07-22 15:54:47 +09002896}
2897
Justin Yun13decfb2021-03-08 19:25:55 +09002898func TestProductVariant(t *testing.T) {
2899 ctx := testApex(t, `
2900 apex {
2901 name: "myapex",
2902 key: "myapex.key",
2903 updatable: false,
2904 product_specific: true,
2905 binaries: ["foo"],
2906 }
2907
2908 apex_key {
2909 name: "myapex.key",
2910 public_key: "testkey.avbpubkey",
2911 private_key: "testkey.pem",
2912 }
2913
2914 cc_binary {
2915 name: "foo",
2916 product_available: true,
2917 apex_available: ["myapex"],
2918 srcs: ["foo.cpp"],
2919 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002920 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2921 variables.ProductVndkVersion = proptools.StringPtr("current")
2922 }),
2923 )
Justin Yun13decfb2021-03-08 19:25:55 +09002924
2925 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002926 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002927 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2928 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2929 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2930 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2931}
2932
Jooyung Han8e5685d2020-09-21 11:02:57 +09002933func TestApex_withPrebuiltFirmware(t *testing.T) {
2934 testCases := []struct {
2935 name string
2936 additionalProp string
2937 }{
2938 {"system apex with prebuilt_firmware", ""},
2939 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2940 }
2941 for _, tc := range testCases {
2942 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002943 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002944 apex {
2945 name: "myapex",
2946 key: "myapex.key",
2947 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002948 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002949 `+tc.additionalProp+`
2950 }
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956 prebuilt_firmware {
2957 name: "myfirmware",
2958 src: "myfirmware.bin",
2959 filename_from_src: true,
2960 `+tc.additionalProp+`
2961 }
2962 `)
2963 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2964 "etc/firmware/myfirmware.bin",
2965 })
2966 })
2967 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002968}
2969
Jooyung Hanefb184e2020-06-25 17:14:25 +09002970func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002971 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002972 apex {
2973 name: "myapex",
2974 key: "myapex.key",
2975 vendor: true,
2976 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002977 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002978 }
2979
2980 apex_key {
2981 name: "myapex.key",
2982 public_key: "testkey.avbpubkey",
2983 private_key: "testkey.pem",
2984 }
2985
2986 cc_library {
2987 name: "mylib",
2988 vendor_available: true,
2989 }
2990 `)
2991
2992 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002993 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002994 name := apexBundle.BaseModuleName()
2995 prefix := "TARGET_"
2996 var builder strings.Builder
2997 data.Custom(&builder, name, prefix, "", data)
2998 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00002999 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003000}
3001
Jooyung Han2ed99d02020-06-24 23:26:26 +09003002func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003003 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003004 apex {
3005 name: "myapex",
3006 key: "myapex.key",
3007 vintf_fragments: ["fragment.xml"],
3008 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003009 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003010 }
3011 apex_key {
3012 name: "myapex.key",
3013 public_key: "testkey.avbpubkey",
3014 private_key: "testkey.pem",
3015 }
3016 cc_binary {
3017 name: "mybin",
3018 }
3019 `)
3020
3021 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003022 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003023 name := apexBundle.BaseModuleName()
3024 prefix := "TARGET_"
3025 var builder strings.Builder
3026 data.Custom(&builder, name, prefix, "", data)
3027 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003028 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003029 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003030}
3031
Jiyong Park16e91a02018-12-20 18:18:08 +09003032func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003033 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003034 apex {
3035 name: "myapex",
3036 key: "myapex.key",
3037 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003038 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003039 }
3040
3041 apex_key {
3042 name: "myapex.key",
3043 public_key: "testkey.avbpubkey",
3044 private_key: "testkey.pem",
3045 }
3046
3047 cc_library {
3048 name: "mylib",
3049 srcs: ["mylib.cpp"],
3050 system_shared_libs: [],
3051 stl: "none",
3052 stubs: {
3053 versions: ["1", "2", "3"],
3054 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003055 apex_available: [
3056 "//apex_available:platform",
3057 "myapex",
3058 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003059 }
3060
3061 cc_binary {
3062 name: "not_in_apex",
3063 srcs: ["mylib.cpp"],
3064 static_libs: ["mylib"],
3065 static_executable: true,
3066 system_shared_libs: [],
3067 stl: "none",
3068 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003069 `)
3070
Colin Cross7113d202019-11-20 16:39:12 -08003071 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003072
3073 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003074 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003075}
Jiyong Park9335a262018-12-24 11:31:58 +09003076
3077func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003078 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003079 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003080 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003081 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003082 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003083 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003084 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003085 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003086 }
3087
3088 cc_library {
3089 name: "mylib",
3090 srcs: ["mylib.cpp"],
3091 system_shared_libs: [],
3092 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003093 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003094 }
3095
3096 apex_key {
3097 name: "myapex.key",
3098 public_key: "testkey.avbpubkey",
3099 private_key: "testkey.pem",
3100 }
3101
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003102 android_app_certificate {
3103 name: "myapex.certificate",
3104 certificate: "testkey",
3105 }
3106
3107 android_app_certificate {
3108 name: "myapex.certificate.override",
3109 certificate: "testkey.override",
3110 }
3111
Jiyong Park9335a262018-12-24 11:31:58 +09003112 `)
3113
3114 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003115 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003116
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003117 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3118 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003119 "vendor/foo/devkeys/testkey.avbpubkey")
3120 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003121 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3122 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003123 "vendor/foo/devkeys/testkey.pem")
3124 }
3125
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003126 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003127 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003128 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003129 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003130 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003131 }
3132}
Jiyong Park58e364a2019-01-19 19:24:06 +09003133
Jooyung Hanf121a652019-12-17 14:30:11 +09003134func TestCertificate(t *testing.T) {
3135 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003136 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003137 apex {
3138 name: "myapex",
3139 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003140 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003141 }
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }`)
3147 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3148 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3149 if actual := rule.Args["certificates"]; actual != expected {
3150 t.Errorf("certificates should be %q, not %q", expected, actual)
3151 }
3152 })
3153 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003154 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003155 apex {
3156 name: "myapex_keytest",
3157 key: "myapex.key",
3158 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003159 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003160 }
3161 apex_key {
3162 name: "myapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }
3166 android_app_certificate {
3167 name: "myapex.certificate.override",
3168 certificate: "testkey.override",
3169 }`)
3170 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3171 expected := "testkey.override.x509.pem testkey.override.pk8"
3172 if actual := rule.Args["certificates"]; actual != expected {
3173 t.Errorf("certificates should be %q, not %q", expected, actual)
3174 }
3175 })
3176 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003177 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003178 apex {
3179 name: "myapex",
3180 key: "myapex.key",
3181 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003182 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003183 }
3184 apex_key {
3185 name: "myapex.key",
3186 public_key: "testkey.avbpubkey",
3187 private_key: "testkey.pem",
3188 }
3189 android_app_certificate {
3190 name: "myapex.certificate",
3191 certificate: "testkey",
3192 }`)
3193 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3194 expected := "testkey.x509.pem testkey.pk8"
3195 if actual := rule.Args["certificates"]; actual != expected {
3196 t.Errorf("certificates should be %q, not %q", expected, actual)
3197 }
3198 })
3199 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003200 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003201 apex {
3202 name: "myapex_keytest",
3203 key: "myapex.key",
3204 file_contexts: ":myapex-file_contexts",
3205 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003206 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003207 }
3208 apex_key {
3209 name: "myapex.key",
3210 public_key: "testkey.avbpubkey",
3211 private_key: "testkey.pem",
3212 }
3213 android_app_certificate {
3214 name: "myapex.certificate.override",
3215 certificate: "testkey.override",
3216 }`)
3217 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3218 expected := "testkey.override.x509.pem testkey.override.pk8"
3219 if actual := rule.Args["certificates"]; actual != expected {
3220 t.Errorf("certificates should be %q, not %q", expected, actual)
3221 }
3222 })
3223 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003224 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003225 apex {
3226 name: "myapex",
3227 key: "myapex.key",
3228 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003229 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003230 }
3231 apex_key {
3232 name: "myapex.key",
3233 public_key: "testkey.avbpubkey",
3234 private_key: "testkey.pem",
3235 }`)
3236 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3237 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3238 if actual := rule.Args["certificates"]; actual != expected {
3239 t.Errorf("certificates should be %q, not %q", expected, actual)
3240 }
3241 })
3242 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003243 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003244 apex {
3245 name: "myapex_keytest",
3246 key: "myapex.key",
3247 file_contexts: ":myapex-file_contexts",
3248 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003249 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003250 }
3251 apex_key {
3252 name: "myapex.key",
3253 public_key: "testkey.avbpubkey",
3254 private_key: "testkey.pem",
3255 }
3256 android_app_certificate {
3257 name: "myapex.certificate.override",
3258 certificate: "testkey.override",
3259 }`)
3260 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3261 expected := "testkey.override.x509.pem testkey.override.pk8"
3262 if actual := rule.Args["certificates"]; actual != expected {
3263 t.Errorf("certificates should be %q, not %q", expected, actual)
3264 }
3265 })
3266}
3267
Jiyong Park58e364a2019-01-19 19:24:06 +09003268func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003269 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003270 apex {
3271 name: "myapex",
3272 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003273 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003274 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003275 }
3276
3277 apex {
3278 name: "otherapex",
3279 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003280 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003281 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003282 }
3283
3284 apex_key {
3285 name: "myapex.key",
3286 public_key: "testkey.avbpubkey",
3287 private_key: "testkey.pem",
3288 }
3289
3290 cc_library {
3291 name: "mylib",
3292 srcs: ["mylib.cpp"],
3293 system_shared_libs: [],
3294 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003295 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003296 "myapex",
3297 "otherapex",
3298 ],
Jooyung Han24282772020-03-21 23:20:55 +09003299 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003300 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003301 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003302 cc_library {
3303 name: "mylib2",
3304 srcs: ["mylib.cpp"],
3305 system_shared_libs: [],
3306 stl: "none",
3307 apex_available: [
3308 "myapex",
3309 "otherapex",
3310 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003311 static_libs: ["mylib3"],
3312 recovery_available: true,
3313 min_sdk_version: "29",
3314 }
3315 cc_library {
3316 name: "mylib3",
3317 srcs: ["mylib.cpp"],
3318 system_shared_libs: [],
3319 stl: "none",
3320 apex_available: [
3321 "myapex",
3322 "otherapex",
3323 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003324 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003325 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003326 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003327 `)
3328
Jooyung Hanc87a0592020-03-02 17:44:33 +09003329 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003330 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003331 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003332
Vinh Tranf9754732023-01-19 22:41:46 -05003333 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003334 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003335 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003336
Vinh Tranf9754732023-01-19 22:41:46 -05003337 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003338 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003339 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003340
Colin Crossaede88c2020-08-11 12:17:01 -07003341 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3342 // each variant defines additional macros to distinguish which apex variant it is built for
3343
3344 // non-APEX variant does not have __ANDROID_APEX__ defined
3345 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3346 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3347
Vinh Tranf9754732023-01-19 22:41:46 -05003348 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003349 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3350 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003351
Jooyung Hanc87a0592020-03-02 17:44:33 +09003352 // non-APEX variant does not have __ANDROID_APEX__ defined
3353 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3354 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3355
Vinh Tranf9754732023-01-19 22:41:46 -05003356 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003357 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003358 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003359}
Jiyong Park7e636d02019-01-28 16:16:54 +09003360
3361func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003362 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003363 apex {
3364 name: "myapex",
3365 key: "myapex.key",
3366 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003367 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003368 }
3369
3370 apex_key {
3371 name: "myapex.key",
3372 public_key: "testkey.avbpubkey",
3373 private_key: "testkey.pem",
3374 }
3375
3376 cc_library_headers {
3377 name: "mylib_headers",
3378 export_include_dirs: ["my_include"],
3379 system_shared_libs: [],
3380 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003381 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003382 }
3383
3384 cc_library {
3385 name: "mylib",
3386 srcs: ["mylib.cpp"],
3387 system_shared_libs: [],
3388 stl: "none",
3389 header_libs: ["mylib_headers"],
3390 export_header_lib_headers: ["mylib_headers"],
3391 stubs: {
3392 versions: ["1", "2", "3"],
3393 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003394 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003395 }
3396
3397 cc_library {
3398 name: "otherlib",
3399 srcs: ["mylib.cpp"],
3400 system_shared_libs: [],
3401 stl: "none",
3402 shared_libs: ["mylib"],
3403 }
3404 `)
3405
Colin Cross7113d202019-11-20 16:39:12 -08003406 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003407
3408 // Ensure that the include path of the header lib is exported to 'otherlib'
3409 ensureContains(t, cFlags, "-Imy_include")
3410}
Alex Light9670d332019-01-29 18:07:33 -08003411
Jiyong Park7cd10e32020-01-14 09:22:18 +09003412type fileInApex struct {
3413 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003414 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003415 isLink bool
3416}
3417
Jooyung Han1724d582022-12-21 10:17:44 +09003418func (f fileInApex) String() string {
3419 return f.src + ":" + f.path
3420}
3421
3422func (f fileInApex) match(expectation string) bool {
3423 parts := strings.Split(expectation, ":")
3424 if len(parts) == 1 {
3425 match, _ := path.Match(parts[0], f.path)
3426 return match
3427 }
3428 if len(parts) == 2 {
3429 matchSrc, _ := path.Match(parts[0], f.src)
3430 matchDst, _ := path.Match(parts[1], f.path)
3431 return matchSrc && matchDst
3432 }
3433 panic("invalid expected file specification: " + expectation)
3434}
3435
Jooyung Hana57af4a2020-01-23 05:36:59 +00003436func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003437 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003438 module := ctx.ModuleForTests(moduleName, variant)
3439 apexRule := module.MaybeRule("apexRule")
3440 apexDir := "/image.apex/"
3441 if apexRule.Rule == nil {
3442 apexRule = module.Rule("zipApexRule")
3443 apexDir = "/image.zipapex/"
3444 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003445 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003446 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003447 for _, cmd := range strings.Split(copyCmds, "&&") {
3448 cmd = strings.TrimSpace(cmd)
3449 if cmd == "" {
3450 continue
3451 }
3452 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003453 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003454 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003455 switch terms[0] {
3456 case "mkdir":
3457 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003458 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003459 t.Fatal("copyCmds contains invalid cp command", cmd)
3460 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003461 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003462 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003463 isLink = false
3464 case "ln":
3465 if len(terms) != 3 && len(terms) != 4 {
3466 // ln LINK TARGET or ln -s LINK TARGET
3467 t.Fatal("copyCmds contains invalid ln command", cmd)
3468 }
3469 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003470 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003471 isLink = true
3472 default:
3473 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3474 }
3475 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003476 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003477 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003478 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003479 }
Jooyung Han1724d582022-12-21 10:17:44 +09003480 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003481 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003482 }
3483 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003484 return ret
3485}
3486
Jooyung Hana57af4a2020-01-23 05:36:59 +00003487func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3488 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003489 var failed bool
3490 var surplus []string
3491 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003492 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Han1724d582022-12-21 10:17:44 +09003493 matchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003494 for _, expected := range files {
Jooyung Han1724d582022-12-21 10:17:44 +09003495 if file.match(expected) {
3496 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003497 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003498 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003499 }
3500 }
Jooyung Han1724d582022-12-21 10:17:44 +09003501 if !matchFound {
3502 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003503 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003504 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003505
Jooyung Han31c470b2019-10-18 16:26:59 +09003506 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003507 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003508 t.Log("surplus files", surplus)
3509 failed = true
3510 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003511
3512 if len(files) > len(filesMatched) {
3513 var missing []string
3514 for _, expected := range files {
3515 if !filesMatched[expected] {
3516 missing = append(missing, expected)
3517 }
3518 }
3519 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003520 t.Log("missing files", missing)
3521 failed = true
3522 }
3523 if failed {
3524 t.Fail()
3525 }
3526}
3527
Jooyung Han344d5432019-08-23 11:17:39 +09003528func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003529 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003530 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003531 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003532 "etc/llndk.libraries.29.txt",
3533 "etc/vndkcore.libraries.29.txt",
3534 "etc/vndksp.libraries.29.txt",
3535 "etc/vndkprivate.libraries.29.txt",
3536 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003537 }
3538 testCases := []struct {
3539 vndkVersion string
3540 expectedFiles []string
3541 }{
3542 {
3543 vndkVersion: "current",
3544 expectedFiles: append(commonFiles,
3545 "lib/libvndk.so",
3546 "lib/libvndksp.so",
3547 "lib64/libvndk.so",
3548 "lib64/libvndksp.so"),
3549 },
3550 {
3551 vndkVersion: "",
3552 expectedFiles: append(commonFiles,
3553 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3554 "lib/libvndksp.so",
3555 "lib64/libvndksp.so"),
3556 },
3557 }
3558 for _, tc := range testCases {
3559 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3560 ctx := testApex(t, `
3561 apex_vndk {
3562 name: "com.android.vndk.current",
3563 key: "com.android.vndk.current.key",
3564 updatable: false,
3565 }
3566
3567 apex_key {
3568 name: "com.android.vndk.current.key",
3569 public_key: "testkey.avbpubkey",
3570 private_key: "testkey.pem",
3571 }
3572
3573 cc_library {
3574 name: "libvndk",
3575 srcs: ["mylib.cpp"],
3576 vendor_available: true,
3577 product_available: true,
3578 vndk: {
3579 enabled: true,
3580 },
3581 system_shared_libs: [],
3582 stl: "none",
3583 apex_available: [ "com.android.vndk.current" ],
3584 }
3585
3586 cc_library {
3587 name: "libvndksp",
3588 srcs: ["mylib.cpp"],
3589 vendor_available: true,
3590 product_available: true,
3591 vndk: {
3592 enabled: true,
3593 support_system_process: true,
3594 },
3595 system_shared_libs: [],
3596 stl: "none",
3597 apex_available: [ "com.android.vndk.current" ],
3598 }
3599
3600 // VNDK-Ext should not cause any problems
3601
3602 cc_library {
3603 name: "libvndk.ext",
3604 srcs: ["mylib2.cpp"],
3605 vendor: true,
3606 vndk: {
3607 enabled: true,
3608 extends: "libvndk",
3609 },
3610 system_shared_libs: [],
3611 stl: "none",
3612 }
3613
3614 cc_library {
3615 name: "libvndksp.ext",
3616 srcs: ["mylib2.cpp"],
3617 vendor: true,
3618 vndk: {
3619 enabled: true,
3620 support_system_process: true,
3621 extends: "libvndksp",
3622 },
3623 system_shared_libs: [],
3624 stl: "none",
3625 }
3626 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3627 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3628 }))
3629 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3630 })
3631 }
Jooyung Han344d5432019-08-23 11:17:39 +09003632}
3633
3634func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003635 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003636 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003637 name: "com.android.vndk.current",
3638 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003639 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003640 }
3641
3642 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003643 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003644 public_key: "testkey.avbpubkey",
3645 private_key: "testkey.pem",
3646 }
3647
3648 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003649 name: "libvndk",
3650 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003651 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003652 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003653 vndk: {
3654 enabled: true,
3655 },
3656 system_shared_libs: [],
3657 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003658 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003659 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003660
3661 cc_prebuilt_library_shared {
3662 name: "libvndk.arm",
3663 srcs: ["libvndk.arm.so"],
3664 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003665 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003666 vndk: {
3667 enabled: true,
3668 },
3669 enabled: false,
3670 arch: {
3671 arm: {
3672 enabled: true,
3673 },
3674 },
3675 system_shared_libs: [],
3676 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003677 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003678 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003679 `+vndkLibrariesTxtFiles("current"),
3680 withFiles(map[string][]byte{
3681 "libvndk.so": nil,
3682 "libvndk.arm.so": nil,
3683 }))
Colin Cross2807f002021-03-02 10:15:29 -08003684 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003685 "lib/libvndk.so",
3686 "lib/libvndk.arm.so",
3687 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003688 "lib/libc++.so",
3689 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003690 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003691 })
Jooyung Han344d5432019-08-23 11:17:39 +09003692}
3693
Jooyung Han39edb6c2019-11-06 16:53:07 +09003694func vndkLibrariesTxtFiles(vers ...string) (result string) {
3695 for _, v := range vers {
3696 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003697 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003698 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003699 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003700 name: "` + txt + `.libraries.txt",
3701 }
3702 `
3703 }
3704 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003705 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003706 result += `
3707 prebuilt_etc {
3708 name: "` + txt + `.libraries.` + v + `.txt",
3709 src: "dummy.txt",
3710 }
3711 `
3712 }
3713 }
3714 }
3715 return
3716}
3717
Jooyung Han344d5432019-08-23 11:17:39 +09003718func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003719 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003720 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003721 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003722 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003723 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003724 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003725 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003726 }
3727
3728 apex_key {
3729 name: "myapex.key",
3730 public_key: "testkey.avbpubkey",
3731 private_key: "testkey.pem",
3732 }
3733
Jooyung Han31c470b2019-10-18 16:26:59 +09003734 vndk_prebuilt_shared {
3735 name: "libvndk27",
3736 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003737 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003738 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003739 vndk: {
3740 enabled: true,
3741 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003742 target_arch: "arm64",
3743 arch: {
3744 arm: {
3745 srcs: ["libvndk27_arm.so"],
3746 },
3747 arm64: {
3748 srcs: ["libvndk27_arm64.so"],
3749 },
3750 },
Colin Cross2807f002021-03-02 10:15:29 -08003751 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003752 }
3753
3754 vndk_prebuilt_shared {
3755 name: "libvndk27",
3756 version: "27",
3757 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003758 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003759 vndk: {
3760 enabled: true,
3761 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003762 target_arch: "x86_64",
3763 arch: {
3764 x86: {
3765 srcs: ["libvndk27_x86.so"],
3766 },
3767 x86_64: {
3768 srcs: ["libvndk27_x86_64.so"],
3769 },
3770 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003771 }
3772 `+vndkLibrariesTxtFiles("27"),
3773 withFiles(map[string][]byte{
3774 "libvndk27_arm.so": nil,
3775 "libvndk27_arm64.so": nil,
3776 "libvndk27_x86.so": nil,
3777 "libvndk27_x86_64.so": nil,
3778 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003779
Colin Cross2807f002021-03-02 10:15:29 -08003780 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003781 "lib/libvndk27_arm.so",
3782 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003783 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003784 })
Jooyung Han344d5432019-08-23 11:17:39 +09003785}
3786
Jooyung Han90eee022019-10-01 20:02:42 +09003787func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003788 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003789 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003790 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003791 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003792 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003793 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003794 }
3795 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003796 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003797 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003798 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003799 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003800 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003801 }
3802 apex_key {
3803 name: "myapex.key",
3804 public_key: "testkey.avbpubkey",
3805 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003806 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003807
3808 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003809 module := ctx.ModuleForTests(moduleName, "android_common_image")
3810 apexManifestRule := module.Rule("apexManifestRule")
3811 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003812 }
3813
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003814 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003815 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003816}
3817
Jooyung Han344d5432019-08-23 11:17:39 +09003818func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003819 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003820 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003821 name: "com.android.vndk.current",
3822 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003823 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003824 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003825 }
3826
3827 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003828 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003829 public_key: "testkey.avbpubkey",
3830 private_key: "testkey.pem",
3831 }
3832
3833 cc_library {
3834 name: "libvndk",
3835 srcs: ["mylib.cpp"],
3836 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003837 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003838 native_bridge_supported: true,
3839 host_supported: true,
3840 vndk: {
3841 enabled: true,
3842 },
3843 system_shared_libs: [],
3844 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003845 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003846 }
Colin Cross2807f002021-03-02 10:15:29 -08003847 `+vndkLibrariesTxtFiles("current"),
3848 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003849
Colin Cross2807f002021-03-02 10:15:29 -08003850 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003851 "lib/libvndk.so",
3852 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003853 "lib/libc++.so",
3854 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003855 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003856 })
Jooyung Han344d5432019-08-23 11:17:39 +09003857}
3858
3859func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003860 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003861 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003862 name: "com.android.vndk.current",
3863 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003864 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003865 native_bridge_supported: true,
3866 }
3867
3868 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003869 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003870 public_key: "testkey.avbpubkey",
3871 private_key: "testkey.pem",
3872 }
3873
3874 cc_library {
3875 name: "libvndk",
3876 srcs: ["mylib.cpp"],
3877 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003878 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003879 native_bridge_supported: true,
3880 host_supported: true,
3881 vndk: {
3882 enabled: true,
3883 },
3884 system_shared_libs: [],
3885 stl: "none",
3886 }
3887 `)
3888}
3889
Jooyung Han31c470b2019-10-18 16:26:59 +09003890func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003891 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003892 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003893 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003894 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003895 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003896 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003897 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003898 }
3899
3900 apex_key {
3901 name: "myapex.key",
3902 public_key: "testkey.avbpubkey",
3903 private_key: "testkey.pem",
3904 }
3905
3906 vndk_prebuilt_shared {
3907 name: "libvndk27",
3908 version: "27",
3909 target_arch: "arm",
3910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003911 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003912 vndk: {
3913 enabled: true,
3914 },
3915 arch: {
3916 arm: {
3917 srcs: ["libvndk27.so"],
3918 }
3919 },
3920 }
3921
3922 vndk_prebuilt_shared {
3923 name: "libvndk27",
3924 version: "27",
3925 target_arch: "arm",
3926 binder32bit: true,
3927 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003928 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003929 vndk: {
3930 enabled: true,
3931 },
3932 arch: {
3933 arm: {
3934 srcs: ["libvndk27binder32.so"],
3935 }
3936 },
Colin Cross2807f002021-03-02 10:15:29 -08003937 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003938 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003939 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003940 withFiles(map[string][]byte{
3941 "libvndk27.so": nil,
3942 "libvndk27binder32.so": nil,
3943 }),
3944 withBinder32bit,
3945 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003946 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003947 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3948 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003949 },
3950 }),
3951 )
3952
Colin Cross2807f002021-03-02 10:15:29 -08003953 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003954 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003955 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003956 })
3957}
3958
Jooyung Han45a96772020-06-15 14:59:42 +09003959func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003960 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003961 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003962 name: "com.android.vndk.current",
3963 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003964 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003965 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003966 }
3967
3968 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003969 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003970 public_key: "testkey.avbpubkey",
3971 private_key: "testkey.pem",
3972 }
3973
3974 cc_library {
3975 name: "libz",
3976 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003977 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003978 vndk: {
3979 enabled: true,
3980 },
3981 stubs: {
3982 symbol_file: "libz.map.txt",
3983 versions: ["30"],
3984 }
3985 }
3986 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3987 "libz.map.txt": nil,
3988 }))
3989
Colin Cross2807f002021-03-02 10:15:29 -08003990 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003991 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3992 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09003993 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
3994 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
3995 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
3996 "*/*",
3997 })
Jooyung Han45a96772020-06-15 14:59:42 +09003998}
3999
Jooyung Hane1633032019-08-01 17:41:43 +09004000func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004001 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004002 apex {
4003 name: "myapex_nodep",
4004 key: "myapex.key",
4005 native_shared_libs: ["lib_nodep"],
4006 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004007 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004008 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004009 }
4010
4011 apex {
4012 name: "myapex_dep",
4013 key: "myapex.key",
4014 native_shared_libs: ["lib_dep"],
4015 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004016 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004017 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004018 }
4019
4020 apex {
4021 name: "myapex_provider",
4022 key: "myapex.key",
4023 native_shared_libs: ["libfoo"],
4024 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004025 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004026 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004027 }
4028
4029 apex {
4030 name: "myapex_selfcontained",
4031 key: "myapex.key",
4032 native_shared_libs: ["lib_dep", "libfoo"],
4033 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004034 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004035 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004036 }
4037
4038 apex_key {
4039 name: "myapex.key",
4040 public_key: "testkey.avbpubkey",
4041 private_key: "testkey.pem",
4042 }
4043
4044 cc_library {
4045 name: "lib_nodep",
4046 srcs: ["mylib.cpp"],
4047 system_shared_libs: [],
4048 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004049 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004050 }
4051
4052 cc_library {
4053 name: "lib_dep",
4054 srcs: ["mylib.cpp"],
4055 shared_libs: ["libfoo"],
4056 system_shared_libs: [],
4057 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004058 apex_available: [
4059 "myapex_dep",
4060 "myapex_provider",
4061 "myapex_selfcontained",
4062 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004063 }
4064
4065 cc_library {
4066 name: "libfoo",
4067 srcs: ["mytest.cpp"],
4068 stubs: {
4069 versions: ["1"],
4070 },
4071 system_shared_libs: [],
4072 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004073 apex_available: [
4074 "myapex_provider",
4075 "myapex_selfcontained",
4076 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004077 }
4078 `)
4079
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004080 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004081 var provideNativeLibs, requireNativeLibs []string
4082
Sundong Ahnabb64432019-10-22 13:58:29 +09004083 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004084 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4085 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004086 ensureListEmpty(t, provideNativeLibs)
4087 ensureListEmpty(t, requireNativeLibs)
4088
Sundong Ahnabb64432019-10-22 13:58:29 +09004089 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004090 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4091 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004092 ensureListEmpty(t, provideNativeLibs)
4093 ensureListContains(t, requireNativeLibs, "libfoo.so")
4094
Sundong Ahnabb64432019-10-22 13:58:29 +09004095 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004096 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4097 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004098 ensureListContains(t, provideNativeLibs, "libfoo.so")
4099 ensureListEmpty(t, requireNativeLibs)
4100
Sundong Ahnabb64432019-10-22 13:58:29 +09004101 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004102 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4103 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004104 ensureListContains(t, provideNativeLibs, "libfoo.so")
4105 ensureListEmpty(t, requireNativeLibs)
4106}
4107
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004108func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004109 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004110 apex {
4111 name: "myapex",
4112 key: "myapex.key",
4113 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004114 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004115 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004116 }
4117
4118 apex_key {
4119 name: "myapex.key",
4120 public_key: "testkey.avbpubkey",
4121 private_key: "testkey.pem",
4122 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004123
4124 cc_library {
4125 name: "mylib",
4126 srcs: ["mylib.cpp"],
4127 system_shared_libs: [],
4128 stl: "none",
4129 apex_available: [
4130 "//apex_available:platform",
4131 "myapex",
4132 ],
4133 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004134 `)
4135
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004136 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004137 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004138 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004139 name := apexBundle.BaseModuleName()
4140 prefix := "TARGET_"
4141 var builder strings.Builder
4142 data.Custom(&builder, name, prefix, "", data)
4143 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00004144 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Parkdb334862020-02-05 17:19:28 +09004145 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004146}
4147
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004148func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4149 ctx := testApex(t, `
4150 apex {
4151 name: "myapex",
4152 key: "myapex.key",
4153 apex_name: "com.android.myapex",
4154 native_shared_libs: ["mylib"],
4155 updatable: false,
4156 }
4157
4158 apex_key {
4159 name: "myapex.key",
4160 public_key: "testkey.avbpubkey",
4161 private_key: "testkey.pem",
4162 }
4163
4164 cc_library {
4165 name: "mylib",
4166 srcs: ["mylib.cpp"],
4167 system_shared_libs: [],
4168 stl: "none",
4169 apex_available: [
4170 "//apex_available:platform",
4171 "myapex",
4172 ],
4173 }
4174 `, android.FixtureMergeEnv(map[string]string{
4175 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4176 }))
4177
4178 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
4179 apexManifestRule := module.Rule("apexManifestRule")
4180 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4181}
4182
Vinh Tran8f5310f2022-10-07 18:16:47 -04004183func TestCompileMultilibProp(t *testing.T) {
4184 testCases := []struct {
4185 compileMultiLibProp string
4186 containedLibs []string
4187 notContainedLibs []string
4188 }{
4189 {
4190 containedLibs: []string{
4191 "image.apex/lib64/mylib.so",
4192 "image.apex/lib/mylib.so",
4193 },
4194 compileMultiLibProp: `compile_multilib: "both",`,
4195 },
4196 {
4197 containedLibs: []string{"image.apex/lib64/mylib.so"},
4198 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4199 compileMultiLibProp: `compile_multilib: "first",`,
4200 },
4201 {
4202 containedLibs: []string{"image.apex/lib64/mylib.so"},
4203 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4204 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4205 },
4206 {
4207 containedLibs: []string{"image.apex/lib64/mylib.so"},
4208 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4209 compileMultiLibProp: `compile_multilib: "64",`,
4210 },
4211 {
4212 containedLibs: []string{"image.apex/lib/mylib.so"},
4213 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4214 compileMultiLibProp: `compile_multilib: "32",`,
4215 },
4216 }
4217 for _, testCase := range testCases {
4218 ctx := testApex(t, fmt.Sprintf(`
4219 apex {
4220 name: "myapex",
4221 key: "myapex.key",
4222 %s
4223 native_shared_libs: ["mylib"],
4224 updatable: false,
4225 }
4226 apex_key {
4227 name: "myapex.key",
4228 public_key: "testkey.avbpubkey",
4229 private_key: "testkey.pem",
4230 }
4231 cc_library {
4232 name: "mylib",
4233 srcs: ["mylib.cpp"],
4234 apex_available: [
4235 "//apex_available:platform",
4236 "myapex",
4237 ],
4238 }
4239 `, testCase.compileMultiLibProp),
4240 )
4241 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4242 apexRule := module.Rule("apexRule")
4243 copyCmds := apexRule.Args["copy_commands"]
4244 for _, containedLib := range testCase.containedLibs {
4245 ensureContains(t, copyCmds, containedLib)
4246 }
4247 for _, notContainedLib := range testCase.notContainedLibs {
4248 ensureNotContains(t, copyCmds, notContainedLib)
4249 }
4250 }
4251}
4252
Alex Light0851b882019-02-07 13:20:53 -08004253func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004254 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004255 apex {
4256 name: "myapex",
4257 key: "myapex.key",
4258 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004259 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004260 }
4261
4262 apex_key {
4263 name: "myapex.key",
4264 public_key: "testkey.avbpubkey",
4265 private_key: "testkey.pem",
4266 }
4267
4268 cc_library {
4269 name: "mylib_common",
4270 srcs: ["mylib.cpp"],
4271 system_shared_libs: [],
4272 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004273 apex_available: [
4274 "//apex_available:platform",
4275 "myapex",
4276 ],
Alex Light0851b882019-02-07 13:20:53 -08004277 }
4278 `)
4279
Sundong Ahnabb64432019-10-22 13:58:29 +09004280 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004281 apexRule := module.Rule("apexRule")
4282 copyCmds := apexRule.Args["copy_commands"]
4283
4284 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4285 t.Log("Apex was a test apex!")
4286 t.Fail()
4287 }
4288 // Ensure that main rule creates an output
4289 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4290
4291 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004292 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004293
4294 // Ensure that both direct and indirect deps are copied into apex
4295 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4296
Colin Cross7113d202019-11-20 16:39:12 -08004297 // Ensure that the platform variant ends with _shared
4298 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004299
Colin Cross56a83212020-09-15 18:30:11 -07004300 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004301 t.Log("Found mylib_common not in any apex!")
4302 t.Fail()
4303 }
4304}
4305
4306func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004307 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004308 apex_test {
4309 name: "myapex",
4310 key: "myapex.key",
4311 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004312 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004313 }
4314
4315 apex_key {
4316 name: "myapex.key",
4317 public_key: "testkey.avbpubkey",
4318 private_key: "testkey.pem",
4319 }
4320
4321 cc_library {
4322 name: "mylib_common_test",
4323 srcs: ["mylib.cpp"],
4324 system_shared_libs: [],
4325 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004326 // TODO: remove //apex_available:platform
4327 apex_available: [
4328 "//apex_available:platform",
4329 "myapex",
4330 ],
Alex Light0851b882019-02-07 13:20:53 -08004331 }
4332 `)
4333
Sundong Ahnabb64432019-10-22 13:58:29 +09004334 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004335 apexRule := module.Rule("apexRule")
4336 copyCmds := apexRule.Args["copy_commands"]
4337
4338 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4339 t.Log("Apex was not a test apex!")
4340 t.Fail()
4341 }
4342 // Ensure that main rule creates an output
4343 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4344
4345 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004346 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004347
4348 // Ensure that both direct and indirect deps are copied into apex
4349 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4350
Colin Cross7113d202019-11-20 16:39:12 -08004351 // Ensure that the platform variant ends with _shared
4352 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004353}
4354
Alex Light9670d332019-01-29 18:07:33 -08004355func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004356 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004357 apex {
4358 name: "myapex",
4359 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004360 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004361 multilib: {
4362 first: {
4363 native_shared_libs: ["mylib_common"],
4364 }
4365 },
4366 target: {
4367 android: {
4368 multilib: {
4369 first: {
4370 native_shared_libs: ["mylib"],
4371 }
4372 }
4373 },
4374 host: {
4375 multilib: {
4376 first: {
4377 native_shared_libs: ["mylib2"],
4378 }
4379 }
4380 }
4381 }
4382 }
4383
4384 apex_key {
4385 name: "myapex.key",
4386 public_key: "testkey.avbpubkey",
4387 private_key: "testkey.pem",
4388 }
4389
4390 cc_library {
4391 name: "mylib",
4392 srcs: ["mylib.cpp"],
4393 system_shared_libs: [],
4394 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004395 // TODO: remove //apex_available:platform
4396 apex_available: [
4397 "//apex_available:platform",
4398 "myapex",
4399 ],
Alex Light9670d332019-01-29 18:07:33 -08004400 }
4401
4402 cc_library {
4403 name: "mylib_common",
4404 srcs: ["mylib.cpp"],
4405 system_shared_libs: [],
4406 stl: "none",
4407 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004408 // TODO: remove //apex_available:platform
4409 apex_available: [
4410 "//apex_available:platform",
4411 "myapex",
4412 ],
Alex Light9670d332019-01-29 18:07:33 -08004413 }
4414
4415 cc_library {
4416 name: "mylib2",
4417 srcs: ["mylib.cpp"],
4418 system_shared_libs: [],
4419 stl: "none",
4420 compile_multilib: "first",
4421 }
4422 `)
4423
Sundong Ahnabb64432019-10-22 13:58:29 +09004424 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004425 copyCmds := apexRule.Args["copy_commands"]
4426
4427 // Ensure that main rule creates an output
4428 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4429
4430 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004431 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4432 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4433 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004434
4435 // Ensure that both direct and indirect deps are copied into apex
4436 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4437 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4438 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4439
Colin Cross7113d202019-11-20 16:39:12 -08004440 // Ensure that the platform variant ends with _shared
4441 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4442 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4443 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004444}
Jiyong Park04480cf2019-02-06 00:16:29 +09004445
Jiyong Park59140302020-12-14 18:44:04 +09004446func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004447 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004448 apex {
4449 name: "myapex",
4450 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004451 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004452 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004453 arch: {
4454 arm64: {
4455 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004456 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004457 },
4458 x86_64: {
4459 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004460 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004461 },
4462 }
4463 }
4464
4465 apex_key {
4466 name: "myapex.key",
4467 public_key: "testkey.avbpubkey",
4468 private_key: "testkey.pem",
4469 }
4470
4471 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004472 name: "mylib.generic",
4473 srcs: ["mylib.cpp"],
4474 system_shared_libs: [],
4475 stl: "none",
4476 // TODO: remove //apex_available:platform
4477 apex_available: [
4478 "//apex_available:platform",
4479 "myapex",
4480 ],
4481 }
4482
4483 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004484 name: "mylib.arm64",
4485 srcs: ["mylib.cpp"],
4486 system_shared_libs: [],
4487 stl: "none",
4488 // TODO: remove //apex_available:platform
4489 apex_available: [
4490 "//apex_available:platform",
4491 "myapex",
4492 ],
4493 }
4494
4495 cc_library {
4496 name: "mylib.x64",
4497 srcs: ["mylib.cpp"],
4498 system_shared_libs: [],
4499 stl: "none",
4500 // TODO: remove //apex_available:platform
4501 apex_available: [
4502 "//apex_available:platform",
4503 "myapex",
4504 ],
4505 }
4506 `)
4507
4508 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4509 copyCmds := apexRule.Args["copy_commands"]
4510
4511 // Ensure that apex variant is created for the direct dep
4512 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004513 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004514 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4515
4516 // Ensure that both direct and indirect deps are copied into apex
4517 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4518 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4519}
4520
Jiyong Park04480cf2019-02-06 00:16:29 +09004521func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004522 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004523 apex {
4524 name: "myapex",
4525 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004526 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004527 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004528 }
4529
4530 apex_key {
4531 name: "myapex.key",
4532 public_key: "testkey.avbpubkey",
4533 private_key: "testkey.pem",
4534 }
4535
4536 sh_binary {
4537 name: "myscript",
4538 src: "mylib.cpp",
4539 filename: "myscript.sh",
4540 sub_dir: "script",
4541 }
4542 `)
4543
Sundong Ahnabb64432019-10-22 13:58:29 +09004544 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004545 copyCmds := apexRule.Args["copy_commands"]
4546
4547 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4548}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004549
Jooyung Han91df2082019-11-20 01:49:42 +09004550func TestApexInVariousPartition(t *testing.T) {
4551 testcases := []struct {
4552 propName, parition, flattenedPartition string
4553 }{
4554 {"", "system", "system_ext"},
4555 {"product_specific: true", "product", "product"},
4556 {"soc_specific: true", "vendor", "vendor"},
4557 {"proprietary: true", "vendor", "vendor"},
4558 {"vendor: true", "vendor", "vendor"},
4559 {"system_ext_specific: true", "system_ext", "system_ext"},
4560 }
4561 for _, tc := range testcases {
4562 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004563 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004564 apex {
4565 name: "myapex",
4566 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004567 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004568 `+tc.propName+`
4569 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004570
Jooyung Han91df2082019-11-20 01:49:42 +09004571 apex_key {
4572 name: "myapex.key",
4573 public_key: "testkey.avbpubkey",
4574 private_key: "testkey.pem",
4575 }
4576 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004577
Jooyung Han91df2082019-11-20 01:49:42 +09004578 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004579 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4580 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004581 if actual != expected {
4582 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4583 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004584
Jooyung Han91df2082019-11-20 01:49:42 +09004585 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004586 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4587 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004588 if actual != expected {
4589 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4590 }
4591 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004592 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004593}
Jiyong Park67882562019-03-21 01:11:21 +09004594
Jooyung Han580eb4f2020-06-24 19:33:06 +09004595func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004596 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004597 apex {
4598 name: "myapex",
4599 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004600 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004601 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004602
Jooyung Han580eb4f2020-06-24 19:33:06 +09004603 apex_key {
4604 name: "myapex.key",
4605 public_key: "testkey.avbpubkey",
4606 private_key: "testkey.pem",
4607 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004608 `)
4609 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004610 rule := module.Output("file_contexts")
4611 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4612}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004613
Jooyung Han580eb4f2020-06-24 19:33:06 +09004614func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004615 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004616 apex {
4617 name: "myapex",
4618 key: "myapex.key",
4619 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004620 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004621 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004622
Jooyung Han580eb4f2020-06-24 19:33:06 +09004623 apex_key {
4624 name: "myapex.key",
4625 public_key: "testkey.avbpubkey",
4626 private_key: "testkey.pem",
4627 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004628 `, withFiles(map[string][]byte{
4629 "my_own_file_contexts": nil,
4630 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004631}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004632
Jooyung Han580eb4f2020-06-24 19:33:06 +09004633func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004634 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004635 apex {
4636 name: "myapex",
4637 key: "myapex.key",
4638 product_specific: true,
4639 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004640 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004641 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004642
Jooyung Han580eb4f2020-06-24 19:33:06 +09004643 apex_key {
4644 name: "myapex.key",
4645 public_key: "testkey.avbpubkey",
4646 private_key: "testkey.pem",
4647 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004648 `)
4649
Colin Cross1c460562021-02-16 17:55:47 -08004650 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004651 apex {
4652 name: "myapex",
4653 key: "myapex.key",
4654 product_specific: true,
4655 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004656 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004657 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004658
Jooyung Han580eb4f2020-06-24 19:33:06 +09004659 apex_key {
4660 name: "myapex.key",
4661 public_key: "testkey.avbpubkey",
4662 private_key: "testkey.pem",
4663 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004664 `, withFiles(map[string][]byte{
4665 "product_specific_file_contexts": nil,
4666 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004667 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4668 rule := module.Output("file_contexts")
4669 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4670}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004671
Jooyung Han580eb4f2020-06-24 19:33:06 +09004672func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004673 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004674 apex {
4675 name: "myapex",
4676 key: "myapex.key",
4677 product_specific: true,
4678 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004679 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004680 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004681
Jooyung Han580eb4f2020-06-24 19:33:06 +09004682 apex_key {
4683 name: "myapex.key",
4684 public_key: "testkey.avbpubkey",
4685 private_key: "testkey.pem",
4686 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004687
Jooyung Han580eb4f2020-06-24 19:33:06 +09004688 filegroup {
4689 name: "my-file-contexts",
4690 srcs: ["product_specific_file_contexts"],
4691 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004692 `, withFiles(map[string][]byte{
4693 "product_specific_file_contexts": nil,
4694 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004695 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4696 rule := module.Output("file_contexts")
4697 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004698}
4699
Jiyong Park67882562019-03-21 01:11:21 +09004700func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004701 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004702 apex_key {
4703 name: "myapex.key",
4704 public_key: ":my.avbpubkey",
4705 private_key: ":my.pem",
4706 product_specific: true,
4707 }
4708
4709 filegroup {
4710 name: "my.avbpubkey",
4711 srcs: ["testkey2.avbpubkey"],
4712 }
4713
4714 filegroup {
4715 name: "my.pem",
4716 srcs: ["testkey2.pem"],
4717 }
4718 `)
4719
4720 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4721 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004722 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004723 if actual_pubkey != expected_pubkey {
4724 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4725 }
4726 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004727 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004728 if actual_privkey != expected_privkey {
4729 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4730 }
4731}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004732
4733func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004734 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004735 prebuilt_apex {
4736 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004737 arch: {
4738 arm64: {
4739 src: "myapex-arm64.apex",
4740 },
4741 arm: {
4742 src: "myapex-arm.apex",
4743 },
4744 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004745 }
4746 `)
4747
Wei Li340ee8e2022-03-18 17:33:24 -07004748 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4749 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004750
Jiyong Parkc95714e2019-03-29 14:23:10 +09004751 expectedInput := "myapex-arm64.apex"
4752 if prebuilt.inputApex.String() != expectedInput {
4753 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4754 }
Wei Li340ee8e2022-03-18 17:33:24 -07004755 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4756 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4757 rule := testingModule.Rule("genProvenanceMetaData")
4758 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4759 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4760 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4761 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004762}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004763
Paul Duffinc0609c62021-03-01 17:27:16 +00004764func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004765 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004766 prebuilt_apex {
4767 name: "myapex",
4768 }
4769 `)
4770}
4771
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004772func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004773 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004774 prebuilt_apex {
4775 name: "myapex",
4776 src: "myapex-arm.apex",
4777 filename: "notmyapex.apex",
4778 }
4779 `)
4780
Wei Li340ee8e2022-03-18 17:33:24 -07004781 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4782 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004783
4784 expected := "notmyapex.apex"
4785 if p.installFilename != expected {
4786 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4787 }
Wei Li340ee8e2022-03-18 17:33:24 -07004788 rule := testingModule.Rule("genProvenanceMetaData")
4789 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4790 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4791 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4792 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004793}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004794
Samiul Islam7c02e262021-09-08 17:48:28 +01004795func TestApexSetFilenameOverride(t *testing.T) {
4796 testApex(t, `
4797 apex_set {
4798 name: "com.company.android.myapex",
4799 apex_name: "com.android.myapex",
4800 set: "company-myapex.apks",
4801 filename: "com.company.android.myapex.apex"
4802 }
4803 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4804
4805 testApex(t, `
4806 apex_set {
4807 name: "com.company.android.myapex",
4808 apex_name: "com.android.myapex",
4809 set: "company-myapex.apks",
4810 filename: "com.company.android.myapex.capex"
4811 }
4812 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4813
4814 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4815 apex_set {
4816 name: "com.company.android.myapex",
4817 apex_name: "com.android.myapex",
4818 set: "company-myapex.apks",
4819 filename: "some-random-suffix"
4820 }
4821 `)
4822}
4823
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004824func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004825 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004826 prebuilt_apex {
4827 name: "myapex.prebuilt",
4828 src: "myapex-arm.apex",
4829 overrides: [
4830 "myapex",
4831 ],
4832 }
4833 `)
4834
Wei Li340ee8e2022-03-18 17:33:24 -07004835 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4836 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004837
4838 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004839 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004840 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004841 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004842 }
Wei Li340ee8e2022-03-18 17:33:24 -07004843 rule := testingModule.Rule("genProvenanceMetaData")
4844 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4845 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4846 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4847 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004848}
4849
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004850func TestPrebuiltApexName(t *testing.T) {
4851 testApex(t, `
4852 prebuilt_apex {
4853 name: "com.company.android.myapex",
4854 apex_name: "com.android.myapex",
4855 src: "company-myapex-arm.apex",
4856 }
4857 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4858
4859 testApex(t, `
4860 apex_set {
4861 name: "com.company.android.myapex",
4862 apex_name: "com.android.myapex",
4863 set: "company-myapex.apks",
4864 }
4865 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4866}
4867
4868func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4869 _ = android.GroupFixturePreparers(
4870 java.PrepareForTestWithJavaDefaultModules,
4871 PrepareForTestWithApexBuildComponents,
4872 android.FixtureWithRootAndroidBp(`
4873 platform_bootclasspath {
4874 name: "platform-bootclasspath",
4875 fragments: [
4876 {
4877 apex: "com.android.art",
4878 module: "art-bootclasspath-fragment",
4879 },
4880 ],
4881 }
4882
4883 prebuilt_apex {
4884 name: "com.company.android.art",
4885 apex_name: "com.android.art",
4886 src: "com.company.android.art-arm.apex",
4887 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4888 }
4889
4890 prebuilt_bootclasspath_fragment {
4891 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004892 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004893 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004894 hidden_api: {
4895 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4896 metadata: "my-bootclasspath-fragment/metadata.csv",
4897 index: "my-bootclasspath-fragment/index.csv",
4898 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4899 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4900 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004901 }
4902
4903 java_import {
4904 name: "core-oj",
4905 jars: ["prebuilt.jar"],
4906 }
4907 `),
4908 ).RunTest(t)
4909}
4910
Paul Duffin092153d2021-01-26 11:42:39 +00004911// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4912// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004913func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004914 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004915
Paul Duffin89886cb2021-02-05 16:44:03 +00004916 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004917 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004918 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004919 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004920 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004921 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004922 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4923 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4924 android.NormalizePathForTesting(dexJarBuildPath))
4925 }
4926
4927 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004928 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004929 // Make sure the import has been given the correct path to the dex jar.
4930 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4931 dexJarBuildPath := p.DexJarInstallPath()
4932 stem := android.RemoveOptionalPrebuiltPrefix(name)
4933 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4934 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4935 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004936 }
4937
Paul Duffin39853512021-02-26 11:09:39 +00004938 ensureNoSourceVariant := 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 that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004941 android.AssertArrayString(t, "Check if there is no source variant",
4942 []string{"android_common"},
4943 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004944 }
4945
4946 t.Run("prebuilt only", func(t *testing.T) {
4947 bp := `
4948 prebuilt_apex {
4949 name: "myapex",
4950 arch: {
4951 arm64: {
4952 src: "myapex-arm64.apex",
4953 },
4954 arm: {
4955 src: "myapex-arm.apex",
4956 },
4957 },
Paul Duffin39853512021-02-26 11:09:39 +00004958 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004959 }
4960
4961 java_import {
4962 name: "libfoo",
4963 jars: ["libfoo.jar"],
4964 }
Paul Duffin39853512021-02-26 11:09:39 +00004965
4966 java_sdk_library_import {
4967 name: "libbar",
4968 public: {
4969 jars: ["libbar.jar"],
4970 },
4971 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004972 `
4973
4974 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4975 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4976
Martin Stjernholm44825602021-09-17 01:44:12 +01004977 deapexerName := deapexerModuleName("myapex")
4978 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4979
Paul Duffinf6932af2021-02-26 18:21:56 +00004980 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004981 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004982 rule := deapexer.Rule("deapexer")
4983 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4984 t.Errorf("expected: %q, found: %q", expected, actual)
4985 }
4986
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004987 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004988 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004989 rule = prebuiltApex.Rule("android/soong/android.Cp")
4990 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4991 t.Errorf("expected: %q, found: %q", expected, actual)
4992 }
4993
Paul Duffin89886cb2021-02-05 16:44:03 +00004994 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004995 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004996
4997 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004998 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004999 })
5000
5001 t.Run("prebuilt with source preferred", func(t *testing.T) {
5002
5003 bp := `
5004 prebuilt_apex {
5005 name: "myapex",
5006 arch: {
5007 arm64: {
5008 src: "myapex-arm64.apex",
5009 },
5010 arm: {
5011 src: "myapex-arm.apex",
5012 },
5013 },
Paul Duffin39853512021-02-26 11:09:39 +00005014 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005015 }
5016
5017 java_import {
5018 name: "libfoo",
5019 jars: ["libfoo.jar"],
5020 }
5021
5022 java_library {
5023 name: "libfoo",
5024 }
Paul Duffin39853512021-02-26 11:09:39 +00005025
5026 java_sdk_library_import {
5027 name: "libbar",
5028 public: {
5029 jars: ["libbar.jar"],
5030 },
5031 }
5032
5033 java_sdk_library {
5034 name: "libbar",
5035 srcs: ["foo/bar/MyClass.java"],
5036 unsafe_ignore_missing_latest_api: true,
5037 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005038 `
5039
5040 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5041 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5042
Paul Duffin89886cb2021-02-05 16:44:03 +00005043 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005044 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005045 ensureNoSourceVariant(t, ctx, "libfoo")
5046
5047 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005048 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005049 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005050 })
5051
5052 t.Run("prebuilt preferred with source", func(t *testing.T) {
5053 bp := `
5054 prebuilt_apex {
5055 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005056 arch: {
5057 arm64: {
5058 src: "myapex-arm64.apex",
5059 },
5060 arm: {
5061 src: "myapex-arm.apex",
5062 },
5063 },
Paul Duffin39853512021-02-26 11:09:39 +00005064 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005065 }
5066
5067 java_import {
5068 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005069 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005070 jars: ["libfoo.jar"],
5071 }
5072
5073 java_library {
5074 name: "libfoo",
5075 }
Paul Duffin39853512021-02-26 11:09:39 +00005076
5077 java_sdk_library_import {
5078 name: "libbar",
5079 prefer: true,
5080 public: {
5081 jars: ["libbar.jar"],
5082 },
5083 }
5084
5085 java_sdk_library {
5086 name: "libbar",
5087 srcs: ["foo/bar/MyClass.java"],
5088 unsafe_ignore_missing_latest_api: true,
5089 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005090 `
5091
5092 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5093 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5094
Paul Duffin89886cb2021-02-05 16:44:03 +00005095 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005096 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005097 ensureNoSourceVariant(t, ctx, "libfoo")
5098
5099 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005100 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005101 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005102 })
5103}
5104
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005105func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005106 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005107 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005108 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5109 // is disabled.
5110 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5111 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005112
Paul Duffin37856732021-02-26 14:24:15 +00005113 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5114 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005115 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005116 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005117 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005118 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005119 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005120 foundLibfooJar = true
5121 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005122 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005123 }
5124 }
5125 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005126 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 +00005127 }
5128 }
5129
Paul Duffin40a3f652021-07-19 13:11:24 +01005130 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005131 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005132 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005133 var rule android.TestingBuildParams
5134
5135 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5136 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005137 }
5138
Paul Duffin40a3f652021-07-19 13:11:24 +01005139 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5140 t.Helper()
5141 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5142 var rule android.TestingBuildParams
5143
5144 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5145 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5146 }
5147
Paul Duffin89f570a2021-06-16 01:42:33 +01005148 fragment := java.ApexVariantReference{
5149 Apex: proptools.StringPtr("myapex"),
5150 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5151 }
5152
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005153 t.Run("prebuilt only", func(t *testing.T) {
5154 bp := `
5155 prebuilt_apex {
5156 name: "myapex",
5157 arch: {
5158 arm64: {
5159 src: "myapex-arm64.apex",
5160 },
5161 arm: {
5162 src: "myapex-arm.apex",
5163 },
5164 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005165 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5166 }
5167
5168 prebuilt_bootclasspath_fragment {
5169 name: "my-bootclasspath-fragment",
5170 contents: ["libfoo", "libbar"],
5171 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005172 hidden_api: {
5173 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5174 metadata: "my-bootclasspath-fragment/metadata.csv",
5175 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005176 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5177 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5178 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005179 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005180 }
5181
5182 java_import {
5183 name: "libfoo",
5184 jars: ["libfoo.jar"],
5185 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005186 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005187 }
Paul Duffin37856732021-02-26 14:24:15 +00005188
5189 java_sdk_library_import {
5190 name: "libbar",
5191 public: {
5192 jars: ["libbar.jar"],
5193 },
5194 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005195 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005196 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005197 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005198 `
5199
Paul Duffin89f570a2021-06-16 01:42:33 +01005200 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005201 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5202 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005203
Paul Duffin537ea3d2021-05-14 10:38:00 +01005204 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005205 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005206 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005207 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005208 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5209 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005210 })
5211
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005212 t.Run("apex_set only", func(t *testing.T) {
5213 bp := `
5214 apex_set {
5215 name: "myapex",
5216 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005217 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5218 }
5219
5220 prebuilt_bootclasspath_fragment {
5221 name: "my-bootclasspath-fragment",
5222 contents: ["libfoo", "libbar"],
5223 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005224 hidden_api: {
5225 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5226 metadata: "my-bootclasspath-fragment/metadata.csv",
5227 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005228 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5229 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5230 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005231 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005232 }
5233
5234 java_import {
5235 name: "libfoo",
5236 jars: ["libfoo.jar"],
5237 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005238 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005239 }
5240
5241 java_sdk_library_import {
5242 name: "libbar",
5243 public: {
5244 jars: ["libbar.jar"],
5245 },
5246 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005247 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005248 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005249 }
5250 `
5251
Paul Duffin89f570a2021-06-16 01:42:33 +01005252 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005253 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5254 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5255
Paul Duffin537ea3d2021-05-14 10:38:00 +01005256 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005257 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005258 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005259 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005260 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5261 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005262 })
5263
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005264 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5265 bp := `
5266 prebuilt_apex {
5267 name: "myapex",
5268 arch: {
5269 arm64: {
5270 src: "myapex-arm64.apex",
5271 },
5272 arm: {
5273 src: "myapex-arm.apex",
5274 },
5275 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005276 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5277 }
5278
5279 prebuilt_bootclasspath_fragment {
5280 name: "my-bootclasspath-fragment",
5281 contents: ["libfoo", "libbar"],
5282 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005283 hidden_api: {
5284 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5285 metadata: "my-bootclasspath-fragment/metadata.csv",
5286 index: "my-bootclasspath-fragment/index.csv",
5287 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5288 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5289 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005290 }
5291
5292 java_import {
5293 name: "libfoo",
5294 jars: ["libfoo.jar"],
5295 apex_available: ["myapex"],
5296 }
5297
5298 java_library {
5299 name: "libfoo",
5300 srcs: ["foo/bar/MyClass.java"],
5301 apex_available: ["myapex"],
5302 }
Paul Duffin37856732021-02-26 14:24:15 +00005303
5304 java_sdk_library_import {
5305 name: "libbar",
5306 public: {
5307 jars: ["libbar.jar"],
5308 },
5309 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005310 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005311 }
5312
5313 java_sdk_library {
5314 name: "libbar",
5315 srcs: ["foo/bar/MyClass.java"],
5316 unsafe_ignore_missing_latest_api: true,
5317 apex_available: ["myapex"],
5318 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005319 `
5320
5321 // In this test the source (java_library) libfoo is active since the
5322 // prebuilt (java_import) defaults to prefer:false. However the
5323 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5324 // find the dex boot jar in it. We either need to disable the source libfoo
5325 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005326 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005327 // dexbootjar check is skipped if AllowMissingDependencies is true
5328 preparerAllowMissingDeps := android.GroupFixturePreparers(
5329 preparer,
5330 android.PrepareForTestWithAllowMissingDependencies,
5331 )
5332 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005333 })
5334
5335 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5336 bp := `
5337 prebuilt_apex {
5338 name: "myapex",
5339 arch: {
5340 arm64: {
5341 src: "myapex-arm64.apex",
5342 },
5343 arm: {
5344 src: "myapex-arm.apex",
5345 },
5346 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005347 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5348 }
5349
5350 prebuilt_bootclasspath_fragment {
5351 name: "my-bootclasspath-fragment",
5352 contents: ["libfoo", "libbar"],
5353 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005354 hidden_api: {
5355 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5356 metadata: "my-bootclasspath-fragment/metadata.csv",
5357 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005358 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5359 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5360 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005361 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005362 }
5363
5364 java_import {
5365 name: "libfoo",
5366 prefer: true,
5367 jars: ["libfoo.jar"],
5368 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005369 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005370 }
5371
5372 java_library {
5373 name: "libfoo",
5374 srcs: ["foo/bar/MyClass.java"],
5375 apex_available: ["myapex"],
5376 }
Paul Duffin37856732021-02-26 14:24:15 +00005377
5378 java_sdk_library_import {
5379 name: "libbar",
5380 prefer: true,
5381 public: {
5382 jars: ["libbar.jar"],
5383 },
5384 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005385 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005386 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005387 }
5388
5389 java_sdk_library {
5390 name: "libbar",
5391 srcs: ["foo/bar/MyClass.java"],
5392 unsafe_ignore_missing_latest_api: true,
5393 apex_available: ["myapex"],
5394 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005395 `
5396
Paul Duffin89f570a2021-06-16 01:42:33 +01005397 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005398 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5399 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005400
Paul Duffin537ea3d2021-05-14 10:38:00 +01005401 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005402 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005403 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005404 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005405 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5406 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005407 })
5408
5409 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5410 bp := `
5411 apex {
5412 name: "myapex",
5413 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005414 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005415 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005416 }
5417
5418 apex_key {
5419 name: "myapex.key",
5420 public_key: "testkey.avbpubkey",
5421 private_key: "testkey.pem",
5422 }
5423
5424 prebuilt_apex {
5425 name: "myapex",
5426 arch: {
5427 arm64: {
5428 src: "myapex-arm64.apex",
5429 },
5430 arm: {
5431 src: "myapex-arm.apex",
5432 },
5433 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005434 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5435 }
5436
5437 prebuilt_bootclasspath_fragment {
5438 name: "my-bootclasspath-fragment",
5439 contents: ["libfoo", "libbar"],
5440 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005441 hidden_api: {
5442 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5443 metadata: "my-bootclasspath-fragment/metadata.csv",
5444 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005445 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5446 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5447 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005448 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005449 }
5450
5451 java_import {
5452 name: "libfoo",
5453 jars: ["libfoo.jar"],
5454 apex_available: ["myapex"],
5455 }
5456
5457 java_library {
5458 name: "libfoo",
5459 srcs: ["foo/bar/MyClass.java"],
5460 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005461 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005462 }
Paul Duffin37856732021-02-26 14:24:15 +00005463
5464 java_sdk_library_import {
5465 name: "libbar",
5466 public: {
5467 jars: ["libbar.jar"],
5468 },
5469 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005470 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005471 }
5472
5473 java_sdk_library {
5474 name: "libbar",
5475 srcs: ["foo/bar/MyClass.java"],
5476 unsafe_ignore_missing_latest_api: true,
5477 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005478 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005479 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005480 `
5481
Paul Duffin89f570a2021-06-16 01:42:33 +01005482 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005483 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5484 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005485
Paul Duffin537ea3d2021-05-14 10:38:00 +01005486 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005487 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005488 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005489 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005490 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5491 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005492 })
5493
5494 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5495 bp := `
5496 apex {
5497 name: "myapex",
5498 enabled: false,
5499 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005500 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005501 }
5502
5503 apex_key {
5504 name: "myapex.key",
5505 public_key: "testkey.avbpubkey",
5506 private_key: "testkey.pem",
5507 }
5508
5509 prebuilt_apex {
5510 name: "myapex",
5511 arch: {
5512 arm64: {
5513 src: "myapex-arm64.apex",
5514 },
5515 arm: {
5516 src: "myapex-arm.apex",
5517 },
5518 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005519 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5520 }
5521
5522 prebuilt_bootclasspath_fragment {
5523 name: "my-bootclasspath-fragment",
5524 contents: ["libfoo", "libbar"],
5525 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005526 hidden_api: {
5527 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5528 metadata: "my-bootclasspath-fragment/metadata.csv",
5529 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005530 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5531 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5532 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005533 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005534 }
5535
5536 java_import {
5537 name: "libfoo",
5538 prefer: true,
5539 jars: ["libfoo.jar"],
5540 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005541 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005542 }
5543
5544 java_library {
5545 name: "libfoo",
5546 srcs: ["foo/bar/MyClass.java"],
5547 apex_available: ["myapex"],
5548 }
Paul Duffin37856732021-02-26 14:24:15 +00005549
5550 java_sdk_library_import {
5551 name: "libbar",
5552 prefer: true,
5553 public: {
5554 jars: ["libbar.jar"],
5555 },
5556 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005557 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005558 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005559 }
5560
5561 java_sdk_library {
5562 name: "libbar",
5563 srcs: ["foo/bar/MyClass.java"],
5564 unsafe_ignore_missing_latest_api: true,
5565 apex_available: ["myapex"],
5566 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005567 `
5568
Paul Duffin89f570a2021-06-16 01:42:33 +01005569 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005570 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5571 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005572
Paul Duffin537ea3d2021-05-14 10:38:00 +01005573 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005574 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005575 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005576 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005577 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5578 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005579 })
5580}
5581
Roland Levillain630846d2019-06-26 12:48:34 +01005582func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005583 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005584 apex_test {
5585 name: "myapex",
5586 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005587 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005588 tests: [
5589 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005590 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005591 ],
5592 }
5593
5594 apex_key {
5595 name: "myapex.key",
5596 public_key: "testkey.avbpubkey",
5597 private_key: "testkey.pem",
5598 }
5599
Liz Kammer1c14a212020-05-12 15:26:55 -07005600 filegroup {
5601 name: "fg",
5602 srcs: [
5603 "baz",
5604 "bar/baz"
5605 ],
5606 }
5607
Roland Levillain630846d2019-06-26 12:48:34 +01005608 cc_test {
5609 name: "mytest",
5610 gtest: false,
5611 srcs: ["mytest.cpp"],
5612 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005613 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005614 system_shared_libs: [],
5615 static_executable: true,
5616 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005617 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005618 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005619
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005620 cc_library {
5621 name: "mylib",
5622 srcs: ["mylib.cpp"],
5623 system_shared_libs: [],
5624 stl: "none",
5625 }
5626
Liz Kammer5bd365f2020-05-27 15:15:11 -07005627 filegroup {
5628 name: "fg2",
5629 srcs: [
5630 "testdata/baz"
5631 ],
5632 }
5633
Roland Levillain9b5fde92019-06-28 15:41:19 +01005634 cc_test {
5635 name: "mytests",
5636 gtest: false,
5637 srcs: [
5638 "mytest1.cpp",
5639 "mytest2.cpp",
5640 "mytest3.cpp",
5641 ],
5642 test_per_src: true,
5643 relative_install_path: "test",
5644 system_shared_libs: [],
5645 static_executable: true,
5646 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005647 data: [
5648 ":fg",
5649 ":fg2",
5650 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005651 }
Roland Levillain630846d2019-06-26 12:48:34 +01005652 `)
5653
Sundong Ahnabb64432019-10-22 13:58:29 +09005654 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005655 copyCmds := apexRule.Args["copy_commands"]
5656
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005657 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005658 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005659 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005660
Liz Kammer1c14a212020-05-12 15:26:55 -07005661 //Ensure that test data are copied into apex.
5662 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5663 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5664
Roland Levillain9b5fde92019-06-28 15:41:19 +01005665 // Ensure that test deps built with `test_per_src` are copied into apex.
5666 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5667 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5668 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005669
5670 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005671 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005672 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005673 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005674 prefix := "TARGET_"
5675 var builder strings.Builder
5676 data.Custom(&builder, name, prefix, "", data)
5677 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005678 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5679 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5680 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5681 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5682 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5683 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005684 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005685
5686 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005687 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005688 data.Custom(&builder, name, prefix, "", data)
5689 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005690 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5691 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005692}
5693
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005694func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005695 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005696 apex {
5697 name: "myapex",
5698 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005699 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005700 }
5701 apex_key {
5702 name: "myapex.key",
5703 public_key: "testkey.avbpubkey",
5704 private_key: "testkey.pem",
5705 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005706 `,
5707 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5708 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5709 }),
5710 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005711 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005712 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005713 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005714 var builder strings.Builder
5715 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5716 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005717 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005718}
5719
Jooyung Hand48f3c32019-08-23 11:18:57 +09005720func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5721 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5722 apex {
5723 name: "myapex",
5724 key: "myapex.key",
5725 native_shared_libs: ["libfoo"],
5726 }
5727
5728 apex_key {
5729 name: "myapex.key",
5730 public_key: "testkey.avbpubkey",
5731 private_key: "testkey.pem",
5732 }
5733
5734 cc_library {
5735 name: "libfoo",
5736 stl: "none",
5737 system_shared_libs: [],
5738 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005739 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005740 }
5741 `)
5742 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5743 apex {
5744 name: "myapex",
5745 key: "myapex.key",
5746 java_libs: ["myjar"],
5747 }
5748
5749 apex_key {
5750 name: "myapex.key",
5751 public_key: "testkey.avbpubkey",
5752 private_key: "testkey.pem",
5753 }
5754
5755 java_library {
5756 name: "myjar",
5757 srcs: ["foo/bar/MyClass.java"],
5758 sdk_version: "none",
5759 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005760 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005761 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005762 }
5763 `)
5764}
5765
Bill Peckhama41a6962021-01-11 10:58:54 -08005766func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005767 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005768 apex {
5769 name: "myapex",
5770 key: "myapex.key",
5771 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005772 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005773 }
5774
5775 apex_key {
5776 name: "myapex.key",
5777 public_key: "testkey.avbpubkey",
5778 private_key: "testkey.pem",
5779 }
5780
5781 java_import {
5782 name: "myjavaimport",
5783 apex_available: ["myapex"],
5784 jars: ["my.jar"],
5785 compile_dex: true,
5786 }
5787 `)
5788
5789 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5790 apexRule := module.Rule("apexRule")
5791 copyCmds := apexRule.Args["copy_commands"]
5792 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5793}
5794
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005795func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005796 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005797 apex {
5798 name: "myapex",
5799 key: "myapex.key",
5800 apps: [
5801 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005802 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005803 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005804 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005805 }
5806
5807 apex_key {
5808 name: "myapex.key",
5809 public_key: "testkey.avbpubkey",
5810 private_key: "testkey.pem",
5811 }
5812
5813 android_app {
5814 name: "AppFoo",
5815 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005816 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005817 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005818 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005819 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005820 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005821 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005822
5823 android_app {
5824 name: "AppFooPriv",
5825 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005826 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005827 system_modules: "none",
5828 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005829 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005830 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005831 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005832
5833 cc_library_shared {
5834 name: "libjni",
5835 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005836 shared_libs: ["libfoo"],
5837 stl: "none",
5838 system_shared_libs: [],
5839 apex_available: [ "myapex" ],
5840 sdk_version: "current",
5841 }
5842
5843 cc_library_shared {
5844 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005845 stl: "none",
5846 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005847 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005848 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005849 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005850 `)
5851
Sundong Ahnabb64432019-10-22 13:58:29 +09005852 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005853 apexRule := module.Rule("apexRule")
5854 copyCmds := apexRule.Args["copy_commands"]
5855
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005856 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5857 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005858
Colin Crossaede88c2020-08-11 12:17:01 -07005859 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005860 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005861 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005862 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005863 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005864 // JNI libraries including transitive deps are
5865 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005866 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005867 // ... embedded inside APK (jnilibs.zip)
5868 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5869 // ... and not directly inside the APEX
5870 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5871 }
Dario Frenicde2a032019-10-27 00:29:22 +01005872}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005873
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005874func TestApexWithAppImportBuildId(t *testing.T) {
5875 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5876 for _, id := range invalidBuildIds {
5877 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5878 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5879 variables.BuildId = proptools.StringPtr(id)
5880 })
5881 testApexError(t, message, `apex {
5882 name: "myapex",
5883 key: "myapex.key",
5884 apps: ["AppFooPrebuilt"],
5885 updatable: false,
5886 }
5887
5888 apex_key {
5889 name: "myapex.key",
5890 public_key: "testkey.avbpubkey",
5891 private_key: "testkey.pem",
5892 }
5893
5894 android_app_import {
5895 name: "AppFooPrebuilt",
5896 apk: "PrebuiltAppFoo.apk",
5897 presigned: true,
5898 apex_available: ["myapex"],
5899 }
5900 `, fixture)
5901 }
5902}
5903
Dario Frenicde2a032019-10-27 00:29:22 +01005904func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005905 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005906 apex {
5907 name: "myapex",
5908 key: "myapex.key",
5909 apps: [
5910 "AppFooPrebuilt",
5911 "AppFooPrivPrebuilt",
5912 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005913 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005914 }
5915
5916 apex_key {
5917 name: "myapex.key",
5918 public_key: "testkey.avbpubkey",
5919 private_key: "testkey.pem",
5920 }
5921
5922 android_app_import {
5923 name: "AppFooPrebuilt",
5924 apk: "PrebuiltAppFoo.apk",
5925 presigned: true,
5926 dex_preopt: {
5927 enabled: false,
5928 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005929 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005930 }
5931
5932 android_app_import {
5933 name: "AppFooPrivPrebuilt",
5934 apk: "PrebuiltAppFooPriv.apk",
5935 privileged: true,
5936 presigned: true,
5937 dex_preopt: {
5938 enabled: false,
5939 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005940 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005941 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005942 }
5943 `)
5944
Sundong Ahnabb64432019-10-22 13:58:29 +09005945 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005946 apexRule := module.Rule("apexRule")
5947 copyCmds := apexRule.Args["copy_commands"]
5948
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005949 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5950 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005951}
5952
5953func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005954 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005955 apex {
5956 name: "myapex",
5957 key: "myapex.key",
5958 apps: [
5959 "AppFoo",
5960 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005961 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005962 }
5963
5964 apex_key {
5965 name: "myapex.key",
5966 public_key: "testkey.avbpubkey",
5967 private_key: "testkey.pem",
5968 }
5969
5970 android_app {
5971 name: "AppFoo",
5972 srcs: ["foo/bar/MyClass.java"],
5973 sdk_version: "none",
5974 system_modules: "none",
5975 apex_available: [ "myapex" ],
5976 }
5977
5978 android_app_import {
5979 name: "AppFoo",
5980 apk: "AppFooPrebuilt.apk",
5981 filename: "AppFooPrebuilt.apk",
5982 presigned: true,
5983 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005984 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005985 }
5986 `, withFiles(map[string][]byte{
5987 "AppFooPrebuilt.apk": nil,
5988 }))
5989
5990 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005991 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005992 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005993}
5994
Dario Freni6f3937c2019-12-20 22:58:03 +00005995func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005996 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005997 apex {
5998 name: "myapex",
5999 key: "myapex.key",
6000 apps: [
6001 "TesterHelpAppFoo",
6002 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006003 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006004 }
6005
6006 apex_key {
6007 name: "myapex.key",
6008 public_key: "testkey.avbpubkey",
6009 private_key: "testkey.pem",
6010 }
6011
6012 android_test_helper_app {
6013 name: "TesterHelpAppFoo",
6014 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006015 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006016 }
6017
6018 `)
6019
6020 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6021 apexRule := module.Rule("apexRule")
6022 copyCmds := apexRule.Args["copy_commands"]
6023
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006024 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006025}
6026
Jooyung Han18020ea2019-11-13 10:50:48 +09006027func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6028 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006029 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006030 apex {
6031 name: "myapex",
6032 key: "myapex.key",
6033 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006034 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006035 }
6036
6037 apex_key {
6038 name: "myapex.key",
6039 public_key: "testkey.avbpubkey",
6040 private_key: "testkey.pem",
6041 }
6042
6043 apex {
6044 name: "otherapex",
6045 key: "myapex.key",
6046 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006047 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006048 }
6049
6050 cc_defaults {
6051 name: "libfoo-defaults",
6052 apex_available: ["otherapex"],
6053 }
6054
6055 cc_library {
6056 name: "libfoo",
6057 defaults: ["libfoo-defaults"],
6058 stl: "none",
6059 system_shared_libs: [],
6060 }`)
6061}
6062
Paul Duffine52e66f2020-03-30 17:54:29 +01006063func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006064 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006065 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006066 apex {
6067 name: "myapex",
6068 key: "myapex.key",
6069 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006070 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006071 }
6072
6073 apex_key {
6074 name: "myapex.key",
6075 public_key: "testkey.avbpubkey",
6076 private_key: "testkey.pem",
6077 }
6078
6079 apex {
6080 name: "otherapex",
6081 key: "otherapex.key",
6082 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006083 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006084 }
6085
6086 apex_key {
6087 name: "otherapex.key",
6088 public_key: "testkey.avbpubkey",
6089 private_key: "testkey.pem",
6090 }
6091
6092 cc_library {
6093 name: "libfoo",
6094 stl: "none",
6095 system_shared_libs: [],
6096 apex_available: ["otherapex"],
6097 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006098}
Jiyong Park127b40b2019-09-30 16:04:35 +09006099
Paul Duffine52e66f2020-03-30 17:54:29 +01006100func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006101 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006102 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006103.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006104.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006105.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006106.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006107.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006108.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006109 apex {
6110 name: "myapex",
6111 key: "myapex.key",
6112 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006113 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006114 }
6115
6116 apex_key {
6117 name: "myapex.key",
6118 public_key: "testkey.avbpubkey",
6119 private_key: "testkey.pem",
6120 }
6121
Jiyong Park127b40b2019-09-30 16:04:35 +09006122 cc_library {
6123 name: "libfoo",
6124 stl: "none",
6125 shared_libs: ["libbar"],
6126 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006127 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006128 }
6129
6130 cc_library {
6131 name: "libbar",
6132 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006133 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006134 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006135 apex_available: ["myapex"],
6136 }
6137
6138 cc_library {
6139 name: "libbaz",
6140 stl: "none",
6141 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006142 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006143}
Jiyong Park127b40b2019-09-30 16:04:35 +09006144
Paul Duffine52e66f2020-03-30 17:54:29 +01006145func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006146 testApexError(t, "\"otherapex\" is not a valid module name", `
6147 apex {
6148 name: "myapex",
6149 key: "myapex.key",
6150 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006151 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006152 }
6153
6154 apex_key {
6155 name: "myapex.key",
6156 public_key: "testkey.avbpubkey",
6157 private_key: "testkey.pem",
6158 }
6159
6160 cc_library {
6161 name: "libfoo",
6162 stl: "none",
6163 system_shared_libs: [],
6164 apex_available: ["otherapex"],
6165 }`)
6166
Paul Duffine52e66f2020-03-30 17:54:29 +01006167 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006168 apex {
6169 name: "myapex",
6170 key: "myapex.key",
6171 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006172 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006173 }
6174
6175 apex_key {
6176 name: "myapex.key",
6177 public_key: "testkey.avbpubkey",
6178 private_key: "testkey.pem",
6179 }
6180
6181 cc_library {
6182 name: "libfoo",
6183 stl: "none",
6184 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006185 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006186 apex_available: ["myapex"],
6187 }
6188
6189 cc_library {
6190 name: "libbar",
6191 stl: "none",
6192 system_shared_libs: [],
6193 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006194 }
6195
6196 cc_library {
6197 name: "libbaz",
6198 stl: "none",
6199 system_shared_libs: [],
6200 stubs: {
6201 versions: ["10", "20", "30"],
6202 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006203 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006204}
Jiyong Park127b40b2019-09-30 16:04:35 +09006205
Jiyong Park89e850a2020-04-07 16:37:39 +09006206func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006207 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006208 apex {
6209 name: "myapex",
6210 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006211 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006212 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006213 }
6214
6215 apex_key {
6216 name: "myapex.key",
6217 public_key: "testkey.avbpubkey",
6218 private_key: "testkey.pem",
6219 }
6220
6221 cc_library {
6222 name: "libfoo",
6223 stl: "none",
6224 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006225 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006226 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006227 }
6228
6229 cc_library {
6230 name: "libfoo2",
6231 stl: "none",
6232 system_shared_libs: [],
6233 shared_libs: ["libbaz"],
6234 apex_available: ["//apex_available:platform"],
6235 }
6236
6237 cc_library {
6238 name: "libbar",
6239 stl: "none",
6240 system_shared_libs: [],
6241 apex_available: ["myapex"],
6242 }
6243
6244 cc_library {
6245 name: "libbaz",
6246 stl: "none",
6247 system_shared_libs: [],
6248 apex_available: ["myapex"],
6249 stubs: {
6250 versions: ["1"],
6251 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006252 }`)
6253
Jiyong Park89e850a2020-04-07 16:37:39 +09006254 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6255 // because it depends on libbar which isn't available to platform
6256 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6257 if libfoo.NotAvailableForPlatform() != true {
6258 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6259 }
6260
6261 // libfoo2 however can be available to platform because it depends on libbaz which provides
6262 // stubs
6263 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6264 if libfoo2.NotAvailableForPlatform() == true {
6265 t.Errorf("%q should be available to platform", libfoo2.String())
6266 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006267}
Jiyong Parka90ca002019-10-07 15:47:24 +09006268
Paul Duffine52e66f2020-03-30 17:54:29 +01006269func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006270 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006271 apex {
6272 name: "myapex",
6273 key: "myapex.key",
6274 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006275 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006276 }
6277
6278 apex_key {
6279 name: "myapex.key",
6280 public_key: "testkey.avbpubkey",
6281 private_key: "testkey.pem",
6282 }
6283
6284 cc_library {
6285 name: "libfoo",
6286 stl: "none",
6287 system_shared_libs: [],
6288 apex_available: ["myapex"],
6289 static: {
6290 apex_available: ["//apex_available:platform"],
6291 },
6292 }`)
6293
Jiyong Park89e850a2020-04-07 16:37:39 +09006294 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6295 if libfooShared.NotAvailableForPlatform() != true {
6296 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6297 }
6298 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6299 if libfooStatic.NotAvailableForPlatform() != false {
6300 t.Errorf("%q should be available to platform", libfooStatic.String())
6301 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006302}
6303
Jiyong Park5d790c32019-11-15 18:40:32 +09006304func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006305 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006306 apex {
6307 name: "myapex",
6308 key: "myapex.key",
6309 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006310 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006311 prebuilts: ["myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006312 bootclasspath_fragments: ["mybootclasspath_fragment"],
6313 systemserverclasspath_fragments: ["mysystemserverclasspath_fragment"],
6314 java_libs: ["myjava_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006315 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006316 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006317 }
6318
6319 override_apex {
6320 name: "override_myapex",
6321 base: "myapex",
6322 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006323 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006324 prebuilts: ["override_myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006325 bootclasspath_fragments: ["override_bootclasspath_fragment"],
6326 systemserverclasspath_fragments: ["override_systemserverclasspath_fragment"],
6327 java_libs: ["override_java_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006328 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006329 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006330 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006331 key: "mynewapex.key",
6332 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006333 }
6334
6335 apex_key {
6336 name: "myapex.key",
6337 public_key: "testkey.avbpubkey",
6338 private_key: "testkey.pem",
6339 }
6340
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006341 apex_key {
6342 name: "mynewapex.key",
6343 public_key: "testkey2.avbpubkey",
6344 private_key: "testkey2.pem",
6345 }
6346
6347 android_app_certificate {
6348 name: "myapex.certificate",
6349 certificate: "testkey",
6350 }
6351
Jiyong Park5d790c32019-11-15 18:40:32 +09006352 android_app {
6353 name: "app",
6354 srcs: ["foo/bar/MyClass.java"],
6355 package_name: "foo",
6356 sdk_version: "none",
6357 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006358 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006359 }
6360
6361 override_android_app {
6362 name: "override_app",
6363 base: "app",
6364 package_name: "bar",
6365 }
markchien7c803b82021-08-26 22:10:06 +08006366
6367 bpf {
6368 name: "bpf",
6369 srcs: ["bpf.c"],
6370 }
6371
6372 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006373 name: "overrideBpf",
6374 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006375 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006376
6377 prebuilt_etc {
6378 name: "myetc",
6379 src: "myprebuilt",
6380 }
6381
6382 prebuilt_etc {
6383 name: "override_myetc",
6384 src: "override_myprebuilt",
6385 }
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006386
6387 java_library {
6388 name: "bcplib",
6389 srcs: ["a.java"],
6390 compile_dex: true,
6391 apex_available: ["myapex"],
6392 permitted_packages: ["bcp.lib"],
6393 }
6394
6395 bootclasspath_fragment {
6396 name: "mybootclasspath_fragment",
6397 contents: ["bcplib"],
6398 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01006399 hidden_api: {
6400 split_packages: ["*"],
6401 },
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006402 }
6403
6404 java_library {
6405 name: "override_bcplib",
6406 srcs: ["a.java"],
6407 compile_dex: true,
6408 apex_available: ["myapex"],
6409 permitted_packages: ["override.bcp.lib"],
6410 }
6411
6412 bootclasspath_fragment {
6413 name: "override_bootclasspath_fragment",
6414 contents: ["override_bcplib"],
6415 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01006416 hidden_api: {
6417 split_packages: ["*"],
6418 },
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006419 }
6420
6421 java_library {
6422 name: "systemserverlib",
6423 srcs: ["a.java"],
6424 apex_available: ["myapex"],
6425 }
6426
6427 systemserverclasspath_fragment {
6428 name: "mysystemserverclasspath_fragment",
6429 standalone_contents: ["systemserverlib"],
6430 apex_available: ["myapex"],
6431 }
6432
6433 java_library {
6434 name: "override_systemserverlib",
6435 srcs: ["a.java"],
6436 apex_available: ["myapex"],
6437 }
6438
6439 systemserverclasspath_fragment {
6440 name: "override_systemserverclasspath_fragment",
6441 standalone_contents: ["override_systemserverlib"],
6442 apex_available: ["myapex"],
6443 }
6444
6445 java_library {
6446 name: "myjava_library",
6447 srcs: ["a.java"],
6448 compile_dex: true,
6449 apex_available: ["myapex"],
6450 }
6451
6452 java_library {
6453 name: "override_java_library",
6454 srcs: ["a.java"],
6455 compile_dex: true,
6456 apex_available: ["myapex"],
6457 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006458 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006459
Jiyong Park317645e2019-12-05 13:20:58 +09006460 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6461 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6462 if originalVariant.GetOverriddenBy() != "" {
6463 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6464 }
6465 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6466 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6467 }
6468
Jiyong Park5d790c32019-11-15 18:40:32 +09006469 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6470 apexRule := module.Rule("apexRule")
6471 copyCmds := apexRule.Args["copy_commands"]
6472
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006473 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6474 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006475
markchien7c803b82021-08-26 22:10:06 +08006476 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006477 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006478
Daniel Norman5a3ce132021-08-26 15:44:43 -07006479 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6480 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6481
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006482 apexBundle := module.Module().(*apexBundle)
6483 name := apexBundle.Name()
6484 if name != "override_myapex" {
6485 t.Errorf("name should be \"override_myapex\", but was %q", name)
6486 }
6487
Baligh Uddin004d7172020-02-19 21:29:28 -08006488 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6489 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6490 }
6491
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006492 android.AssertArrayString(t, "Bootclasspath_fragments does not match",
6493 []string{"override_bootclasspath_fragment"}, apexBundle.overridableProperties.Bootclasspath_fragments)
6494 android.AssertArrayString(t, "Systemserverclasspath_fragments does not match",
6495 []string{"override_systemserverclasspath_fragment"}, apexBundle.overridableProperties.Systemserverclasspath_fragments)
6496 android.AssertArrayString(t, "Java_libs does not match",
6497 []string{"override_java_library"}, apexBundle.overridableProperties.Java_libs)
6498
Jiyong Park20bacab2020-03-03 11:45:41 +09006499 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006500 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006501 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6502
6503 signApkRule := module.Rule("signapk")
6504 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006505
Colin Crossaa255532020-07-03 13:18:24 -07006506 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006507 var builder strings.Builder
6508 data.Custom(&builder, name, "TARGET_", "", data)
6509 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006510 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6511 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6512 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
6513 ensureContains(t, androidMk, "LOCAL_MODULE := override_bcplib.override_myapex")
6514 ensureContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.override_myapex")
6515 ensureContains(t, androidMk, "LOCAL_MODULE := override_java_library.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006516 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006517 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006518 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006519 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006520 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006521 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006522 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_bcplib.myapex")
6523 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.myapex")
6524 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_java_library.pb.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006525 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006526}
6527
Albert Martineefabcf2022-03-21 20:11:16 +00006528func TestMinSdkVersionOverride(t *testing.T) {
6529 // Override from 29 to 31
6530 minSdkOverride31 := "31"
6531 ctx := testApex(t, `
6532 apex {
6533 name: "myapex",
6534 key: "myapex.key",
6535 native_shared_libs: ["mylib"],
6536 updatable: true,
6537 min_sdk_version: "29"
6538 }
6539
6540 override_apex {
6541 name: "override_myapex",
6542 base: "myapex",
6543 logging_parent: "com.foo.bar",
6544 package_name: "test.overridden.package"
6545 }
6546
6547 apex_key {
6548 name: "myapex.key",
6549 public_key: "testkey.avbpubkey",
6550 private_key: "testkey.pem",
6551 }
6552
6553 cc_library {
6554 name: "mylib",
6555 srcs: ["mylib.cpp"],
6556 runtime_libs: ["libbar"],
6557 system_shared_libs: [],
6558 stl: "none",
6559 apex_available: [ "myapex" ],
6560 min_sdk_version: "apex_inherit"
6561 }
6562
6563 cc_library {
6564 name: "libbar",
6565 srcs: ["mylib.cpp"],
6566 system_shared_libs: [],
6567 stl: "none",
6568 apex_available: [ "myapex" ],
6569 min_sdk_version: "apex_inherit"
6570 }
6571
6572 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6573
6574 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6575 copyCmds := apexRule.Args["copy_commands"]
6576
6577 // Ensure that direct non-stubs dep is always included
6578 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6579
6580 // Ensure that runtime_libs dep in included
6581 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6582
6583 // Ensure libraries target overridden min_sdk_version value
6584 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6585}
6586
6587func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6588 // Attempt to override from 31 to 29, should be a NOOP
6589 minSdkOverride29 := "29"
6590 ctx := testApex(t, `
6591 apex {
6592 name: "myapex",
6593 key: "myapex.key",
6594 native_shared_libs: ["mylib"],
6595 updatable: true,
6596 min_sdk_version: "31"
6597 }
6598
6599 override_apex {
6600 name: "override_myapex",
6601 base: "myapex",
6602 logging_parent: "com.foo.bar",
6603 package_name: "test.overridden.package"
6604 }
6605
6606 apex_key {
6607 name: "myapex.key",
6608 public_key: "testkey.avbpubkey",
6609 private_key: "testkey.pem",
6610 }
6611
6612 cc_library {
6613 name: "mylib",
6614 srcs: ["mylib.cpp"],
6615 runtime_libs: ["libbar"],
6616 system_shared_libs: [],
6617 stl: "none",
6618 apex_available: [ "myapex" ],
6619 min_sdk_version: "apex_inherit"
6620 }
6621
6622 cc_library {
6623 name: "libbar",
6624 srcs: ["mylib.cpp"],
6625 system_shared_libs: [],
6626 stl: "none",
6627 apex_available: [ "myapex" ],
6628 min_sdk_version: "apex_inherit"
6629 }
6630
6631 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6632
6633 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6634 copyCmds := apexRule.Args["copy_commands"]
6635
6636 // Ensure that direct non-stubs dep is always included
6637 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6638
6639 // Ensure that runtime_libs dep in included
6640 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6641
6642 // Ensure libraries target the original min_sdk_version value rather than the overridden
6643 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6644}
6645
Jooyung Han214bf372019-11-12 13:03:50 +09006646func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006647 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006648 apex {
6649 name: "myapex",
6650 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006651 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006652 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006653 }
6654
6655 apex_key {
6656 name: "myapex.key",
6657 public_key: "testkey.avbpubkey",
6658 private_key: "testkey.pem",
6659 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006660
6661 cc_library {
6662 name: "mylib",
6663 srcs: ["mylib.cpp"],
6664 stl: "libc++",
6665 system_shared_libs: [],
6666 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006667 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006668 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006669 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006670
6671 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6672 args := module.Rule("apexRule").Args
6673 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006674 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006675
6676 // The copies of the libraries in the apex should have one more dependency than
6677 // the ones outside the apex, namely the unwinder. Ideally we should check
6678 // the dependency names directly here but for some reason the names are blank in
6679 // this test.
6680 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006681 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006682 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6683 if len(apexImplicits) != len(nonApexImplicits)+1 {
6684 t.Errorf("%q missing unwinder dep", lib)
6685 }
6686 }
Jooyung Han214bf372019-11-12 13:03:50 +09006687}
6688
Paul Duffine05480a2021-03-08 15:07:14 +00006689var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006690 "api/current.txt": nil,
6691 "api/removed.txt": nil,
6692 "api/system-current.txt": nil,
6693 "api/system-removed.txt": nil,
6694 "api/test-current.txt": nil,
6695 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006696
Anton Hanssondff2c782020-12-21 17:10:01 +00006697 "100/public/api/foo.txt": nil,
6698 "100/public/api/foo-removed.txt": nil,
6699 "100/system/api/foo.txt": nil,
6700 "100/system/api/foo-removed.txt": nil,
6701
Paul Duffineedc5d52020-06-12 17:46:39 +01006702 // For java_sdk_library_import
6703 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006704}
6705
Jooyung Han58f26ab2019-12-18 15:34:32 +09006706func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006707 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006708 apex {
6709 name: "myapex",
6710 key: "myapex.key",
6711 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006712 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006713 }
6714
6715 apex_key {
6716 name: "myapex.key",
6717 public_key: "testkey.avbpubkey",
6718 private_key: "testkey.pem",
6719 }
6720
6721 java_sdk_library {
6722 name: "foo",
6723 srcs: ["a.java"],
6724 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006725 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006726 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006727
6728 prebuilt_apis {
6729 name: "sdk",
6730 api_dirs: ["100"],
6731 }
Paul Duffin9b879592020-05-26 13:21:35 +01006732 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006733
6734 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006735 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006736 "javalib/foo.jar",
6737 "etc/permissions/foo.xml",
6738 })
6739 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006740 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006741 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 +09006742}
6743
Paul Duffin9b879592020-05-26 13:21:35 +01006744func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006745 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006746 apex {
6747 name: "myapex",
6748 key: "myapex.key",
6749 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006750 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006751 }
6752
6753 apex_key {
6754 name: "myapex.key",
6755 public_key: "testkey.avbpubkey",
6756 private_key: "testkey.pem",
6757 }
6758
6759 java_sdk_library {
6760 name: "foo",
6761 srcs: ["a.java"],
6762 api_packages: ["foo"],
6763 apex_available: ["myapex"],
6764 sdk_version: "none",
6765 system_modules: "none",
6766 }
6767
6768 java_library {
6769 name: "bar",
6770 srcs: ["a.java"],
6771 libs: ["foo"],
6772 apex_available: ["myapex"],
6773 sdk_version: "none",
6774 system_modules: "none",
6775 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006776
6777 prebuilt_apis {
6778 name: "sdk",
6779 api_dirs: ["100"],
6780 }
Paul Duffin9b879592020-05-26 13:21:35 +01006781 `, withFiles(filesForSdkLibrary))
6782
6783 // java_sdk_library installs both impl jar and permission XML
6784 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6785 "javalib/bar.jar",
6786 "javalib/foo.jar",
6787 "etc/permissions/foo.xml",
6788 })
6789
6790 // The bar library should depend on the implementation jar.
6791 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006792 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006793 t.Errorf("expected %q, found %#q", expected, actual)
6794 }
6795}
6796
6797func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006798 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006799 apex {
6800 name: "myapex",
6801 key: "myapex.key",
6802 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006803 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006804 }
6805
6806 apex_key {
6807 name: "myapex.key",
6808 public_key: "testkey.avbpubkey",
6809 private_key: "testkey.pem",
6810 }
6811
6812 java_sdk_library {
6813 name: "foo",
6814 srcs: ["a.java"],
6815 api_packages: ["foo"],
6816 apex_available: ["myapex"],
6817 sdk_version: "none",
6818 system_modules: "none",
6819 }
6820
6821 java_library {
6822 name: "bar",
6823 srcs: ["a.java"],
6824 libs: ["foo"],
6825 sdk_version: "none",
6826 system_modules: "none",
6827 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006828
6829 prebuilt_apis {
6830 name: "sdk",
6831 api_dirs: ["100"],
6832 }
Paul Duffin9b879592020-05-26 13:21:35 +01006833 `, withFiles(filesForSdkLibrary))
6834
6835 // java_sdk_library installs both impl jar and permission XML
6836 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6837 "javalib/foo.jar",
6838 "etc/permissions/foo.xml",
6839 })
6840
6841 // The bar library should depend on the stubs jar.
6842 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006843 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006844 t.Errorf("expected %q, found %#q", expected, actual)
6845 }
6846}
6847
Paul Duffineedc5d52020-06-12 17:46:39 +01006848func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006849 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006850 prebuilt_apis {
6851 name: "sdk",
6852 api_dirs: ["100"],
6853 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006854 withFiles(map[string][]byte{
6855 "apex/a.java": nil,
6856 "apex/apex_manifest.json": nil,
6857 "apex/Android.bp": []byte(`
6858 package {
6859 default_visibility: ["//visibility:private"],
6860 }
6861
6862 apex {
6863 name: "myapex",
6864 key: "myapex.key",
6865 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006866 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006867 }
6868
6869 apex_key {
6870 name: "myapex.key",
6871 public_key: "testkey.avbpubkey",
6872 private_key: "testkey.pem",
6873 }
6874
6875 java_library {
6876 name: "bar",
6877 srcs: ["a.java"],
6878 libs: ["foo"],
6879 apex_available: ["myapex"],
6880 sdk_version: "none",
6881 system_modules: "none",
6882 }
6883`),
6884 "source/a.java": nil,
6885 "source/api/current.txt": nil,
6886 "source/api/removed.txt": nil,
6887 "source/Android.bp": []byte(`
6888 package {
6889 default_visibility: ["//visibility:private"],
6890 }
6891
6892 java_sdk_library {
6893 name: "foo",
6894 visibility: ["//apex"],
6895 srcs: ["a.java"],
6896 api_packages: ["foo"],
6897 apex_available: ["myapex"],
6898 sdk_version: "none",
6899 system_modules: "none",
6900 public: {
6901 enabled: true,
6902 },
6903 }
6904`),
6905 "prebuilt/a.jar": nil,
6906 "prebuilt/Android.bp": []byte(`
6907 package {
6908 default_visibility: ["//visibility:private"],
6909 }
6910
6911 java_sdk_library_import {
6912 name: "foo",
6913 visibility: ["//apex", "//source"],
6914 apex_available: ["myapex"],
6915 prefer: true,
6916 public: {
6917 jars: ["a.jar"],
6918 },
6919 }
6920`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006921 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006922 )
6923
6924 // java_sdk_library installs both impl jar and permission XML
6925 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6926 "javalib/bar.jar",
6927 "javalib/foo.jar",
6928 "etc/permissions/foo.xml",
6929 })
6930
6931 // The bar library should depend on the implementation jar.
6932 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006933 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006934 t.Errorf("expected %q, found %#q", expected, actual)
6935 }
6936}
6937
6938func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6939 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6940 apex {
6941 name: "myapex",
6942 key: "myapex.key",
6943 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006944 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006945 }
6946
6947 apex_key {
6948 name: "myapex.key",
6949 public_key: "testkey.avbpubkey",
6950 private_key: "testkey.pem",
6951 }
6952
6953 java_sdk_library_import {
6954 name: "foo",
6955 apex_available: ["myapex"],
6956 prefer: true,
6957 public: {
6958 jars: ["a.jar"],
6959 },
6960 }
6961
6962 `, withFiles(filesForSdkLibrary))
6963}
6964
atrost6e126252020-01-27 17:01:16 +00006965func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006966 result := android.GroupFixturePreparers(
6967 prepareForApexTest,
6968 java.PrepareForTestWithPlatformCompatConfig,
6969 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006970 apex {
6971 name: "myapex",
6972 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006973 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006974 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006975 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006976 }
6977
6978 apex_key {
6979 name: "myapex.key",
6980 public_key: "testkey.avbpubkey",
6981 private_key: "testkey.pem",
6982 }
6983
6984 platform_compat_config {
6985 name: "myjar-platform-compat-config",
6986 src: ":myjar",
6987 }
6988
6989 java_library {
6990 name: "myjar",
6991 srcs: ["foo/bar/MyClass.java"],
6992 sdk_version: "none",
6993 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006994 apex_available: [ "myapex" ],
6995 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006996
6997 // Make sure that a preferred prebuilt does not affect the apex contents.
6998 prebuilt_platform_compat_config {
6999 name: "myjar-platform-compat-config",
7000 metadata: "compat-config/metadata.xml",
7001 prefer: true,
7002 }
atrost6e126252020-01-27 17:01:16 +00007003 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007004 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007005 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7006 "etc/compatconfig/myjar-platform-compat-config.xml",
7007 "javalib/myjar.jar",
7008 })
7009}
7010
Jooyung Han862c0d62022-12-21 10:15:37 +09007011func TestNoDupeApexFiles(t *testing.T) {
7012 android.GroupFixturePreparers(
7013 android.PrepareForTestWithAndroidBuildComponents,
7014 PrepareForTestWithApexBuildComponents,
7015 prepareForTestWithMyapex,
7016 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7017 ).
7018 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7019 RunTestWithBp(t, `
7020 apex {
7021 name: "myapex",
7022 key: "myapex.key",
7023 prebuilts: ["foo", "bar"],
7024 updatable: false,
7025 }
7026
7027 apex_key {
7028 name: "myapex.key",
7029 public_key: "testkey.avbpubkey",
7030 private_key: "testkey.pem",
7031 }
7032
7033 prebuilt_etc {
7034 name: "foo",
7035 src: "myprebuilt",
7036 filename_from_src: true,
7037 }
7038
7039 prebuilt_etc {
7040 name: "bar",
7041 src: "myprebuilt",
7042 filename_from_src: true,
7043 }
7044 `)
7045}
7046
Jiyong Park479321d2019-12-16 11:47:12 +09007047func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7048 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7049 apex {
7050 name: "myapex",
7051 key: "myapex.key",
7052 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007053 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007054 }
7055
7056 apex_key {
7057 name: "myapex.key",
7058 public_key: "testkey.avbpubkey",
7059 private_key: "testkey.pem",
7060 }
7061
7062 java_library {
7063 name: "myjar",
7064 srcs: ["foo/bar/MyClass.java"],
7065 sdk_version: "none",
7066 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007067 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007068 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007069 }
7070 `)
7071}
7072
Jiyong Park7afd1072019-12-30 16:56:33 +09007073func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007074 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007075 apex {
7076 name: "myapex",
7077 key: "myapex.key",
7078 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007079 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007080 }
7081
7082 apex_key {
7083 name: "myapex.key",
7084 public_key: "testkey.avbpubkey",
7085 private_key: "testkey.pem",
7086 }
7087
7088 cc_library {
7089 name: "mylib",
7090 srcs: ["mylib.cpp"],
7091 system_shared_libs: [],
7092 stl: "none",
7093 required: ["a", "b"],
7094 host_required: ["c", "d"],
7095 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007096 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007097 }
7098 `)
7099
7100 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007101 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007102 name := apexBundle.BaseModuleName()
7103 prefix := "TARGET_"
7104 var builder strings.Builder
7105 data.Custom(&builder, name, prefix, "", data)
7106 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007107 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 -08007108 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7109 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007110}
7111
Jiyong Park7cd10e32020-01-14 09:22:18 +09007112func TestSymlinksFromApexToSystem(t *testing.T) {
7113 bp := `
7114 apex {
7115 name: "myapex",
7116 key: "myapex.key",
7117 native_shared_libs: ["mylib"],
7118 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007119 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007120 }
7121
Jiyong Park9d677202020-02-19 16:29:35 +09007122 apex {
7123 name: "myapex.updatable",
7124 key: "myapex.key",
7125 native_shared_libs: ["mylib"],
7126 java_libs: ["myjar"],
7127 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09007128 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09007129 }
7130
Jiyong Park7cd10e32020-01-14 09:22:18 +09007131 apex_key {
7132 name: "myapex.key",
7133 public_key: "testkey.avbpubkey",
7134 private_key: "testkey.pem",
7135 }
7136
7137 cc_library {
7138 name: "mylib",
7139 srcs: ["mylib.cpp"],
7140 shared_libs: ["myotherlib"],
7141 system_shared_libs: [],
7142 stl: "none",
7143 apex_available: [
7144 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007145 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007146 "//apex_available:platform",
7147 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007148 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007149 }
7150
7151 cc_library {
7152 name: "myotherlib",
7153 srcs: ["mylib.cpp"],
7154 system_shared_libs: [],
7155 stl: "none",
7156 apex_available: [
7157 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007158 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007159 "//apex_available:platform",
7160 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007161 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007162 }
7163
7164 java_library {
7165 name: "myjar",
7166 srcs: ["foo/bar/MyClass.java"],
7167 sdk_version: "none",
7168 system_modules: "none",
7169 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007170 apex_available: [
7171 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007172 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007173 "//apex_available:platform",
7174 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007175 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007176 }
7177
7178 java_library {
7179 name: "myotherjar",
7180 srcs: ["foo/bar/MyClass.java"],
7181 sdk_version: "none",
7182 system_modules: "none",
7183 apex_available: [
7184 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007185 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007186 "//apex_available:platform",
7187 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007188 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007189 }
7190 `
7191
7192 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7193 for _, f := range files {
7194 if f.path == file {
7195 if f.isLink {
7196 t.Errorf("%q is not a real file", file)
7197 }
7198 return
7199 }
7200 }
7201 t.Errorf("%q is not found", file)
7202 }
7203
7204 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
7205 for _, f := range files {
7206 if f.path == file {
7207 if !f.isLink {
7208 t.Errorf("%q is not a symlink", file)
7209 }
7210 return
7211 }
7212 }
7213 t.Errorf("%q is not found", file)
7214 }
7215
Jiyong Park9d677202020-02-19 16:29:35 +09007216 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7217 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007218 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007219 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007220 ensureRealfileExists(t, files, "javalib/myjar.jar")
7221 ensureRealfileExists(t, files, "lib64/mylib.so")
7222 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7223
Jiyong Park9d677202020-02-19 16:29:35 +09007224 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7225 ensureRealfileExists(t, files, "javalib/myjar.jar")
7226 ensureRealfileExists(t, files, "lib64/mylib.so")
7227 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7228
7229 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007230 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007231 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007232 ensureRealfileExists(t, files, "javalib/myjar.jar")
7233 ensureRealfileExists(t, files, "lib64/mylib.so")
7234 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007235
7236 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7237 ensureRealfileExists(t, files, "javalib/myjar.jar")
7238 ensureRealfileExists(t, files, "lib64/mylib.so")
7239 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007240}
7241
Yo Chiange8128052020-07-23 20:09:18 +08007242func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007243 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007244 apex {
7245 name: "myapex",
7246 key: "myapex.key",
7247 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007248 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007249 }
7250
7251 apex_key {
7252 name: "myapex.key",
7253 public_key: "testkey.avbpubkey",
7254 private_key: "testkey.pem",
7255 }
7256
7257 cc_library_shared {
7258 name: "mylib",
7259 srcs: ["mylib.cpp"],
7260 shared_libs: ["myotherlib"],
7261 system_shared_libs: [],
7262 stl: "none",
7263 apex_available: [
7264 "myapex",
7265 "//apex_available:platform",
7266 ],
7267 }
7268
7269 cc_prebuilt_library_shared {
7270 name: "myotherlib",
7271 srcs: ["prebuilt.so"],
7272 system_shared_libs: [],
7273 stl: "none",
7274 apex_available: [
7275 "myapex",
7276 "//apex_available:platform",
7277 ],
7278 }
7279 `)
7280
Prerana Patilb1896c82022-11-09 18:14:34 +00007281 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007282 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007283 var builder strings.Builder
7284 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7285 androidMk := builder.String()
7286 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007287 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007288 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7289 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7290 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007291 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 +08007292}
7293
Jooyung Han643adc42020-02-27 13:50:06 +09007294func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007295 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007296 apex {
7297 name: "myapex",
7298 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007299 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007300 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007301 }
7302
7303 apex_key {
7304 name: "myapex.key",
7305 public_key: "testkey.avbpubkey",
7306 private_key: "testkey.pem",
7307 }
7308
7309 cc_library {
7310 name: "mylib",
7311 srcs: ["mylib.cpp"],
7312 shared_libs: ["mylib2"],
7313 system_shared_libs: [],
7314 stl: "none",
7315 apex_available: [ "myapex" ],
7316 }
7317
7318 cc_library {
7319 name: "mylib2",
7320 srcs: ["mylib.cpp"],
7321 system_shared_libs: [],
7322 stl: "none",
7323 apex_available: [ "myapex" ],
7324 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007325
7326 rust_ffi_shared {
7327 name: "libfoo.rust",
7328 crate_name: "foo",
7329 srcs: ["foo.rs"],
7330 shared_libs: ["libfoo.shared_from_rust"],
7331 prefer_rlib: true,
7332 apex_available: ["myapex"],
7333 }
7334
7335 cc_library_shared {
7336 name: "libfoo.shared_from_rust",
7337 srcs: ["mylib.cpp"],
7338 system_shared_libs: [],
7339 stl: "none",
7340 stubs: {
7341 versions: ["10", "11", "12"],
7342 },
7343 }
7344
Jooyung Han643adc42020-02-27 13:50:06 +09007345 `)
7346
7347 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7348 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007349 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007350 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7351 "lib64/mylib.so",
7352 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007353 "lib64/libfoo.rust.so",
7354 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7355 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007356 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007357
7358 // b/220397949
7359 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007360}
7361
Jooyung Han49f67012020-04-17 13:43:10 +09007362func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007363 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007364 apex {
7365 name: "myapex",
7366 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007367 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007368 }
7369 apex_key {
7370 name: "myapex.key",
7371 public_key: "testkey.avbpubkey",
7372 private_key: "testkey.pem",
7373 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007374 `,
7375 android.FixtureModifyConfig(func(config android.Config) {
7376 delete(config.Targets, android.Android)
7377 config.AndroidCommonTarget = android.Target{}
7378 }),
7379 )
Jooyung Han49f67012020-04-17 13:43:10 +09007380
7381 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7382 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7383 }
7384}
7385
Jiyong Parkbd159612020-02-28 15:22:21 +09007386func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007387 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007388 apex {
7389 name: "myapex",
7390 key: "myapex.key",
7391 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007392 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007393 }
7394
7395 apex_key {
7396 name: "myapex.key",
7397 public_key: "testkey.avbpubkey",
7398 private_key: "testkey.pem",
7399 }
7400
7401 android_app {
7402 name: "AppFoo",
7403 srcs: ["foo/bar/MyClass.java"],
7404 sdk_version: "none",
7405 system_modules: "none",
7406 apex_available: [ "myapex" ],
7407 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007408 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007409
Colin Crosscf371cc2020-11-13 11:48:42 -08007410 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007411 content := bundleConfigRule.Args["content"]
7412
7413 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007414 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 +09007415}
7416
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007417func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007418 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007419 apex {
7420 name: "myapex",
7421 key: "myapex.key",
7422 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007423 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007424 }
7425
7426 apex_key {
7427 name: "myapex.key",
7428 public_key: "testkey.avbpubkey",
7429 private_key: "testkey.pem",
7430 }
7431
7432 android_app_set {
7433 name: "AppSet",
7434 set: "AppSet.apks",
7435 }`)
7436 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007437 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007438 content := bundleConfigRule.Args["content"]
7439 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7440 s := mod.Rule("apexRule").Args["copy_commands"]
7441 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007442 if len(copyCmds) != 4 {
7443 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007444 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007445 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7446 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007447 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7448 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007449
7450 // Ensure that canned_fs_config has an entry for the app set zip file
7451 generateFsRule := mod.Rule("generateFsConfig")
7452 cmd := generateFsRule.RuleParams.Command
7453 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007454}
7455
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007456func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007457 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007458 apex_set {
7459 name: "myapex",
7460 filename: "foo_v2.apex",
7461 sanitized: {
7462 none: { set: "myapex.apks", },
7463 hwaddress: { set: "myapex.hwasan.apks", },
7464 },
Paul Duffin24704672021-04-06 16:09:30 +01007465 }
7466 `
7467 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007468
Paul Duffin24704672021-04-06 16:09:30 +01007469 // Check that the extractor produces the correct output file from the correct input file.
7470 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007471
Paul Duffin24704672021-04-06 16:09:30 +01007472 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7473 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007474
Paul Duffin24704672021-04-06 16:09:30 +01007475 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7476
7477 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007478 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7479 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007480
7481 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007482}
7483
Pranav Guptaeba03b02022-09-27 00:27:08 +00007484func TestApexSetApksModuleAssignment(t *testing.T) {
7485 ctx := testApex(t, `
7486 apex_set {
7487 name: "myapex",
7488 set: ":myapex_apks_file",
7489 }
7490
7491 filegroup {
7492 name: "myapex_apks_file",
7493 srcs: ["myapex.apks"],
7494 }
7495 `)
7496
7497 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7498
7499 // Check that the extractor produces the correct apks file from the input module
7500 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7501 extractedApex := m.Output(extractorOutput)
7502
7503 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7504}
7505
Paul Duffin89f570a2021-06-16 01:42:33 +01007506func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007507 t.Helper()
7508
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007509 bp := `
7510 java_library {
7511 name: "some-updatable-apex-lib",
7512 srcs: ["a.java"],
7513 sdk_version: "current",
7514 apex_available: [
7515 "some-updatable-apex",
7516 ],
satayevabcd5972021-08-06 17:49:46 +01007517 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007518 }
7519
7520 java_library {
7521 name: "some-non-updatable-apex-lib",
7522 srcs: ["a.java"],
7523 apex_available: [
7524 "some-non-updatable-apex",
7525 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007526 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007527 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007528 }
7529
7530 bootclasspath_fragment {
7531 name: "some-non-updatable-fragment",
7532 contents: ["some-non-updatable-apex-lib"],
7533 apex_available: [
7534 "some-non-updatable-apex",
7535 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007536 hidden_api: {
7537 split_packages: ["*"],
7538 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007539 }
7540
7541 java_library {
7542 name: "some-platform-lib",
7543 srcs: ["a.java"],
7544 sdk_version: "current",
7545 installable: true,
7546 }
7547
7548 java_library {
7549 name: "some-art-lib",
7550 srcs: ["a.java"],
7551 sdk_version: "current",
7552 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007553 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007554 ],
7555 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007556 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007557 }
7558
7559 apex {
7560 name: "some-updatable-apex",
7561 key: "some-updatable-apex.key",
7562 java_libs: ["some-updatable-apex-lib"],
7563 updatable: true,
7564 min_sdk_version: "current",
7565 }
7566
7567 apex {
7568 name: "some-non-updatable-apex",
7569 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007570 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007571 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007572 }
7573
7574 apex_key {
7575 name: "some-updatable-apex.key",
7576 }
7577
7578 apex_key {
7579 name: "some-non-updatable-apex.key",
7580 }
7581
7582 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007583 name: "com.android.art.debug",
7584 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007585 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007586 updatable: true,
7587 min_sdk_version: "current",
7588 }
7589
Paul Duffinf23bc472021-04-27 12:42:20 +01007590 bootclasspath_fragment {
7591 name: "art-bootclasspath-fragment",
7592 image_name: "art",
7593 contents: ["some-art-lib"],
7594 apex_available: [
7595 "com.android.art.debug",
7596 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007597 hidden_api: {
7598 split_packages: ["*"],
7599 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007600 }
7601
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007602 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007603 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007604 }
7605
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007606 filegroup {
7607 name: "some-updatable-apex-file_contexts",
7608 srcs: [
7609 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7610 ],
7611 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007612
7613 filegroup {
7614 name: "some-non-updatable-apex-file_contexts",
7615 srcs: [
7616 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7617 ],
7618 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007619 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007620
Paul Duffin89f570a2021-06-16 01:42:33 +01007621 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007622}
7623
Paul Duffin89f570a2021-06-16 01:42:33 +01007624func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007625 t.Helper()
7626
Paul Duffin55607122021-03-30 23:32:51 +01007627 fs := android.MockFS{
7628 "a.java": nil,
7629 "a.jar": nil,
7630 "apex_manifest.json": nil,
7631 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007632 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007633 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7634 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7635 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007636 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007637 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007638
Paul Duffin55607122021-03-30 23:32:51 +01007639 errorHandler := android.FixtureExpectsNoErrors
7640 if errmsg != "" {
7641 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007642 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007643
Paul Duffin55607122021-03-30 23:32:51 +01007644 result := android.GroupFixturePreparers(
7645 cc.PrepareForTestWithCcDefaultModules,
7646 java.PrepareForTestWithHiddenApiBuildComponents,
7647 java.PrepareForTestWithJavaDefaultModules,
7648 java.PrepareForTestWithJavaSdkLibraryFiles,
7649 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007650 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007651 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007652 android.FixtureModifyMockFS(func(fs android.MockFS) {
7653 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7654 insert := ""
7655 for _, fragment := range fragments {
7656 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7657 }
7658 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7659 platform_bootclasspath {
7660 name: "platform-bootclasspath",
7661 fragments: [
7662 %s
7663 ],
7664 }
7665 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007666 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007667 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007668 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007669 ).
7670 ExtendWithErrorHandler(errorHandler).
7671 RunTestWithBp(t, bp)
7672
7673 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007674}
7675
Paul Duffin5556c5f2022-06-09 17:32:21 +00007676func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007677 preparers := android.GroupFixturePreparers(
7678 java.PrepareForTestWithJavaDefaultModules,
7679 PrepareForTestWithApexBuildComponents,
7680 ).
7681 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7682 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7683
7684 bpBase := `
7685 apex_set {
7686 name: "com.android.myapex",
7687 installable: true,
7688 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7689 set: "myapex.apks",
7690 }
7691
7692 apex_set {
7693 name: "com.mycompany.android.myapex",
7694 apex_name: "com.android.myapex",
7695 installable: true,
7696 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7697 set: "company-myapex.apks",
7698 }
7699
7700 prebuilt_bootclasspath_fragment {
7701 name: "my-bootclasspath-fragment",
7702 apex_available: ["com.android.myapex"],
7703 %s
7704 }
7705 `
7706
7707 t.Run("java_import", func(t *testing.T) {
7708 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7709 java_import {
7710 name: "libfoo",
7711 jars: ["libfoo.jar"],
7712 apex_available: ["com.android.myapex"],
7713 }
7714 `)
7715 })
7716
7717 t.Run("java_sdk_library_import", func(t *testing.T) {
7718 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7719 java_sdk_library_import {
7720 name: "libfoo",
7721 public: {
7722 jars: ["libbar.jar"],
7723 },
7724 apex_available: ["com.android.myapex"],
7725 }
7726 `)
7727 })
7728
7729 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7730 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7731 image_name: "art",
7732 contents: ["libfoo"],
7733 `)+`
7734 java_sdk_library_import {
7735 name: "libfoo",
7736 public: {
7737 jars: ["libbar.jar"],
7738 },
7739 apex_available: ["com.android.myapex"],
7740 }
7741 `)
7742 })
7743}
7744
Paul Duffin5556c5f2022-06-09 17:32:21 +00007745func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7746 preparers := android.GroupFixturePreparers(
7747 java.PrepareForTestWithJavaDefaultModules,
7748 PrepareForTestWithApexBuildComponents,
7749 )
7750
7751 bpBase := `
7752 apex_set {
7753 name: "com.android.myapex",
7754 installable: true,
7755 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7756 set: "myapex.apks",
7757 }
7758
7759 apex_set {
7760 name: "com.android.myapex_compressed",
7761 apex_name: "com.android.myapex",
7762 installable: true,
7763 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7764 set: "myapex_compressed.apks",
7765 }
7766
7767 prebuilt_bootclasspath_fragment {
7768 name: "my-bootclasspath-fragment",
7769 apex_available: [
7770 "com.android.myapex",
7771 "com.android.myapex_compressed",
7772 ],
7773 hidden_api: {
7774 annotation_flags: "annotation-flags.csv",
7775 metadata: "metadata.csv",
7776 index: "index.csv",
7777 signature_patterns: "signature_patterns.csv",
7778 },
7779 %s
7780 }
7781 `
7782
7783 t.Run("java_import", func(t *testing.T) {
7784 result := preparers.RunTestWithBp(t,
7785 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7786 java_import {
7787 name: "libfoo",
7788 jars: ["libfoo.jar"],
7789 apex_available: [
7790 "com.android.myapex",
7791 "com.android.myapex_compressed",
7792 ],
7793 }
7794 `)
7795
7796 module := result.Module("libfoo", "android_common_com.android.myapex")
7797 usesLibraryDep := module.(java.UsesLibraryDependency)
7798 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7799 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7800 usesLibraryDep.DexJarBuildPath().Path())
7801 })
7802
7803 t.Run("java_sdk_library_import", func(t *testing.T) {
7804 result := preparers.RunTestWithBp(t,
7805 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7806 java_sdk_library_import {
7807 name: "libfoo",
7808 public: {
7809 jars: ["libbar.jar"],
7810 },
7811 apex_available: [
7812 "com.android.myapex",
7813 "com.android.myapex_compressed",
7814 ],
7815 compile_dex: true,
7816 }
7817 `)
7818
7819 module := result.Module("libfoo", "android_common_com.android.myapex")
7820 usesLibraryDep := module.(java.UsesLibraryDependency)
7821 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7822 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7823 usesLibraryDep.DexJarBuildPath().Path())
7824 })
7825
7826 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7827 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7828 image_name: "art",
7829 contents: ["libfoo"],
7830 `)+`
7831 java_sdk_library_import {
7832 name: "libfoo",
7833 public: {
7834 jars: ["libbar.jar"],
7835 },
7836 apex_available: [
7837 "com.android.myapex",
7838 "com.android.myapex_compressed",
7839 ],
7840 compile_dex: true,
7841 }
7842 `)
7843 })
7844}
7845
Jooyung Han548640b2020-04-27 12:10:30 +09007846func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7847 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7848 apex {
7849 name: "myapex",
7850 key: "myapex.key",
7851 updatable: true,
7852 }
7853
7854 apex_key {
7855 name: "myapex.key",
7856 public_key: "testkey.avbpubkey",
7857 private_key: "testkey.pem",
7858 }
7859 `)
7860}
7861
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007862func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7863 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7864 apex {
7865 name: "myapex",
7866 key: "myapex.key",
7867 }
7868
7869 apex_key {
7870 name: "myapex.key",
7871 public_key: "testkey.avbpubkey",
7872 private_key: "testkey.pem",
7873 }
7874 `)
7875}
7876
Daniel Norman69109112021-12-02 12:52:42 -08007877func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7878 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7879 apex {
7880 name: "myapex",
7881 key: "myapex.key",
7882 updatable: true,
7883 soc_specific: true,
7884 }
7885
7886 apex_key {
7887 name: "myapex.key",
7888 public_key: "testkey.avbpubkey",
7889 private_key: "testkey.pem",
7890 }
7891 `)
7892}
7893
satayevb98371c2021-06-15 16:49:50 +01007894func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7895 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7896 apex {
7897 name: "myapex",
7898 key: "myapex.key",
7899 systemserverclasspath_fragments: [
7900 "mysystemserverclasspathfragment",
7901 ],
7902 min_sdk_version: "29",
7903 updatable: true,
7904 }
7905
7906 apex_key {
7907 name: "myapex.key",
7908 public_key: "testkey.avbpubkey",
7909 private_key: "testkey.pem",
7910 }
7911
7912 java_library {
7913 name: "foo",
7914 srcs: ["b.java"],
7915 min_sdk_version: "29",
7916 installable: true,
7917 apex_available: [
7918 "myapex",
7919 ],
7920 }
7921
7922 systemserverclasspath_fragment {
7923 name: "mysystemserverclasspathfragment",
7924 generate_classpaths_proto: false,
7925 contents: [
7926 "foo",
7927 ],
7928 apex_available: [
7929 "myapex",
7930 ],
7931 }
satayevabcd5972021-08-06 17:49:46 +01007932 `,
7933 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7934 )
satayevb98371c2021-06-15 16:49:50 +01007935}
7936
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007937func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007938 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7939 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7940 // modules to be included in the BootJars.
7941 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7942 return android.GroupFixturePreparers(
7943 dexpreopt.FixtureSetBootJars(bootJars...),
7944 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7945 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7946 }),
7947 )
7948 }
7949
7950 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7951 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7952 // specified in the ArtApexJars configuration.
7953 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7954 return android.GroupFixturePreparers(
7955 dexpreopt.FixtureSetArtBootJars(bootJars...),
7956 dexpreopt.FixtureSetBootJars(bootJars...),
7957 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7958 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7959 }),
7960 )
7961 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007962
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007963 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007964 preparer := android.GroupFixturePreparers(
7965 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7966 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7967 )
7968 fragments := []java.ApexVariantReference{
7969 {
7970 Apex: proptools.StringPtr("com.android.art.debug"),
7971 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7972 },
7973 {
7974 Apex: proptools.StringPtr("some-non-updatable-apex"),
7975 Module: proptools.StringPtr("some-non-updatable-fragment"),
7976 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007977 }
satayevabcd5972021-08-06 17:49:46 +01007978 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007979 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007980
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007981 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007982 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7983 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007984 preparer := android.GroupFixturePreparers(
7985 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7986 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7987 )
Paul Duffin60264a02021-04-12 20:02:36 +01007988 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007989 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007990
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007991 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 +01007992 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 +01007993 // Update the dexpreopt ArtApexJars directly.
7994 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7995 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007996 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007997
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007998 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 +01007999 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 +01008000 // Update the dexpreopt ArtApexJars directly.
8001 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8002 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008003 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008004
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008005 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 +01008006 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 +01008007 preparer := android.GroupFixturePreparers(
8008 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8009 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8010 )
Paul Duffin60264a02021-04-12 20:02:36 +01008011 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008012 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008013
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008014 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 +01008015 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008016 fragment := java.ApexVariantReference{
8017 Apex: proptools.StringPtr("some-non-updatable-apex"),
8018 Module: proptools.StringPtr("some-non-updatable-fragment"),
8019 }
8020 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008021 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008022
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008023 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008024 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008025 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8026 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008027 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008028
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008029 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008030 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008031 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8032 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008033 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008034
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008035 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008036 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008037 // Update the dexpreopt ArtApexJars directly.
8038 preparer := prepareSetArtJars("platform:some-platform-lib")
8039 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008040 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008041
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008042 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008043 preparer := android.GroupFixturePreparers(
8044 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8045 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8046 )
8047 fragments := []java.ApexVariantReference{
8048 {
8049 Apex: proptools.StringPtr("some-non-updatable-apex"),
8050 Module: proptools.StringPtr("some-non-updatable-fragment"),
8051 },
8052 }
8053 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008054 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008055}
8056
8057func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008058 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008059 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008060 fragment := java.ApexVariantReference{
8061 Apex: proptools.StringPtr("myapex"),
8062 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8063 }
8064
Paul Duffin064b70c2020-11-02 17:32:38 +00008065 testDexpreoptWithApexes(t, `
8066 prebuilt_apex {
8067 name: "myapex" ,
8068 arch: {
8069 arm64: {
8070 src: "myapex-arm64.apex",
8071 },
8072 arm: {
8073 src: "myapex-arm.apex",
8074 },
8075 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008076 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8077 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008078
Paul Duffin89f570a2021-06-16 01:42:33 +01008079 prebuilt_bootclasspath_fragment {
8080 name: "my-bootclasspath-fragment",
8081 contents: ["libfoo"],
8082 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008083 hidden_api: {
8084 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8085 metadata: "my-bootclasspath-fragment/metadata.csv",
8086 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008087 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8088 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8089 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008090 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008091 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008092
Paul Duffin89f570a2021-06-16 01:42:33 +01008093 java_import {
8094 name: "libfoo",
8095 jars: ["libfoo.jar"],
8096 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008097 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008098 }
8099 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008100 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008101}
8102
Spandan Dasf14e2542021-11-12 00:01:37 +00008103func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008104 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008105 bp += `
8106 apex_key {
8107 name: "myapex.key",
8108 public_key: "testkey.avbpubkey",
8109 private_key: "testkey.pem",
8110 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008111 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008112 "lib1/src/A.java": nil,
8113 "lib2/src/B.java": nil,
8114 "system/sepolicy/apex/myapex-file_contexts": nil,
8115 }
8116
Paul Duffin45338f02021-03-30 23:07:52 +01008117 errorHandler := android.FixtureExpectsNoErrors
8118 if errmsg != "" {
8119 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008120 }
Colin Crossae8600b2020-10-29 17:09:13 -07008121
Paul Duffin45338f02021-03-30 23:07:52 +01008122 android.GroupFixturePreparers(
8123 android.PrepareForTestWithAndroidBuildComponents,
8124 java.PrepareForTestWithJavaBuildComponents,
8125 PrepareForTestWithApexBuildComponents,
8126 android.PrepareForTestWithNeverallowRules(rules),
8127 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008128 apexBootJars := make([]string, 0, len(bootJars))
8129 for _, apexBootJar := range bootJars {
8130 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008131 }
satayevd604b212021-07-21 14:23:52 +01008132 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008133 }),
8134 fs.AddToFixture(),
8135 ).
8136 ExtendWithErrorHandler(errorHandler).
8137 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008138}
8139
8140func TestApexPermittedPackagesRules(t *testing.T) {
8141 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008142 name string
8143 expectedError string
8144 bp string
8145 bootJars []string
8146 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008147 }{
8148
8149 {
8150 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8151 expectedError: "",
8152 bp: `
8153 java_library {
8154 name: "bcp_lib1",
8155 srcs: ["lib1/src/*.java"],
8156 permitted_packages: ["foo.bar"],
8157 apex_available: ["myapex"],
8158 sdk_version: "none",
8159 system_modules: "none",
8160 }
8161 java_library {
8162 name: "nonbcp_lib2",
8163 srcs: ["lib2/src/*.java"],
8164 apex_available: ["myapex"],
8165 permitted_packages: ["a.b"],
8166 sdk_version: "none",
8167 system_modules: "none",
8168 }
8169 apex {
8170 name: "myapex",
8171 key: "myapex.key",
8172 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008173 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008174 }`,
8175 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008176 bcpPermittedPackages: map[string][]string{
8177 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008178 "foo.bar",
8179 },
8180 },
8181 },
8182 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008183 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008184 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 +01008185 bp: `
8186 java_library {
8187 name: "bcp_lib1",
8188 srcs: ["lib1/src/*.java"],
8189 apex_available: ["myapex"],
8190 permitted_packages: ["foo.bar"],
8191 sdk_version: "none",
8192 system_modules: "none",
8193 }
8194 java_library {
8195 name: "bcp_lib2",
8196 srcs: ["lib2/src/*.java"],
8197 apex_available: ["myapex"],
8198 permitted_packages: ["foo.bar", "bar.baz"],
8199 sdk_version: "none",
8200 system_modules: "none",
8201 }
8202 apex {
8203 name: "myapex",
8204 key: "myapex.key",
8205 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008206 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008207 }
8208 `,
8209 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008210 bcpPermittedPackages: map[string][]string{
8211 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008212 "foo.bar",
8213 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008214 "bcp_lib2": []string{
8215 "foo.bar",
8216 },
8217 },
8218 },
8219 {
8220 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8221 expectedError: "",
8222 bp: `
8223 java_library {
8224 name: "bcp_lib_restricted",
8225 srcs: ["lib1/src/*.java"],
8226 apex_available: ["myapex"],
8227 permitted_packages: ["foo.bar"],
8228 sdk_version: "none",
8229 min_sdk_version: "29",
8230 system_modules: "none",
8231 }
8232 java_library {
8233 name: "bcp_lib_unrestricted",
8234 srcs: ["lib2/src/*.java"],
8235 apex_available: ["myapex"],
8236 permitted_packages: ["foo.bar", "bar.baz"],
8237 sdk_version: "none",
8238 min_sdk_version: "29",
8239 system_modules: "none",
8240 }
8241 apex {
8242 name: "myapex",
8243 key: "myapex.key",
8244 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8245 updatable: true,
8246 min_sdk_version: "29",
8247 }
8248 `,
8249 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8250 bcpPermittedPackages: map[string][]string{
8251 "bcp_lib1_non_updateable": []string{
8252 "foo.bar",
8253 },
8254 // 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 +01008255 },
8256 },
8257 }
8258 for _, tc := range testcases {
8259 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008260 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8261 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008262 })
8263 }
8264}
8265
Jiyong Park62304bb2020-04-13 16:19:48 +09008266func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008267 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008268 apex {
8269 name: "myapex",
8270 key: "myapex.key",
8271 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008272 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008273 }
8274
8275 apex_key {
8276 name: "myapex.key",
8277 public_key: "testkey.avbpubkey",
8278 private_key: "testkey.pem",
8279 }
8280
8281 cc_library {
8282 name: "mylib",
8283 srcs: ["mylib.cpp"],
8284 system_shared_libs: [],
8285 stl: "none",
8286 stubs: {
8287 versions: ["1"],
8288 },
8289 apex_available: ["myapex"],
8290 }
8291
8292 cc_library {
8293 name: "myprivlib",
8294 srcs: ["mylib.cpp"],
8295 system_shared_libs: [],
8296 stl: "none",
8297 apex_available: ["myapex"],
8298 }
8299
8300
8301 cc_test {
8302 name: "mytest",
8303 gtest: false,
8304 srcs: ["mylib.cpp"],
8305 system_shared_libs: [],
8306 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008307 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008308 test_for: ["myapex"]
8309 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008310
8311 cc_library {
8312 name: "mytestlib",
8313 srcs: ["mylib.cpp"],
8314 system_shared_libs: [],
8315 shared_libs: ["mylib", "myprivlib"],
8316 stl: "none",
8317 test_for: ["myapex"],
8318 }
8319
8320 cc_benchmark {
8321 name: "mybench",
8322 srcs: ["mylib.cpp"],
8323 system_shared_libs: [],
8324 shared_libs: ["mylib", "myprivlib"],
8325 stl: "none",
8326 test_for: ["myapex"],
8327 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008328 `)
8329
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008330 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008331 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008332 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8333 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8334 }
8335
8336 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008337 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008338 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8339 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8340 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8341}
Jiyong Park46a512f2020-12-04 18:02:13 +09008342
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008343func TestIndirectTestFor(t *testing.T) {
8344 ctx := testApex(t, `
8345 apex {
8346 name: "myapex",
8347 key: "myapex.key",
8348 native_shared_libs: ["mylib", "myprivlib"],
8349 updatable: false,
8350 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008351
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008352 apex_key {
8353 name: "myapex.key",
8354 public_key: "testkey.avbpubkey",
8355 private_key: "testkey.pem",
8356 }
8357
8358 cc_library {
8359 name: "mylib",
8360 srcs: ["mylib.cpp"],
8361 system_shared_libs: [],
8362 stl: "none",
8363 stubs: {
8364 versions: ["1"],
8365 },
8366 apex_available: ["myapex"],
8367 }
8368
8369 cc_library {
8370 name: "myprivlib",
8371 srcs: ["mylib.cpp"],
8372 system_shared_libs: [],
8373 stl: "none",
8374 shared_libs: ["mylib"],
8375 apex_available: ["myapex"],
8376 }
8377
8378 cc_library {
8379 name: "mytestlib",
8380 srcs: ["mylib.cpp"],
8381 system_shared_libs: [],
8382 shared_libs: ["myprivlib"],
8383 stl: "none",
8384 test_for: ["myapex"],
8385 }
8386 `)
8387
8388 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008389 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008390 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8391 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8392 }
8393
8394 // The platform variant of mytestlib links to the platform variant of the
8395 // internal myprivlib.
8396 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8397
8398 // The platform variant of myprivlib links to the platform variant of mylib
8399 // and bypasses its stubs.
8400 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 +09008401}
8402
Martin Stjernholmec009002021-03-27 15:18:31 +00008403func TestTestForForLibInOtherApex(t *testing.T) {
8404 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8405 _ = testApex(t, `
8406 apex {
8407 name: "com.android.art",
8408 key: "myapex.key",
8409 native_shared_libs: ["mylib"],
8410 updatable: false,
8411 }
8412
8413 apex {
8414 name: "com.android.art.debug",
8415 key: "myapex.key",
8416 native_shared_libs: ["mylib", "mytestlib"],
8417 updatable: false,
8418 }
8419
8420 apex_key {
8421 name: "myapex.key",
8422 public_key: "testkey.avbpubkey",
8423 private_key: "testkey.pem",
8424 }
8425
8426 cc_library {
8427 name: "mylib",
8428 srcs: ["mylib.cpp"],
8429 system_shared_libs: [],
8430 stl: "none",
8431 stubs: {
8432 versions: ["1"],
8433 },
8434 apex_available: ["com.android.art", "com.android.art.debug"],
8435 }
8436
8437 cc_library {
8438 name: "mytestlib",
8439 srcs: ["mylib.cpp"],
8440 system_shared_libs: [],
8441 shared_libs: ["mylib"],
8442 stl: "none",
8443 apex_available: ["com.android.art.debug"],
8444 test_for: ["com.android.art"],
8445 }
8446 `,
8447 android.MockFS{
8448 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8449 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8450 }.AddToFixture())
8451}
8452
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008453// TODO(jungjw): Move this to proptools
8454func intPtr(i int) *int {
8455 return &i
8456}
8457
8458func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008459 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008460 apex_set {
8461 name: "myapex",
8462 set: "myapex.apks",
8463 filename: "foo_v2.apex",
8464 overrides: ["foo"],
8465 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008466 `,
8467 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8468 variables.Platform_sdk_version = intPtr(30)
8469 }),
8470 android.FixtureModifyConfig(func(config android.Config) {
8471 config.Targets[android.Android] = []android.Target{
8472 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8473 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8474 }
8475 }),
8476 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008477
Paul Duffin24704672021-04-06 16:09:30 +01008478 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008479
8480 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008481 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008482 actual := extractedApex.Args["abis"]
8483 expected := "ARMEABI_V7A,ARM64_V8A"
8484 if actual != expected {
8485 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8486 }
8487 actual = extractedApex.Args["sdk-version"]
8488 expected = "30"
8489 if actual != expected {
8490 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8491 }
8492
Paul Duffin6717d882021-06-15 19:09:41 +01008493 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008494 a := m.Module().(*ApexSet)
8495 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008496 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008497 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8498 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8499 }
8500}
8501
Anton Hansson805e0a52022-11-25 14:06:46 +00008502func TestApexSet_NativeBridge(t *testing.T) {
8503 ctx := testApex(t, `
8504 apex_set {
8505 name: "myapex",
8506 set: "myapex.apks",
8507 filename: "foo_v2.apex",
8508 overrides: ["foo"],
8509 }
8510 `,
8511 android.FixtureModifyConfig(func(config android.Config) {
8512 config.Targets[android.Android] = []android.Target{
8513 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8514 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8515 }
8516 }),
8517 )
8518
8519 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8520
8521 // Check extract_apks tool parameters. No native bridge arch expected
8522 extractedApex := m.Output("extracted/myapex.apks")
8523 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8524}
8525
Jiyong Park7d95a512020-05-10 15:16:24 +09008526func TestNoStaticLinkingToStubsLib(t *testing.T) {
8527 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8528 apex {
8529 name: "myapex",
8530 key: "myapex.key",
8531 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008532 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008533 }
8534
8535 apex_key {
8536 name: "myapex.key",
8537 public_key: "testkey.avbpubkey",
8538 private_key: "testkey.pem",
8539 }
8540
8541 cc_library {
8542 name: "mylib",
8543 srcs: ["mylib.cpp"],
8544 static_libs: ["otherlib"],
8545 system_shared_libs: [],
8546 stl: "none",
8547 apex_available: [ "myapex" ],
8548 }
8549
8550 cc_library {
8551 name: "otherlib",
8552 srcs: ["mylib.cpp"],
8553 system_shared_libs: [],
8554 stl: "none",
8555 stubs: {
8556 versions: ["1", "2", "3"],
8557 },
8558 apex_available: [ "myapex" ],
8559 }
8560 `)
8561}
8562
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008563func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008564 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008565 apex {
8566 name: "myapex",
8567 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008568 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008569 custom_sign_tool: "sign_myapex",
8570 }
8571
8572 apex_key {
8573 name: "myapex.key",
8574 public_key: "testkey.avbpubkey",
8575 private_key: "testkey.pem",
8576 }
8577 `)
8578
8579 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8580 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8581 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"`)
8582}
8583
8584func TestApexKeysTxtOverrides(t *testing.T) {
8585 ctx := testApex(t, `
8586 apex {
8587 name: "myapex",
8588 key: "myapex.key",
8589 updatable: false,
8590 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008591 }
8592
8593 apex_key {
8594 name: "myapex.key",
8595 public_key: "testkey.avbpubkey",
8596 private_key: "testkey.pem",
8597 }
8598
8599 prebuilt_apex {
8600 name: "myapex",
8601 prefer: true,
8602 arch: {
8603 arm64: {
8604 src: "myapex-arm64.apex",
8605 },
8606 arm: {
8607 src: "myapex-arm.apex",
8608 },
8609 },
8610 }
8611
8612 apex_set {
8613 name: "myapex_set",
8614 set: "myapex.apks",
8615 filename: "myapex_set.apex",
8616 overrides: ["myapex"],
8617 }
8618 `)
8619
8620 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8621 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8622 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 +09008623 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 +09008624}
8625
Jooyung Han938b5932020-06-20 12:47:47 +09008626func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008627 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008628 apex {
8629 name: "myapex",
8630 key: "myapex.key",
8631 apps: ["app"],
8632 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008633 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008634 }
8635
8636 apex_key {
8637 name: "myapex.key",
8638 public_key: "testkey.avbpubkey",
8639 private_key: "testkey.pem",
8640 }
8641
8642 android_app {
8643 name: "app",
8644 srcs: ["foo/bar/MyClass.java"],
8645 package_name: "foo",
8646 sdk_version: "none",
8647 system_modules: "none",
8648 apex_available: [ "myapex" ],
8649 }
8650 `, withFiles(map[string][]byte{
8651 "sub/Android.bp": []byte(`
8652 override_apex {
8653 name: "override_myapex",
8654 base: "myapex",
8655 apps: ["override_app"],
8656 allowed_files: ":allowed",
8657 }
8658 // Overridable "path" property should be referenced indirectly
8659 filegroup {
8660 name: "allowed",
8661 srcs: ["allowed.txt"],
8662 }
8663 override_android_app {
8664 name: "override_app",
8665 base: "app",
8666 package_name: "bar",
8667 }
8668 `),
8669 }))
8670
8671 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8672 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8673 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8674 }
8675
8676 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8677 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8678 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8679 }
8680}
8681
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008682func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008683 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008684 apex {
8685 name: "myapex",
8686 key: "myapex.key",
8687 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008688 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008689 }
8690
8691 apex_key {
8692 name: "myapex.key",
8693 public_key: "testkey.avbpubkey",
8694 private_key: "testkey.pem",
8695 }
8696
8697 cc_library {
8698 name: "mylib",
8699 srcs: ["mylib.cpp"],
8700 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008701 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008702 },
8703 apex_available: ["myapex"],
8704 }
8705
8706 cc_prebuilt_library_shared {
8707 name: "mylib",
8708 prefer: false,
8709 srcs: ["prebuilt.so"],
8710 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008711 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008712 },
8713 apex_available: ["myapex"],
8714 }
8715 `)
8716}
8717
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008718func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008719 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008720 apex {
8721 name: "myapex",
8722 key: "myapex.key",
8723 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008724 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008725 }
8726 apex_key {
8727 name: "myapex.key",
8728 public_key: "testkey.avbpubkey",
8729 private_key: "testkey.pem",
8730 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008731 `,
8732 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8733 variables.CompressedApex = proptools.BoolPtr(true)
8734 }),
8735 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008736
8737 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8738 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8739
8740 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8741 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8742
8743 // Make sure output of bundle is .capex
8744 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8745 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8746
8747 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008748 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008749 var builder strings.Builder
8750 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8751 androidMk := builder.String()
8752 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8753}
8754
Martin Stjernholm2856c662020-12-02 15:03:42 +00008755func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008756 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008757 apex {
8758 name: "myapex",
8759 key: "myapex.key",
8760 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008761 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008762 }
8763
8764 apex_key {
8765 name: "myapex.key",
8766 public_key: "testkey.avbpubkey",
8767 private_key: "testkey.pem",
8768 }
8769
8770 cc_library {
8771 name: "mylib",
8772 srcs: ["mylib.cpp"],
8773 apex_available: ["myapex"],
8774 shared_libs: ["otherlib"],
8775 system_shared_libs: [],
8776 }
8777
8778 cc_library {
8779 name: "otherlib",
8780 srcs: ["mylib.cpp"],
8781 stubs: {
8782 versions: ["current"],
8783 },
8784 }
8785
8786 cc_prebuilt_library_shared {
8787 name: "otherlib",
8788 prefer: true,
8789 srcs: ["prebuilt.so"],
8790 stubs: {
8791 versions: ["current"],
8792 },
8793 }
8794 `)
8795
8796 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008797 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008798 var builder strings.Builder
8799 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8800 androidMk := builder.String()
8801
8802 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8803 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008804 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 +00008805}
8806
Jiyong Parke3867542020-12-03 17:28:25 +09008807func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008808 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008809 apex {
8810 name: "myapex",
8811 key: "myapex.key",
8812 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008813 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008814 }
8815
8816 apex_key {
8817 name: "myapex.key",
8818 public_key: "testkey.avbpubkey",
8819 private_key: "testkey.pem",
8820 }
8821
8822 cc_library {
8823 name: "mylib",
8824 srcs: ["mylib.cpp"],
8825 system_shared_libs: [],
8826 stl: "none",
8827 apex_available: ["myapex"],
8828 shared_libs: ["mylib2"],
8829 target: {
8830 apex: {
8831 exclude_shared_libs: ["mylib2"],
8832 },
8833 },
8834 }
8835
8836 cc_library {
8837 name: "mylib2",
8838 srcs: ["mylib.cpp"],
8839 system_shared_libs: [],
8840 stl: "none",
8841 }
8842 `)
8843
8844 // Check if mylib is linked to mylib2 for the non-apex target
8845 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8846 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8847
8848 // Make sure that the link doesn't occur for the apex target
8849 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8850 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8851
8852 // It shouldn't appear in the copy cmd as well.
8853 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8854 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8855}
8856
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008857func TestPrebuiltStubLibDep(t *testing.T) {
8858 bpBase := `
8859 apex {
8860 name: "myapex",
8861 key: "myapex.key",
8862 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008863 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008864 }
8865 apex_key {
8866 name: "myapex.key",
8867 public_key: "testkey.avbpubkey",
8868 private_key: "testkey.pem",
8869 }
8870 cc_library {
8871 name: "mylib",
8872 srcs: ["mylib.cpp"],
8873 apex_available: ["myapex"],
8874 shared_libs: ["stublib"],
8875 system_shared_libs: [],
8876 }
8877 apex {
8878 name: "otherapex",
8879 enabled: %s,
8880 key: "myapex.key",
8881 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008882 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008883 }
8884 `
8885
8886 stublibSourceBp := `
8887 cc_library {
8888 name: "stublib",
8889 srcs: ["mylib.cpp"],
8890 apex_available: ["otherapex"],
8891 system_shared_libs: [],
8892 stl: "none",
8893 stubs: {
8894 versions: ["1"],
8895 },
8896 }
8897 `
8898
8899 stublibPrebuiltBp := `
8900 cc_prebuilt_library_shared {
8901 name: "stublib",
8902 srcs: ["prebuilt.so"],
8903 apex_available: ["otherapex"],
8904 stubs: {
8905 versions: ["1"],
8906 },
8907 %s
8908 }
8909 `
8910
8911 tests := []struct {
8912 name string
8913 stublibBp string
8914 usePrebuilt bool
8915 modNames []string // Modules to collect AndroidMkEntries for
8916 otherApexEnabled []string
8917 }{
8918 {
8919 name: "only_source",
8920 stublibBp: stublibSourceBp,
8921 usePrebuilt: false,
8922 modNames: []string{"stublib"},
8923 otherApexEnabled: []string{"true", "false"},
8924 },
8925 {
8926 name: "source_preferred",
8927 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8928 usePrebuilt: false,
8929 modNames: []string{"stublib", "prebuilt_stublib"},
8930 otherApexEnabled: []string{"true", "false"},
8931 },
8932 {
8933 name: "prebuilt_preferred",
8934 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8935 usePrebuilt: true,
8936 modNames: []string{"stublib", "prebuilt_stublib"},
8937 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8938 },
8939 {
8940 name: "only_prebuilt",
8941 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8942 usePrebuilt: true,
8943 modNames: []string{"stublib"},
8944 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8945 },
8946 }
8947
8948 for _, test := range tests {
8949 t.Run(test.name, func(t *testing.T) {
8950 for _, otherApexEnabled := range test.otherApexEnabled {
8951 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008952 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008953
8954 type modAndMkEntries struct {
8955 mod *cc.Module
8956 mkEntries android.AndroidMkEntries
8957 }
8958 entries := []*modAndMkEntries{}
8959
8960 // Gather shared lib modules that are installable
8961 for _, modName := range test.modNames {
8962 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8963 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8964 continue
8965 }
8966 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008967 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008968 continue
8969 }
Colin Crossaa255532020-07-03 13:18:24 -07008970 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008971 if ent.Disabled {
8972 continue
8973 }
8974 entries = append(entries, &modAndMkEntries{
8975 mod: mod,
8976 mkEntries: ent,
8977 })
8978 }
8979 }
8980 }
8981
8982 var entry *modAndMkEntries = nil
8983 for _, ent := range entries {
8984 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8985 if entry != nil {
8986 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8987 } else {
8988 entry = ent
8989 }
8990 }
8991 }
8992
8993 if entry == nil {
8994 t.Errorf("AndroidMk entry for \"stublib\" missing")
8995 } else {
8996 isPrebuilt := entry.mod.Prebuilt() != nil
8997 if isPrebuilt != test.usePrebuilt {
8998 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8999 }
9000 if !entry.mod.IsStubs() {
9001 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9002 }
9003 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9004 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9005 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009006 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009007 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009008 if !android.InList(expected, cflags) {
9009 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9010 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009011 }
9012 })
9013 }
9014 })
9015 }
9016}
9017
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009018func TestHostApexInHostOnlyBuild(t *testing.T) {
9019 testApex(t, `
9020 apex {
9021 name: "myapex",
9022 host_supported: true,
9023 key: "myapex.key",
9024 updatable: false,
9025 payload_type: "zip",
9026 }
9027 apex_key {
9028 name: "myapex.key",
9029 public_key: "testkey.avbpubkey",
9030 private_key: "testkey.pem",
9031 }
9032 `,
9033 android.FixtureModifyConfig(func(config android.Config) {
9034 // We may not have device targets in all builds, e.g. in
9035 // prebuilts/build-tools/build-prebuilts.sh
9036 config.Targets[android.Android] = []android.Target{}
9037 }))
9038}
9039
Colin Crossc33e5212021-05-25 18:16:02 -07009040func TestApexJavaCoverage(t *testing.T) {
9041 bp := `
9042 apex {
9043 name: "myapex",
9044 key: "myapex.key",
9045 java_libs: ["mylib"],
9046 bootclasspath_fragments: ["mybootclasspathfragment"],
9047 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9048 updatable: false,
9049 }
9050
9051 apex_key {
9052 name: "myapex.key",
9053 public_key: "testkey.avbpubkey",
9054 private_key: "testkey.pem",
9055 }
9056
9057 java_library {
9058 name: "mylib",
9059 srcs: ["mylib.java"],
9060 apex_available: ["myapex"],
9061 compile_dex: true,
9062 }
9063
9064 bootclasspath_fragment {
9065 name: "mybootclasspathfragment",
9066 contents: ["mybootclasspathlib"],
9067 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009068 hidden_api: {
9069 split_packages: ["*"],
9070 },
Colin Crossc33e5212021-05-25 18:16:02 -07009071 }
9072
9073 java_library {
9074 name: "mybootclasspathlib",
9075 srcs: ["mybootclasspathlib.java"],
9076 apex_available: ["myapex"],
9077 compile_dex: true,
9078 }
9079
9080 systemserverclasspath_fragment {
9081 name: "mysystemserverclasspathfragment",
9082 contents: ["mysystemserverclasspathlib"],
9083 apex_available: ["myapex"],
9084 }
9085
9086 java_library {
9087 name: "mysystemserverclasspathlib",
9088 srcs: ["mysystemserverclasspathlib.java"],
9089 apex_available: ["myapex"],
9090 compile_dex: true,
9091 }
9092 `
9093
9094 result := android.GroupFixturePreparers(
9095 PrepareForTestWithApexBuildComponents,
9096 prepareForTestWithMyapex,
9097 java.PrepareForTestWithJavaDefaultModules,
9098 android.PrepareForTestWithAndroidBuildComponents,
9099 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009100 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9101 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009102 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009103 ).RunTest(t)
9104
9105 // Make sure jacoco ran on both mylib and mybootclasspathlib
9106 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9107 t.Errorf("Failed to find jacoco rule for mylib")
9108 }
9109 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9110 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9111 }
9112 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9113 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9114 }
9115}
9116
Jiyong Park192600a2021-08-03 07:52:17 +00009117func TestProhibitStaticExecutable(t *testing.T) {
9118 testApexError(t, `executable mybin is static`, `
9119 apex {
9120 name: "myapex",
9121 key: "myapex.key",
9122 binaries: ["mybin"],
9123 min_sdk_version: "29",
9124 }
9125
9126 apex_key {
9127 name: "myapex.key",
9128 public_key: "testkey.avbpubkey",
9129 private_key: "testkey.pem",
9130 }
9131
9132 cc_binary {
9133 name: "mybin",
9134 srcs: ["mylib.cpp"],
9135 relative_install_path: "foo/bar",
9136 static_executable: true,
9137 system_shared_libs: [],
9138 stl: "none",
9139 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009140 min_sdk_version: "29",
9141 }
9142 `)
9143
9144 testApexError(t, `executable mybin.rust is static`, `
9145 apex {
9146 name: "myapex",
9147 key: "myapex.key",
9148 binaries: ["mybin.rust"],
9149 min_sdk_version: "29",
9150 }
9151
9152 apex_key {
9153 name: "myapex.key",
9154 public_key: "testkey.avbpubkey",
9155 private_key: "testkey.pem",
9156 }
9157
9158 rust_binary {
9159 name: "mybin.rust",
9160 srcs: ["foo.rs"],
9161 static_executable: true,
9162 apex_available: ["myapex"],
9163 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009164 }
9165 `)
9166}
9167
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009168func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9169 ctx := testApex(t, `
9170 apex {
9171 name: "myapex",
9172 key: "myapex.key",
9173 updatable: false,
9174 java_libs: ["foo"],
9175 }
9176
9177 apex_key {
9178 name: "myapex.key",
9179 public_key: "testkey.avbpubkey",
9180 private_key: "testkey.pem",
9181 }
9182
9183 java_library {
9184 name: "foo",
9185 srcs: ["foo.java"],
9186 apex_available: ["myapex"],
9187 installable: true,
9188 }
9189 `,
9190 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9191 )
9192
9193 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9194 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9195 var builder strings.Builder
9196 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9197 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009198 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 +00009199}
9200
9201func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9202 ctx := testApex(t, `
9203 prebuilt_apex {
9204 name: "myapex",
9205 arch: {
9206 arm64: {
9207 src: "myapex-arm64.apex",
9208 },
9209 arm: {
9210 src: "myapex-arm.apex",
9211 },
9212 },
9213 exported_java_libs: ["foo"],
9214 }
9215
9216 java_import {
9217 name: "foo",
9218 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009219 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009220 }
9221 `,
9222 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9223 )
9224
9225 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9226 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9227 mainModuleEntries := entriesList[0]
9228 android.AssertArrayString(t,
9229 "LOCAL_REQUIRED_MODULES",
9230 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9231 []string{
9232 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9233 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9234 })
9235}
9236
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009237func TestAndroidMk_RequiredModules(t *testing.T) {
9238 ctx := testApex(t, `
9239 apex {
9240 name: "myapex",
9241 key: "myapex.key",
9242 updatable: false,
9243 java_libs: ["foo"],
9244 required: ["otherapex"],
9245 }
9246
9247 apex {
9248 name: "otherapex",
9249 key: "myapex.key",
9250 updatable: false,
9251 java_libs: ["foo"],
9252 required: ["otherapex"],
9253 }
9254
9255 apex_key {
9256 name: "myapex.key",
9257 public_key: "testkey.avbpubkey",
9258 private_key: "testkey.pem",
9259 }
9260
9261 java_library {
9262 name: "foo",
9263 srcs: ["foo.java"],
9264 apex_available: ["myapex", "otherapex"],
9265 installable: true,
9266 }
9267 `)
9268
9269 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9270 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9271 var builder strings.Builder
9272 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9273 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009274 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009275}
9276
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009277func TestAndroidMk_RequiredDeps(t *testing.T) {
9278 ctx := testApex(t, `
9279 apex {
9280 name: "myapex",
9281 key: "myapex.key",
9282 updatable: false,
9283 }
9284
9285 apex_key {
9286 name: "myapex.key",
9287 public_key: "testkey.avbpubkey",
9288 private_key: "testkey.pem",
9289 }
9290 `)
9291
9292 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009293 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009294 data := android.AndroidMkDataForTest(t, ctx, bundle)
9295 var builder strings.Builder
9296 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9297 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009298 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009299
9300 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009301 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009302 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9303 var flattenedBuilder strings.Builder
9304 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9305 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009306 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009307}
9308
Jooyung Hana6d36672022-02-24 13:58:07 +09009309func TestApexOutputFileProducer(t *testing.T) {
9310 for _, tc := range []struct {
9311 name string
9312 ref string
9313 expected_data []string
9314 }{
9315 {
9316 name: "test_using_output",
9317 ref: ":myapex",
9318 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9319 },
9320 {
9321 name: "test_using_apex",
9322 ref: ":myapex{.apex}",
9323 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9324 },
9325 } {
9326 t.Run(tc.name, func(t *testing.T) {
9327 ctx := testApex(t, `
9328 apex {
9329 name: "myapex",
9330 key: "myapex.key",
9331 compressible: true,
9332 updatable: false,
9333 }
9334
9335 apex_key {
9336 name: "myapex.key",
9337 public_key: "testkey.avbpubkey",
9338 private_key: "testkey.pem",
9339 }
9340
9341 java_test {
9342 name: "`+tc.name+`",
9343 srcs: ["a.java"],
9344 data: ["`+tc.ref+`"],
9345 }
9346 `,
9347 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9348 variables.CompressedApex = proptools.BoolPtr(true)
9349 }))
9350 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9351 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9352 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9353 })
9354 }
9355}
9356
satayev758968a2021-12-06 11:42:40 +00009357func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9358 preparer := android.GroupFixturePreparers(
9359 PrepareForTestWithApexBuildComponents,
9360 prepareForTestWithMyapex,
9361 java.PrepareForTestWithJavaSdkLibraryFiles,
9362 java.PrepareForTestWithJavaDefaultModules,
9363 android.PrepareForTestWithAndroidBuildComponents,
9364 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9365 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9366 )
9367
9368 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9369 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9370 preparer.RunTestWithBp(t, `
9371 apex {
9372 name: "myapex",
9373 key: "myapex.key",
9374 bootclasspath_fragments: ["mybootclasspathfragment"],
9375 min_sdk_version: "30",
9376 updatable: false,
9377 }
9378
9379 apex_key {
9380 name: "myapex.key",
9381 public_key: "testkey.avbpubkey",
9382 private_key: "testkey.pem",
9383 }
9384
9385 bootclasspath_fragment {
9386 name: "mybootclasspathfragment",
9387 contents: ["mybootclasspathlib"],
9388 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009389 hidden_api: {
9390 split_packages: ["*"],
9391 },
satayev758968a2021-12-06 11:42:40 +00009392 }
9393
9394 java_sdk_library {
9395 name: "mybootclasspathlib",
9396 srcs: ["mybootclasspathlib.java"],
9397 apex_available: ["myapex"],
9398 compile_dex: true,
9399 unsafe_ignore_missing_latest_api: true,
9400 min_sdk_version: "31",
9401 static_libs: ["util"],
9402 }
9403
9404 java_library {
9405 name: "util",
9406 srcs: ["a.java"],
9407 apex_available: ["myapex"],
9408 min_sdk_version: "31",
9409 static_libs: ["another_util"],
9410 }
9411
9412 java_library {
9413 name: "another_util",
9414 srcs: ["a.java"],
9415 min_sdk_version: "31",
9416 apex_available: ["myapex"],
9417 }
9418 `)
9419 })
9420
9421 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9422 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9423 preparer.RunTestWithBp(t, `
9424 apex {
9425 name: "myapex",
9426 key: "myapex.key",
9427 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9428 min_sdk_version: "30",
9429 updatable: false,
9430 }
9431
9432 apex_key {
9433 name: "myapex.key",
9434 public_key: "testkey.avbpubkey",
9435 private_key: "testkey.pem",
9436 }
9437
9438 systemserverclasspath_fragment {
9439 name: "mysystemserverclasspathfragment",
9440 contents: ["mysystemserverclasspathlib"],
9441 apex_available: ["myapex"],
9442 }
9443
9444 java_sdk_library {
9445 name: "mysystemserverclasspathlib",
9446 srcs: ["mysystemserverclasspathlib.java"],
9447 apex_available: ["myapex"],
9448 compile_dex: true,
9449 min_sdk_version: "32",
9450 unsafe_ignore_missing_latest_api: true,
9451 static_libs: ["util"],
9452 }
9453
9454 java_library {
9455 name: "util",
9456 srcs: ["a.java"],
9457 apex_available: ["myapex"],
9458 min_sdk_version: "31",
9459 static_libs: ["another_util"],
9460 }
9461
9462 java_library {
9463 name: "another_util",
9464 srcs: ["a.java"],
9465 min_sdk_version: "31",
9466 apex_available: ["myapex"],
9467 }
9468 `)
9469 })
9470
9471 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9472 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9473 RunTestWithBp(t, `
9474 apex {
9475 name: "myapex",
9476 key: "myapex.key",
9477 bootclasspath_fragments: ["mybootclasspathfragment"],
9478 min_sdk_version: "30",
9479 updatable: false,
9480 }
9481
9482 apex_key {
9483 name: "myapex.key",
9484 public_key: "testkey.avbpubkey",
9485 private_key: "testkey.pem",
9486 }
9487
9488 bootclasspath_fragment {
9489 name: "mybootclasspathfragment",
9490 contents: ["mybootclasspathlib"],
9491 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009492 hidden_api: {
9493 split_packages: ["*"],
9494 },
satayev758968a2021-12-06 11:42:40 +00009495 }
9496
9497 java_sdk_library {
9498 name: "mybootclasspathlib",
9499 srcs: ["mybootclasspathlib.java"],
9500 apex_available: ["myapex"],
9501 compile_dex: true,
9502 unsafe_ignore_missing_latest_api: true,
9503 }
9504 `)
9505 })
9506
9507 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9508 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9509 RunTestWithBp(t, `
9510 apex {
9511 name: "myapex",
9512 key: "myapex.key",
9513 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9514 min_sdk_version: "30",
9515 updatable: false,
9516 }
9517
9518 apex_key {
9519 name: "myapex.key",
9520 public_key: "testkey.avbpubkey",
9521 private_key: "testkey.pem",
9522 }
9523
9524 systemserverclasspath_fragment {
9525 name: "mysystemserverclasspathfragment",
9526 contents: ["mysystemserverclasspathlib"],
9527 apex_available: ["myapex"],
9528 }
9529
9530 java_sdk_library {
9531 name: "mysystemserverclasspathlib",
9532 srcs: ["mysystemserverclasspathlib.java"],
9533 apex_available: ["myapex"],
9534 compile_dex: true,
9535 unsafe_ignore_missing_latest_api: true,
9536 }
9537 `)
9538 })
9539}
9540
Jiakai Zhang6decef92022-01-12 17:56:19 +00009541// Verifies that the APEX depends on all the Make modules in the list.
9542func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9543 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9544 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009545 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009546 }
9547}
9548
9549// Verifies that the APEX does not depend on any of the Make modules in the list.
9550func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9551 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9552 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009553 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009554 }
9555}
9556
Spandan Das66773252022-01-15 00:23:18 +00009557func TestApexStrictUpdtabilityLint(t *testing.T) {
9558 bpTemplate := `
9559 apex {
9560 name: "myapex",
9561 key: "myapex.key",
9562 java_libs: ["myjavalib"],
9563 updatable: %v,
9564 min_sdk_version: "29",
9565 }
9566 apex_key {
9567 name: "myapex.key",
9568 }
9569 java_library {
9570 name: "myjavalib",
9571 srcs: ["MyClass.java"],
9572 apex_available: [ "myapex" ],
9573 lint: {
9574 strict_updatability_linting: %v,
9575 },
9576 sdk_version: "current",
9577 min_sdk_version: "29",
9578 }
9579 `
9580 fs := android.MockFS{
9581 "lint-baseline.xml": nil,
9582 }
9583
9584 testCases := []struct {
9585 testCaseName string
9586 apexUpdatable bool
9587 javaStrictUpdtabilityLint bool
9588 lintFileExists bool
9589 disallowedFlagExpected bool
9590 }{
9591 {
9592 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9593 apexUpdatable: true,
9594 javaStrictUpdtabilityLint: true,
9595 lintFileExists: false,
9596 disallowedFlagExpected: false,
9597 },
9598 {
9599 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9600 apexUpdatable: false,
9601 javaStrictUpdtabilityLint: false,
9602 lintFileExists: true,
9603 disallowedFlagExpected: false,
9604 },
9605 {
9606 testCaseName: "non-updatable apex respects strict updatability of javalib",
9607 apexUpdatable: false,
9608 javaStrictUpdtabilityLint: true,
9609 lintFileExists: true,
9610 disallowedFlagExpected: true,
9611 },
9612 {
9613 testCaseName: "updatable apex sets strict updatability of javalib to true",
9614 apexUpdatable: true,
9615 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9616 lintFileExists: true,
9617 disallowedFlagExpected: true,
9618 },
9619 }
9620
9621 for _, testCase := range testCases {
9622 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9623 fixtures := []android.FixturePreparer{}
9624 if testCase.lintFileExists {
9625 fixtures = append(fixtures, fs.AddToFixture())
9626 }
9627
9628 result := testApex(t, bp, fixtures...)
9629 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9630 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9631 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9632
9633 if disallowedFlagActual != testCase.disallowedFlagExpected {
9634 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9635 }
9636 }
9637}
9638
Spandan Dasd9c23ab2022-02-10 02:34:13 +00009639func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9640 bp := `
9641 apex {
9642 name: "myapex",
9643 key: "myapex.key",
9644 java_libs: ["myjavalib"],
9645 updatable: true,
9646 min_sdk_version: "29",
9647 }
9648 apex_key {
9649 name: "myapex.key",
9650 }
9651 java_library {
9652 name: "myjavalib",
9653 srcs: ["MyClass.java"],
9654 apex_available: [ "myapex" ],
9655 sdk_version: "current",
9656 min_sdk_version: "29",
9657 }
9658 `
9659
9660 testCases := []struct {
9661 testCaseName string
9662 moduleDirectory string
9663 disallowedFlagExpected bool
9664 }{
9665 {
9666 testCaseName: "lintable module defined outside libcore",
9667 moduleDirectory: "",
9668 disallowedFlagExpected: true,
9669 },
9670 {
9671 testCaseName: "lintable module defined in libcore root directory",
9672 moduleDirectory: "libcore/",
9673 disallowedFlagExpected: false,
9674 },
9675 {
9676 testCaseName: "lintable module defined in libcore child directory",
9677 moduleDirectory: "libcore/childdir/",
9678 disallowedFlagExpected: true,
9679 },
9680 }
9681
9682 for _, testCase := range testCases {
9683 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9684 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9685 result := testApex(t, "", lintFileCreator, bpFileCreator)
9686 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9687 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9688 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9689 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9690
9691 if disallowedFlagActual != testCase.disallowedFlagExpected {
9692 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9693 }
9694 }
9695}
9696
Spandan Das66773252022-01-15 00:23:18 +00009697// checks transtive deps of an apex coming from bootclasspath_fragment
9698func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9699 bp := `
9700 apex {
9701 name: "myapex",
9702 key: "myapex.key",
9703 bootclasspath_fragments: ["mybootclasspathfragment"],
9704 updatable: true,
9705 min_sdk_version: "29",
9706 }
9707 apex_key {
9708 name: "myapex.key",
9709 }
9710 bootclasspath_fragment {
9711 name: "mybootclasspathfragment",
9712 contents: ["myjavalib"],
9713 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009714 hidden_api: {
9715 split_packages: ["*"],
9716 },
Spandan Das66773252022-01-15 00:23:18 +00009717 }
9718 java_library {
9719 name: "myjavalib",
9720 srcs: ["MyClass.java"],
9721 apex_available: [ "myapex" ],
9722 sdk_version: "current",
9723 min_sdk_version: "29",
9724 compile_dex: true,
9725 }
9726 `
9727 fs := android.MockFS{
9728 "lint-baseline.xml": nil,
9729 }
9730
9731 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9732 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9733 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9734 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9735 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9736 }
9737}
9738
Spandan Das42e89502022-05-06 22:12:55 +00009739// updatable apexes should propagate updatable=true to its apps
9740func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9741 bp := `
9742 apex {
9743 name: "myapex",
9744 key: "myapex.key",
9745 updatable: %v,
9746 apps: [
9747 "myapp",
9748 ],
9749 min_sdk_version: "30",
9750 }
9751 apex_key {
9752 name: "myapex.key",
9753 }
9754 android_app {
9755 name: "myapp",
9756 updatable: %v,
9757 apex_available: [
9758 "myapex",
9759 ],
9760 sdk_version: "current",
9761 min_sdk_version: "30",
9762 }
9763 `
9764 testCases := []struct {
9765 name string
9766 apex_is_updatable_bp bool
9767 app_is_updatable_bp bool
9768 app_is_updatable_expected bool
9769 }{
9770 {
9771 name: "Non-updatable apex respects updatable property of non-updatable app",
9772 apex_is_updatable_bp: false,
9773 app_is_updatable_bp: false,
9774 app_is_updatable_expected: false,
9775 },
9776 {
9777 name: "Non-updatable apex respects updatable property of updatable app",
9778 apex_is_updatable_bp: false,
9779 app_is_updatable_bp: true,
9780 app_is_updatable_expected: true,
9781 },
9782 {
9783 name: "Updatable apex respects updatable property of updatable app",
9784 apex_is_updatable_bp: true,
9785 app_is_updatable_bp: true,
9786 app_is_updatable_expected: true,
9787 },
9788 {
9789 name: "Updatable apex sets updatable=true on non-updatable app",
9790 apex_is_updatable_bp: true,
9791 app_is_updatable_bp: false,
9792 app_is_updatable_expected: true,
9793 },
9794 }
9795 for _, testCase := range testCases {
9796 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9797 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9798 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9799 }
9800}
9801
Kiyoung Kim487689e2022-07-26 09:48:22 +09009802func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9803 bp := `
9804 apex {
9805 name: "myapex",
9806 key: "myapex.key",
9807 native_shared_libs: ["libfoo"],
9808 min_sdk_version: "29",
9809 }
9810 apex_key {
9811 name: "myapex.key",
9812 }
9813 cc_library {
9814 name: "libfoo",
9815 shared_libs: ["libc"],
9816 apex_available: ["myapex"],
9817 min_sdk_version: "29",
9818 }
9819 cc_api_library {
9820 name: "libc",
9821 src: "libc.so",
9822 min_sdk_version: "29",
9823 recovery_available: true,
9824 }
9825 api_imports {
9826 name: "api_imports",
9827 shared_libs: [
9828 "libc",
9829 ],
9830 header_libs: [],
9831 }
9832 `
9833 result := testApex(t, bp)
9834
9835 hasDep := func(m android.Module, wantDep android.Module) bool {
9836 t.Helper()
9837 var found bool
9838 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9839 if dep == wantDep {
9840 found = true
9841 }
9842 })
9843 return found
9844 }
9845
9846 libfooApexVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex29").Module()
9847 libcApexVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared_apex29").Module()
9848
9849 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(libfooApexVariant, libcApexVariant))
9850
9851 // libfoo core variant should be buildable in the same inner tree since
9852 // certain mcombo files might build system and apexes in the same inner tree
9853 // libfoo core variant should link against source libc
9854 libfooCoreVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
9855 libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
9856 android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
9857}
Dennis Shend4f5d932023-01-31 20:27:21 +00009858
9859func TestTrimmedApex(t *testing.T) {
9860 bp := `
9861 apex {
9862 name: "myapex",
9863 key: "myapex.key",
9864 native_shared_libs: ["libfoo","libbaz"],
9865 min_sdk_version: "29",
9866 trim_against: "mydcla",
9867 }
9868 apex {
9869 name: "mydcla",
9870 key: "myapex.key",
9871 native_shared_libs: ["libfoo","libbar"],
9872 min_sdk_version: "29",
9873 file_contexts: ":myapex-file_contexts",
9874 dynamic_common_lib_apex: true,
9875 }
9876 apex_key {
9877 name: "myapex.key",
9878 }
9879 cc_library {
9880 name: "libfoo",
9881 shared_libs: ["libc"],
9882 apex_available: ["myapex","mydcla"],
9883 min_sdk_version: "29",
9884 }
9885 cc_library {
9886 name: "libbar",
9887 shared_libs: ["libc"],
9888 apex_available: ["myapex","mydcla"],
9889 min_sdk_version: "29",
9890 }
9891 cc_library {
9892 name: "libbaz",
9893 shared_libs: ["libc"],
9894 apex_available: ["myapex","mydcla"],
9895 min_sdk_version: "29",
9896 }
9897 cc_api_library {
9898 name: "libc",
9899 src: "libc.so",
9900 min_sdk_version: "29",
9901 recovery_available: true,
9902 }
9903 api_imports {
9904 name: "api_imports",
9905 shared_libs: [
9906 "libc",
9907 ],
9908 header_libs: [],
9909 }
9910 `
9911 ctx := testApex(t, bp)
9912 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
9913 apexRule := module.MaybeRule("apexRule")
9914 if apexRule.Rule == nil {
9915 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
9916 }
9917
9918 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
9919 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
9920 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
9921 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
9922 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
9923 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
9924}