blob: 85446e1b1886480e3d0eb8e22897e11c457661f5 [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
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003487func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003488 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003489 var failed bool
3490 var surplus []string
3491 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003492 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003493 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003494 for _, expected := range expectedFiles {
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
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003512 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003513 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003514 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003515 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
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003528func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3529 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3530}
3531
3532func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3533 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3534 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3535 if deapexer.Output != nil {
3536 outputs = append(outputs, deapexer.Output.String())
3537 }
3538 for _, output := range deapexer.ImplicitOutputs {
3539 outputs = append(outputs, output.String())
3540 }
3541 actualFiles := make([]fileInApex, 0, len(outputs))
3542 for _, output := range outputs {
3543 dir := "/deapexer/"
3544 pos := strings.LastIndex(output, dir)
3545 if pos == -1 {
3546 t.Fatal("Unknown deapexer output ", output)
3547 }
3548 path := output[pos+len(dir):]
3549 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3550 }
3551 assertFileListEquals(t, files, actualFiles)
3552}
3553
Jooyung Han344d5432019-08-23 11:17:39 +09003554func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003555 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003556 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003557 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003558 "etc/llndk.libraries.29.txt",
3559 "etc/vndkcore.libraries.29.txt",
3560 "etc/vndksp.libraries.29.txt",
3561 "etc/vndkprivate.libraries.29.txt",
3562 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003563 }
3564 testCases := []struct {
3565 vndkVersion string
3566 expectedFiles []string
3567 }{
3568 {
3569 vndkVersion: "current",
3570 expectedFiles: append(commonFiles,
3571 "lib/libvndk.so",
3572 "lib/libvndksp.so",
3573 "lib64/libvndk.so",
3574 "lib64/libvndksp.so"),
3575 },
3576 {
3577 vndkVersion: "",
3578 expectedFiles: append(commonFiles,
3579 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3580 "lib/libvndksp.so",
3581 "lib64/libvndksp.so"),
3582 },
3583 }
3584 for _, tc := range testCases {
3585 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3586 ctx := testApex(t, `
3587 apex_vndk {
3588 name: "com.android.vndk.current",
3589 key: "com.android.vndk.current.key",
3590 updatable: false,
3591 }
3592
3593 apex_key {
3594 name: "com.android.vndk.current.key",
3595 public_key: "testkey.avbpubkey",
3596 private_key: "testkey.pem",
3597 }
3598
3599 cc_library {
3600 name: "libvndk",
3601 srcs: ["mylib.cpp"],
3602 vendor_available: true,
3603 product_available: true,
3604 vndk: {
3605 enabled: true,
3606 },
3607 system_shared_libs: [],
3608 stl: "none",
3609 apex_available: [ "com.android.vndk.current" ],
3610 }
3611
3612 cc_library {
3613 name: "libvndksp",
3614 srcs: ["mylib.cpp"],
3615 vendor_available: true,
3616 product_available: true,
3617 vndk: {
3618 enabled: true,
3619 support_system_process: true,
3620 },
3621 system_shared_libs: [],
3622 stl: "none",
3623 apex_available: [ "com.android.vndk.current" ],
3624 }
3625
3626 // VNDK-Ext should not cause any problems
3627
3628 cc_library {
3629 name: "libvndk.ext",
3630 srcs: ["mylib2.cpp"],
3631 vendor: true,
3632 vndk: {
3633 enabled: true,
3634 extends: "libvndk",
3635 },
3636 system_shared_libs: [],
3637 stl: "none",
3638 }
3639
3640 cc_library {
3641 name: "libvndksp.ext",
3642 srcs: ["mylib2.cpp"],
3643 vendor: true,
3644 vndk: {
3645 enabled: true,
3646 support_system_process: true,
3647 extends: "libvndksp",
3648 },
3649 system_shared_libs: [],
3650 stl: "none",
3651 }
3652 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3653 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3654 }))
3655 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3656 })
3657 }
Jooyung Han344d5432019-08-23 11:17:39 +09003658}
3659
3660func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003661 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003662 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003663 name: "com.android.vndk.current",
3664 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003665 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003666 }
3667
3668 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003669 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003670 public_key: "testkey.avbpubkey",
3671 private_key: "testkey.pem",
3672 }
3673
3674 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003675 name: "libvndk",
3676 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003677 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003678 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003679 vndk: {
3680 enabled: true,
3681 },
3682 system_shared_libs: [],
3683 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003684 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003685 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003686
3687 cc_prebuilt_library_shared {
3688 name: "libvndk.arm",
3689 srcs: ["libvndk.arm.so"],
3690 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003691 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003692 vndk: {
3693 enabled: true,
3694 },
3695 enabled: false,
3696 arch: {
3697 arm: {
3698 enabled: true,
3699 },
3700 },
3701 system_shared_libs: [],
3702 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003703 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003704 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003705 `+vndkLibrariesTxtFiles("current"),
3706 withFiles(map[string][]byte{
3707 "libvndk.so": nil,
3708 "libvndk.arm.so": nil,
3709 }))
Colin Cross2807f002021-03-02 10:15:29 -08003710 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003711 "lib/libvndk.so",
3712 "lib/libvndk.arm.so",
3713 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003714 "lib/libc++.so",
3715 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003716 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003717 })
Jooyung Han344d5432019-08-23 11:17:39 +09003718}
3719
Jooyung Han39edb6c2019-11-06 16:53:07 +09003720func vndkLibrariesTxtFiles(vers ...string) (result string) {
3721 for _, v := range vers {
3722 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003723 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003724 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003725 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003726 name: "` + txt + `.libraries.txt",
3727 }
3728 `
3729 }
3730 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003731 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003732 result += `
3733 prebuilt_etc {
3734 name: "` + txt + `.libraries.` + v + `.txt",
3735 src: "dummy.txt",
3736 }
3737 `
3738 }
3739 }
3740 }
3741 return
3742}
3743
Jooyung Han344d5432019-08-23 11:17:39 +09003744func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003745 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003746 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003747 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003748 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003749 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003750 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003751 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003752 }
3753
3754 apex_key {
3755 name: "myapex.key",
3756 public_key: "testkey.avbpubkey",
3757 private_key: "testkey.pem",
3758 }
3759
Jooyung Han31c470b2019-10-18 16:26:59 +09003760 vndk_prebuilt_shared {
3761 name: "libvndk27",
3762 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003763 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003764 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003765 vndk: {
3766 enabled: true,
3767 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003768 target_arch: "arm64",
3769 arch: {
3770 arm: {
3771 srcs: ["libvndk27_arm.so"],
3772 },
3773 arm64: {
3774 srcs: ["libvndk27_arm64.so"],
3775 },
3776 },
Colin Cross2807f002021-03-02 10:15:29 -08003777 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003778 }
3779
3780 vndk_prebuilt_shared {
3781 name: "libvndk27",
3782 version: "27",
3783 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003784 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003785 vndk: {
3786 enabled: true,
3787 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003788 target_arch: "x86_64",
3789 arch: {
3790 x86: {
3791 srcs: ["libvndk27_x86.so"],
3792 },
3793 x86_64: {
3794 srcs: ["libvndk27_x86_64.so"],
3795 },
3796 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003797 }
3798 `+vndkLibrariesTxtFiles("27"),
3799 withFiles(map[string][]byte{
3800 "libvndk27_arm.so": nil,
3801 "libvndk27_arm64.so": nil,
3802 "libvndk27_x86.so": nil,
3803 "libvndk27_x86_64.so": nil,
3804 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003805
Colin Cross2807f002021-03-02 10:15:29 -08003806 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003807 "lib/libvndk27_arm.so",
3808 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003809 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003810 })
Jooyung Han344d5432019-08-23 11:17:39 +09003811}
3812
Jooyung Han90eee022019-10-01 20:02:42 +09003813func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003814 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003815 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003816 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003817 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003818 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003819 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003820 }
3821 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003822 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003823 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003824 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003825 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003826 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003827 }
3828 apex_key {
3829 name: "myapex.key",
3830 public_key: "testkey.avbpubkey",
3831 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003832 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003833
3834 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003835 module := ctx.ModuleForTests(moduleName, "android_common_image")
3836 apexManifestRule := module.Rule("apexManifestRule")
3837 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003838 }
3839
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003840 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003841 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003842}
3843
Jooyung Han344d5432019-08-23 11:17:39 +09003844func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003845 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003846 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003847 name: "com.android.vndk.current",
3848 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003849 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003850 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003851 }
3852
3853 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003854 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003855 public_key: "testkey.avbpubkey",
3856 private_key: "testkey.pem",
3857 }
3858
3859 cc_library {
3860 name: "libvndk",
3861 srcs: ["mylib.cpp"],
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003864 native_bridge_supported: true,
3865 host_supported: true,
3866 vndk: {
3867 enabled: true,
3868 },
3869 system_shared_libs: [],
3870 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003871 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003872 }
Colin Cross2807f002021-03-02 10:15:29 -08003873 `+vndkLibrariesTxtFiles("current"),
3874 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003875
Colin Cross2807f002021-03-02 10:15:29 -08003876 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003877 "lib/libvndk.so",
3878 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003879 "lib/libc++.so",
3880 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003881 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003882 })
Jooyung Han344d5432019-08-23 11:17:39 +09003883}
3884
3885func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003886 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003887 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003888 name: "com.android.vndk.current",
3889 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003890 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003891 native_bridge_supported: true,
3892 }
3893
3894 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003895 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003896 public_key: "testkey.avbpubkey",
3897 private_key: "testkey.pem",
3898 }
3899
3900 cc_library {
3901 name: "libvndk",
3902 srcs: ["mylib.cpp"],
3903 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003904 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003905 native_bridge_supported: true,
3906 host_supported: true,
3907 vndk: {
3908 enabled: true,
3909 },
3910 system_shared_libs: [],
3911 stl: "none",
3912 }
3913 `)
3914}
3915
Jooyung Han31c470b2019-10-18 16:26:59 +09003916func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003917 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003918 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003919 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003920 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003921 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003922 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003923 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003924 }
3925
3926 apex_key {
3927 name: "myapex.key",
3928 public_key: "testkey.avbpubkey",
3929 private_key: "testkey.pem",
3930 }
3931
3932 vndk_prebuilt_shared {
3933 name: "libvndk27",
3934 version: "27",
3935 target_arch: "arm",
3936 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003937 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003938 vndk: {
3939 enabled: true,
3940 },
3941 arch: {
3942 arm: {
3943 srcs: ["libvndk27.so"],
3944 }
3945 },
3946 }
3947
3948 vndk_prebuilt_shared {
3949 name: "libvndk27",
3950 version: "27",
3951 target_arch: "arm",
3952 binder32bit: true,
3953 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003954 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003955 vndk: {
3956 enabled: true,
3957 },
3958 arch: {
3959 arm: {
3960 srcs: ["libvndk27binder32.so"],
3961 }
3962 },
Colin Cross2807f002021-03-02 10:15:29 -08003963 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003964 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003965 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003966 withFiles(map[string][]byte{
3967 "libvndk27.so": nil,
3968 "libvndk27binder32.so": nil,
3969 }),
3970 withBinder32bit,
3971 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003972 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003973 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3974 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003975 },
3976 }),
3977 )
3978
Colin Cross2807f002021-03-02 10:15:29 -08003979 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003980 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003981 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003982 })
3983}
3984
Jooyung Han45a96772020-06-15 14:59:42 +09003985func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003986 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003987 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003988 name: "com.android.vndk.current",
3989 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003990 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003991 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003992 }
3993
3994 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003995 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003996 public_key: "testkey.avbpubkey",
3997 private_key: "testkey.pem",
3998 }
3999
4000 cc_library {
4001 name: "libz",
4002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004003 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004004 vndk: {
4005 enabled: true,
4006 },
4007 stubs: {
4008 symbol_file: "libz.map.txt",
4009 versions: ["30"],
4010 }
4011 }
4012 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4013 "libz.map.txt": nil,
4014 }))
4015
Colin Cross2807f002021-03-02 10:15:29 -08004016 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004017 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4018 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004019 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4020 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4021 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4022 "*/*",
4023 })
Jooyung Han45a96772020-06-15 14:59:42 +09004024}
4025
Jooyung Hane1633032019-08-01 17:41:43 +09004026func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004027 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004028 apex {
4029 name: "myapex_nodep",
4030 key: "myapex.key",
4031 native_shared_libs: ["lib_nodep"],
4032 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004033 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004034 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004035 }
4036
4037 apex {
4038 name: "myapex_dep",
4039 key: "myapex.key",
4040 native_shared_libs: ["lib_dep"],
4041 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004042 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004043 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004044 }
4045
4046 apex {
4047 name: "myapex_provider",
4048 key: "myapex.key",
4049 native_shared_libs: ["libfoo"],
4050 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004051 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004052 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004053 }
4054
4055 apex {
4056 name: "myapex_selfcontained",
4057 key: "myapex.key",
4058 native_shared_libs: ["lib_dep", "libfoo"],
4059 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004060 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004061 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004062 }
4063
4064 apex_key {
4065 name: "myapex.key",
4066 public_key: "testkey.avbpubkey",
4067 private_key: "testkey.pem",
4068 }
4069
4070 cc_library {
4071 name: "lib_nodep",
4072 srcs: ["mylib.cpp"],
4073 system_shared_libs: [],
4074 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004075 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004076 }
4077
4078 cc_library {
4079 name: "lib_dep",
4080 srcs: ["mylib.cpp"],
4081 shared_libs: ["libfoo"],
4082 system_shared_libs: [],
4083 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004084 apex_available: [
4085 "myapex_dep",
4086 "myapex_provider",
4087 "myapex_selfcontained",
4088 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004089 }
4090
4091 cc_library {
4092 name: "libfoo",
4093 srcs: ["mytest.cpp"],
4094 stubs: {
4095 versions: ["1"],
4096 },
4097 system_shared_libs: [],
4098 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004099 apex_available: [
4100 "myapex_provider",
4101 "myapex_selfcontained",
4102 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004103 }
4104 `)
4105
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004106 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004107 var provideNativeLibs, requireNativeLibs []string
4108
Sundong Ahnabb64432019-10-22 13:58:29 +09004109 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004110 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4111 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004112 ensureListEmpty(t, provideNativeLibs)
4113 ensureListEmpty(t, requireNativeLibs)
4114
Sundong Ahnabb64432019-10-22 13:58:29 +09004115 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004116 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4117 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004118 ensureListEmpty(t, provideNativeLibs)
4119 ensureListContains(t, requireNativeLibs, "libfoo.so")
4120
Sundong Ahnabb64432019-10-22 13:58:29 +09004121 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004122 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4123 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004124 ensureListContains(t, provideNativeLibs, "libfoo.so")
4125 ensureListEmpty(t, requireNativeLibs)
4126
Sundong Ahnabb64432019-10-22 13:58:29 +09004127 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004128 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4129 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004130 ensureListContains(t, provideNativeLibs, "libfoo.so")
4131 ensureListEmpty(t, requireNativeLibs)
4132}
4133
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004134func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4135 ctx := testApex(t, `
4136 apex {
4137 name: "myapex",
4138 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004139 native_shared_libs: ["mylib"],
4140 updatable: false,
4141 }
4142
4143 apex_key {
4144 name: "myapex.key",
4145 public_key: "testkey.avbpubkey",
4146 private_key: "testkey.pem",
4147 }
4148
4149 cc_library {
4150 name: "mylib",
4151 srcs: ["mylib.cpp"],
4152 system_shared_libs: [],
4153 stl: "none",
4154 apex_available: [
4155 "//apex_available:platform",
4156 "myapex",
4157 ],
4158 }
4159 `, android.FixtureMergeEnv(map[string]string{
4160 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4161 }))
4162
Jooyung Han63dff462023-02-09 00:11:27 +00004163 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004164 apexManifestRule := module.Rule("apexManifestRule")
4165 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4166}
4167
Vinh Tran8f5310f2022-10-07 18:16:47 -04004168func TestCompileMultilibProp(t *testing.T) {
4169 testCases := []struct {
4170 compileMultiLibProp string
4171 containedLibs []string
4172 notContainedLibs []string
4173 }{
4174 {
4175 containedLibs: []string{
4176 "image.apex/lib64/mylib.so",
4177 "image.apex/lib/mylib.so",
4178 },
4179 compileMultiLibProp: `compile_multilib: "both",`,
4180 },
4181 {
4182 containedLibs: []string{"image.apex/lib64/mylib.so"},
4183 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4184 compileMultiLibProp: `compile_multilib: "first",`,
4185 },
4186 {
4187 containedLibs: []string{"image.apex/lib64/mylib.so"},
4188 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4189 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4190 },
4191 {
4192 containedLibs: []string{"image.apex/lib64/mylib.so"},
4193 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4194 compileMultiLibProp: `compile_multilib: "64",`,
4195 },
4196 {
4197 containedLibs: []string{"image.apex/lib/mylib.so"},
4198 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4199 compileMultiLibProp: `compile_multilib: "32",`,
4200 },
4201 }
4202 for _, testCase := range testCases {
4203 ctx := testApex(t, fmt.Sprintf(`
4204 apex {
4205 name: "myapex",
4206 key: "myapex.key",
4207 %s
4208 native_shared_libs: ["mylib"],
4209 updatable: false,
4210 }
4211 apex_key {
4212 name: "myapex.key",
4213 public_key: "testkey.avbpubkey",
4214 private_key: "testkey.pem",
4215 }
4216 cc_library {
4217 name: "mylib",
4218 srcs: ["mylib.cpp"],
4219 apex_available: [
4220 "//apex_available:platform",
4221 "myapex",
4222 ],
4223 }
4224 `, testCase.compileMultiLibProp),
4225 )
4226 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4227 apexRule := module.Rule("apexRule")
4228 copyCmds := apexRule.Args["copy_commands"]
4229 for _, containedLib := range testCase.containedLibs {
4230 ensureContains(t, copyCmds, containedLib)
4231 }
4232 for _, notContainedLib := range testCase.notContainedLibs {
4233 ensureNotContains(t, copyCmds, notContainedLib)
4234 }
4235 }
4236}
4237
Alex Light0851b882019-02-07 13:20:53 -08004238func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004239 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004240 apex {
4241 name: "myapex",
4242 key: "myapex.key",
4243 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004244 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004245 }
4246
4247 apex_key {
4248 name: "myapex.key",
4249 public_key: "testkey.avbpubkey",
4250 private_key: "testkey.pem",
4251 }
4252
4253 cc_library {
4254 name: "mylib_common",
4255 srcs: ["mylib.cpp"],
4256 system_shared_libs: [],
4257 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004258 apex_available: [
4259 "//apex_available:platform",
4260 "myapex",
4261 ],
Alex Light0851b882019-02-07 13:20:53 -08004262 }
4263 `)
4264
Sundong Ahnabb64432019-10-22 13:58:29 +09004265 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004266 apexRule := module.Rule("apexRule")
4267 copyCmds := apexRule.Args["copy_commands"]
4268
4269 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4270 t.Log("Apex was a test apex!")
4271 t.Fail()
4272 }
4273 // Ensure that main rule creates an output
4274 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4275
4276 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004277 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004278
4279 // Ensure that both direct and indirect deps are copied into apex
4280 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4281
Colin Cross7113d202019-11-20 16:39:12 -08004282 // Ensure that the platform variant ends with _shared
4283 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004284
Colin Cross56a83212020-09-15 18:30:11 -07004285 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004286 t.Log("Found mylib_common not in any apex!")
4287 t.Fail()
4288 }
4289}
4290
4291func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004292 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004293 apex_test {
4294 name: "myapex",
4295 key: "myapex.key",
4296 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004297 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004298 }
4299
4300 apex_key {
4301 name: "myapex.key",
4302 public_key: "testkey.avbpubkey",
4303 private_key: "testkey.pem",
4304 }
4305
4306 cc_library {
4307 name: "mylib_common_test",
4308 srcs: ["mylib.cpp"],
4309 system_shared_libs: [],
4310 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004311 // TODO: remove //apex_available:platform
4312 apex_available: [
4313 "//apex_available:platform",
4314 "myapex",
4315 ],
Alex Light0851b882019-02-07 13:20:53 -08004316 }
4317 `)
4318
Sundong Ahnabb64432019-10-22 13:58:29 +09004319 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004320 apexRule := module.Rule("apexRule")
4321 copyCmds := apexRule.Args["copy_commands"]
4322
4323 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4324 t.Log("Apex was not a test apex!")
4325 t.Fail()
4326 }
4327 // Ensure that main rule creates an output
4328 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4329
4330 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004331 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004332
4333 // Ensure that both direct and indirect deps are copied into apex
4334 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4335
Colin Cross7113d202019-11-20 16:39:12 -08004336 // Ensure that the platform variant ends with _shared
4337 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004338}
4339
Alex Light9670d332019-01-29 18:07:33 -08004340func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004341 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004342 apex {
4343 name: "myapex",
4344 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004345 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004346 multilib: {
4347 first: {
4348 native_shared_libs: ["mylib_common"],
4349 }
4350 },
4351 target: {
4352 android: {
4353 multilib: {
4354 first: {
4355 native_shared_libs: ["mylib"],
4356 }
4357 }
4358 },
4359 host: {
4360 multilib: {
4361 first: {
4362 native_shared_libs: ["mylib2"],
4363 }
4364 }
4365 }
4366 }
4367 }
4368
4369 apex_key {
4370 name: "myapex.key",
4371 public_key: "testkey.avbpubkey",
4372 private_key: "testkey.pem",
4373 }
4374
4375 cc_library {
4376 name: "mylib",
4377 srcs: ["mylib.cpp"],
4378 system_shared_libs: [],
4379 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004380 // TODO: remove //apex_available:platform
4381 apex_available: [
4382 "//apex_available:platform",
4383 "myapex",
4384 ],
Alex Light9670d332019-01-29 18:07:33 -08004385 }
4386
4387 cc_library {
4388 name: "mylib_common",
4389 srcs: ["mylib.cpp"],
4390 system_shared_libs: [],
4391 stl: "none",
4392 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004393 // TODO: remove //apex_available:platform
4394 apex_available: [
4395 "//apex_available:platform",
4396 "myapex",
4397 ],
Alex Light9670d332019-01-29 18:07:33 -08004398 }
4399
4400 cc_library {
4401 name: "mylib2",
4402 srcs: ["mylib.cpp"],
4403 system_shared_libs: [],
4404 stl: "none",
4405 compile_multilib: "first",
4406 }
4407 `)
4408
Sundong Ahnabb64432019-10-22 13:58:29 +09004409 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004410 copyCmds := apexRule.Args["copy_commands"]
4411
4412 // Ensure that main rule creates an output
4413 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4414
4415 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004416 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4417 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4418 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004419
4420 // Ensure that both direct and indirect deps are copied into apex
4421 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4422 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4423 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4424
Colin Cross7113d202019-11-20 16:39:12 -08004425 // Ensure that the platform variant ends with _shared
4426 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4427 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4428 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004429}
Jiyong Park04480cf2019-02-06 00:16:29 +09004430
Jiyong Park59140302020-12-14 18:44:04 +09004431func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004432 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004433 apex {
4434 name: "myapex",
4435 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004436 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004437 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004438 arch: {
4439 arm64: {
4440 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004441 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004442 },
4443 x86_64: {
4444 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004445 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004446 },
4447 }
4448 }
4449
4450 apex_key {
4451 name: "myapex.key",
4452 public_key: "testkey.avbpubkey",
4453 private_key: "testkey.pem",
4454 }
4455
4456 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004457 name: "mylib.generic",
4458 srcs: ["mylib.cpp"],
4459 system_shared_libs: [],
4460 stl: "none",
4461 // TODO: remove //apex_available:platform
4462 apex_available: [
4463 "//apex_available:platform",
4464 "myapex",
4465 ],
4466 }
4467
4468 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004469 name: "mylib.arm64",
4470 srcs: ["mylib.cpp"],
4471 system_shared_libs: [],
4472 stl: "none",
4473 // TODO: remove //apex_available:platform
4474 apex_available: [
4475 "//apex_available:platform",
4476 "myapex",
4477 ],
4478 }
4479
4480 cc_library {
4481 name: "mylib.x64",
4482 srcs: ["mylib.cpp"],
4483 system_shared_libs: [],
4484 stl: "none",
4485 // TODO: remove //apex_available:platform
4486 apex_available: [
4487 "//apex_available:platform",
4488 "myapex",
4489 ],
4490 }
4491 `)
4492
4493 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4494 copyCmds := apexRule.Args["copy_commands"]
4495
4496 // Ensure that apex variant is created for the direct dep
4497 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004498 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004499 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4500
4501 // Ensure that both direct and indirect deps are copied into apex
4502 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4503 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4504}
4505
Jiyong Park04480cf2019-02-06 00:16:29 +09004506func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004507 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004508 apex {
4509 name: "myapex",
4510 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004511 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004512 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004513 }
4514
4515 apex_key {
4516 name: "myapex.key",
4517 public_key: "testkey.avbpubkey",
4518 private_key: "testkey.pem",
4519 }
4520
4521 sh_binary {
4522 name: "myscript",
4523 src: "mylib.cpp",
4524 filename: "myscript.sh",
4525 sub_dir: "script",
4526 }
4527 `)
4528
Sundong Ahnabb64432019-10-22 13:58:29 +09004529 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004530 copyCmds := apexRule.Args["copy_commands"]
4531
4532 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4533}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004534
Jooyung Han91df2082019-11-20 01:49:42 +09004535func TestApexInVariousPartition(t *testing.T) {
4536 testcases := []struct {
4537 propName, parition, flattenedPartition string
4538 }{
4539 {"", "system", "system_ext"},
4540 {"product_specific: true", "product", "product"},
4541 {"soc_specific: true", "vendor", "vendor"},
4542 {"proprietary: true", "vendor", "vendor"},
4543 {"vendor: true", "vendor", "vendor"},
4544 {"system_ext_specific: true", "system_ext", "system_ext"},
4545 }
4546 for _, tc := range testcases {
4547 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004548 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004549 apex {
4550 name: "myapex",
4551 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004552 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004553 `+tc.propName+`
4554 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004555
Jooyung Han91df2082019-11-20 01:49:42 +09004556 apex_key {
4557 name: "myapex.key",
4558 public_key: "testkey.avbpubkey",
4559 private_key: "testkey.pem",
4560 }
4561 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004562
Jooyung Han91df2082019-11-20 01:49:42 +09004563 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004564 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4565 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004566 if actual != expected {
4567 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4568 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004569
Jooyung Han91df2082019-11-20 01:49:42 +09004570 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004571 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4572 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004573 if actual != expected {
4574 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4575 }
4576 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004577 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004578}
Jiyong Park67882562019-03-21 01:11:21 +09004579
Jooyung Han580eb4f2020-06-24 19:33:06 +09004580func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004581 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004582 apex {
4583 name: "myapex",
4584 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004585 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004586 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004587
Jooyung Han580eb4f2020-06-24 19:33:06 +09004588 apex_key {
4589 name: "myapex.key",
4590 public_key: "testkey.avbpubkey",
4591 private_key: "testkey.pem",
4592 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004593 `)
4594 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004595 rule := module.Output("file_contexts")
4596 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4597}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004598
Jooyung Han580eb4f2020-06-24 19:33:06 +09004599func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004600 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004601 apex {
4602 name: "myapex",
4603 key: "myapex.key",
4604 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004605 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004606 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004607
Jooyung Han580eb4f2020-06-24 19:33:06 +09004608 apex_key {
4609 name: "myapex.key",
4610 public_key: "testkey.avbpubkey",
4611 private_key: "testkey.pem",
4612 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004613 `, withFiles(map[string][]byte{
4614 "my_own_file_contexts": nil,
4615 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004616}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004617
Jooyung Han580eb4f2020-06-24 19:33:06 +09004618func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004619 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004620 apex {
4621 name: "myapex",
4622 key: "myapex.key",
4623 product_specific: true,
4624 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004625 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004626 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004627
Jooyung Han580eb4f2020-06-24 19:33:06 +09004628 apex_key {
4629 name: "myapex.key",
4630 public_key: "testkey.avbpubkey",
4631 private_key: "testkey.pem",
4632 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004633 `)
4634
Colin Cross1c460562021-02-16 17:55:47 -08004635 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004636 apex {
4637 name: "myapex",
4638 key: "myapex.key",
4639 product_specific: true,
4640 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004641 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004642 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004643
Jooyung Han580eb4f2020-06-24 19:33:06 +09004644 apex_key {
4645 name: "myapex.key",
4646 public_key: "testkey.avbpubkey",
4647 private_key: "testkey.pem",
4648 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004649 `, withFiles(map[string][]byte{
4650 "product_specific_file_contexts": nil,
4651 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004652 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4653 rule := module.Output("file_contexts")
4654 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4655}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004656
Jooyung Han580eb4f2020-06-24 19:33:06 +09004657func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004658 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004659 apex {
4660 name: "myapex",
4661 key: "myapex.key",
4662 product_specific: true,
4663 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004664 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004665 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004666
Jooyung Han580eb4f2020-06-24 19:33:06 +09004667 apex_key {
4668 name: "myapex.key",
4669 public_key: "testkey.avbpubkey",
4670 private_key: "testkey.pem",
4671 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004672
Jooyung Han580eb4f2020-06-24 19:33:06 +09004673 filegroup {
4674 name: "my-file-contexts",
4675 srcs: ["product_specific_file_contexts"],
4676 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004677 `, withFiles(map[string][]byte{
4678 "product_specific_file_contexts": nil,
4679 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004680 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4681 rule := module.Output("file_contexts")
4682 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004683}
4684
Jiyong Park67882562019-03-21 01:11:21 +09004685func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004686 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004687 apex_key {
4688 name: "myapex.key",
4689 public_key: ":my.avbpubkey",
4690 private_key: ":my.pem",
4691 product_specific: true,
4692 }
4693
4694 filegroup {
4695 name: "my.avbpubkey",
4696 srcs: ["testkey2.avbpubkey"],
4697 }
4698
4699 filegroup {
4700 name: "my.pem",
4701 srcs: ["testkey2.pem"],
4702 }
4703 `)
4704
4705 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4706 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004707 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004708 if actual_pubkey != expected_pubkey {
4709 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4710 }
4711 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004712 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004713 if actual_privkey != expected_privkey {
4714 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4715 }
4716}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004717
4718func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004719 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004720 prebuilt_apex {
4721 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004722 arch: {
4723 arm64: {
4724 src: "myapex-arm64.apex",
4725 },
4726 arm: {
4727 src: "myapex-arm.apex",
4728 },
4729 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004730 }
4731 `)
4732
Wei Li340ee8e2022-03-18 17:33:24 -07004733 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4734 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004735
Jiyong Parkc95714e2019-03-29 14:23:10 +09004736 expectedInput := "myapex-arm64.apex"
4737 if prebuilt.inputApex.String() != expectedInput {
4738 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4739 }
Wei Li340ee8e2022-03-18 17:33:24 -07004740 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4741 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4742 rule := testingModule.Rule("genProvenanceMetaData")
4743 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4744 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4745 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4746 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004747}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004748
Paul Duffinc0609c62021-03-01 17:27:16 +00004749func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004750 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004751 prebuilt_apex {
4752 name: "myapex",
4753 }
4754 `)
4755}
4756
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004757func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004758 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004759 prebuilt_apex {
4760 name: "myapex",
4761 src: "myapex-arm.apex",
4762 filename: "notmyapex.apex",
4763 }
4764 `)
4765
Wei Li340ee8e2022-03-18 17:33:24 -07004766 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4767 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004768
4769 expected := "notmyapex.apex"
4770 if p.installFilename != expected {
4771 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4772 }
Wei Li340ee8e2022-03-18 17:33:24 -07004773 rule := testingModule.Rule("genProvenanceMetaData")
4774 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4775 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4776 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4777 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004778}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004779
Samiul Islam7c02e262021-09-08 17:48:28 +01004780func TestApexSetFilenameOverride(t *testing.T) {
4781 testApex(t, `
4782 apex_set {
4783 name: "com.company.android.myapex",
4784 apex_name: "com.android.myapex",
4785 set: "company-myapex.apks",
4786 filename: "com.company.android.myapex.apex"
4787 }
4788 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4789
4790 testApex(t, `
4791 apex_set {
4792 name: "com.company.android.myapex",
4793 apex_name: "com.android.myapex",
4794 set: "company-myapex.apks",
4795 filename: "com.company.android.myapex.capex"
4796 }
4797 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4798
4799 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4800 apex_set {
4801 name: "com.company.android.myapex",
4802 apex_name: "com.android.myapex",
4803 set: "company-myapex.apks",
4804 filename: "some-random-suffix"
4805 }
4806 `)
4807}
4808
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004809func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004810 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004811 prebuilt_apex {
4812 name: "myapex.prebuilt",
4813 src: "myapex-arm.apex",
4814 overrides: [
4815 "myapex",
4816 ],
4817 }
4818 `)
4819
Wei Li340ee8e2022-03-18 17:33:24 -07004820 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4821 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004822
4823 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004824 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004825 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004826 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004827 }
Wei Li340ee8e2022-03-18 17:33:24 -07004828 rule := testingModule.Rule("genProvenanceMetaData")
4829 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4830 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4831 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4832 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004833}
4834
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004835func TestPrebuiltApexName(t *testing.T) {
4836 testApex(t, `
4837 prebuilt_apex {
4838 name: "com.company.android.myapex",
4839 apex_name: "com.android.myapex",
4840 src: "company-myapex-arm.apex",
4841 }
4842 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4843
4844 testApex(t, `
4845 apex_set {
4846 name: "com.company.android.myapex",
4847 apex_name: "com.android.myapex",
4848 set: "company-myapex.apks",
4849 }
4850 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4851}
4852
4853func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4854 _ = android.GroupFixturePreparers(
4855 java.PrepareForTestWithJavaDefaultModules,
4856 PrepareForTestWithApexBuildComponents,
4857 android.FixtureWithRootAndroidBp(`
4858 platform_bootclasspath {
4859 name: "platform-bootclasspath",
4860 fragments: [
4861 {
4862 apex: "com.android.art",
4863 module: "art-bootclasspath-fragment",
4864 },
4865 ],
4866 }
4867
4868 prebuilt_apex {
4869 name: "com.company.android.art",
4870 apex_name: "com.android.art",
4871 src: "com.company.android.art-arm.apex",
4872 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4873 }
4874
4875 prebuilt_bootclasspath_fragment {
4876 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004877 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004878 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004879 hidden_api: {
4880 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4881 metadata: "my-bootclasspath-fragment/metadata.csv",
4882 index: "my-bootclasspath-fragment/index.csv",
4883 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4884 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4885 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004886 }
4887
4888 java_import {
4889 name: "core-oj",
4890 jars: ["prebuilt.jar"],
4891 }
4892 `),
4893 ).RunTest(t)
4894}
4895
Paul Duffin092153d2021-01-26 11:42:39 +00004896// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4897// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004898func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004899 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004900
Paul Duffin89886cb2021-02-05 16:44:03 +00004901 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004902 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004903 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004904 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004905 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004906 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004907 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4908 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4909 android.NormalizePathForTesting(dexJarBuildPath))
4910 }
4911
4912 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004913 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004914 // Make sure the import has been given the correct path to the dex jar.
4915 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4916 dexJarBuildPath := p.DexJarInstallPath()
4917 stem := android.RemoveOptionalPrebuiltPrefix(name)
4918 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4919 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4920 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004921 }
4922
Paul Duffin39853512021-02-26 11:09:39 +00004923 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004924 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004925 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004926 android.AssertArrayString(t, "Check if there is no source variant",
4927 []string{"android_common"},
4928 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004929 }
4930
4931 t.Run("prebuilt only", func(t *testing.T) {
4932 bp := `
4933 prebuilt_apex {
4934 name: "myapex",
4935 arch: {
4936 arm64: {
4937 src: "myapex-arm64.apex",
4938 },
4939 arm: {
4940 src: "myapex-arm.apex",
4941 },
4942 },
Paul Duffin39853512021-02-26 11:09:39 +00004943 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004944 }
4945
4946 java_import {
4947 name: "libfoo",
4948 jars: ["libfoo.jar"],
4949 }
Paul Duffin39853512021-02-26 11:09:39 +00004950
4951 java_sdk_library_import {
4952 name: "libbar",
4953 public: {
4954 jars: ["libbar.jar"],
4955 },
4956 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004957 `
4958
4959 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4960 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4961
Martin Stjernholm44825602021-09-17 01:44:12 +01004962 deapexerName := deapexerModuleName("myapex")
4963 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4964
Paul Duffinf6932af2021-02-26 18:21:56 +00004965 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004966 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004967 rule := deapexer.Rule("deapexer")
4968 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4969 t.Errorf("expected: %q, found: %q", expected, actual)
4970 }
4971
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004972 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004973 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004974 rule = prebuiltApex.Rule("android/soong/android.Cp")
4975 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4976 t.Errorf("expected: %q, found: %q", expected, actual)
4977 }
4978
Paul Duffin89886cb2021-02-05 16:44:03 +00004979 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004980 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004981
4982 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004983 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004984 })
4985
4986 t.Run("prebuilt with source preferred", func(t *testing.T) {
4987
4988 bp := `
4989 prebuilt_apex {
4990 name: "myapex",
4991 arch: {
4992 arm64: {
4993 src: "myapex-arm64.apex",
4994 },
4995 arm: {
4996 src: "myapex-arm.apex",
4997 },
4998 },
Paul Duffin39853512021-02-26 11:09:39 +00004999 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005000 }
5001
5002 java_import {
5003 name: "libfoo",
5004 jars: ["libfoo.jar"],
5005 }
5006
5007 java_library {
5008 name: "libfoo",
5009 }
Paul Duffin39853512021-02-26 11:09:39 +00005010
5011 java_sdk_library_import {
5012 name: "libbar",
5013 public: {
5014 jars: ["libbar.jar"],
5015 },
5016 }
5017
5018 java_sdk_library {
5019 name: "libbar",
5020 srcs: ["foo/bar/MyClass.java"],
5021 unsafe_ignore_missing_latest_api: true,
5022 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005023 `
5024
5025 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5026 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5027
Paul Duffin89886cb2021-02-05 16:44:03 +00005028 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005029 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005030 ensureNoSourceVariant(t, ctx, "libfoo")
5031
5032 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005033 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005034 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005035 })
5036
5037 t.Run("prebuilt preferred with source", func(t *testing.T) {
5038 bp := `
5039 prebuilt_apex {
5040 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005041 arch: {
5042 arm64: {
5043 src: "myapex-arm64.apex",
5044 },
5045 arm: {
5046 src: "myapex-arm.apex",
5047 },
5048 },
Paul Duffin39853512021-02-26 11:09:39 +00005049 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005050 }
5051
5052 java_import {
5053 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005054 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005055 jars: ["libfoo.jar"],
5056 }
5057
5058 java_library {
5059 name: "libfoo",
5060 }
Paul Duffin39853512021-02-26 11:09:39 +00005061
5062 java_sdk_library_import {
5063 name: "libbar",
5064 prefer: true,
5065 public: {
5066 jars: ["libbar.jar"],
5067 },
5068 }
5069
5070 java_sdk_library {
5071 name: "libbar",
5072 srcs: ["foo/bar/MyClass.java"],
5073 unsafe_ignore_missing_latest_api: true,
5074 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005075 `
5076
5077 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5078 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5079
Paul Duffin89886cb2021-02-05 16:44:03 +00005080 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005081 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005082 ensureNoSourceVariant(t, ctx, "libfoo")
5083
5084 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005085 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005086 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005087 })
5088}
5089
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005090func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005091 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005092 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005093 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5094 // is disabled.
5095 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5096 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005097
Paul Duffin37856732021-02-26 14:24:15 +00005098 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5099 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005100 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005101 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005102 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005103 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005104 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005105 foundLibfooJar = true
5106 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005107 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005108 }
5109 }
5110 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005111 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 +00005112 }
5113 }
5114
Paul Duffin40a3f652021-07-19 13:11:24 +01005115 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005116 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005117 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005118 var rule android.TestingBuildParams
5119
5120 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5121 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005122 }
5123
Paul Duffin40a3f652021-07-19 13:11:24 +01005124 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5125 t.Helper()
5126 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5127 var rule android.TestingBuildParams
5128
5129 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5130 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5131 }
5132
Paul Duffin89f570a2021-06-16 01:42:33 +01005133 fragment := java.ApexVariantReference{
5134 Apex: proptools.StringPtr("myapex"),
5135 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5136 }
5137
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005138 t.Run("prebuilt only", func(t *testing.T) {
5139 bp := `
5140 prebuilt_apex {
5141 name: "myapex",
5142 arch: {
5143 arm64: {
5144 src: "myapex-arm64.apex",
5145 },
5146 arm: {
5147 src: "myapex-arm.apex",
5148 },
5149 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005150 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5151 }
5152
5153 prebuilt_bootclasspath_fragment {
5154 name: "my-bootclasspath-fragment",
5155 contents: ["libfoo", "libbar"],
5156 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005157 hidden_api: {
5158 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5159 metadata: "my-bootclasspath-fragment/metadata.csv",
5160 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005161 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5162 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5163 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005164 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005165 }
5166
5167 java_import {
5168 name: "libfoo",
5169 jars: ["libfoo.jar"],
5170 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005171 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005172 }
Paul Duffin37856732021-02-26 14:24:15 +00005173
5174 java_sdk_library_import {
5175 name: "libbar",
5176 public: {
5177 jars: ["libbar.jar"],
5178 },
5179 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005180 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005181 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005182 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005183 `
5184
Paul Duffin89f570a2021-06-16 01:42:33 +01005185 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005186 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5187 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005188
Paul Duffin537ea3d2021-05-14 10:38:00 +01005189 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005190 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005191 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005192 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005193 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5194 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005195 })
5196
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005197 t.Run("apex_set only", func(t *testing.T) {
5198 bp := `
5199 apex_set {
5200 name: "myapex",
5201 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005202 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5203 }
5204
5205 prebuilt_bootclasspath_fragment {
5206 name: "my-bootclasspath-fragment",
5207 contents: ["libfoo", "libbar"],
5208 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005209 hidden_api: {
5210 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5211 metadata: "my-bootclasspath-fragment/metadata.csv",
5212 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005213 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5214 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5215 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005216 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005217 }
5218
5219 java_import {
5220 name: "libfoo",
5221 jars: ["libfoo.jar"],
5222 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005223 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005224 }
5225
5226 java_sdk_library_import {
5227 name: "libbar",
5228 public: {
5229 jars: ["libbar.jar"],
5230 },
5231 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005232 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005233 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005234 }
5235 `
5236
Paul Duffin89f570a2021-06-16 01:42:33 +01005237 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005238 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5239 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5240
Paul Duffin537ea3d2021-05-14 10:38:00 +01005241 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005242 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005243 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005244 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005245 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5246 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005247 })
5248
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005249 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5250 bp := `
5251 prebuilt_apex {
5252 name: "myapex",
5253 arch: {
5254 arm64: {
5255 src: "myapex-arm64.apex",
5256 },
5257 arm: {
5258 src: "myapex-arm.apex",
5259 },
5260 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005261 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5262 }
5263
5264 prebuilt_bootclasspath_fragment {
5265 name: "my-bootclasspath-fragment",
5266 contents: ["libfoo", "libbar"],
5267 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005268 hidden_api: {
5269 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5270 metadata: "my-bootclasspath-fragment/metadata.csv",
5271 index: "my-bootclasspath-fragment/index.csv",
5272 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5273 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5274 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005275 }
5276
5277 java_import {
5278 name: "libfoo",
5279 jars: ["libfoo.jar"],
5280 apex_available: ["myapex"],
5281 }
5282
5283 java_library {
5284 name: "libfoo",
5285 srcs: ["foo/bar/MyClass.java"],
5286 apex_available: ["myapex"],
5287 }
Paul Duffin37856732021-02-26 14:24:15 +00005288
5289 java_sdk_library_import {
5290 name: "libbar",
5291 public: {
5292 jars: ["libbar.jar"],
5293 },
5294 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005295 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005296 }
5297
5298 java_sdk_library {
5299 name: "libbar",
5300 srcs: ["foo/bar/MyClass.java"],
5301 unsafe_ignore_missing_latest_api: true,
5302 apex_available: ["myapex"],
5303 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005304 `
5305
5306 // In this test the source (java_library) libfoo is active since the
5307 // prebuilt (java_import) defaults to prefer:false. However the
5308 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5309 // find the dex boot jar in it. We either need to disable the source libfoo
5310 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005311 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005312 // dexbootjar check is skipped if AllowMissingDependencies is true
5313 preparerAllowMissingDeps := android.GroupFixturePreparers(
5314 preparer,
5315 android.PrepareForTestWithAllowMissingDependencies,
5316 )
5317 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005318 })
5319
5320 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5321 bp := `
5322 prebuilt_apex {
5323 name: "myapex",
5324 arch: {
5325 arm64: {
5326 src: "myapex-arm64.apex",
5327 },
5328 arm: {
5329 src: "myapex-arm.apex",
5330 },
5331 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005332 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5333 }
5334
5335 prebuilt_bootclasspath_fragment {
5336 name: "my-bootclasspath-fragment",
5337 contents: ["libfoo", "libbar"],
5338 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005339 hidden_api: {
5340 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5341 metadata: "my-bootclasspath-fragment/metadata.csv",
5342 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005343 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5344 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5345 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005346 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005347 }
5348
5349 java_import {
5350 name: "libfoo",
5351 prefer: true,
5352 jars: ["libfoo.jar"],
5353 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005354 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005355 }
5356
5357 java_library {
5358 name: "libfoo",
5359 srcs: ["foo/bar/MyClass.java"],
5360 apex_available: ["myapex"],
5361 }
Paul Duffin37856732021-02-26 14:24:15 +00005362
5363 java_sdk_library_import {
5364 name: "libbar",
5365 prefer: true,
5366 public: {
5367 jars: ["libbar.jar"],
5368 },
5369 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005370 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005371 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005372 }
5373
5374 java_sdk_library {
5375 name: "libbar",
5376 srcs: ["foo/bar/MyClass.java"],
5377 unsafe_ignore_missing_latest_api: true,
5378 apex_available: ["myapex"],
5379 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005380 `
5381
Paul Duffin89f570a2021-06-16 01:42:33 +01005382 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005383 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5384 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005385
Paul Duffin537ea3d2021-05-14 10:38:00 +01005386 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005387 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005388 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005389 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005390 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5391 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005392 })
5393
5394 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5395 bp := `
5396 apex {
5397 name: "myapex",
5398 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005399 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005400 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005401 }
5402
5403 apex_key {
5404 name: "myapex.key",
5405 public_key: "testkey.avbpubkey",
5406 private_key: "testkey.pem",
5407 }
5408
5409 prebuilt_apex {
5410 name: "myapex",
5411 arch: {
5412 arm64: {
5413 src: "myapex-arm64.apex",
5414 },
5415 arm: {
5416 src: "myapex-arm.apex",
5417 },
5418 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005419 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5420 }
5421
5422 prebuilt_bootclasspath_fragment {
5423 name: "my-bootclasspath-fragment",
5424 contents: ["libfoo", "libbar"],
5425 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005426 hidden_api: {
5427 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5428 metadata: "my-bootclasspath-fragment/metadata.csv",
5429 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005430 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5431 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5432 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005433 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005434 }
5435
5436 java_import {
5437 name: "libfoo",
5438 jars: ["libfoo.jar"],
5439 apex_available: ["myapex"],
5440 }
5441
5442 java_library {
5443 name: "libfoo",
5444 srcs: ["foo/bar/MyClass.java"],
5445 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005446 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005447 }
Paul Duffin37856732021-02-26 14:24:15 +00005448
5449 java_sdk_library_import {
5450 name: "libbar",
5451 public: {
5452 jars: ["libbar.jar"],
5453 },
5454 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005455 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005456 }
5457
5458 java_sdk_library {
5459 name: "libbar",
5460 srcs: ["foo/bar/MyClass.java"],
5461 unsafe_ignore_missing_latest_api: true,
5462 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005463 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005464 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005465 `
5466
Paul Duffin89f570a2021-06-16 01:42:33 +01005467 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005468 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5469 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005470
Paul Duffin537ea3d2021-05-14 10:38:00 +01005471 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005472 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005473 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005474 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005475 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5476 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005477 })
5478
5479 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5480 bp := `
5481 apex {
5482 name: "myapex",
5483 enabled: false,
5484 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005485 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005486 }
5487
5488 apex_key {
5489 name: "myapex.key",
5490 public_key: "testkey.avbpubkey",
5491 private_key: "testkey.pem",
5492 }
5493
5494 prebuilt_apex {
5495 name: "myapex",
5496 arch: {
5497 arm64: {
5498 src: "myapex-arm64.apex",
5499 },
5500 arm: {
5501 src: "myapex-arm.apex",
5502 },
5503 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005504 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5505 }
5506
5507 prebuilt_bootclasspath_fragment {
5508 name: "my-bootclasspath-fragment",
5509 contents: ["libfoo", "libbar"],
5510 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005511 hidden_api: {
5512 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5513 metadata: "my-bootclasspath-fragment/metadata.csv",
5514 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005515 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5516 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5517 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005518 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005519 }
5520
5521 java_import {
5522 name: "libfoo",
5523 prefer: true,
5524 jars: ["libfoo.jar"],
5525 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005526 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005527 }
5528
5529 java_library {
5530 name: "libfoo",
5531 srcs: ["foo/bar/MyClass.java"],
5532 apex_available: ["myapex"],
5533 }
Paul Duffin37856732021-02-26 14:24:15 +00005534
5535 java_sdk_library_import {
5536 name: "libbar",
5537 prefer: true,
5538 public: {
5539 jars: ["libbar.jar"],
5540 },
5541 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005542 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005543 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005544 }
5545
5546 java_sdk_library {
5547 name: "libbar",
5548 srcs: ["foo/bar/MyClass.java"],
5549 unsafe_ignore_missing_latest_api: true,
5550 apex_available: ["myapex"],
5551 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005552 `
5553
Paul Duffin89f570a2021-06-16 01:42:33 +01005554 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005555 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5556 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005557
Paul Duffin537ea3d2021-05-14 10:38:00 +01005558 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005559 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005560 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005561 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005562 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5563 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005564 })
5565}
5566
Roland Levillain630846d2019-06-26 12:48:34 +01005567func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005568 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005569 apex_test {
5570 name: "myapex",
5571 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005572 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005573 tests: [
5574 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005575 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005576 ],
5577 }
5578
5579 apex_key {
5580 name: "myapex.key",
5581 public_key: "testkey.avbpubkey",
5582 private_key: "testkey.pem",
5583 }
5584
Liz Kammer1c14a212020-05-12 15:26:55 -07005585 filegroup {
5586 name: "fg",
5587 srcs: [
5588 "baz",
5589 "bar/baz"
5590 ],
5591 }
5592
Roland Levillain630846d2019-06-26 12:48:34 +01005593 cc_test {
5594 name: "mytest",
5595 gtest: false,
5596 srcs: ["mytest.cpp"],
5597 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005598 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005599 system_shared_libs: [],
5600 static_executable: true,
5601 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005602 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005603 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005604
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005605 cc_library {
5606 name: "mylib",
5607 srcs: ["mylib.cpp"],
5608 system_shared_libs: [],
5609 stl: "none",
5610 }
5611
Liz Kammer5bd365f2020-05-27 15:15:11 -07005612 filegroup {
5613 name: "fg2",
5614 srcs: [
5615 "testdata/baz"
5616 ],
5617 }
5618
Roland Levillain9b5fde92019-06-28 15:41:19 +01005619 cc_test {
5620 name: "mytests",
5621 gtest: false,
5622 srcs: [
5623 "mytest1.cpp",
5624 "mytest2.cpp",
5625 "mytest3.cpp",
5626 ],
5627 test_per_src: true,
5628 relative_install_path: "test",
5629 system_shared_libs: [],
5630 static_executable: true,
5631 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005632 data: [
5633 ":fg",
5634 ":fg2",
5635 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005636 }
Roland Levillain630846d2019-06-26 12:48:34 +01005637 `)
5638
Sundong Ahnabb64432019-10-22 13:58:29 +09005639 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005640 copyCmds := apexRule.Args["copy_commands"]
5641
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005642 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005643 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005644 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005645
Liz Kammer1c14a212020-05-12 15:26:55 -07005646 //Ensure that test data are copied into apex.
5647 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5648 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5649
Roland Levillain9b5fde92019-06-28 15:41:19 +01005650 // Ensure that test deps built with `test_per_src` are copied into apex.
5651 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5652 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5653 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005654
5655 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005656 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005657 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005658 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005659 prefix := "TARGET_"
5660 var builder strings.Builder
5661 data.Custom(&builder, name, prefix, "", data)
5662 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005663 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5664 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5665 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5666 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5667 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5668 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005669 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005670
5671 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005672 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005673 data.Custom(&builder, name, prefix, "", data)
5674 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005675 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5676 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005677}
5678
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005679func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005680 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005681 apex {
5682 name: "myapex",
5683 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005684 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005685 }
5686 apex_key {
5687 name: "myapex.key",
5688 public_key: "testkey.avbpubkey",
5689 private_key: "testkey.pem",
5690 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005691 `,
5692 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5693 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5694 }),
5695 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005696 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005697 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005698 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005699 var builder strings.Builder
5700 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5701 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005702 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005703}
5704
Jooyung Hand48f3c32019-08-23 11:18:57 +09005705func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5706 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5707 apex {
5708 name: "myapex",
5709 key: "myapex.key",
5710 native_shared_libs: ["libfoo"],
5711 }
5712
5713 apex_key {
5714 name: "myapex.key",
5715 public_key: "testkey.avbpubkey",
5716 private_key: "testkey.pem",
5717 }
5718
5719 cc_library {
5720 name: "libfoo",
5721 stl: "none",
5722 system_shared_libs: [],
5723 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005724 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005725 }
5726 `)
5727 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5728 apex {
5729 name: "myapex",
5730 key: "myapex.key",
5731 java_libs: ["myjar"],
5732 }
5733
5734 apex_key {
5735 name: "myapex.key",
5736 public_key: "testkey.avbpubkey",
5737 private_key: "testkey.pem",
5738 }
5739
5740 java_library {
5741 name: "myjar",
5742 srcs: ["foo/bar/MyClass.java"],
5743 sdk_version: "none",
5744 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005745 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005746 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005747 }
5748 `)
5749}
5750
Bill Peckhama41a6962021-01-11 10:58:54 -08005751func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005752 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005753 apex {
5754 name: "myapex",
5755 key: "myapex.key",
5756 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005757 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005758 }
5759
5760 apex_key {
5761 name: "myapex.key",
5762 public_key: "testkey.avbpubkey",
5763 private_key: "testkey.pem",
5764 }
5765
5766 java_import {
5767 name: "myjavaimport",
5768 apex_available: ["myapex"],
5769 jars: ["my.jar"],
5770 compile_dex: true,
5771 }
5772 `)
5773
5774 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5775 apexRule := module.Rule("apexRule")
5776 copyCmds := apexRule.Args["copy_commands"]
5777 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5778}
5779
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005780func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005781 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005782 apex {
5783 name: "myapex",
5784 key: "myapex.key",
5785 apps: [
5786 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005787 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005788 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005789 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005790 }
5791
5792 apex_key {
5793 name: "myapex.key",
5794 public_key: "testkey.avbpubkey",
5795 private_key: "testkey.pem",
5796 }
5797
5798 android_app {
5799 name: "AppFoo",
5800 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005801 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005802 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005803 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005804 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005805 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005806 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005807
5808 android_app {
5809 name: "AppFooPriv",
5810 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005811 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005812 system_modules: "none",
5813 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005814 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005815 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005816 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005817
5818 cc_library_shared {
5819 name: "libjni",
5820 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005821 shared_libs: ["libfoo"],
5822 stl: "none",
5823 system_shared_libs: [],
5824 apex_available: [ "myapex" ],
5825 sdk_version: "current",
5826 }
5827
5828 cc_library_shared {
5829 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005830 stl: "none",
5831 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005832 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005833 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005834 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005835 `)
5836
Sundong Ahnabb64432019-10-22 13:58:29 +09005837 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005838 apexRule := module.Rule("apexRule")
5839 copyCmds := apexRule.Args["copy_commands"]
5840
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005841 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5842 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005843
Colin Crossaede88c2020-08-11 12:17:01 -07005844 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005845 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005846 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005847 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005848 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005849 // JNI libraries including transitive deps are
5850 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005851 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005852 // ... embedded inside APK (jnilibs.zip)
5853 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5854 // ... and not directly inside the APEX
5855 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5856 }
Dario Frenicde2a032019-10-27 00:29:22 +01005857}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005858
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005859func TestApexWithAppImportBuildId(t *testing.T) {
5860 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5861 for _, id := range invalidBuildIds {
5862 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5863 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5864 variables.BuildId = proptools.StringPtr(id)
5865 })
5866 testApexError(t, message, `apex {
5867 name: "myapex",
5868 key: "myapex.key",
5869 apps: ["AppFooPrebuilt"],
5870 updatable: false,
5871 }
5872
5873 apex_key {
5874 name: "myapex.key",
5875 public_key: "testkey.avbpubkey",
5876 private_key: "testkey.pem",
5877 }
5878
5879 android_app_import {
5880 name: "AppFooPrebuilt",
5881 apk: "PrebuiltAppFoo.apk",
5882 presigned: true,
5883 apex_available: ["myapex"],
5884 }
5885 `, fixture)
5886 }
5887}
5888
Dario Frenicde2a032019-10-27 00:29:22 +01005889func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005890 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005891 apex {
5892 name: "myapex",
5893 key: "myapex.key",
5894 apps: [
5895 "AppFooPrebuilt",
5896 "AppFooPrivPrebuilt",
5897 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005898 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005899 }
5900
5901 apex_key {
5902 name: "myapex.key",
5903 public_key: "testkey.avbpubkey",
5904 private_key: "testkey.pem",
5905 }
5906
5907 android_app_import {
5908 name: "AppFooPrebuilt",
5909 apk: "PrebuiltAppFoo.apk",
5910 presigned: true,
5911 dex_preopt: {
5912 enabled: false,
5913 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005914 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005915 }
5916
5917 android_app_import {
5918 name: "AppFooPrivPrebuilt",
5919 apk: "PrebuiltAppFooPriv.apk",
5920 privileged: true,
5921 presigned: true,
5922 dex_preopt: {
5923 enabled: false,
5924 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005925 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005926 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005927 }
5928 `)
5929
Sundong Ahnabb64432019-10-22 13:58:29 +09005930 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005931 apexRule := module.Rule("apexRule")
5932 copyCmds := apexRule.Args["copy_commands"]
5933
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005934 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5935 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005936}
5937
5938func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005939 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005940 apex {
5941 name: "myapex",
5942 key: "myapex.key",
5943 apps: [
5944 "AppFoo",
5945 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005946 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005947 }
5948
5949 apex_key {
5950 name: "myapex.key",
5951 public_key: "testkey.avbpubkey",
5952 private_key: "testkey.pem",
5953 }
5954
5955 android_app {
5956 name: "AppFoo",
5957 srcs: ["foo/bar/MyClass.java"],
5958 sdk_version: "none",
5959 system_modules: "none",
5960 apex_available: [ "myapex" ],
5961 }
5962
5963 android_app_import {
5964 name: "AppFoo",
5965 apk: "AppFooPrebuilt.apk",
5966 filename: "AppFooPrebuilt.apk",
5967 presigned: true,
5968 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005969 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005970 }
5971 `, withFiles(map[string][]byte{
5972 "AppFooPrebuilt.apk": nil,
5973 }))
5974
5975 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005976 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005977 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005978}
5979
Dario Freni6f3937c2019-12-20 22:58:03 +00005980func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005981 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005982 apex {
5983 name: "myapex",
5984 key: "myapex.key",
5985 apps: [
5986 "TesterHelpAppFoo",
5987 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005988 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005989 }
5990
5991 apex_key {
5992 name: "myapex.key",
5993 public_key: "testkey.avbpubkey",
5994 private_key: "testkey.pem",
5995 }
5996
5997 android_test_helper_app {
5998 name: "TesterHelpAppFoo",
5999 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006000 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006001 }
6002
6003 `)
6004
6005 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6006 apexRule := module.Rule("apexRule")
6007 copyCmds := apexRule.Args["copy_commands"]
6008
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006009 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006010}
6011
Jooyung Han18020ea2019-11-13 10:50:48 +09006012func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6013 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006014 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006015 apex {
6016 name: "myapex",
6017 key: "myapex.key",
6018 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006019 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006020 }
6021
6022 apex_key {
6023 name: "myapex.key",
6024 public_key: "testkey.avbpubkey",
6025 private_key: "testkey.pem",
6026 }
6027
6028 apex {
6029 name: "otherapex",
6030 key: "myapex.key",
6031 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006032 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006033 }
6034
6035 cc_defaults {
6036 name: "libfoo-defaults",
6037 apex_available: ["otherapex"],
6038 }
6039
6040 cc_library {
6041 name: "libfoo",
6042 defaults: ["libfoo-defaults"],
6043 stl: "none",
6044 system_shared_libs: [],
6045 }`)
6046}
6047
Paul Duffine52e66f2020-03-30 17:54:29 +01006048func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006049 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006050 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006051 apex {
6052 name: "myapex",
6053 key: "myapex.key",
6054 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006055 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006056 }
6057
6058 apex_key {
6059 name: "myapex.key",
6060 public_key: "testkey.avbpubkey",
6061 private_key: "testkey.pem",
6062 }
6063
6064 apex {
6065 name: "otherapex",
6066 key: "otherapex.key",
6067 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006068 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006069 }
6070
6071 apex_key {
6072 name: "otherapex.key",
6073 public_key: "testkey.avbpubkey",
6074 private_key: "testkey.pem",
6075 }
6076
6077 cc_library {
6078 name: "libfoo",
6079 stl: "none",
6080 system_shared_libs: [],
6081 apex_available: ["otherapex"],
6082 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006083}
Jiyong Park127b40b2019-09-30 16:04:35 +09006084
Paul Duffine52e66f2020-03-30 17:54:29 +01006085func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006086 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006087 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006088.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006089.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006090.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006091.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006092.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006093.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006094 apex {
6095 name: "myapex",
6096 key: "myapex.key",
6097 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006098 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006099 }
6100
6101 apex_key {
6102 name: "myapex.key",
6103 public_key: "testkey.avbpubkey",
6104 private_key: "testkey.pem",
6105 }
6106
Jiyong Park127b40b2019-09-30 16:04:35 +09006107 cc_library {
6108 name: "libfoo",
6109 stl: "none",
6110 shared_libs: ["libbar"],
6111 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006112 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006113 }
6114
6115 cc_library {
6116 name: "libbar",
6117 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006118 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006119 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006120 apex_available: ["myapex"],
6121 }
6122
6123 cc_library {
6124 name: "libbaz",
6125 stl: "none",
6126 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006127 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006128}
Jiyong Park127b40b2019-09-30 16:04:35 +09006129
Paul Duffine52e66f2020-03-30 17:54:29 +01006130func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006131 testApexError(t, "\"otherapex\" is not a valid module name", `
6132 apex {
6133 name: "myapex",
6134 key: "myapex.key",
6135 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006136 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006137 }
6138
6139 apex_key {
6140 name: "myapex.key",
6141 public_key: "testkey.avbpubkey",
6142 private_key: "testkey.pem",
6143 }
6144
6145 cc_library {
6146 name: "libfoo",
6147 stl: "none",
6148 system_shared_libs: [],
6149 apex_available: ["otherapex"],
6150 }`)
6151
Paul Duffine52e66f2020-03-30 17:54:29 +01006152 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006153 apex {
6154 name: "myapex",
6155 key: "myapex.key",
6156 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006157 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006158 }
6159
6160 apex_key {
6161 name: "myapex.key",
6162 public_key: "testkey.avbpubkey",
6163 private_key: "testkey.pem",
6164 }
6165
6166 cc_library {
6167 name: "libfoo",
6168 stl: "none",
6169 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006170 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006171 apex_available: ["myapex"],
6172 }
6173
6174 cc_library {
6175 name: "libbar",
6176 stl: "none",
6177 system_shared_libs: [],
6178 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006179 }
6180
6181 cc_library {
6182 name: "libbaz",
6183 stl: "none",
6184 system_shared_libs: [],
6185 stubs: {
6186 versions: ["10", "20", "30"],
6187 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006188 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006189}
Jiyong Park127b40b2019-09-30 16:04:35 +09006190
Jiyong Park89e850a2020-04-07 16:37:39 +09006191func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006192 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006193 apex {
6194 name: "myapex",
6195 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006196 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006197 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006198 }
6199
6200 apex_key {
6201 name: "myapex.key",
6202 public_key: "testkey.avbpubkey",
6203 private_key: "testkey.pem",
6204 }
6205
6206 cc_library {
6207 name: "libfoo",
6208 stl: "none",
6209 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006210 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006211 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006212 }
6213
6214 cc_library {
6215 name: "libfoo2",
6216 stl: "none",
6217 system_shared_libs: [],
6218 shared_libs: ["libbaz"],
6219 apex_available: ["//apex_available:platform"],
6220 }
6221
6222 cc_library {
6223 name: "libbar",
6224 stl: "none",
6225 system_shared_libs: [],
6226 apex_available: ["myapex"],
6227 }
6228
6229 cc_library {
6230 name: "libbaz",
6231 stl: "none",
6232 system_shared_libs: [],
6233 apex_available: ["myapex"],
6234 stubs: {
6235 versions: ["1"],
6236 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006237 }`)
6238
Jiyong Park89e850a2020-04-07 16:37:39 +09006239 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6240 // because it depends on libbar which isn't available to platform
6241 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6242 if libfoo.NotAvailableForPlatform() != true {
6243 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6244 }
6245
6246 // libfoo2 however can be available to platform because it depends on libbaz which provides
6247 // stubs
6248 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6249 if libfoo2.NotAvailableForPlatform() == true {
6250 t.Errorf("%q should be available to platform", libfoo2.String())
6251 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006252}
Jiyong Parka90ca002019-10-07 15:47:24 +09006253
Paul Duffine52e66f2020-03-30 17:54:29 +01006254func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006255 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006256 apex {
6257 name: "myapex",
6258 key: "myapex.key",
6259 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006260 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006261 }
6262
6263 apex_key {
6264 name: "myapex.key",
6265 public_key: "testkey.avbpubkey",
6266 private_key: "testkey.pem",
6267 }
6268
6269 cc_library {
6270 name: "libfoo",
6271 stl: "none",
6272 system_shared_libs: [],
6273 apex_available: ["myapex"],
6274 static: {
6275 apex_available: ["//apex_available:platform"],
6276 },
6277 }`)
6278
Jiyong Park89e850a2020-04-07 16:37:39 +09006279 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6280 if libfooShared.NotAvailableForPlatform() != true {
6281 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6282 }
6283 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6284 if libfooStatic.NotAvailableForPlatform() != false {
6285 t.Errorf("%q should be available to platform", libfooStatic.String())
6286 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006287}
6288
Jiyong Park5d790c32019-11-15 18:40:32 +09006289func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006290 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006291 apex {
6292 name: "myapex",
6293 key: "myapex.key",
6294 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006295 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006296 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006297 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006298 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006299 }
6300
6301 override_apex {
6302 name: "override_myapex",
6303 base: "myapex",
6304 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006305 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006306 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006307 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006308 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006309 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006310 key: "mynewapex.key",
6311 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006312 }
6313
6314 apex_key {
6315 name: "myapex.key",
6316 public_key: "testkey.avbpubkey",
6317 private_key: "testkey.pem",
6318 }
6319
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006320 apex_key {
6321 name: "mynewapex.key",
6322 public_key: "testkey2.avbpubkey",
6323 private_key: "testkey2.pem",
6324 }
6325
6326 android_app_certificate {
6327 name: "myapex.certificate",
6328 certificate: "testkey",
6329 }
6330
Jiyong Park5d790c32019-11-15 18:40:32 +09006331 android_app {
6332 name: "app",
6333 srcs: ["foo/bar/MyClass.java"],
6334 package_name: "foo",
6335 sdk_version: "none",
6336 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006337 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006338 }
6339
6340 override_android_app {
6341 name: "override_app",
6342 base: "app",
6343 package_name: "bar",
6344 }
markchien7c803b82021-08-26 22:10:06 +08006345
6346 bpf {
6347 name: "bpf",
6348 srcs: ["bpf.c"],
6349 }
6350
6351 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006352 name: "overrideBpf",
6353 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006354 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006355
6356 prebuilt_etc {
6357 name: "myetc",
6358 src: "myprebuilt",
6359 }
6360
6361 prebuilt_etc {
6362 name: "override_myetc",
6363 src: "override_myprebuilt",
6364 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006365 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006366
Jiyong Park317645e2019-12-05 13:20:58 +09006367 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6368 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6369 if originalVariant.GetOverriddenBy() != "" {
6370 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6371 }
6372 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6373 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6374 }
6375
Jiyong Park5d790c32019-11-15 18:40:32 +09006376 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6377 apexRule := module.Rule("apexRule")
6378 copyCmds := apexRule.Args["copy_commands"]
6379
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006380 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6381 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006382
markchien7c803b82021-08-26 22:10:06 +08006383 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006384 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006385
Daniel Norman5a3ce132021-08-26 15:44:43 -07006386 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6387 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6388
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006389 apexBundle := module.Module().(*apexBundle)
6390 name := apexBundle.Name()
6391 if name != "override_myapex" {
6392 t.Errorf("name should be \"override_myapex\", but was %q", name)
6393 }
6394
Baligh Uddin004d7172020-02-19 21:29:28 -08006395 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6396 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6397 }
6398
Jiyong Park20bacab2020-03-03 11:45:41 +09006399 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006400 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006401 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6402
6403 signApkRule := module.Rule("signapk")
6404 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006405
Colin Crossaa255532020-07-03 13:18:24 -07006406 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006407 var builder strings.Builder
6408 data.Custom(&builder, name, "TARGET_", "", data)
6409 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006410 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6411 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6412 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006413 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006414 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006415 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006416 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006417 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006418 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6419 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006420}
6421
Albert Martineefabcf2022-03-21 20:11:16 +00006422func TestMinSdkVersionOverride(t *testing.T) {
6423 // Override from 29 to 31
6424 minSdkOverride31 := "31"
6425 ctx := testApex(t, `
6426 apex {
6427 name: "myapex",
6428 key: "myapex.key",
6429 native_shared_libs: ["mylib"],
6430 updatable: true,
6431 min_sdk_version: "29"
6432 }
6433
6434 override_apex {
6435 name: "override_myapex",
6436 base: "myapex",
6437 logging_parent: "com.foo.bar",
6438 package_name: "test.overridden.package"
6439 }
6440
6441 apex_key {
6442 name: "myapex.key",
6443 public_key: "testkey.avbpubkey",
6444 private_key: "testkey.pem",
6445 }
6446
6447 cc_library {
6448 name: "mylib",
6449 srcs: ["mylib.cpp"],
6450 runtime_libs: ["libbar"],
6451 system_shared_libs: [],
6452 stl: "none",
6453 apex_available: [ "myapex" ],
6454 min_sdk_version: "apex_inherit"
6455 }
6456
6457 cc_library {
6458 name: "libbar",
6459 srcs: ["mylib.cpp"],
6460 system_shared_libs: [],
6461 stl: "none",
6462 apex_available: [ "myapex" ],
6463 min_sdk_version: "apex_inherit"
6464 }
6465
6466 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6467
6468 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6469 copyCmds := apexRule.Args["copy_commands"]
6470
6471 // Ensure that direct non-stubs dep is always included
6472 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6473
6474 // Ensure that runtime_libs dep in included
6475 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6476
6477 // Ensure libraries target overridden min_sdk_version value
6478 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6479}
6480
6481func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6482 // Attempt to override from 31 to 29, should be a NOOP
6483 minSdkOverride29 := "29"
6484 ctx := testApex(t, `
6485 apex {
6486 name: "myapex",
6487 key: "myapex.key",
6488 native_shared_libs: ["mylib"],
6489 updatable: true,
6490 min_sdk_version: "31"
6491 }
6492
6493 override_apex {
6494 name: "override_myapex",
6495 base: "myapex",
6496 logging_parent: "com.foo.bar",
6497 package_name: "test.overridden.package"
6498 }
6499
6500 apex_key {
6501 name: "myapex.key",
6502 public_key: "testkey.avbpubkey",
6503 private_key: "testkey.pem",
6504 }
6505
6506 cc_library {
6507 name: "mylib",
6508 srcs: ["mylib.cpp"],
6509 runtime_libs: ["libbar"],
6510 system_shared_libs: [],
6511 stl: "none",
6512 apex_available: [ "myapex" ],
6513 min_sdk_version: "apex_inherit"
6514 }
6515
6516 cc_library {
6517 name: "libbar",
6518 srcs: ["mylib.cpp"],
6519 system_shared_libs: [],
6520 stl: "none",
6521 apex_available: [ "myapex" ],
6522 min_sdk_version: "apex_inherit"
6523 }
6524
6525 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6526
6527 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6528 copyCmds := apexRule.Args["copy_commands"]
6529
6530 // Ensure that direct non-stubs dep is always included
6531 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6532
6533 // Ensure that runtime_libs dep in included
6534 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6535
6536 // Ensure libraries target the original min_sdk_version value rather than the overridden
6537 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6538}
6539
Jooyung Han214bf372019-11-12 13:03:50 +09006540func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006541 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006542 apex {
6543 name: "myapex",
6544 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006545 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006546 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006547 }
6548
6549 apex_key {
6550 name: "myapex.key",
6551 public_key: "testkey.avbpubkey",
6552 private_key: "testkey.pem",
6553 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006554
6555 cc_library {
6556 name: "mylib",
6557 srcs: ["mylib.cpp"],
6558 stl: "libc++",
6559 system_shared_libs: [],
6560 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006561 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006562 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006563 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006564
6565 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6566 args := module.Rule("apexRule").Args
6567 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006568 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006569
6570 // The copies of the libraries in the apex should have one more dependency than
6571 // the ones outside the apex, namely the unwinder. Ideally we should check
6572 // the dependency names directly here but for some reason the names are blank in
6573 // this test.
6574 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006575 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006576 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6577 if len(apexImplicits) != len(nonApexImplicits)+1 {
6578 t.Errorf("%q missing unwinder dep", lib)
6579 }
6580 }
Jooyung Han214bf372019-11-12 13:03:50 +09006581}
6582
Paul Duffine05480a2021-03-08 15:07:14 +00006583var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006584 "api/current.txt": nil,
6585 "api/removed.txt": nil,
6586 "api/system-current.txt": nil,
6587 "api/system-removed.txt": nil,
6588 "api/test-current.txt": nil,
6589 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006590
Anton Hanssondff2c782020-12-21 17:10:01 +00006591 "100/public/api/foo.txt": nil,
6592 "100/public/api/foo-removed.txt": nil,
6593 "100/system/api/foo.txt": nil,
6594 "100/system/api/foo-removed.txt": nil,
6595
Paul Duffineedc5d52020-06-12 17:46:39 +01006596 // For java_sdk_library_import
6597 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006598}
6599
Jooyung Han58f26ab2019-12-18 15:34:32 +09006600func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006601 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006602 apex {
6603 name: "myapex",
6604 key: "myapex.key",
6605 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006606 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006607 }
6608
6609 apex_key {
6610 name: "myapex.key",
6611 public_key: "testkey.avbpubkey",
6612 private_key: "testkey.pem",
6613 }
6614
6615 java_sdk_library {
6616 name: "foo",
6617 srcs: ["a.java"],
6618 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006619 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006620 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006621
6622 prebuilt_apis {
6623 name: "sdk",
6624 api_dirs: ["100"],
6625 }
Paul Duffin9b879592020-05-26 13:21:35 +01006626 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006627
6628 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006629 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006630 "javalib/foo.jar",
6631 "etc/permissions/foo.xml",
6632 })
6633 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006634 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006635 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 +09006636}
6637
Paul Duffin9b879592020-05-26 13:21:35 +01006638func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006639 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006640 apex {
6641 name: "myapex",
6642 key: "myapex.key",
6643 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006644 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006645 }
6646
6647 apex_key {
6648 name: "myapex.key",
6649 public_key: "testkey.avbpubkey",
6650 private_key: "testkey.pem",
6651 }
6652
6653 java_sdk_library {
6654 name: "foo",
6655 srcs: ["a.java"],
6656 api_packages: ["foo"],
6657 apex_available: ["myapex"],
6658 sdk_version: "none",
6659 system_modules: "none",
6660 }
6661
6662 java_library {
6663 name: "bar",
6664 srcs: ["a.java"],
6665 libs: ["foo"],
6666 apex_available: ["myapex"],
6667 sdk_version: "none",
6668 system_modules: "none",
6669 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006670
6671 prebuilt_apis {
6672 name: "sdk",
6673 api_dirs: ["100"],
6674 }
Paul Duffin9b879592020-05-26 13:21:35 +01006675 `, withFiles(filesForSdkLibrary))
6676
6677 // java_sdk_library installs both impl jar and permission XML
6678 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6679 "javalib/bar.jar",
6680 "javalib/foo.jar",
6681 "etc/permissions/foo.xml",
6682 })
6683
6684 // The bar library should depend on the implementation jar.
6685 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006686 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006687 t.Errorf("expected %q, found %#q", expected, actual)
6688 }
6689}
6690
6691func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006692 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006693 apex {
6694 name: "myapex",
6695 key: "myapex.key",
6696 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006697 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006698 }
6699
6700 apex_key {
6701 name: "myapex.key",
6702 public_key: "testkey.avbpubkey",
6703 private_key: "testkey.pem",
6704 }
6705
6706 java_sdk_library {
6707 name: "foo",
6708 srcs: ["a.java"],
6709 api_packages: ["foo"],
6710 apex_available: ["myapex"],
6711 sdk_version: "none",
6712 system_modules: "none",
6713 }
6714
6715 java_library {
6716 name: "bar",
6717 srcs: ["a.java"],
6718 libs: ["foo"],
6719 sdk_version: "none",
6720 system_modules: "none",
6721 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006722
6723 prebuilt_apis {
6724 name: "sdk",
6725 api_dirs: ["100"],
6726 }
Paul Duffin9b879592020-05-26 13:21:35 +01006727 `, withFiles(filesForSdkLibrary))
6728
6729 // java_sdk_library installs both impl jar and permission XML
6730 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6731 "javalib/foo.jar",
6732 "etc/permissions/foo.xml",
6733 })
6734
6735 // The bar library should depend on the stubs jar.
6736 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006737 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006738 t.Errorf("expected %q, found %#q", expected, actual)
6739 }
6740}
6741
Paul Duffineedc5d52020-06-12 17:46:39 +01006742func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006743 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006744 prebuilt_apis {
6745 name: "sdk",
6746 api_dirs: ["100"],
6747 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006748 withFiles(map[string][]byte{
6749 "apex/a.java": nil,
6750 "apex/apex_manifest.json": nil,
6751 "apex/Android.bp": []byte(`
6752 package {
6753 default_visibility: ["//visibility:private"],
6754 }
6755
6756 apex {
6757 name: "myapex",
6758 key: "myapex.key",
6759 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006760 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006761 }
6762
6763 apex_key {
6764 name: "myapex.key",
6765 public_key: "testkey.avbpubkey",
6766 private_key: "testkey.pem",
6767 }
6768
6769 java_library {
6770 name: "bar",
6771 srcs: ["a.java"],
6772 libs: ["foo"],
6773 apex_available: ["myapex"],
6774 sdk_version: "none",
6775 system_modules: "none",
6776 }
6777`),
6778 "source/a.java": nil,
6779 "source/api/current.txt": nil,
6780 "source/api/removed.txt": nil,
6781 "source/Android.bp": []byte(`
6782 package {
6783 default_visibility: ["//visibility:private"],
6784 }
6785
6786 java_sdk_library {
6787 name: "foo",
6788 visibility: ["//apex"],
6789 srcs: ["a.java"],
6790 api_packages: ["foo"],
6791 apex_available: ["myapex"],
6792 sdk_version: "none",
6793 system_modules: "none",
6794 public: {
6795 enabled: true,
6796 },
6797 }
6798`),
6799 "prebuilt/a.jar": nil,
6800 "prebuilt/Android.bp": []byte(`
6801 package {
6802 default_visibility: ["//visibility:private"],
6803 }
6804
6805 java_sdk_library_import {
6806 name: "foo",
6807 visibility: ["//apex", "//source"],
6808 apex_available: ["myapex"],
6809 prefer: true,
6810 public: {
6811 jars: ["a.jar"],
6812 },
6813 }
6814`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006815 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006816 )
6817
6818 // java_sdk_library installs both impl jar and permission XML
6819 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6820 "javalib/bar.jar",
6821 "javalib/foo.jar",
6822 "etc/permissions/foo.xml",
6823 })
6824
6825 // The bar library should depend on the implementation jar.
6826 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006827 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006828 t.Errorf("expected %q, found %#q", expected, actual)
6829 }
6830}
6831
6832func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6833 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6834 apex {
6835 name: "myapex",
6836 key: "myapex.key",
6837 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006838 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006839 }
6840
6841 apex_key {
6842 name: "myapex.key",
6843 public_key: "testkey.avbpubkey",
6844 private_key: "testkey.pem",
6845 }
6846
6847 java_sdk_library_import {
6848 name: "foo",
6849 apex_available: ["myapex"],
6850 prefer: true,
6851 public: {
6852 jars: ["a.jar"],
6853 },
6854 }
6855
6856 `, withFiles(filesForSdkLibrary))
6857}
6858
atrost6e126252020-01-27 17:01:16 +00006859func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006860 result := android.GroupFixturePreparers(
6861 prepareForApexTest,
6862 java.PrepareForTestWithPlatformCompatConfig,
6863 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006864 apex {
6865 name: "myapex",
6866 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006867 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006868 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006869 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006870 }
6871
6872 apex_key {
6873 name: "myapex.key",
6874 public_key: "testkey.avbpubkey",
6875 private_key: "testkey.pem",
6876 }
6877
6878 platform_compat_config {
6879 name: "myjar-platform-compat-config",
6880 src: ":myjar",
6881 }
6882
6883 java_library {
6884 name: "myjar",
6885 srcs: ["foo/bar/MyClass.java"],
6886 sdk_version: "none",
6887 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006888 apex_available: [ "myapex" ],
6889 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006890
6891 // Make sure that a preferred prebuilt does not affect the apex contents.
6892 prebuilt_platform_compat_config {
6893 name: "myjar-platform-compat-config",
6894 metadata: "compat-config/metadata.xml",
6895 prefer: true,
6896 }
atrost6e126252020-01-27 17:01:16 +00006897 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006898 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006899 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6900 "etc/compatconfig/myjar-platform-compat-config.xml",
6901 "javalib/myjar.jar",
6902 })
6903}
6904
Jooyung Han862c0d62022-12-21 10:15:37 +09006905func TestNoDupeApexFiles(t *testing.T) {
6906 android.GroupFixturePreparers(
6907 android.PrepareForTestWithAndroidBuildComponents,
6908 PrepareForTestWithApexBuildComponents,
6909 prepareForTestWithMyapex,
6910 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
6911 ).
6912 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
6913 RunTestWithBp(t, `
6914 apex {
6915 name: "myapex",
6916 key: "myapex.key",
6917 prebuilts: ["foo", "bar"],
6918 updatable: false,
6919 }
6920
6921 apex_key {
6922 name: "myapex.key",
6923 public_key: "testkey.avbpubkey",
6924 private_key: "testkey.pem",
6925 }
6926
6927 prebuilt_etc {
6928 name: "foo",
6929 src: "myprebuilt",
6930 filename_from_src: true,
6931 }
6932
6933 prebuilt_etc {
6934 name: "bar",
6935 src: "myprebuilt",
6936 filename_from_src: true,
6937 }
6938 `)
6939}
6940
Jiyong Park479321d2019-12-16 11:47:12 +09006941func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6942 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6943 apex {
6944 name: "myapex",
6945 key: "myapex.key",
6946 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006947 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006948 }
6949
6950 apex_key {
6951 name: "myapex.key",
6952 public_key: "testkey.avbpubkey",
6953 private_key: "testkey.pem",
6954 }
6955
6956 java_library {
6957 name: "myjar",
6958 srcs: ["foo/bar/MyClass.java"],
6959 sdk_version: "none",
6960 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006961 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006962 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006963 }
6964 `)
6965}
6966
Jiyong Park7afd1072019-12-30 16:56:33 +09006967func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006968 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006969 apex {
6970 name: "myapex",
6971 key: "myapex.key",
6972 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006973 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006974 }
6975
6976 apex_key {
6977 name: "myapex.key",
6978 public_key: "testkey.avbpubkey",
6979 private_key: "testkey.pem",
6980 }
6981
6982 cc_library {
6983 name: "mylib",
6984 srcs: ["mylib.cpp"],
6985 system_shared_libs: [],
6986 stl: "none",
6987 required: ["a", "b"],
6988 host_required: ["c", "d"],
6989 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006990 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006991 }
6992 `)
6993
6994 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006995 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006996 name := apexBundle.BaseModuleName()
6997 prefix := "TARGET_"
6998 var builder strings.Builder
6999 data.Custom(&builder, name, prefix, "", data)
7000 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007001 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 -08007002 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7003 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007004}
7005
Jiyong Park7cd10e32020-01-14 09:22:18 +09007006func TestSymlinksFromApexToSystem(t *testing.T) {
7007 bp := `
7008 apex {
7009 name: "myapex",
7010 key: "myapex.key",
7011 native_shared_libs: ["mylib"],
7012 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007013 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007014 }
7015
Jiyong Park9d677202020-02-19 16:29:35 +09007016 apex {
7017 name: "myapex.updatable",
7018 key: "myapex.key",
7019 native_shared_libs: ["mylib"],
7020 java_libs: ["myjar"],
7021 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09007022 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09007023 }
7024
Jiyong Park7cd10e32020-01-14 09:22:18 +09007025 apex_key {
7026 name: "myapex.key",
7027 public_key: "testkey.avbpubkey",
7028 private_key: "testkey.pem",
7029 }
7030
7031 cc_library {
7032 name: "mylib",
7033 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007034 shared_libs: [
7035 "myotherlib",
7036 "myotherlib_ext",
7037 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007038 system_shared_libs: [],
7039 stl: "none",
7040 apex_available: [
7041 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007042 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007043 "//apex_available:platform",
7044 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007045 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007046 }
7047
7048 cc_library {
7049 name: "myotherlib",
7050 srcs: ["mylib.cpp"],
7051 system_shared_libs: [],
7052 stl: "none",
7053 apex_available: [
7054 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007055 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007056 "//apex_available:platform",
7057 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007058 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007059 }
7060
Jiyong Parkce243632023-02-17 18:22:25 +09007061 cc_library {
7062 name: "myotherlib_ext",
7063 srcs: ["mylib.cpp"],
7064 system_shared_libs: [],
7065 system_ext_specific: true,
7066 stl: "none",
7067 apex_available: [
7068 "myapex",
7069 "myapex.updatable",
7070 "//apex_available:platform",
7071 ],
7072 min_sdk_version: "current",
7073 }
7074
Jiyong Park7cd10e32020-01-14 09:22:18 +09007075 java_library {
7076 name: "myjar",
7077 srcs: ["foo/bar/MyClass.java"],
7078 sdk_version: "none",
7079 system_modules: "none",
7080 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007081 apex_available: [
7082 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007083 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007084 "//apex_available:platform",
7085 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007086 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007087 }
7088
7089 java_library {
7090 name: "myotherjar",
7091 srcs: ["foo/bar/MyClass.java"],
7092 sdk_version: "none",
7093 system_modules: "none",
7094 apex_available: [
7095 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007096 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007097 "//apex_available:platform",
7098 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007099 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007100 }
7101 `
7102
7103 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7104 for _, f := range files {
7105 if f.path == file {
7106 if f.isLink {
7107 t.Errorf("%q is not a real file", file)
7108 }
7109 return
7110 }
7111 }
7112 t.Errorf("%q is not found", file)
7113 }
7114
Jiyong Parkce243632023-02-17 18:22:25 +09007115 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007116 for _, f := range files {
7117 if f.path == file {
7118 if !f.isLink {
7119 t.Errorf("%q is not a symlink", file)
7120 }
Jiyong Parkce243632023-02-17 18:22:25 +09007121 if f.src != target {
7122 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7123 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007124 return
7125 }
7126 }
7127 t.Errorf("%q is not found", file)
7128 }
7129
Jiyong Park9d677202020-02-19 16:29:35 +09007130 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7131 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007132 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007133 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007134 ensureRealfileExists(t, files, "javalib/myjar.jar")
7135 ensureRealfileExists(t, files, "lib64/mylib.so")
7136 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007137 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007138
Jiyong Park9d677202020-02-19 16:29:35 +09007139 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7140 ensureRealfileExists(t, files, "javalib/myjar.jar")
7141 ensureRealfileExists(t, files, "lib64/mylib.so")
7142 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007143 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007144
7145 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007146 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007147 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007148 ensureRealfileExists(t, files, "javalib/myjar.jar")
7149 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007150 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7151 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007152
7153 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7154 ensureRealfileExists(t, files, "javalib/myjar.jar")
7155 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007156 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7157 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007158}
7159
Yo Chiange8128052020-07-23 20:09:18 +08007160func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007161 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007162 apex {
7163 name: "myapex",
7164 key: "myapex.key",
7165 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007166 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007167 }
7168
7169 apex_key {
7170 name: "myapex.key",
7171 public_key: "testkey.avbpubkey",
7172 private_key: "testkey.pem",
7173 }
7174
7175 cc_library_shared {
7176 name: "mylib",
7177 srcs: ["mylib.cpp"],
7178 shared_libs: ["myotherlib"],
7179 system_shared_libs: [],
7180 stl: "none",
7181 apex_available: [
7182 "myapex",
7183 "//apex_available:platform",
7184 ],
7185 }
7186
7187 cc_prebuilt_library_shared {
7188 name: "myotherlib",
7189 srcs: ["prebuilt.so"],
7190 system_shared_libs: [],
7191 stl: "none",
7192 apex_available: [
7193 "myapex",
7194 "//apex_available:platform",
7195 ],
7196 }
7197 `)
7198
Prerana Patilb1896c82022-11-09 18:14:34 +00007199 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007200 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007201 var builder strings.Builder
7202 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7203 androidMk := builder.String()
7204 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007205 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007206 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7207 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7208 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007209 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 +08007210}
7211
Jooyung Han643adc42020-02-27 13:50:06 +09007212func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007213 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007214 apex {
7215 name: "myapex",
7216 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007217 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007218 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007219 }
7220
7221 apex_key {
7222 name: "myapex.key",
7223 public_key: "testkey.avbpubkey",
7224 private_key: "testkey.pem",
7225 }
7226
7227 cc_library {
7228 name: "mylib",
7229 srcs: ["mylib.cpp"],
7230 shared_libs: ["mylib2"],
7231 system_shared_libs: [],
7232 stl: "none",
7233 apex_available: [ "myapex" ],
7234 }
7235
7236 cc_library {
7237 name: "mylib2",
7238 srcs: ["mylib.cpp"],
7239 system_shared_libs: [],
7240 stl: "none",
7241 apex_available: [ "myapex" ],
7242 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007243
7244 rust_ffi_shared {
7245 name: "libfoo.rust",
7246 crate_name: "foo",
7247 srcs: ["foo.rs"],
7248 shared_libs: ["libfoo.shared_from_rust"],
7249 prefer_rlib: true,
7250 apex_available: ["myapex"],
7251 }
7252
7253 cc_library_shared {
7254 name: "libfoo.shared_from_rust",
7255 srcs: ["mylib.cpp"],
7256 system_shared_libs: [],
7257 stl: "none",
7258 stubs: {
7259 versions: ["10", "11", "12"],
7260 },
7261 }
7262
Jooyung Han643adc42020-02-27 13:50:06 +09007263 `)
7264
7265 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7266 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007267 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007268 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7269 "lib64/mylib.so",
7270 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007271 "lib64/libfoo.rust.so",
7272 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7273 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007274 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007275
7276 // b/220397949
7277 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007278}
7279
Jooyung Han49f67012020-04-17 13:43:10 +09007280func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007281 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007282 apex {
7283 name: "myapex",
7284 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007285 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007286 }
7287 apex_key {
7288 name: "myapex.key",
7289 public_key: "testkey.avbpubkey",
7290 private_key: "testkey.pem",
7291 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007292 `,
7293 android.FixtureModifyConfig(func(config android.Config) {
7294 delete(config.Targets, android.Android)
7295 config.AndroidCommonTarget = android.Target{}
7296 }),
7297 )
Jooyung Han49f67012020-04-17 13:43:10 +09007298
7299 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7300 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7301 }
7302}
7303
Jiyong Parkbd159612020-02-28 15:22:21 +09007304func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007305 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007306 apex {
7307 name: "myapex",
7308 key: "myapex.key",
7309 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007310 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007311 }
7312
7313 apex_key {
7314 name: "myapex.key",
7315 public_key: "testkey.avbpubkey",
7316 private_key: "testkey.pem",
7317 }
7318
7319 android_app {
7320 name: "AppFoo",
7321 srcs: ["foo/bar/MyClass.java"],
7322 sdk_version: "none",
7323 system_modules: "none",
7324 apex_available: [ "myapex" ],
7325 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007326 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007327
Colin Crosscf371cc2020-11-13 11:48:42 -08007328 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007329 content := bundleConfigRule.Args["content"]
7330
7331 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007332 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 +09007333}
7334
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007335func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007336 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007337 apex {
7338 name: "myapex",
7339 key: "myapex.key",
7340 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007341 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007342 }
7343
7344 apex_key {
7345 name: "myapex.key",
7346 public_key: "testkey.avbpubkey",
7347 private_key: "testkey.pem",
7348 }
7349
7350 android_app_set {
7351 name: "AppSet",
7352 set: "AppSet.apks",
7353 }`)
7354 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007355 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007356 content := bundleConfigRule.Args["content"]
7357 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7358 s := mod.Rule("apexRule").Args["copy_commands"]
7359 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007360 if len(copyCmds) != 4 {
7361 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007362 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007363 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7364 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007365 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7366 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007367
7368 // Ensure that canned_fs_config has an entry for the app set zip file
7369 generateFsRule := mod.Rule("generateFsConfig")
7370 cmd := generateFsRule.RuleParams.Command
7371 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007372}
7373
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007374func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007375 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007376 apex_set {
7377 name: "myapex",
7378 filename: "foo_v2.apex",
7379 sanitized: {
7380 none: { set: "myapex.apks", },
7381 hwaddress: { set: "myapex.hwasan.apks", },
7382 },
Paul Duffin24704672021-04-06 16:09:30 +01007383 }
7384 `
7385 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007386
Paul Duffin24704672021-04-06 16:09:30 +01007387 // Check that the extractor produces the correct output file from the correct input file.
7388 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007389
Paul Duffin24704672021-04-06 16:09:30 +01007390 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7391 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007392
Paul Duffin24704672021-04-06 16:09:30 +01007393 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7394
7395 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007396 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7397 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007398
7399 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007400}
7401
Pranav Guptaeba03b02022-09-27 00:27:08 +00007402func TestApexSetApksModuleAssignment(t *testing.T) {
7403 ctx := testApex(t, `
7404 apex_set {
7405 name: "myapex",
7406 set: ":myapex_apks_file",
7407 }
7408
7409 filegroup {
7410 name: "myapex_apks_file",
7411 srcs: ["myapex.apks"],
7412 }
7413 `)
7414
7415 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7416
7417 // Check that the extractor produces the correct apks file from the input module
7418 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7419 extractedApex := m.Output(extractorOutput)
7420
7421 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7422}
7423
Paul Duffin89f570a2021-06-16 01:42:33 +01007424func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007425 t.Helper()
7426
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007427 bp := `
7428 java_library {
7429 name: "some-updatable-apex-lib",
7430 srcs: ["a.java"],
7431 sdk_version: "current",
7432 apex_available: [
7433 "some-updatable-apex",
7434 ],
satayevabcd5972021-08-06 17:49:46 +01007435 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007436 }
7437
7438 java_library {
7439 name: "some-non-updatable-apex-lib",
7440 srcs: ["a.java"],
7441 apex_available: [
7442 "some-non-updatable-apex",
7443 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007444 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007445 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007446 }
7447
7448 bootclasspath_fragment {
7449 name: "some-non-updatable-fragment",
7450 contents: ["some-non-updatable-apex-lib"],
7451 apex_available: [
7452 "some-non-updatable-apex",
7453 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007454 hidden_api: {
7455 split_packages: ["*"],
7456 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007457 }
7458
7459 java_library {
7460 name: "some-platform-lib",
7461 srcs: ["a.java"],
7462 sdk_version: "current",
7463 installable: true,
7464 }
7465
7466 java_library {
7467 name: "some-art-lib",
7468 srcs: ["a.java"],
7469 sdk_version: "current",
7470 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007471 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007472 ],
7473 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007474 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007475 }
7476
7477 apex {
7478 name: "some-updatable-apex",
7479 key: "some-updatable-apex.key",
7480 java_libs: ["some-updatable-apex-lib"],
7481 updatable: true,
7482 min_sdk_version: "current",
7483 }
7484
7485 apex {
7486 name: "some-non-updatable-apex",
7487 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007488 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007489 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007490 }
7491
7492 apex_key {
7493 name: "some-updatable-apex.key",
7494 }
7495
7496 apex_key {
7497 name: "some-non-updatable-apex.key",
7498 }
7499
7500 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007501 name: "com.android.art.debug",
7502 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007503 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007504 updatable: true,
7505 min_sdk_version: "current",
7506 }
7507
Paul Duffinf23bc472021-04-27 12:42:20 +01007508 bootclasspath_fragment {
7509 name: "art-bootclasspath-fragment",
7510 image_name: "art",
7511 contents: ["some-art-lib"],
7512 apex_available: [
7513 "com.android.art.debug",
7514 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007515 hidden_api: {
7516 split_packages: ["*"],
7517 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007518 }
7519
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007520 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007521 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007522 }
7523
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007524 filegroup {
7525 name: "some-updatable-apex-file_contexts",
7526 srcs: [
7527 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7528 ],
7529 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007530
7531 filegroup {
7532 name: "some-non-updatable-apex-file_contexts",
7533 srcs: [
7534 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7535 ],
7536 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007537 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007538
Paul Duffin89f570a2021-06-16 01:42:33 +01007539 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007540}
7541
Paul Duffin89f570a2021-06-16 01:42:33 +01007542func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007543 t.Helper()
7544
Paul Duffin55607122021-03-30 23:32:51 +01007545 fs := android.MockFS{
7546 "a.java": nil,
7547 "a.jar": nil,
7548 "apex_manifest.json": nil,
7549 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007550 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007551 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7552 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7553 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007554 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007555 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007556
Paul Duffin55607122021-03-30 23:32:51 +01007557 errorHandler := android.FixtureExpectsNoErrors
7558 if errmsg != "" {
7559 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007560 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007561
Paul Duffin55607122021-03-30 23:32:51 +01007562 result := android.GroupFixturePreparers(
7563 cc.PrepareForTestWithCcDefaultModules,
7564 java.PrepareForTestWithHiddenApiBuildComponents,
7565 java.PrepareForTestWithJavaDefaultModules,
7566 java.PrepareForTestWithJavaSdkLibraryFiles,
7567 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007568 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007569 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007570 android.FixtureModifyMockFS(func(fs android.MockFS) {
7571 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7572 insert := ""
7573 for _, fragment := range fragments {
7574 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7575 }
7576 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7577 platform_bootclasspath {
7578 name: "platform-bootclasspath",
7579 fragments: [
7580 %s
7581 ],
7582 }
7583 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007584 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007585 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007586 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007587 ).
7588 ExtendWithErrorHandler(errorHandler).
7589 RunTestWithBp(t, bp)
7590
7591 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007592}
7593
Paul Duffin5556c5f2022-06-09 17:32:21 +00007594func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007595 preparers := android.GroupFixturePreparers(
7596 java.PrepareForTestWithJavaDefaultModules,
7597 PrepareForTestWithApexBuildComponents,
7598 ).
7599 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7600 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7601
7602 bpBase := `
7603 apex_set {
7604 name: "com.android.myapex",
7605 installable: true,
7606 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7607 set: "myapex.apks",
7608 }
7609
7610 apex_set {
7611 name: "com.mycompany.android.myapex",
7612 apex_name: "com.android.myapex",
7613 installable: true,
7614 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7615 set: "company-myapex.apks",
7616 }
7617
7618 prebuilt_bootclasspath_fragment {
7619 name: "my-bootclasspath-fragment",
7620 apex_available: ["com.android.myapex"],
7621 %s
7622 }
7623 `
7624
7625 t.Run("java_import", func(t *testing.T) {
7626 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7627 java_import {
7628 name: "libfoo",
7629 jars: ["libfoo.jar"],
7630 apex_available: ["com.android.myapex"],
7631 }
7632 `)
7633 })
7634
7635 t.Run("java_sdk_library_import", func(t *testing.T) {
7636 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7637 java_sdk_library_import {
7638 name: "libfoo",
7639 public: {
7640 jars: ["libbar.jar"],
7641 },
7642 apex_available: ["com.android.myapex"],
7643 }
7644 `)
7645 })
7646
7647 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7648 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7649 image_name: "art",
7650 contents: ["libfoo"],
7651 `)+`
7652 java_sdk_library_import {
7653 name: "libfoo",
7654 public: {
7655 jars: ["libbar.jar"],
7656 },
7657 apex_available: ["com.android.myapex"],
7658 }
7659 `)
7660 })
7661}
7662
Paul Duffin5556c5f2022-06-09 17:32:21 +00007663func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7664 preparers := android.GroupFixturePreparers(
7665 java.PrepareForTestWithJavaDefaultModules,
7666 PrepareForTestWithApexBuildComponents,
7667 )
7668
7669 bpBase := `
7670 apex_set {
7671 name: "com.android.myapex",
7672 installable: true,
7673 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7674 set: "myapex.apks",
7675 }
7676
7677 apex_set {
7678 name: "com.android.myapex_compressed",
7679 apex_name: "com.android.myapex",
7680 installable: true,
7681 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7682 set: "myapex_compressed.apks",
7683 }
7684
7685 prebuilt_bootclasspath_fragment {
7686 name: "my-bootclasspath-fragment",
7687 apex_available: [
7688 "com.android.myapex",
7689 "com.android.myapex_compressed",
7690 ],
7691 hidden_api: {
7692 annotation_flags: "annotation-flags.csv",
7693 metadata: "metadata.csv",
7694 index: "index.csv",
7695 signature_patterns: "signature_patterns.csv",
7696 },
7697 %s
7698 }
7699 `
7700
7701 t.Run("java_import", func(t *testing.T) {
7702 result := preparers.RunTestWithBp(t,
7703 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7704 java_import {
7705 name: "libfoo",
7706 jars: ["libfoo.jar"],
7707 apex_available: [
7708 "com.android.myapex",
7709 "com.android.myapex_compressed",
7710 ],
7711 }
7712 `)
7713
7714 module := result.Module("libfoo", "android_common_com.android.myapex")
7715 usesLibraryDep := module.(java.UsesLibraryDependency)
7716 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7717 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7718 usesLibraryDep.DexJarBuildPath().Path())
7719 })
7720
7721 t.Run("java_sdk_library_import", func(t *testing.T) {
7722 result := preparers.RunTestWithBp(t,
7723 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7724 java_sdk_library_import {
7725 name: "libfoo",
7726 public: {
7727 jars: ["libbar.jar"],
7728 },
7729 apex_available: [
7730 "com.android.myapex",
7731 "com.android.myapex_compressed",
7732 ],
7733 compile_dex: true,
7734 }
7735 `)
7736
7737 module := result.Module("libfoo", "android_common_com.android.myapex")
7738 usesLibraryDep := module.(java.UsesLibraryDependency)
7739 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7740 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7741 usesLibraryDep.DexJarBuildPath().Path())
7742 })
7743
7744 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7745 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7746 image_name: "art",
7747 contents: ["libfoo"],
7748 `)+`
7749 java_sdk_library_import {
7750 name: "libfoo",
7751 public: {
7752 jars: ["libbar.jar"],
7753 },
7754 apex_available: [
7755 "com.android.myapex",
7756 "com.android.myapex_compressed",
7757 ],
7758 compile_dex: true,
7759 }
7760 `)
7761 })
7762}
7763
Jooyung Han548640b2020-04-27 12:10:30 +09007764func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7765 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7766 apex {
7767 name: "myapex",
7768 key: "myapex.key",
7769 updatable: true,
7770 }
7771
7772 apex_key {
7773 name: "myapex.key",
7774 public_key: "testkey.avbpubkey",
7775 private_key: "testkey.pem",
7776 }
7777 `)
7778}
7779
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007780func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7781 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7782 apex {
7783 name: "myapex",
7784 key: "myapex.key",
7785 }
7786
7787 apex_key {
7788 name: "myapex.key",
7789 public_key: "testkey.avbpubkey",
7790 private_key: "testkey.pem",
7791 }
7792 `)
7793}
7794
Daniel Norman69109112021-12-02 12:52:42 -08007795func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7796 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7797 apex {
7798 name: "myapex",
7799 key: "myapex.key",
7800 updatable: true,
7801 soc_specific: true,
7802 }
7803
7804 apex_key {
7805 name: "myapex.key",
7806 public_key: "testkey.avbpubkey",
7807 private_key: "testkey.pem",
7808 }
7809 `)
7810}
7811
satayevb98371c2021-06-15 16:49:50 +01007812func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7813 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7814 apex {
7815 name: "myapex",
7816 key: "myapex.key",
7817 systemserverclasspath_fragments: [
7818 "mysystemserverclasspathfragment",
7819 ],
7820 min_sdk_version: "29",
7821 updatable: true,
7822 }
7823
7824 apex_key {
7825 name: "myapex.key",
7826 public_key: "testkey.avbpubkey",
7827 private_key: "testkey.pem",
7828 }
7829
7830 java_library {
7831 name: "foo",
7832 srcs: ["b.java"],
7833 min_sdk_version: "29",
7834 installable: true,
7835 apex_available: [
7836 "myapex",
7837 ],
7838 }
7839
7840 systemserverclasspath_fragment {
7841 name: "mysystemserverclasspathfragment",
7842 generate_classpaths_proto: false,
7843 contents: [
7844 "foo",
7845 ],
7846 apex_available: [
7847 "myapex",
7848 ],
7849 }
satayevabcd5972021-08-06 17:49:46 +01007850 `,
7851 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7852 )
satayevb98371c2021-06-15 16:49:50 +01007853}
7854
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007855func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007856 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7857 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7858 // modules to be included in the BootJars.
7859 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7860 return android.GroupFixturePreparers(
7861 dexpreopt.FixtureSetBootJars(bootJars...),
7862 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7863 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7864 }),
7865 )
7866 }
7867
7868 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7869 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7870 // specified in the ArtApexJars configuration.
7871 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7872 return android.GroupFixturePreparers(
7873 dexpreopt.FixtureSetArtBootJars(bootJars...),
7874 dexpreopt.FixtureSetBootJars(bootJars...),
7875 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7876 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7877 }),
7878 )
7879 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007880
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007881 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007882 preparer := android.GroupFixturePreparers(
7883 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7884 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7885 )
7886 fragments := []java.ApexVariantReference{
7887 {
7888 Apex: proptools.StringPtr("com.android.art.debug"),
7889 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7890 },
7891 {
7892 Apex: proptools.StringPtr("some-non-updatable-apex"),
7893 Module: proptools.StringPtr("some-non-updatable-fragment"),
7894 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007895 }
satayevabcd5972021-08-06 17:49:46 +01007896 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007897 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007898
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007899 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007900 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7901 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007902 preparer := android.GroupFixturePreparers(
7903 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7904 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7905 )
Paul Duffin60264a02021-04-12 20:02:36 +01007906 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007907 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007908
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007909 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 +01007910 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 +01007911 // Update the dexpreopt ArtApexJars directly.
7912 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7913 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007914 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007915
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007916 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 +01007917 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 +01007918 // Update the dexpreopt ArtApexJars directly.
7919 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7920 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007921 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007922
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007923 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 +01007924 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 +01007925 preparer := android.GroupFixturePreparers(
7926 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7927 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7928 )
Paul Duffin60264a02021-04-12 20:02:36 +01007929 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007930 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007931
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007932 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 +01007933 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007934 fragment := java.ApexVariantReference{
7935 Apex: proptools.StringPtr("some-non-updatable-apex"),
7936 Module: proptools.StringPtr("some-non-updatable-fragment"),
7937 }
7938 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007939 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007940
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007941 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007942 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007943 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7944 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007945 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007946
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007947 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007948 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007949 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7950 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007951 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007952
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007953 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007954 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007955 // Update the dexpreopt ArtApexJars directly.
7956 preparer := prepareSetArtJars("platform:some-platform-lib")
7957 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007958 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007959
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007960 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007961 preparer := android.GroupFixturePreparers(
7962 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7963 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7964 )
7965 fragments := []java.ApexVariantReference{
7966 {
7967 Apex: proptools.StringPtr("some-non-updatable-apex"),
7968 Module: proptools.StringPtr("some-non-updatable-fragment"),
7969 },
7970 }
7971 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007972 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007973}
7974
7975func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007976 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007977 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007978 fragment := java.ApexVariantReference{
7979 Apex: proptools.StringPtr("myapex"),
7980 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7981 }
7982
Paul Duffin064b70c2020-11-02 17:32:38 +00007983 testDexpreoptWithApexes(t, `
7984 prebuilt_apex {
7985 name: "myapex" ,
7986 arch: {
7987 arm64: {
7988 src: "myapex-arm64.apex",
7989 },
7990 arm: {
7991 src: "myapex-arm.apex",
7992 },
7993 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007994 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7995 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007996
Paul Duffin89f570a2021-06-16 01:42:33 +01007997 prebuilt_bootclasspath_fragment {
7998 name: "my-bootclasspath-fragment",
7999 contents: ["libfoo"],
8000 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008001 hidden_api: {
8002 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8003 metadata: "my-bootclasspath-fragment/metadata.csv",
8004 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008005 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8006 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8007 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008008 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008009 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008010
Paul Duffin89f570a2021-06-16 01:42:33 +01008011 java_import {
8012 name: "libfoo",
8013 jars: ["libfoo.jar"],
8014 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008015 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008016 }
8017 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008018 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008019}
8020
Spandan Dasf14e2542021-11-12 00:01:37 +00008021func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008022 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008023 bp += `
8024 apex_key {
8025 name: "myapex.key",
8026 public_key: "testkey.avbpubkey",
8027 private_key: "testkey.pem",
8028 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008029 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008030 "lib1/src/A.java": nil,
8031 "lib2/src/B.java": nil,
8032 "system/sepolicy/apex/myapex-file_contexts": nil,
8033 }
8034
Paul Duffin45338f02021-03-30 23:07:52 +01008035 errorHandler := android.FixtureExpectsNoErrors
8036 if errmsg != "" {
8037 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008038 }
Colin Crossae8600b2020-10-29 17:09:13 -07008039
Paul Duffin45338f02021-03-30 23:07:52 +01008040 android.GroupFixturePreparers(
8041 android.PrepareForTestWithAndroidBuildComponents,
8042 java.PrepareForTestWithJavaBuildComponents,
8043 PrepareForTestWithApexBuildComponents,
8044 android.PrepareForTestWithNeverallowRules(rules),
8045 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008046 apexBootJars := make([]string, 0, len(bootJars))
8047 for _, apexBootJar := range bootJars {
8048 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008049 }
satayevd604b212021-07-21 14:23:52 +01008050 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008051 }),
8052 fs.AddToFixture(),
8053 ).
8054 ExtendWithErrorHandler(errorHandler).
8055 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008056}
8057
8058func TestApexPermittedPackagesRules(t *testing.T) {
8059 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008060 name string
8061 expectedError string
8062 bp string
8063 bootJars []string
8064 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008065 }{
8066
8067 {
8068 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8069 expectedError: "",
8070 bp: `
8071 java_library {
8072 name: "bcp_lib1",
8073 srcs: ["lib1/src/*.java"],
8074 permitted_packages: ["foo.bar"],
8075 apex_available: ["myapex"],
8076 sdk_version: "none",
8077 system_modules: "none",
8078 }
8079 java_library {
8080 name: "nonbcp_lib2",
8081 srcs: ["lib2/src/*.java"],
8082 apex_available: ["myapex"],
8083 permitted_packages: ["a.b"],
8084 sdk_version: "none",
8085 system_modules: "none",
8086 }
8087 apex {
8088 name: "myapex",
8089 key: "myapex.key",
8090 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008091 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008092 }`,
8093 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008094 bcpPermittedPackages: map[string][]string{
8095 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008096 "foo.bar",
8097 },
8098 },
8099 },
8100 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008101 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008102 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 +01008103 bp: `
8104 java_library {
8105 name: "bcp_lib1",
8106 srcs: ["lib1/src/*.java"],
8107 apex_available: ["myapex"],
8108 permitted_packages: ["foo.bar"],
8109 sdk_version: "none",
8110 system_modules: "none",
8111 }
8112 java_library {
8113 name: "bcp_lib2",
8114 srcs: ["lib2/src/*.java"],
8115 apex_available: ["myapex"],
8116 permitted_packages: ["foo.bar", "bar.baz"],
8117 sdk_version: "none",
8118 system_modules: "none",
8119 }
8120 apex {
8121 name: "myapex",
8122 key: "myapex.key",
8123 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008124 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008125 }
8126 `,
8127 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008128 bcpPermittedPackages: map[string][]string{
8129 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008130 "foo.bar",
8131 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008132 "bcp_lib2": []string{
8133 "foo.bar",
8134 },
8135 },
8136 },
8137 {
8138 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8139 expectedError: "",
8140 bp: `
8141 java_library {
8142 name: "bcp_lib_restricted",
8143 srcs: ["lib1/src/*.java"],
8144 apex_available: ["myapex"],
8145 permitted_packages: ["foo.bar"],
8146 sdk_version: "none",
8147 min_sdk_version: "29",
8148 system_modules: "none",
8149 }
8150 java_library {
8151 name: "bcp_lib_unrestricted",
8152 srcs: ["lib2/src/*.java"],
8153 apex_available: ["myapex"],
8154 permitted_packages: ["foo.bar", "bar.baz"],
8155 sdk_version: "none",
8156 min_sdk_version: "29",
8157 system_modules: "none",
8158 }
8159 apex {
8160 name: "myapex",
8161 key: "myapex.key",
8162 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8163 updatable: true,
8164 min_sdk_version: "29",
8165 }
8166 `,
8167 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8168 bcpPermittedPackages: map[string][]string{
8169 "bcp_lib1_non_updateable": []string{
8170 "foo.bar",
8171 },
8172 // 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 +01008173 },
8174 },
8175 }
8176 for _, tc := range testcases {
8177 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008178 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8179 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008180 })
8181 }
8182}
8183
Jiyong Park62304bb2020-04-13 16:19:48 +09008184func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008185 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008186 apex {
8187 name: "myapex",
8188 key: "myapex.key",
8189 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008190 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008191 }
8192
8193 apex_key {
8194 name: "myapex.key",
8195 public_key: "testkey.avbpubkey",
8196 private_key: "testkey.pem",
8197 }
8198
8199 cc_library {
8200 name: "mylib",
8201 srcs: ["mylib.cpp"],
8202 system_shared_libs: [],
8203 stl: "none",
8204 stubs: {
8205 versions: ["1"],
8206 },
8207 apex_available: ["myapex"],
8208 }
8209
8210 cc_library {
8211 name: "myprivlib",
8212 srcs: ["mylib.cpp"],
8213 system_shared_libs: [],
8214 stl: "none",
8215 apex_available: ["myapex"],
8216 }
8217
8218
8219 cc_test {
8220 name: "mytest",
8221 gtest: false,
8222 srcs: ["mylib.cpp"],
8223 system_shared_libs: [],
8224 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008225 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008226 test_for: ["myapex"]
8227 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008228
8229 cc_library {
8230 name: "mytestlib",
8231 srcs: ["mylib.cpp"],
8232 system_shared_libs: [],
8233 shared_libs: ["mylib", "myprivlib"],
8234 stl: "none",
8235 test_for: ["myapex"],
8236 }
8237
8238 cc_benchmark {
8239 name: "mybench",
8240 srcs: ["mylib.cpp"],
8241 system_shared_libs: [],
8242 shared_libs: ["mylib", "myprivlib"],
8243 stl: "none",
8244 test_for: ["myapex"],
8245 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008246 `)
8247
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008248 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008249 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008250 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8251 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8252 }
8253
8254 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008255 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008256 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8257 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8258 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8259}
Jiyong Park46a512f2020-12-04 18:02:13 +09008260
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008261func TestIndirectTestFor(t *testing.T) {
8262 ctx := testApex(t, `
8263 apex {
8264 name: "myapex",
8265 key: "myapex.key",
8266 native_shared_libs: ["mylib", "myprivlib"],
8267 updatable: false,
8268 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008269
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008270 apex_key {
8271 name: "myapex.key",
8272 public_key: "testkey.avbpubkey",
8273 private_key: "testkey.pem",
8274 }
8275
8276 cc_library {
8277 name: "mylib",
8278 srcs: ["mylib.cpp"],
8279 system_shared_libs: [],
8280 stl: "none",
8281 stubs: {
8282 versions: ["1"],
8283 },
8284 apex_available: ["myapex"],
8285 }
8286
8287 cc_library {
8288 name: "myprivlib",
8289 srcs: ["mylib.cpp"],
8290 system_shared_libs: [],
8291 stl: "none",
8292 shared_libs: ["mylib"],
8293 apex_available: ["myapex"],
8294 }
8295
8296 cc_library {
8297 name: "mytestlib",
8298 srcs: ["mylib.cpp"],
8299 system_shared_libs: [],
8300 shared_libs: ["myprivlib"],
8301 stl: "none",
8302 test_for: ["myapex"],
8303 }
8304 `)
8305
8306 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008307 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008308 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8309 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8310 }
8311
8312 // The platform variant of mytestlib links to the platform variant of the
8313 // internal myprivlib.
8314 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8315
8316 // The platform variant of myprivlib links to the platform variant of mylib
8317 // and bypasses its stubs.
8318 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 +09008319}
8320
Martin Stjernholmec009002021-03-27 15:18:31 +00008321func TestTestForForLibInOtherApex(t *testing.T) {
8322 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8323 _ = testApex(t, `
8324 apex {
8325 name: "com.android.art",
8326 key: "myapex.key",
8327 native_shared_libs: ["mylib"],
8328 updatable: false,
8329 }
8330
8331 apex {
8332 name: "com.android.art.debug",
8333 key: "myapex.key",
8334 native_shared_libs: ["mylib", "mytestlib"],
8335 updatable: false,
8336 }
8337
8338 apex_key {
8339 name: "myapex.key",
8340 public_key: "testkey.avbpubkey",
8341 private_key: "testkey.pem",
8342 }
8343
8344 cc_library {
8345 name: "mylib",
8346 srcs: ["mylib.cpp"],
8347 system_shared_libs: [],
8348 stl: "none",
8349 stubs: {
8350 versions: ["1"],
8351 },
8352 apex_available: ["com.android.art", "com.android.art.debug"],
8353 }
8354
8355 cc_library {
8356 name: "mytestlib",
8357 srcs: ["mylib.cpp"],
8358 system_shared_libs: [],
8359 shared_libs: ["mylib"],
8360 stl: "none",
8361 apex_available: ["com.android.art.debug"],
8362 test_for: ["com.android.art"],
8363 }
8364 `,
8365 android.MockFS{
8366 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8367 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8368 }.AddToFixture())
8369}
8370
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008371// TODO(jungjw): Move this to proptools
8372func intPtr(i int) *int {
8373 return &i
8374}
8375
8376func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008377 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008378 apex_set {
8379 name: "myapex",
8380 set: "myapex.apks",
8381 filename: "foo_v2.apex",
8382 overrides: ["foo"],
8383 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008384 `,
8385 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8386 variables.Platform_sdk_version = intPtr(30)
8387 }),
8388 android.FixtureModifyConfig(func(config android.Config) {
8389 config.Targets[android.Android] = []android.Target{
8390 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8391 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8392 }
8393 }),
8394 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008395
Paul Duffin24704672021-04-06 16:09:30 +01008396 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008397
8398 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008399 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008400 actual := extractedApex.Args["abis"]
8401 expected := "ARMEABI_V7A,ARM64_V8A"
8402 if actual != expected {
8403 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8404 }
8405 actual = extractedApex.Args["sdk-version"]
8406 expected = "30"
8407 if actual != expected {
8408 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8409 }
8410
Paul Duffin6717d882021-06-15 19:09:41 +01008411 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008412 a := m.Module().(*ApexSet)
8413 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008414 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008415 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8416 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8417 }
8418}
8419
Anton Hansson805e0a52022-11-25 14:06:46 +00008420func TestApexSet_NativeBridge(t *testing.T) {
8421 ctx := testApex(t, `
8422 apex_set {
8423 name: "myapex",
8424 set: "myapex.apks",
8425 filename: "foo_v2.apex",
8426 overrides: ["foo"],
8427 }
8428 `,
8429 android.FixtureModifyConfig(func(config android.Config) {
8430 config.Targets[android.Android] = []android.Target{
8431 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8432 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8433 }
8434 }),
8435 )
8436
8437 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8438
8439 // Check extract_apks tool parameters. No native bridge arch expected
8440 extractedApex := m.Output("extracted/myapex.apks")
8441 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8442}
8443
Jiyong Park7d95a512020-05-10 15:16:24 +09008444func TestNoStaticLinkingToStubsLib(t *testing.T) {
8445 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8446 apex {
8447 name: "myapex",
8448 key: "myapex.key",
8449 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008450 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008451 }
8452
8453 apex_key {
8454 name: "myapex.key",
8455 public_key: "testkey.avbpubkey",
8456 private_key: "testkey.pem",
8457 }
8458
8459 cc_library {
8460 name: "mylib",
8461 srcs: ["mylib.cpp"],
8462 static_libs: ["otherlib"],
8463 system_shared_libs: [],
8464 stl: "none",
8465 apex_available: [ "myapex" ],
8466 }
8467
8468 cc_library {
8469 name: "otherlib",
8470 srcs: ["mylib.cpp"],
8471 system_shared_libs: [],
8472 stl: "none",
8473 stubs: {
8474 versions: ["1", "2", "3"],
8475 },
8476 apex_available: [ "myapex" ],
8477 }
8478 `)
8479}
8480
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008481func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008482 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008483 apex {
8484 name: "myapex",
8485 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008486 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008487 custom_sign_tool: "sign_myapex",
8488 }
8489
8490 apex_key {
8491 name: "myapex.key",
8492 public_key: "testkey.avbpubkey",
8493 private_key: "testkey.pem",
8494 }
8495 `)
8496
8497 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8498 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8499 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"`)
8500}
8501
8502func TestApexKeysTxtOverrides(t *testing.T) {
8503 ctx := testApex(t, `
8504 apex {
8505 name: "myapex",
8506 key: "myapex.key",
8507 updatable: false,
8508 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008509 }
8510
8511 apex_key {
8512 name: "myapex.key",
8513 public_key: "testkey.avbpubkey",
8514 private_key: "testkey.pem",
8515 }
8516
8517 prebuilt_apex {
8518 name: "myapex",
8519 prefer: true,
8520 arch: {
8521 arm64: {
8522 src: "myapex-arm64.apex",
8523 },
8524 arm: {
8525 src: "myapex-arm.apex",
8526 },
8527 },
8528 }
8529
8530 apex_set {
8531 name: "myapex_set",
8532 set: "myapex.apks",
8533 filename: "myapex_set.apex",
8534 overrides: ["myapex"],
8535 }
8536 `)
8537
8538 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8539 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8540 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 +09008541 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 +09008542}
8543
Jooyung Han938b5932020-06-20 12:47:47 +09008544func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008545 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008546 apex {
8547 name: "myapex",
8548 key: "myapex.key",
8549 apps: ["app"],
8550 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008551 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008552 }
8553
8554 apex_key {
8555 name: "myapex.key",
8556 public_key: "testkey.avbpubkey",
8557 private_key: "testkey.pem",
8558 }
8559
8560 android_app {
8561 name: "app",
8562 srcs: ["foo/bar/MyClass.java"],
8563 package_name: "foo",
8564 sdk_version: "none",
8565 system_modules: "none",
8566 apex_available: [ "myapex" ],
8567 }
8568 `, withFiles(map[string][]byte{
8569 "sub/Android.bp": []byte(`
8570 override_apex {
8571 name: "override_myapex",
8572 base: "myapex",
8573 apps: ["override_app"],
8574 allowed_files: ":allowed",
8575 }
8576 // Overridable "path" property should be referenced indirectly
8577 filegroup {
8578 name: "allowed",
8579 srcs: ["allowed.txt"],
8580 }
8581 override_android_app {
8582 name: "override_app",
8583 base: "app",
8584 package_name: "bar",
8585 }
8586 `),
8587 }))
8588
8589 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8590 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8591 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8592 }
8593
8594 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8595 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8596 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8597 }
8598}
8599
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008600func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008601 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008602 apex {
8603 name: "myapex",
8604 key: "myapex.key",
8605 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008606 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008607 }
8608
8609 apex_key {
8610 name: "myapex.key",
8611 public_key: "testkey.avbpubkey",
8612 private_key: "testkey.pem",
8613 }
8614
8615 cc_library {
8616 name: "mylib",
8617 srcs: ["mylib.cpp"],
8618 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008619 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008620 },
8621 apex_available: ["myapex"],
8622 }
8623
8624 cc_prebuilt_library_shared {
8625 name: "mylib",
8626 prefer: false,
8627 srcs: ["prebuilt.so"],
8628 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008629 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008630 },
8631 apex_available: ["myapex"],
8632 }
8633 `)
8634}
8635
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008636func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008637 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008638 apex {
8639 name: "myapex",
8640 key: "myapex.key",
8641 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008642 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008643 }
8644 apex_key {
8645 name: "myapex.key",
8646 public_key: "testkey.avbpubkey",
8647 private_key: "testkey.pem",
8648 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008649 `,
8650 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8651 variables.CompressedApex = proptools.BoolPtr(true)
8652 }),
8653 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008654
8655 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8656 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8657
8658 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8659 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8660
8661 // Make sure output of bundle is .capex
8662 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8663 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8664
8665 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008666 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008667 var builder strings.Builder
8668 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8669 androidMk := builder.String()
8670 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8671}
8672
Martin Stjernholm2856c662020-12-02 15:03:42 +00008673func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008674 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008675 apex {
8676 name: "myapex",
8677 key: "myapex.key",
8678 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008679 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008680 }
8681
8682 apex_key {
8683 name: "myapex.key",
8684 public_key: "testkey.avbpubkey",
8685 private_key: "testkey.pem",
8686 }
8687
8688 cc_library {
8689 name: "mylib",
8690 srcs: ["mylib.cpp"],
8691 apex_available: ["myapex"],
8692 shared_libs: ["otherlib"],
8693 system_shared_libs: [],
8694 }
8695
8696 cc_library {
8697 name: "otherlib",
8698 srcs: ["mylib.cpp"],
8699 stubs: {
8700 versions: ["current"],
8701 },
8702 }
8703
8704 cc_prebuilt_library_shared {
8705 name: "otherlib",
8706 prefer: true,
8707 srcs: ["prebuilt.so"],
8708 stubs: {
8709 versions: ["current"],
8710 },
8711 }
8712 `)
8713
8714 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008715 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008716 var builder strings.Builder
8717 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8718 androidMk := builder.String()
8719
8720 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8721 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008722 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 +00008723}
8724
Jiyong Parke3867542020-12-03 17:28:25 +09008725func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008726 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008727 apex {
8728 name: "myapex",
8729 key: "myapex.key",
8730 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008731 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008732 }
8733
8734 apex_key {
8735 name: "myapex.key",
8736 public_key: "testkey.avbpubkey",
8737 private_key: "testkey.pem",
8738 }
8739
8740 cc_library {
8741 name: "mylib",
8742 srcs: ["mylib.cpp"],
8743 system_shared_libs: [],
8744 stl: "none",
8745 apex_available: ["myapex"],
8746 shared_libs: ["mylib2"],
8747 target: {
8748 apex: {
8749 exclude_shared_libs: ["mylib2"],
8750 },
8751 },
8752 }
8753
8754 cc_library {
8755 name: "mylib2",
8756 srcs: ["mylib.cpp"],
8757 system_shared_libs: [],
8758 stl: "none",
8759 }
8760 `)
8761
8762 // Check if mylib is linked to mylib2 for the non-apex target
8763 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8764 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8765
8766 // Make sure that the link doesn't occur for the apex target
8767 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8768 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8769
8770 // It shouldn't appear in the copy cmd as well.
8771 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8772 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8773}
8774
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008775func TestPrebuiltStubLibDep(t *testing.T) {
8776 bpBase := `
8777 apex {
8778 name: "myapex",
8779 key: "myapex.key",
8780 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008781 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008782 }
8783 apex_key {
8784 name: "myapex.key",
8785 public_key: "testkey.avbpubkey",
8786 private_key: "testkey.pem",
8787 }
8788 cc_library {
8789 name: "mylib",
8790 srcs: ["mylib.cpp"],
8791 apex_available: ["myapex"],
8792 shared_libs: ["stublib"],
8793 system_shared_libs: [],
8794 }
8795 apex {
8796 name: "otherapex",
8797 enabled: %s,
8798 key: "myapex.key",
8799 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008800 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008801 }
8802 `
8803
8804 stublibSourceBp := `
8805 cc_library {
8806 name: "stublib",
8807 srcs: ["mylib.cpp"],
8808 apex_available: ["otherapex"],
8809 system_shared_libs: [],
8810 stl: "none",
8811 stubs: {
8812 versions: ["1"],
8813 },
8814 }
8815 `
8816
8817 stublibPrebuiltBp := `
8818 cc_prebuilt_library_shared {
8819 name: "stublib",
8820 srcs: ["prebuilt.so"],
8821 apex_available: ["otherapex"],
8822 stubs: {
8823 versions: ["1"],
8824 },
8825 %s
8826 }
8827 `
8828
8829 tests := []struct {
8830 name string
8831 stublibBp string
8832 usePrebuilt bool
8833 modNames []string // Modules to collect AndroidMkEntries for
8834 otherApexEnabled []string
8835 }{
8836 {
8837 name: "only_source",
8838 stublibBp: stublibSourceBp,
8839 usePrebuilt: false,
8840 modNames: []string{"stublib"},
8841 otherApexEnabled: []string{"true", "false"},
8842 },
8843 {
8844 name: "source_preferred",
8845 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8846 usePrebuilt: false,
8847 modNames: []string{"stublib", "prebuilt_stublib"},
8848 otherApexEnabled: []string{"true", "false"},
8849 },
8850 {
8851 name: "prebuilt_preferred",
8852 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8853 usePrebuilt: true,
8854 modNames: []string{"stublib", "prebuilt_stublib"},
8855 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8856 },
8857 {
8858 name: "only_prebuilt",
8859 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8860 usePrebuilt: true,
8861 modNames: []string{"stublib"},
8862 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8863 },
8864 }
8865
8866 for _, test := range tests {
8867 t.Run(test.name, func(t *testing.T) {
8868 for _, otherApexEnabled := range test.otherApexEnabled {
8869 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008870 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008871
8872 type modAndMkEntries struct {
8873 mod *cc.Module
8874 mkEntries android.AndroidMkEntries
8875 }
8876 entries := []*modAndMkEntries{}
8877
8878 // Gather shared lib modules that are installable
8879 for _, modName := range test.modNames {
8880 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8881 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8882 continue
8883 }
8884 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008885 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008886 continue
8887 }
Colin Crossaa255532020-07-03 13:18:24 -07008888 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008889 if ent.Disabled {
8890 continue
8891 }
8892 entries = append(entries, &modAndMkEntries{
8893 mod: mod,
8894 mkEntries: ent,
8895 })
8896 }
8897 }
8898 }
8899
8900 var entry *modAndMkEntries = nil
8901 for _, ent := range entries {
8902 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8903 if entry != nil {
8904 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8905 } else {
8906 entry = ent
8907 }
8908 }
8909 }
8910
8911 if entry == nil {
8912 t.Errorf("AndroidMk entry for \"stublib\" missing")
8913 } else {
8914 isPrebuilt := entry.mod.Prebuilt() != nil
8915 if isPrebuilt != test.usePrebuilt {
8916 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8917 }
8918 if !entry.mod.IsStubs() {
8919 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8920 }
8921 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8922 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8923 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008924 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008925 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008926 if !android.InList(expected, cflags) {
8927 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8928 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008929 }
8930 })
8931 }
8932 })
8933 }
8934}
8935
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008936func TestHostApexInHostOnlyBuild(t *testing.T) {
8937 testApex(t, `
8938 apex {
8939 name: "myapex",
8940 host_supported: true,
8941 key: "myapex.key",
8942 updatable: false,
8943 payload_type: "zip",
8944 }
8945 apex_key {
8946 name: "myapex.key",
8947 public_key: "testkey.avbpubkey",
8948 private_key: "testkey.pem",
8949 }
8950 `,
8951 android.FixtureModifyConfig(func(config android.Config) {
8952 // We may not have device targets in all builds, e.g. in
8953 // prebuilts/build-tools/build-prebuilts.sh
8954 config.Targets[android.Android] = []android.Target{}
8955 }))
8956}
8957
Colin Crossc33e5212021-05-25 18:16:02 -07008958func TestApexJavaCoverage(t *testing.T) {
8959 bp := `
8960 apex {
8961 name: "myapex",
8962 key: "myapex.key",
8963 java_libs: ["mylib"],
8964 bootclasspath_fragments: ["mybootclasspathfragment"],
8965 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8966 updatable: false,
8967 }
8968
8969 apex_key {
8970 name: "myapex.key",
8971 public_key: "testkey.avbpubkey",
8972 private_key: "testkey.pem",
8973 }
8974
8975 java_library {
8976 name: "mylib",
8977 srcs: ["mylib.java"],
8978 apex_available: ["myapex"],
8979 compile_dex: true,
8980 }
8981
8982 bootclasspath_fragment {
8983 name: "mybootclasspathfragment",
8984 contents: ["mybootclasspathlib"],
8985 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01008986 hidden_api: {
8987 split_packages: ["*"],
8988 },
Colin Crossc33e5212021-05-25 18:16:02 -07008989 }
8990
8991 java_library {
8992 name: "mybootclasspathlib",
8993 srcs: ["mybootclasspathlib.java"],
8994 apex_available: ["myapex"],
8995 compile_dex: true,
8996 }
8997
8998 systemserverclasspath_fragment {
8999 name: "mysystemserverclasspathfragment",
9000 contents: ["mysystemserverclasspathlib"],
9001 apex_available: ["myapex"],
9002 }
9003
9004 java_library {
9005 name: "mysystemserverclasspathlib",
9006 srcs: ["mysystemserverclasspathlib.java"],
9007 apex_available: ["myapex"],
9008 compile_dex: true,
9009 }
9010 `
9011
9012 result := android.GroupFixturePreparers(
9013 PrepareForTestWithApexBuildComponents,
9014 prepareForTestWithMyapex,
9015 java.PrepareForTestWithJavaDefaultModules,
9016 android.PrepareForTestWithAndroidBuildComponents,
9017 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009018 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9019 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009020 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009021 ).RunTest(t)
9022
9023 // Make sure jacoco ran on both mylib and mybootclasspathlib
9024 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9025 t.Errorf("Failed to find jacoco rule for mylib")
9026 }
9027 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9028 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9029 }
9030 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9031 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9032 }
9033}
9034
Jiyong Park192600a2021-08-03 07:52:17 +00009035func TestProhibitStaticExecutable(t *testing.T) {
9036 testApexError(t, `executable mybin is static`, `
9037 apex {
9038 name: "myapex",
9039 key: "myapex.key",
9040 binaries: ["mybin"],
9041 min_sdk_version: "29",
9042 }
9043
9044 apex_key {
9045 name: "myapex.key",
9046 public_key: "testkey.avbpubkey",
9047 private_key: "testkey.pem",
9048 }
9049
9050 cc_binary {
9051 name: "mybin",
9052 srcs: ["mylib.cpp"],
9053 relative_install_path: "foo/bar",
9054 static_executable: true,
9055 system_shared_libs: [],
9056 stl: "none",
9057 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009058 min_sdk_version: "29",
9059 }
9060 `)
9061
9062 testApexError(t, `executable mybin.rust is static`, `
9063 apex {
9064 name: "myapex",
9065 key: "myapex.key",
9066 binaries: ["mybin.rust"],
9067 min_sdk_version: "29",
9068 }
9069
9070 apex_key {
9071 name: "myapex.key",
9072 public_key: "testkey.avbpubkey",
9073 private_key: "testkey.pem",
9074 }
9075
9076 rust_binary {
9077 name: "mybin.rust",
9078 srcs: ["foo.rs"],
9079 static_executable: true,
9080 apex_available: ["myapex"],
9081 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009082 }
9083 `)
9084}
9085
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009086func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9087 ctx := testApex(t, `
9088 apex {
9089 name: "myapex",
9090 key: "myapex.key",
9091 updatable: false,
9092 java_libs: ["foo"],
9093 }
9094
9095 apex_key {
9096 name: "myapex.key",
9097 public_key: "testkey.avbpubkey",
9098 private_key: "testkey.pem",
9099 }
9100
9101 java_library {
9102 name: "foo",
9103 srcs: ["foo.java"],
9104 apex_available: ["myapex"],
9105 installable: true,
9106 }
9107 `,
9108 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9109 )
9110
9111 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9112 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9113 var builder strings.Builder
9114 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9115 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009116 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 +00009117}
9118
9119func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9120 ctx := testApex(t, `
9121 prebuilt_apex {
9122 name: "myapex",
9123 arch: {
9124 arm64: {
9125 src: "myapex-arm64.apex",
9126 },
9127 arm: {
9128 src: "myapex-arm.apex",
9129 },
9130 },
9131 exported_java_libs: ["foo"],
9132 }
9133
9134 java_import {
9135 name: "foo",
9136 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009137 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009138 }
9139 `,
9140 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9141 )
9142
9143 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9144 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9145 mainModuleEntries := entriesList[0]
9146 android.AssertArrayString(t,
9147 "LOCAL_REQUIRED_MODULES",
9148 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9149 []string{
9150 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9151 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9152 })
9153}
9154
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009155func TestAndroidMk_RequiredModules(t *testing.T) {
9156 ctx := testApex(t, `
9157 apex {
9158 name: "myapex",
9159 key: "myapex.key",
9160 updatable: false,
9161 java_libs: ["foo"],
9162 required: ["otherapex"],
9163 }
9164
9165 apex {
9166 name: "otherapex",
9167 key: "myapex.key",
9168 updatable: false,
9169 java_libs: ["foo"],
9170 required: ["otherapex"],
9171 }
9172
9173 apex_key {
9174 name: "myapex.key",
9175 public_key: "testkey.avbpubkey",
9176 private_key: "testkey.pem",
9177 }
9178
9179 java_library {
9180 name: "foo",
9181 srcs: ["foo.java"],
9182 apex_available: ["myapex", "otherapex"],
9183 installable: true,
9184 }
9185 `)
9186
9187 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9188 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9189 var builder strings.Builder
9190 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9191 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009192 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009193}
9194
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009195func TestAndroidMk_RequiredDeps(t *testing.T) {
9196 ctx := testApex(t, `
9197 apex {
9198 name: "myapex",
9199 key: "myapex.key",
9200 updatable: false,
9201 }
9202
9203 apex_key {
9204 name: "myapex.key",
9205 public_key: "testkey.avbpubkey",
9206 private_key: "testkey.pem",
9207 }
9208 `)
9209
9210 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009211 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009212 data := android.AndroidMkDataForTest(t, ctx, bundle)
9213 var builder strings.Builder
9214 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9215 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009216 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009217
9218 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009219 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009220 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9221 var flattenedBuilder strings.Builder
9222 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9223 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009224 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009225}
9226
Jooyung Hana6d36672022-02-24 13:58:07 +09009227func TestApexOutputFileProducer(t *testing.T) {
9228 for _, tc := range []struct {
9229 name string
9230 ref string
9231 expected_data []string
9232 }{
9233 {
9234 name: "test_using_output",
9235 ref: ":myapex",
9236 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9237 },
9238 {
9239 name: "test_using_apex",
9240 ref: ":myapex{.apex}",
9241 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9242 },
9243 } {
9244 t.Run(tc.name, func(t *testing.T) {
9245 ctx := testApex(t, `
9246 apex {
9247 name: "myapex",
9248 key: "myapex.key",
9249 compressible: true,
9250 updatable: false,
9251 }
9252
9253 apex_key {
9254 name: "myapex.key",
9255 public_key: "testkey.avbpubkey",
9256 private_key: "testkey.pem",
9257 }
9258
9259 java_test {
9260 name: "`+tc.name+`",
9261 srcs: ["a.java"],
9262 data: ["`+tc.ref+`"],
9263 }
9264 `,
9265 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9266 variables.CompressedApex = proptools.BoolPtr(true)
9267 }))
9268 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9269 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9270 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9271 })
9272 }
9273}
9274
satayev758968a2021-12-06 11:42:40 +00009275func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9276 preparer := android.GroupFixturePreparers(
9277 PrepareForTestWithApexBuildComponents,
9278 prepareForTestWithMyapex,
9279 java.PrepareForTestWithJavaSdkLibraryFiles,
9280 java.PrepareForTestWithJavaDefaultModules,
9281 android.PrepareForTestWithAndroidBuildComponents,
9282 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9283 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9284 )
9285
9286 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9287 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9288 preparer.RunTestWithBp(t, `
9289 apex {
9290 name: "myapex",
9291 key: "myapex.key",
9292 bootclasspath_fragments: ["mybootclasspathfragment"],
9293 min_sdk_version: "30",
9294 updatable: false,
9295 }
9296
9297 apex_key {
9298 name: "myapex.key",
9299 public_key: "testkey.avbpubkey",
9300 private_key: "testkey.pem",
9301 }
9302
9303 bootclasspath_fragment {
9304 name: "mybootclasspathfragment",
9305 contents: ["mybootclasspathlib"],
9306 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009307 hidden_api: {
9308 split_packages: ["*"],
9309 },
satayev758968a2021-12-06 11:42:40 +00009310 }
9311
9312 java_sdk_library {
9313 name: "mybootclasspathlib",
9314 srcs: ["mybootclasspathlib.java"],
9315 apex_available: ["myapex"],
9316 compile_dex: true,
9317 unsafe_ignore_missing_latest_api: true,
9318 min_sdk_version: "31",
9319 static_libs: ["util"],
9320 }
9321
9322 java_library {
9323 name: "util",
9324 srcs: ["a.java"],
9325 apex_available: ["myapex"],
9326 min_sdk_version: "31",
9327 static_libs: ["another_util"],
9328 }
9329
9330 java_library {
9331 name: "another_util",
9332 srcs: ["a.java"],
9333 min_sdk_version: "31",
9334 apex_available: ["myapex"],
9335 }
9336 `)
9337 })
9338
9339 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9340 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9341 preparer.RunTestWithBp(t, `
9342 apex {
9343 name: "myapex",
9344 key: "myapex.key",
9345 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9346 min_sdk_version: "30",
9347 updatable: false,
9348 }
9349
9350 apex_key {
9351 name: "myapex.key",
9352 public_key: "testkey.avbpubkey",
9353 private_key: "testkey.pem",
9354 }
9355
9356 systemserverclasspath_fragment {
9357 name: "mysystemserverclasspathfragment",
9358 contents: ["mysystemserverclasspathlib"],
9359 apex_available: ["myapex"],
9360 }
9361
9362 java_sdk_library {
9363 name: "mysystemserverclasspathlib",
9364 srcs: ["mysystemserverclasspathlib.java"],
9365 apex_available: ["myapex"],
9366 compile_dex: true,
9367 min_sdk_version: "32",
9368 unsafe_ignore_missing_latest_api: true,
9369 static_libs: ["util"],
9370 }
9371
9372 java_library {
9373 name: "util",
9374 srcs: ["a.java"],
9375 apex_available: ["myapex"],
9376 min_sdk_version: "31",
9377 static_libs: ["another_util"],
9378 }
9379
9380 java_library {
9381 name: "another_util",
9382 srcs: ["a.java"],
9383 min_sdk_version: "31",
9384 apex_available: ["myapex"],
9385 }
9386 `)
9387 })
9388
9389 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9390 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9391 RunTestWithBp(t, `
9392 apex {
9393 name: "myapex",
9394 key: "myapex.key",
9395 bootclasspath_fragments: ["mybootclasspathfragment"],
9396 min_sdk_version: "30",
9397 updatable: false,
9398 }
9399
9400 apex_key {
9401 name: "myapex.key",
9402 public_key: "testkey.avbpubkey",
9403 private_key: "testkey.pem",
9404 }
9405
9406 bootclasspath_fragment {
9407 name: "mybootclasspathfragment",
9408 contents: ["mybootclasspathlib"],
9409 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009410 hidden_api: {
9411 split_packages: ["*"],
9412 },
satayev758968a2021-12-06 11:42:40 +00009413 }
9414
9415 java_sdk_library {
9416 name: "mybootclasspathlib",
9417 srcs: ["mybootclasspathlib.java"],
9418 apex_available: ["myapex"],
9419 compile_dex: true,
9420 unsafe_ignore_missing_latest_api: true,
9421 }
9422 `)
9423 })
9424
9425 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9426 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9427 RunTestWithBp(t, `
9428 apex {
9429 name: "myapex",
9430 key: "myapex.key",
9431 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9432 min_sdk_version: "30",
9433 updatable: false,
9434 }
9435
9436 apex_key {
9437 name: "myapex.key",
9438 public_key: "testkey.avbpubkey",
9439 private_key: "testkey.pem",
9440 }
9441
9442 systemserverclasspath_fragment {
9443 name: "mysystemserverclasspathfragment",
9444 contents: ["mysystemserverclasspathlib"],
9445 apex_available: ["myapex"],
9446 }
9447
9448 java_sdk_library {
9449 name: "mysystemserverclasspathlib",
9450 srcs: ["mysystemserverclasspathlib.java"],
9451 apex_available: ["myapex"],
9452 compile_dex: true,
9453 unsafe_ignore_missing_latest_api: true,
9454 }
9455 `)
9456 })
9457}
9458
Jiakai Zhang6decef92022-01-12 17:56:19 +00009459// Verifies that the APEX depends on all the Make modules in the list.
9460func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9461 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9462 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009463 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009464 }
9465}
9466
9467// Verifies that the APEX does not depend on any of the Make modules in the list.
9468func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9469 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9470 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009471 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009472 }
9473}
9474
Cole Faust1021ccd2023-02-26 21:15:25 -08009475// TODO(b/193460475): Re-enable this test
9476//func TestApexStrictUpdtabilityLint(t *testing.T) {
9477// bpTemplate := `
9478// apex {
9479// name: "myapex",
9480// key: "myapex.key",
9481// java_libs: ["myjavalib"],
9482// updatable: %v,
9483// min_sdk_version: "29",
9484// }
9485// apex_key {
9486// name: "myapex.key",
9487// }
9488// java_library {
9489// name: "myjavalib",
9490// srcs: ["MyClass.java"],
9491// apex_available: [ "myapex" ],
9492// lint: {
9493// strict_updatability_linting: %v,
9494// },
9495// sdk_version: "current",
9496// min_sdk_version: "29",
9497// }
9498// `
9499// fs := android.MockFS{
9500// "lint-baseline.xml": nil,
9501// }
9502//
9503// testCases := []struct {
9504// testCaseName string
9505// apexUpdatable bool
9506// javaStrictUpdtabilityLint bool
9507// lintFileExists bool
9508// disallowedFlagExpected bool
9509// }{
9510// {
9511// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9512// apexUpdatable: true,
9513// javaStrictUpdtabilityLint: true,
9514// lintFileExists: false,
9515// disallowedFlagExpected: false,
9516// },
9517// {
9518// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9519// apexUpdatable: false,
9520// javaStrictUpdtabilityLint: false,
9521// lintFileExists: true,
9522// disallowedFlagExpected: false,
9523// },
9524// {
9525// testCaseName: "non-updatable apex respects strict updatability of javalib",
9526// apexUpdatable: false,
9527// javaStrictUpdtabilityLint: true,
9528// lintFileExists: true,
9529// disallowedFlagExpected: true,
9530// },
9531// {
9532// testCaseName: "updatable apex sets strict updatability of javalib to true",
9533// apexUpdatable: true,
9534// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9535// lintFileExists: true,
9536// disallowedFlagExpected: true,
9537// },
9538// }
9539//
9540// for _, testCase := range testCases {
9541// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9542// fixtures := []android.FixturePreparer{}
9543// if testCase.lintFileExists {
9544// fixtures = append(fixtures, fs.AddToFixture())
9545// }
9546//
9547// result := testApex(t, bp, fixtures...)
9548// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9549// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9550// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9551//
9552// if disallowedFlagActual != testCase.disallowedFlagExpected {
9553// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9554// }
9555// }
9556//}
9557//
9558//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9559// bp := `
9560// apex {
9561// name: "myapex",
9562// key: "myapex.key",
9563// java_libs: ["myjavalib"],
9564// updatable: true,
9565// min_sdk_version: "29",
9566// }
9567// apex_key {
9568// name: "myapex.key",
9569// }
9570// java_library {
9571// name: "myjavalib",
9572// srcs: ["MyClass.java"],
9573// apex_available: [ "myapex" ],
9574// sdk_version: "current",
9575// min_sdk_version: "29",
9576// }
9577// `
9578//
9579// testCases := []struct {
9580// testCaseName string
9581// moduleDirectory string
9582// disallowedFlagExpected bool
9583// }{
9584// {
9585// testCaseName: "lintable module defined outside libcore",
9586// moduleDirectory: "",
9587// disallowedFlagExpected: true,
9588// },
9589// {
9590// testCaseName: "lintable module defined in libcore root directory",
9591// moduleDirectory: "libcore/",
9592// disallowedFlagExpected: false,
9593// },
9594// {
9595// testCaseName: "lintable module defined in libcore child directory",
9596// moduleDirectory: "libcore/childdir/",
9597// disallowedFlagExpected: true,
9598// },
9599// }
9600//
9601// for _, testCase := range testCases {
9602// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9603// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9604// result := testApex(t, "", lintFileCreator, bpFileCreator)
9605// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9606// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9607// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9608// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9609//
9610// if disallowedFlagActual != testCase.disallowedFlagExpected {
9611// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9612// }
9613// }
9614//}
9615//
9616//// checks transtive deps of an apex coming from bootclasspath_fragment
9617//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9618// bp := `
9619// apex {
9620// name: "myapex",
9621// key: "myapex.key",
9622// bootclasspath_fragments: ["mybootclasspathfragment"],
9623// updatable: true,
9624// min_sdk_version: "29",
9625// }
9626// apex_key {
9627// name: "myapex.key",
9628// }
9629// bootclasspath_fragment {
9630// name: "mybootclasspathfragment",
9631// contents: ["myjavalib"],
9632// apex_available: ["myapex"],
9633// hidden_api: {
9634// split_packages: ["*"],
9635// },
9636// }
9637// java_library {
9638// name: "myjavalib",
9639// srcs: ["MyClass.java"],
9640// apex_available: [ "myapex" ],
9641// sdk_version: "current",
9642// min_sdk_version: "29",
9643// compile_dex: true,
9644// }
9645// `
9646// fs := android.MockFS{
9647// "lint-baseline.xml": nil,
9648// }
9649//
9650// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9651// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9652// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9653// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9654// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9655// }
9656//}
Spandan Das66773252022-01-15 00:23:18 +00009657
Spandan Das42e89502022-05-06 22:12:55 +00009658// updatable apexes should propagate updatable=true to its apps
9659func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9660 bp := `
9661 apex {
9662 name: "myapex",
9663 key: "myapex.key",
9664 updatable: %v,
9665 apps: [
9666 "myapp",
9667 ],
9668 min_sdk_version: "30",
9669 }
9670 apex_key {
9671 name: "myapex.key",
9672 }
9673 android_app {
9674 name: "myapp",
9675 updatable: %v,
9676 apex_available: [
9677 "myapex",
9678 ],
9679 sdk_version: "current",
9680 min_sdk_version: "30",
9681 }
9682 `
9683 testCases := []struct {
9684 name string
9685 apex_is_updatable_bp bool
9686 app_is_updatable_bp bool
9687 app_is_updatable_expected bool
9688 }{
9689 {
9690 name: "Non-updatable apex respects updatable property of non-updatable app",
9691 apex_is_updatable_bp: false,
9692 app_is_updatable_bp: false,
9693 app_is_updatable_expected: false,
9694 },
9695 {
9696 name: "Non-updatable apex respects updatable property of updatable app",
9697 apex_is_updatable_bp: false,
9698 app_is_updatable_bp: true,
9699 app_is_updatable_expected: true,
9700 },
9701 {
9702 name: "Updatable apex respects updatable property of updatable app",
9703 apex_is_updatable_bp: true,
9704 app_is_updatable_bp: true,
9705 app_is_updatable_expected: true,
9706 },
9707 {
9708 name: "Updatable apex sets updatable=true on non-updatable app",
9709 apex_is_updatable_bp: true,
9710 app_is_updatable_bp: false,
9711 app_is_updatable_expected: true,
9712 },
9713 }
9714 for _, testCase := range testCases {
9715 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9716 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9717 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9718 }
9719}
9720
Kiyoung Kim487689e2022-07-26 09:48:22 +09009721func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9722 bp := `
9723 apex {
9724 name: "myapex",
9725 key: "myapex.key",
9726 native_shared_libs: ["libfoo"],
9727 min_sdk_version: "29",
9728 }
9729 apex_key {
9730 name: "myapex.key",
9731 }
9732 cc_library {
9733 name: "libfoo",
9734 shared_libs: ["libc"],
9735 apex_available: ["myapex"],
9736 min_sdk_version: "29",
9737 }
9738 cc_api_library {
9739 name: "libc",
9740 src: "libc.so",
9741 min_sdk_version: "29",
9742 recovery_available: true,
9743 }
9744 api_imports {
9745 name: "api_imports",
9746 shared_libs: [
9747 "libc",
9748 ],
9749 header_libs: [],
9750 }
9751 `
9752 result := testApex(t, bp)
9753
9754 hasDep := func(m android.Module, wantDep android.Module) bool {
9755 t.Helper()
9756 var found bool
9757 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9758 if dep == wantDep {
9759 found = true
9760 }
9761 })
9762 return found
9763 }
9764
9765 libfooApexVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex29").Module()
9766 libcApexVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared_apex29").Module()
9767
9768 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(libfooApexVariant, libcApexVariant))
9769
9770 // libfoo core variant should be buildable in the same inner tree since
9771 // certain mcombo files might build system and apexes in the same inner tree
9772 // libfoo core variant should link against source libc
9773 libfooCoreVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
9774 libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
9775 android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
9776}
Dennis Shend4f5d932023-01-31 20:27:21 +00009777
9778func TestTrimmedApex(t *testing.T) {
9779 bp := `
9780 apex {
9781 name: "myapex",
9782 key: "myapex.key",
9783 native_shared_libs: ["libfoo","libbaz"],
9784 min_sdk_version: "29",
9785 trim_against: "mydcla",
9786 }
9787 apex {
9788 name: "mydcla",
9789 key: "myapex.key",
9790 native_shared_libs: ["libfoo","libbar"],
9791 min_sdk_version: "29",
9792 file_contexts: ":myapex-file_contexts",
9793 dynamic_common_lib_apex: true,
9794 }
9795 apex_key {
9796 name: "myapex.key",
9797 }
9798 cc_library {
9799 name: "libfoo",
9800 shared_libs: ["libc"],
9801 apex_available: ["myapex","mydcla"],
9802 min_sdk_version: "29",
9803 }
9804 cc_library {
9805 name: "libbar",
9806 shared_libs: ["libc"],
9807 apex_available: ["myapex","mydcla"],
9808 min_sdk_version: "29",
9809 }
9810 cc_library {
9811 name: "libbaz",
9812 shared_libs: ["libc"],
9813 apex_available: ["myapex","mydcla"],
9814 min_sdk_version: "29",
9815 }
9816 cc_api_library {
9817 name: "libc",
9818 src: "libc.so",
9819 min_sdk_version: "29",
9820 recovery_available: true,
9821 }
9822 api_imports {
9823 name: "api_imports",
9824 shared_libs: [
9825 "libc",
9826 ],
9827 header_libs: [],
9828 }
9829 `
9830 ctx := testApex(t, bp)
9831 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
9832 apexRule := module.MaybeRule("apexRule")
9833 if apexRule.Rule == nil {
9834 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
9835 }
9836
9837 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
9838 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
9839 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
9840 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
9841 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
9842 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
9843}