blob: 647a575d62bca42778b847059ba52add0658f016 [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"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
Kiyoung Kim487689e2022-07-26 09:48:22 +090029 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090030 "github.com/google/blueprint/proptools"
31
32 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080033 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090034 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000035 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070036 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090037 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090038 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070039 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090040)
41
Jooyung Hand3639552019-08-09 12:57:43 +090042// names returns name list from white space separated string
43func names(s string) (ns []string) {
44 for _, n := range strings.Split(s, " ") {
45 if len(n) > 0 {
46 ns = append(ns, n)
47 }
48 }
49 return
50}
51
Paul Duffin40b62572021-03-20 11:39:01 +000052func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090053 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010054 android.GroupFixturePreparers(
55 prepareForApexTest,
56 android.GroupFixturePreparers(preparers...),
57 ).
Paul Duffine05480a2021-03-08 15:07:14 +000058 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000059 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090060}
61
Paul Duffin40b62572021-03-20 11:39:01 +000062func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090063 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010064
65 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000066 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010067 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000068 }
Paul Duffin284165a2021-03-29 01:50:31 +010069
70 result := android.GroupFixturePreparers(
71 prepareForApexTest,
72 android.GroupFixturePreparers(preparers...),
73 optionalBpPreparer,
74 ).RunTest(t)
75
Paul Duffine05480a2021-03-08 15:07:14 +000076 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090077}
78
Paul Duffin810f33d2021-03-09 14:12:32 +000079func withFiles(files android.MockFS) android.FixturePreparer {
80 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090081}
82
Paul Duffin810f33d2021-03-09 14:12:32 +000083func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
84 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090085 for k, v := range targets {
86 config.Targets[k] = v
87 }
Paul Duffin810f33d2021-03-09 14:12:32 +000088 })
Jooyung Han344d5432019-08-23 11:17:39 +090089}
90
Jooyung Han35155c42020-02-06 17:33:20 +090091// withNativeBridgeTargets sets configuration with targets including:
92// - X86_64 (primary)
93// - X86 (secondary)
94// - Arm64 on X86_64 (native bridge)
95// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000096var withNativeBridgeEnabled = android.FixtureModifyConfig(
97 func(config android.Config) {
98 config.Targets[android.Android] = []android.Target{
99 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
100 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
104 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
107 }
108 },
109)
110
111func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
112 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
113 variables.ManifestPackageNameOverrides = specs
114 })
Jooyung Han35155c42020-02-06 17:33:20 +0900115}
116
Albert Martineefabcf2022-03-21 20:11:16 +0000117func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
118 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
119 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
120 })
121}
122
Paul Duffin810f33d2021-03-09 14:12:32 +0000123var withBinder32bit = android.FixtureModifyProductVariables(
124 func(variables android.FixtureProductVariables) {
125 variables.Binder32bit = proptools.BoolPtr(true)
126 },
127)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900128
Paul Duffin810f33d2021-03-09 14:12:32 +0000129var withUnbundledBuild = android.FixtureModifyProductVariables(
130 func(variables android.FixtureProductVariables) {
131 variables.Unbundled_build = proptools.BoolPtr(true)
132 },
133)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900134
Paul Duffin284165a2021-03-29 01:50:31 +0100135// Legacy preparer used for running tests within the apex package.
136//
137// This includes everything that was needed to run any test in the apex package prior to the
138// introduction of the test fixtures. Tests that are being converted to use fixtures directly
139// rather than through the testApex...() methods should avoid using this and instead use the
140// various preparers directly, using android.GroupFixturePreparers(...) to group them when
141// necessary.
142//
143// deprecated
144var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000145 // General preparers in alphabetical order as test infrastructure will enforce correct
146 // registration order.
147 android.PrepareForTestWithAndroidBuildComponents,
148 bpf.PrepareForTestWithBpf,
149 cc.PrepareForTestWithCcBuildComponents,
150 java.PrepareForTestWithJavaDefaultModules,
151 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
152 rust.PrepareForTestWithRustDefaultModules,
153 sh.PrepareForTestWithShBuildComponents,
154
155 PrepareForTestWithApexBuildComponents,
156
157 // Additional apex test specific preparers.
158 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
159 filegroup {
160 name: "myapex-file_contexts",
161 srcs: [
162 "apex/myapex-file_contexts",
163 ],
164 }
165 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000166 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000167 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000168 "a.java": nil,
169 "PrebuiltAppFoo.apk": nil,
170 "PrebuiltAppFooPriv.apk": nil,
171 "apex_manifest.json": nil,
172 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000173 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
174 "system/sepolicy/apex/myapex2-file_contexts": nil,
175 "system/sepolicy/apex/otherapex-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
177 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700178 "mylib.cpp": nil,
179 "mytest.cpp": nil,
180 "mytest1.cpp": nil,
181 "mytest2.cpp": nil,
182 "mytest3.cpp": nil,
183 "myprebuilt": nil,
184 "my_include": nil,
185 "foo/bar/MyClass.java": nil,
186 "prebuilt.jar": nil,
187 "prebuilt.so": nil,
188 "vendor/foo/devkeys/test.x509.pem": nil,
189 "vendor/foo/devkeys/test.pk8": nil,
190 "testkey.x509.pem": nil,
191 "testkey.pk8": nil,
192 "testkey.override.x509.pem": nil,
193 "testkey.override.pk8": nil,
194 "vendor/foo/devkeys/testkey.avbpubkey": nil,
195 "vendor/foo/devkeys/testkey.pem": nil,
196 "NOTICE": nil,
197 "custom_notice": nil,
198 "custom_notice_for_static_lib": nil,
199 "testkey2.avbpubkey": nil,
200 "testkey2.pem": nil,
201 "myapex-arm64.apex": nil,
202 "myapex-arm.apex": nil,
203 "myapex.apks": nil,
204 "frameworks/base/api/current.txt": nil,
205 "framework/aidl/a.aidl": nil,
206 "dummy.txt": nil,
207 "baz": nil,
208 "bar/baz": nil,
209 "testdata/baz": nil,
210 "AppSet.apks": nil,
211 "foo.rs": nil,
212 "libfoo.jar": nil,
213 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000214 },
215 ),
216
217 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
218 variables.DeviceVndkVersion = proptools.StringPtr("current")
219 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
220 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
221 variables.Platform_sdk_codename = proptools.StringPtr("Q")
222 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000223 // "Tiramisu" needs to be in the next line for compatibility with soong code,
224 // not because of these tests specifically (it's not used by the tests)
225 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900226 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000227 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000228 }),
229)
230
Paul Duffin52bfaa42021-03-23 23:40:12 +0000231var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
232 "system/sepolicy/apex/myapex-file_contexts": nil,
233})
234
Jooyung Han643adc42020-02-27 13:50:06 +0900235// ensure that 'result' equals 'expected'
236func ensureEquals(t *testing.T, result string, expected string) {
237 t.Helper()
238 if result != expected {
239 t.Errorf("%q != %q", expected, result)
240 }
241}
242
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243// ensure that 'result' contains 'expected'
244func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if !strings.Contains(result, expected) {
247 t.Errorf("%q is not found in %q", expected, result)
248 }
249}
250
Liz Kammer5bd365f2020-05-27 15:15:11 -0700251// ensure that 'result' contains 'expected' exactly one time
252func ensureContainsOnce(t *testing.T, result string, expected string) {
253 t.Helper()
254 count := strings.Count(result, expected)
255 if count != 1 {
256 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
257 }
258}
259
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260// ensures that 'result' does not contain 'notExpected'
261func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900262 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263 if strings.Contains(result, notExpected) {
264 t.Errorf("%q is found in %q", notExpected, result)
265 }
266}
267
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700268func ensureMatches(t *testing.T, result string, expectedRex string) {
269 ok, err := regexp.MatchString(expectedRex, result)
270 if err != nil {
271 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
272 return
273 }
274 if !ok {
275 t.Errorf("%s does not match regular expession %s", result, expectedRex)
276 }
277}
278
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900280 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 if !android.InList(expected, result) {
282 t.Errorf("%q is not found in %v", expected, result)
283 }
284}
285
286func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900287 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288 if android.InList(notExpected, result) {
289 t.Errorf("%q is found in %v", notExpected, result)
290 }
291}
292
Jooyung Hane1633032019-08-01 17:41:43 +0900293func ensureListEmpty(t *testing.T, result []string) {
294 t.Helper()
295 if len(result) > 0 {
296 t.Errorf("%q is expected to be empty", result)
297 }
298}
299
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000300func ensureListNotEmpty(t *testing.T, result []string) {
301 t.Helper()
302 if len(result) == 0 {
303 t.Errorf("%q is expected to be not empty", result)
304 }
305}
306
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307// Minimal test
308func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800309 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900310 apex_defaults {
311 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900312 manifest: ":myapex.manifest",
313 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900316 native_shared_libs: [
317 "mylib",
318 "libfoo.ffi",
319 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900320 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800321 multilib: {
322 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900323 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800324 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900325 },
Jiyong Park77acec62020-06-01 21:39:15 +0900326 java_libs: [
327 "myjar",
328 "myjar_dex",
329 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000330 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900331 }
332
Jiyong Park30ca9372019-02-07 16:27:23 +0900333 apex {
334 name: "myapex",
335 defaults: ["myapex-defaults"],
336 }
337
Jiyong Park25fc6a92018-11-18 18:02:45 +0900338 apex_key {
339 name: "myapex.key",
340 public_key: "testkey.avbpubkey",
341 private_key: "testkey.pem",
342 }
343
Jiyong Park809bb722019-02-13 21:33:49 +0900344 filegroup {
345 name: "myapex.manifest",
346 srcs: ["apex_manifest.json"],
347 }
348
349 filegroup {
350 name: "myapex.androidmanifest",
351 srcs: ["AndroidManifest.xml"],
352 }
353
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 cc_library {
355 name: "mylib",
356 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900357 shared_libs: [
358 "mylib2",
359 "libbar.ffi",
360 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361 system_shared_libs: [],
362 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000363 // TODO: remove //apex_available:platform
364 apex_available: [
365 "//apex_available:platform",
366 "myapex",
367 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 }
369
Alex Light3d673592019-01-18 14:37:31 -0800370 cc_binary {
371 name: "foo",
372 srcs: ["mylib.cpp"],
373 compile_multilib: "both",
374 multilib: {
375 lib32: {
376 suffix: "32",
377 },
378 lib64: {
379 suffix: "64",
380 },
381 },
382 symlinks: ["foo_link_"],
383 symlink_preferred_arch: true,
384 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800385 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700386 apex_available: [ "myapex", "com.android.gki.*" ],
387 }
388
Jiyong Park99644e92020-11-17 22:21:02 +0900389 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000390 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900391 srcs: ["foo.rs"],
392 rlibs: ["libfoo.rlib.rust"],
393 dylibs: ["libfoo.dylib.rust"],
394 apex_available: ["myapex"],
395 }
396
397 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000398 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900399 srcs: ["foo.rs"],
400 crate_name: "foo",
401 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900402 shared_libs: ["libfoo.shared_from_rust"],
403 }
404
405 cc_library_shared {
406 name: "libfoo.shared_from_rust",
407 srcs: ["mylib.cpp"],
408 system_shared_libs: [],
409 stl: "none",
410 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900411 }
412
413 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000414 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900415 srcs: ["foo.rs"],
416 crate_name: "foo",
417 apex_available: ["myapex"],
418 }
419
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900420 rust_ffi_shared {
421 name: "libfoo.ffi",
422 srcs: ["foo.rs"],
423 crate_name: "foo",
424 apex_available: ["myapex"],
425 }
426
427 rust_ffi_shared {
428 name: "libbar.ffi",
429 srcs: ["foo.rs"],
430 crate_name: "bar",
431 apex_available: ["myapex"],
432 }
433
Yifan Hongd22a84a2020-07-28 17:37:46 -0700434 apex {
435 name: "com.android.gki.fake",
436 binaries: ["foo"],
437 key: "myapex.key",
438 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000439 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800440 }
441
Paul Duffindddd5462020-04-07 15:25:44 +0100442 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900443 name: "mylib2",
444 srcs: ["mylib.cpp"],
445 system_shared_libs: [],
446 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900447 static_libs: ["libstatic"],
448 // TODO: remove //apex_available:platform
449 apex_available: [
450 "//apex_available:platform",
451 "myapex",
452 ],
453 }
454
Paul Duffindddd5462020-04-07 15:25:44 +0100455 cc_prebuilt_library_shared {
456 name: "mylib2",
457 srcs: ["prebuilt.so"],
458 // TODO: remove //apex_available:platform
459 apex_available: [
460 "//apex_available:platform",
461 "myapex",
462 ],
463 }
464
Jiyong Park9918e1a2020-03-17 19:16:40 +0900465 cc_library_static {
466 name: "libstatic",
467 srcs: ["mylib.cpp"],
468 system_shared_libs: [],
469 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000470 // TODO: remove //apex_available:platform
471 apex_available: [
472 "//apex_available:platform",
473 "myapex",
474 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900475 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476
477 java_library {
478 name: "myjar",
479 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900480 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900481 sdk_version: "none",
482 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900484 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000485 // TODO: remove //apex_available:platform
486 apex_available: [
487 "//apex_available:platform",
488 "myapex",
489 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 }
491
Jiyong Park77acec62020-06-01 21:39:15 +0900492 dex_import {
493 name: "myjar_dex",
494 jars: ["prebuilt.jar"],
495 apex_available: [
496 "//apex_available:platform",
497 "myapex",
498 ],
499 }
500
Jiyong Park7f7766d2019-07-25 22:02:35 +0900501 java_library {
502 name: "myotherjar",
503 srcs: ["foo/bar/MyClass.java"],
504 sdk_version: "none",
505 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900506 // TODO: remove //apex_available:platform
507 apex_available: [
508 "//apex_available:platform",
509 "myapex",
510 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900511 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900512
513 java_library {
514 name: "mysharedjar",
515 srcs: ["foo/bar/MyClass.java"],
516 sdk_version: "none",
517 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900518 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900519 `)
520
Paul Duffina71a67a2021-03-29 00:42:57 +0100521 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900522
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900523 // Make sure that Android.mk is created
524 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700525 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900526 var builder strings.Builder
527 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
528
529 androidMk := builder.String()
530 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
531 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
532
Jiyong Park42cca6c2019-04-01 11:15:50 +0900533 optFlags := apexRule.Args["opt_flags"]
534 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700535 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100536 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900537
Jiyong Park25fc6a92018-11-18 18:02:45 +0900538 copyCmds := apexRule.Args["copy_commands"]
539
540 // Ensure that main rule creates an output
541 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
542
543 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700544 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900549
550 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700551 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
552 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
554 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900557
558 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800559 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
560 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900562 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900564 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
565 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900566 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900567 // .. but not for java libs
568 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900569 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800570
Colin Cross7113d202019-11-20 16:39:12 -0800571 // Ensure that the platform variant ends with _shared or _common
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
573 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900574 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
575 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900576 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
577
578 // Ensure that dynamic dependency to java libs are not included
579 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800580
581 // Ensure that all symlinks are present.
582 found_foo_link_64 := false
583 found_foo := false
584 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900585 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800586 if strings.HasSuffix(cmd, "bin/foo") {
587 found_foo = true
588 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
589 found_foo_link_64 = true
590 }
591 }
592 }
593 good := found_foo && found_foo_link_64
594 if !good {
595 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
596 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900597
Artur Satayeva8bd1132020-04-27 18:07:06 +0100598 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100600 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
601 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
602 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100603
604 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100606 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
608 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800609}
610
Jooyung Hanf21c7972019-12-16 22:32:06 +0900611func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800612 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900613 apex_defaults {
614 name: "myapex-defaults",
615 key: "myapex.key",
616 prebuilts: ["myetc"],
617 native_shared_libs: ["mylib"],
618 java_libs: ["myjar"],
619 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900620 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800621 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000622 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900623 }
624
625 prebuilt_etc {
626 name: "myetc",
627 src: "myprebuilt",
628 }
629
630 apex {
631 name: "myapex",
632 defaults: ["myapex-defaults"],
633 }
634
635 apex_key {
636 name: "myapex.key",
637 public_key: "testkey.avbpubkey",
638 private_key: "testkey.pem",
639 }
640
641 cc_library {
642 name: "mylib",
643 system_shared_libs: [],
644 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000645 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900646 }
647
648 java_library {
649 name: "myjar",
650 srcs: ["foo/bar/MyClass.java"],
651 sdk_version: "none",
652 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000653 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900654 }
655
656 android_app {
657 name: "AppFoo",
658 srcs: ["foo/bar/MyClass.java"],
659 sdk_version: "none",
660 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000661 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900662 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900663
664 runtime_resource_overlay {
665 name: "rro",
666 theme: "blue",
667 }
668
markchien2f59ec92020-09-02 16:23:38 +0800669 bpf {
670 name: "bpf",
671 srcs: ["bpf.c", "bpf2.c"],
672 }
673
Ken Chenfad7f9d2021-11-10 22:02:57 +0800674 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800675 name: "netdTest",
676 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800677 sub_dir: "netd",
678 }
679
Jooyung Hanf21c7972019-12-16 22:32:06 +0900680 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000681 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900682 "etc/myetc",
683 "javalib/myjar.jar",
684 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000685 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900686 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800687 "etc/bpf/bpf.o",
688 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800689 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900690 })
691}
692
Jooyung Han01a3ee22019-11-02 02:52:25 +0900693func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800694 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900695 apex {
696 name: "myapex",
697 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000698 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900699 }
700
701 apex_key {
702 name: "myapex.key",
703 public_key: "testkey.avbpubkey",
704 private_key: "testkey.pem",
705 }
706 `)
707
708 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900709 args := module.Rule("apexRule").Args
710 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
711 t.Error("manifest should be apex_manifest.pb, but " + manifest)
712 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900713}
714
Liz Kammer4854a7d2021-05-27 14:28:27 -0400715func TestApexManifestMinSdkVersion(t *testing.T) {
716 ctx := testApex(t, `
717 apex_defaults {
718 name: "my_defaults",
719 key: "myapex.key",
720 product_specific: true,
721 file_contexts: ":my-file-contexts",
722 updatable: false,
723 }
724 apex {
725 name: "myapex_30",
726 min_sdk_version: "30",
727 defaults: ["my_defaults"],
728 }
729
730 apex {
731 name: "myapex_current",
732 min_sdk_version: "current",
733 defaults: ["my_defaults"],
734 }
735
736 apex {
737 name: "myapex_none",
738 defaults: ["my_defaults"],
739 }
740
741 apex_key {
742 name: "myapex.key",
743 public_key: "testkey.avbpubkey",
744 private_key: "testkey.pem",
745 }
746
747 filegroup {
748 name: "my-file-contexts",
749 srcs: ["product_specific_file_contexts"],
750 }
751 `, withFiles(map[string][]byte{
752 "product_specific_file_contexts": nil,
753 }), android.FixtureModifyProductVariables(
754 func(variables android.FixtureProductVariables) {
755 variables.Unbundled_build = proptools.BoolPtr(true)
756 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
757 }), android.FixtureMergeEnv(map[string]string{
758 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
759 }))
760
761 testCases := []struct {
762 module string
763 minSdkVersion string
764 }{
765 {
766 module: "myapex_30",
767 minSdkVersion: "30",
768 },
769 {
770 module: "myapex_current",
771 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
772 },
773 {
774 module: "myapex_none",
775 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
776 },
777 }
778 for _, tc := range testCases {
779 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
780 args := module.Rule("apexRule").Args
781 optFlags := args["opt_flags"]
782 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
783 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
784 }
785 }
786}
787
Alex Light5098a612018-11-29 17:12:15 -0800788func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800789 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800790 apex {
791 name: "myapex",
792 key: "myapex.key",
793 payload_type: "zip",
794 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000795 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800796 }
797
798 apex_key {
799 name: "myapex.key",
800 public_key: "testkey.avbpubkey",
801 private_key: "testkey.pem",
802 }
803
804 cc_library {
805 name: "mylib",
806 srcs: ["mylib.cpp"],
807 shared_libs: ["mylib2"],
808 system_shared_libs: [],
809 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000810 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800811 }
812
813 cc_library {
814 name: "mylib2",
815 srcs: ["mylib.cpp"],
816 system_shared_libs: [],
817 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000818 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800819 }
820 `)
821
Sundong Ahnabb64432019-10-22 13:58:29 +0900822 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800823 copyCmds := zipApexRule.Args["copy_commands"]
824
825 // Ensure that main rule creates an output
826 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
827
828 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700829 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800830
831 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700832 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800833
834 // Ensure that both direct and indirect deps are copied into apex
835 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
836 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837}
838
839func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800840 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900841 apex {
842 name: "myapex",
843 key: "myapex.key",
844 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900845 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000846 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900847 }
848
849 apex_key {
850 name: "myapex.key",
851 public_key: "testkey.avbpubkey",
852 private_key: "testkey.pem",
853 }
854
855 cc_library {
856 name: "mylib",
857 srcs: ["mylib.cpp"],
858 shared_libs: ["mylib2", "mylib3"],
859 system_shared_libs: [],
860 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000861 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900862 }
863
864 cc_library {
865 name: "mylib2",
866 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900867 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868 system_shared_libs: [],
869 stl: "none",
870 stubs: {
871 versions: ["1", "2", "3"],
872 },
873 }
874
875 cc_library {
876 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900877 srcs: ["mylib.cpp"],
878 shared_libs: ["mylib4"],
879 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900880 stl: "none",
881 stubs: {
882 versions: ["10", "11", "12"],
883 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000884 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900885 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900886
887 cc_library {
888 name: "mylib4",
889 srcs: ["mylib.cpp"],
890 system_shared_libs: [],
891 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000892 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900893 }
Jiyong Park105dc322021-06-11 17:22:09 +0900894
895 rust_binary {
896 name: "foo.rust",
897 srcs: ["foo.rs"],
898 shared_libs: ["libfoo.shared_from_rust"],
899 prefer_rlib: true,
900 apex_available: ["myapex"],
901 }
902
903 cc_library_shared {
904 name: "libfoo.shared_from_rust",
905 srcs: ["mylib.cpp"],
906 system_shared_libs: [],
907 stl: "none",
908 stubs: {
909 versions: ["10", "11", "12"],
910 },
911 }
912
Jiyong Park25fc6a92018-11-18 18:02:45 +0900913 `)
914
Sundong Ahnabb64432019-10-22 13:58:29 +0900915 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 copyCmds := apexRule.Args["copy_commands"]
917
918 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800919 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900920
921 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800922 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900923
924 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800925 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900926
Colin Crossaede88c2020-08-11 12:17:01 -0700927 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928
929 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900930 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900932 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933
934 // 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 -0700935 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700937 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900938
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700939 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
940 // is replaced by sharing of "cFlags" in cc/builder.go.
941 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
942 // module variable representing "cflags". So it was not detected by ensureNotContains.
943 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
944 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
945 // including the original cflags's "-include mylib.h".
946 //
Jiyong Park64379952018-12-13 18:37:29 +0900947 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700948 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
949 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900950
Jiyong Park85cc35a2022-07-17 11:30:47 +0900951 // Ensure that genstub for platform-provided lib is invoked with --systemapi
952 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
953 // Ensure that genstub for apex-provided lib is invoked with --apex
954 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900955
Jooyung Hana57af4a2020-01-23 05:36:59 +0000956 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900957 "lib64/mylib.so",
958 "lib64/mylib3.so",
959 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900960 "bin/foo.rust",
961 "lib64/libc++.so", // by the implicit dependency from foo.rust
962 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900963 })
Jiyong Park105dc322021-06-11 17:22:09 +0900964
965 // Ensure that stub dependency from a rust module is not included
966 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
967 // The rust module is linked to the stub cc library
968 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
969 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
970 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900971
972 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
973 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974}
975
Jiyong Park1bc84122021-06-22 20:23:05 +0900976func TestApexCanUsePrivateApis(t *testing.T) {
977 ctx := testApex(t, `
978 apex {
979 name: "myapex",
980 key: "myapex.key",
981 native_shared_libs: ["mylib"],
982 binaries: ["foo.rust"],
983 updatable: false,
984 platform_apis: true,
985 }
986
987 apex_key {
988 name: "myapex.key",
989 public_key: "testkey.avbpubkey",
990 private_key: "testkey.pem",
991 }
992
993 cc_library {
994 name: "mylib",
995 srcs: ["mylib.cpp"],
996 shared_libs: ["mylib2"],
997 system_shared_libs: [],
998 stl: "none",
999 apex_available: [ "myapex" ],
1000 }
1001
1002 cc_library {
1003 name: "mylib2",
1004 srcs: ["mylib.cpp"],
1005 cflags: ["-include mylib.h"],
1006 system_shared_libs: [],
1007 stl: "none",
1008 stubs: {
1009 versions: ["1", "2", "3"],
1010 },
1011 }
1012
1013 rust_binary {
1014 name: "foo.rust",
1015 srcs: ["foo.rs"],
1016 shared_libs: ["libfoo.shared_from_rust"],
1017 prefer_rlib: true,
1018 apex_available: ["myapex"],
1019 }
1020
1021 cc_library_shared {
1022 name: "libfoo.shared_from_rust",
1023 srcs: ["mylib.cpp"],
1024 system_shared_libs: [],
1025 stl: "none",
1026 stubs: {
1027 versions: ["10", "11", "12"],
1028 },
1029 }
1030 `)
1031
1032 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1033 copyCmds := apexRule.Args["copy_commands"]
1034
1035 // Ensure that indirect stubs dep is not included
1036 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1037 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1038
1039 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1040 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001041 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001042 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1043 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Parkd4a00632022-04-12 12:23:20 +09001044 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001045 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1046 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1047}
1048
Colin Cross7812fd32020-09-25 12:35:10 -07001049func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1050 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001051 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001052 apex {
1053 name: "myapex",
1054 key: "myapex.key",
1055 native_shared_libs: ["mylib", "mylib3"],
1056 min_sdk_version: "29",
1057 }
1058
1059 apex_key {
1060 name: "myapex.key",
1061 public_key: "testkey.avbpubkey",
1062 private_key: "testkey.pem",
1063 }
1064
1065 cc_library {
1066 name: "mylib",
1067 srcs: ["mylib.cpp"],
1068 shared_libs: ["mylib2", "mylib3"],
1069 system_shared_libs: [],
1070 stl: "none",
1071 apex_available: [ "myapex" ],
1072 min_sdk_version: "28",
1073 }
1074
1075 cc_library {
1076 name: "mylib2",
1077 srcs: ["mylib.cpp"],
1078 cflags: ["-include mylib.h"],
1079 system_shared_libs: [],
1080 stl: "none",
1081 stubs: {
1082 versions: ["28", "29", "30", "current"],
1083 },
1084 min_sdk_version: "28",
1085 }
1086
1087 cc_library {
1088 name: "mylib3",
1089 srcs: ["mylib.cpp"],
1090 shared_libs: ["mylib4"],
1091 system_shared_libs: [],
1092 stl: "none",
1093 stubs: {
1094 versions: ["28", "29", "30", "current"],
1095 },
1096 apex_available: [ "myapex" ],
1097 min_sdk_version: "28",
1098 }
1099
1100 cc_library {
1101 name: "mylib4",
1102 srcs: ["mylib.cpp"],
1103 system_shared_libs: [],
1104 stl: "none",
1105 apex_available: [ "myapex" ],
1106 min_sdk_version: "28",
1107 }
1108 `)
1109
1110 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1111 copyCmds := apexRule.Args["copy_commands"]
1112
1113 // Ensure that direct non-stubs dep is always included
1114 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1115
1116 // Ensure that indirect stubs dep is not included
1117 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1118
1119 // Ensure that direct stubs dep is included
1120 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1121
1122 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1123
Jiyong Park55549df2021-02-26 23:57:23 +09001124 // Ensure that mylib is linking with the latest version of stub for mylib2
1125 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001126 // ... and not linking to the non-stub (impl) variant of mylib2
1127 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1128
1129 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1130 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1131 // .. and not linking to the stubs variant of mylib3
1132 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1133
1134 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001135 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001136 ensureNotContains(t, mylib2Cflags, "-include ")
1137
Jiyong Park85cc35a2022-07-17 11:30:47 +09001138 // Ensure that genstub is invoked with --systemapi
1139 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001140
1141 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1142 "lib64/mylib.so",
1143 "lib64/mylib3.so",
1144 "lib64/mylib4.so",
1145 })
1146}
1147
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001148func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1149 t.Parallel()
1150 // myapex (Z)
1151 // mylib -----------------.
1152 // |
1153 // otherapex (29) |
1154 // libstub's versions: 29 Z current
1155 // |
1156 // <platform> |
1157 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001158 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001159 apex {
1160 name: "myapex",
1161 key: "myapex.key",
1162 native_shared_libs: ["mylib"],
1163 min_sdk_version: "Z", // non-final
1164 }
1165
1166 cc_library {
1167 name: "mylib",
1168 srcs: ["mylib.cpp"],
1169 shared_libs: ["libstub"],
1170 apex_available: ["myapex"],
1171 min_sdk_version: "Z",
1172 }
1173
1174 apex_key {
1175 name: "myapex.key",
1176 public_key: "testkey.avbpubkey",
1177 private_key: "testkey.pem",
1178 }
1179
1180 apex {
1181 name: "otherapex",
1182 key: "myapex.key",
1183 native_shared_libs: ["libstub"],
1184 min_sdk_version: "29",
1185 }
1186
1187 cc_library {
1188 name: "libstub",
1189 srcs: ["mylib.cpp"],
1190 stubs: {
1191 versions: ["29", "Z", "current"],
1192 },
1193 apex_available: ["otherapex"],
1194 min_sdk_version: "29",
1195 }
1196
1197 // platform module depending on libstub from otherapex should use the latest stub("current")
1198 cc_library {
1199 name: "libplatform",
1200 srcs: ["mylib.cpp"],
1201 shared_libs: ["libstub"],
1202 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001203 `,
1204 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1205 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1206 variables.Platform_sdk_final = proptools.BoolPtr(false)
1207 variables.Platform_version_active_codenames = []string{"Z"}
1208 }),
1209 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001210
Jiyong Park55549df2021-02-26 23:57:23 +09001211 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001212 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001213 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001214 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001215 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001216
1217 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1218 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1219 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1220 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1221 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1222}
1223
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001224func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001225 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001226 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001227 name: "myapex2",
1228 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001229 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001230 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001231 }
1232
1233 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001234 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001235 public_key: "testkey.avbpubkey",
1236 private_key: "testkey.pem",
1237 }
1238
1239 cc_library {
1240 name: "mylib",
1241 srcs: ["mylib.cpp"],
1242 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001243 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001244 system_shared_libs: [],
1245 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001246 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001247 }
1248
1249 cc_library {
1250 name: "libfoo",
1251 srcs: ["mylib.cpp"],
1252 shared_libs: ["libbar"],
1253 system_shared_libs: [],
1254 stl: "none",
1255 stubs: {
1256 versions: ["10", "20", "30"],
1257 },
1258 }
1259
1260 cc_library {
1261 name: "libbar",
1262 srcs: ["mylib.cpp"],
1263 system_shared_libs: [],
1264 stl: "none",
1265 }
1266
Jiyong Park678c8812020-02-07 17:25:49 +09001267 cc_library_static {
1268 name: "libbaz",
1269 srcs: ["mylib.cpp"],
1270 system_shared_libs: [],
1271 stl: "none",
1272 apex_available: [ "myapex2" ],
1273 }
1274
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001275 `)
1276
Jiyong Park83dc74b2020-01-14 18:38:44 +09001277 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001278 copyCmds := apexRule.Args["copy_commands"]
1279
1280 // Ensure that direct non-stubs dep is always included
1281 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1282
1283 // Ensure that indirect stubs dep is not included
1284 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1285
1286 // Ensure that dependency of stubs is not included
1287 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1288
Colin Crossaede88c2020-08-11 12:17:01 -07001289 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001290
1291 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001292 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001293 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001294 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001295
Jiyong Park3ff16992019-12-27 14:11:47 +09001296 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001297
1298 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1299 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001300
Artur Satayeva8bd1132020-04-27 18:07:06 +01001301 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001302 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001303
Artur Satayeva8bd1132020-04-27 18:07:06 +01001304 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001305 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001306}
1307
Jooyung Hand3639552019-08-09 12:57:43 +09001308func TestApexWithRuntimeLibsDependency(t *testing.T) {
1309 /*
1310 myapex
1311 |
1312 v (runtime_libs)
1313 mylib ------+------> libfoo [provides stub]
1314 |
1315 `------> libbar
1316 */
Colin Cross1c460562021-02-16 17:55:47 -08001317 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001318 apex {
1319 name: "myapex",
1320 key: "myapex.key",
1321 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001322 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001323 }
1324
1325 apex_key {
1326 name: "myapex.key",
1327 public_key: "testkey.avbpubkey",
1328 private_key: "testkey.pem",
1329 }
1330
1331 cc_library {
1332 name: "mylib",
1333 srcs: ["mylib.cpp"],
1334 runtime_libs: ["libfoo", "libbar"],
1335 system_shared_libs: [],
1336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001337 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001338 }
1339
1340 cc_library {
1341 name: "libfoo",
1342 srcs: ["mylib.cpp"],
1343 system_shared_libs: [],
1344 stl: "none",
1345 stubs: {
1346 versions: ["10", "20", "30"],
1347 },
1348 }
1349
1350 cc_library {
1351 name: "libbar",
1352 srcs: ["mylib.cpp"],
1353 system_shared_libs: [],
1354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001355 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001356 }
1357
1358 `)
1359
Sundong Ahnabb64432019-10-22 13:58:29 +09001360 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001361 copyCmds := apexRule.Args["copy_commands"]
1362
1363 // Ensure that direct non-stubs dep is always included
1364 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1365
1366 // Ensure that indirect stubs dep is not included
1367 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1368
1369 // Ensure that runtime_libs dep in included
1370 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1371
Sundong Ahnabb64432019-10-22 13:58:29 +09001372 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001373 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1374 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001375
1376}
1377
Paul Duffina02cae32021-03-09 01:44:06 +00001378var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1379 cc.PrepareForTestWithCcBuildComponents,
1380 PrepareForTestWithApexBuildComponents,
1381 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001382 apex {
1383 name: "com.android.runtime",
1384 key: "com.android.runtime.key",
1385 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001386 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001387 }
1388
1389 apex_key {
1390 name: "com.android.runtime.key",
1391 public_key: "testkey.avbpubkey",
1392 private_key: "testkey.pem",
1393 }
Paul Duffina02cae32021-03-09 01:44:06 +00001394 `),
1395 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1396)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001397
Paul Duffina02cae32021-03-09 01:44:06 +00001398func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001399 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001400 cc_library {
1401 name: "libc",
1402 no_libcrt: true,
1403 nocrt: true,
1404 stl: "none",
1405 system_shared_libs: [],
1406 stubs: { versions: ["1"] },
1407 apex_available: ["com.android.runtime"],
1408
1409 sanitize: {
1410 hwaddress: true,
1411 }
1412 }
1413
1414 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001415 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001416 no_libcrt: true,
1417 nocrt: true,
1418 stl: "none",
1419 system_shared_libs: [],
1420 srcs: [""],
1421 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001422 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001423
1424 sanitize: {
1425 never: true,
1426 },
Paul Duffina02cae32021-03-09 01:44:06 +00001427 } `)
1428 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001429
1430 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1431 "lib64/bionic/libc.so",
1432 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1433 })
1434
Colin Cross4c4c1be2022-02-10 11:41:18 -08001435 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436
1437 installed := hwasan.Description("install libclang_rt.hwasan")
1438 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1439
1440 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1441 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1442 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1443}
1444
1445func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001446 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001447 prepareForTestOfRuntimeApexWithHwasan,
1448 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1449 variables.SanitizeDevice = []string{"hwaddress"}
1450 }),
1451 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001452 cc_library {
1453 name: "libc",
1454 no_libcrt: true,
1455 nocrt: true,
1456 stl: "none",
1457 system_shared_libs: [],
1458 stubs: { versions: ["1"] },
1459 apex_available: ["com.android.runtime"],
1460 }
1461
1462 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001463 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001464 no_libcrt: true,
1465 nocrt: true,
1466 stl: "none",
1467 system_shared_libs: [],
1468 srcs: [""],
1469 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001470 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001471
1472 sanitize: {
1473 never: true,
1474 },
1475 }
Paul Duffina02cae32021-03-09 01:44:06 +00001476 `)
1477 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001478
1479 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1480 "lib64/bionic/libc.so",
1481 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1482 })
1483
Colin Cross4c4c1be2022-02-10 11:41:18 -08001484 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001485
1486 installed := hwasan.Description("install libclang_rt.hwasan")
1487 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1488
1489 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1490 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1491 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1492}
1493
Jooyung Han61b66e92020-03-21 14:21:46 +00001494func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1495 testcases := []struct {
1496 name string
1497 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001498 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001499 shouldLink string
1500 shouldNotLink []string
1501 }{
1502 {
Jiyong Park55549df2021-02-26 23:57:23 +09001503 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001504 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001505 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001506 shouldLink: "current",
1507 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001508 },
1509 {
Jiyong Park55549df2021-02-26 23:57:23 +09001510 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001511 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001512 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001513 shouldLink: "current",
1514 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001515 },
1516 }
1517 for _, tc := range testcases {
1518 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001519 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001520 apex {
1521 name: "myapex",
1522 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001523 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001524 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001525 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001526 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001527
Jooyung Han61b66e92020-03-21 14:21:46 +00001528 apex_key {
1529 name: "myapex.key",
1530 public_key: "testkey.avbpubkey",
1531 private_key: "testkey.pem",
1532 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001533
Jooyung Han61b66e92020-03-21 14:21:46 +00001534 cc_library {
1535 name: "mylib",
1536 srcs: ["mylib.cpp"],
1537 vendor_available: true,
1538 shared_libs: ["libbar"],
1539 system_shared_libs: [],
1540 stl: "none",
1541 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001542 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001543 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001544
Jooyung Han61b66e92020-03-21 14:21:46 +00001545 cc_library {
1546 name: "libbar",
1547 srcs: ["mylib.cpp"],
1548 system_shared_libs: [],
1549 stl: "none",
1550 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001551 llndk: {
1552 symbol_file: "libbar.map.txt",
1553 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001554 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001555 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001556 withUnbundledBuild,
1557 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001558
Jooyung Han61b66e92020-03-21 14:21:46 +00001559 // Ensure that LLNDK dep is not included
1560 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1561 "lib64/mylib.so",
1562 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001563
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 // Ensure that LLNDK dep is required
1565 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1566 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1567 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001568
Steven Moreland2c4000c2021-04-27 02:08:49 +00001569 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1570 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001571 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001572 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001573 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001574
Steven Moreland2c4000c2021-04-27 02:08:49 +00001575 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001576 ver := tc.shouldLink
1577 if tc.shouldLink == "current" {
1578 ver = strconv.Itoa(android.FutureApiLevelInt)
1579 }
1580 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001581 })
1582 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001583}
1584
Jiyong Park25fc6a92018-11-18 18:02:45 +09001585func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001586 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001587 apex {
1588 name: "myapex",
1589 key: "myapex.key",
1590 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001591 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001592 }
1593
1594 apex_key {
1595 name: "myapex.key",
1596 public_key: "testkey.avbpubkey",
1597 private_key: "testkey.pem",
1598 }
1599
1600 cc_library {
1601 name: "mylib",
1602 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001603 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001604 shared_libs: ["libdl#27"],
1605 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001606 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001607 }
1608
1609 cc_library_shared {
1610 name: "mylib_shared",
1611 srcs: ["mylib.cpp"],
1612 shared_libs: ["libdl#27"],
1613 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001614 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001615 }
1616
1617 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001618 name: "libBootstrap",
1619 srcs: ["mylib.cpp"],
1620 stl: "none",
1621 bootstrap: true,
1622 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623 `)
1624
Sundong Ahnabb64432019-10-22 13:58:29 +09001625 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001626 copyCmds := apexRule.Args["copy_commands"]
1627
1628 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001629 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001630 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1631 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001632
1633 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001634 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001635
Colin Crossaede88c2020-08-11 12:17:01 -07001636 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1637 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1638 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001639
1640 // For dependency to libc
1641 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001642 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001644 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001645 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001646 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1647 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001648
1649 // For dependency to libm
1650 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001651 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001652 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001653 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001654 // ... and is not compiling with the stub
1655 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1656 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1657
1658 // For dependency to libdl
1659 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001660 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001661 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001662 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1663 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001664 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001665 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001666 // ... Cflags from stub is correctly exported to mylib
1667 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1668 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001669
1670 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001671 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1672 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1673 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1674 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001675}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001676
Jooyung Han749dc692020-04-15 11:03:39 +09001677func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001678 // there are three links between liba --> libz.
1679 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001680 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001681 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001682 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001683 apex {
1684 name: "myapex",
1685 key: "myapex.key",
1686 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001687 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001688 }
1689
1690 apex {
1691 name: "otherapex",
1692 key: "myapex.key",
1693 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001694 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001695 }
1696
1697 apex_key {
1698 name: "myapex.key",
1699 public_key: "testkey.avbpubkey",
1700 private_key: "testkey.pem",
1701 }
1702
1703 cc_library {
1704 name: "libx",
1705 shared_libs: ["liba"],
1706 system_shared_libs: [],
1707 stl: "none",
1708 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001709 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001710 }
1711
1712 cc_library {
1713 name: "liby",
1714 shared_libs: ["liba"],
1715 system_shared_libs: [],
1716 stl: "none",
1717 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001718 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001719 }
1720
1721 cc_library {
1722 name: "liba",
1723 shared_libs: ["libz"],
1724 system_shared_libs: [],
1725 stl: "none",
1726 apex_available: [
1727 "//apex_available:anyapex",
1728 "//apex_available:platform",
1729 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001730 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001731 }
1732
1733 cc_library {
1734 name: "libz",
1735 system_shared_libs: [],
1736 stl: "none",
1737 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001738 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001739 },
1740 }
Jooyung Han749dc692020-04-15 11:03:39 +09001741 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001742
1743 expectLink := func(from, from_variant, to, to_variant string) {
1744 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1745 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1746 }
1747 expectNoLink := func(from, from_variant, to, to_variant string) {
1748 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1749 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1750 }
1751 // platform liba is linked to non-stub version
1752 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001753 // liba in myapex is linked to current
1754 expectLink("liba", "shared_apex29", "libz", "shared_current")
1755 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001756 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001757 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001758 // liba in otherapex is linked to current
1759 expectLink("liba", "shared_apex30", "libz", "shared_current")
1760 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001761 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1762 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001763}
1764
Jooyung Hanaed150d2020-04-02 01:41:41 +09001765func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001766 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001767 apex {
1768 name: "myapex",
1769 key: "myapex.key",
1770 native_shared_libs: ["libx"],
1771 min_sdk_version: "R",
1772 }
1773
1774 apex_key {
1775 name: "myapex.key",
1776 public_key: "testkey.avbpubkey",
1777 private_key: "testkey.pem",
1778 }
1779
1780 cc_library {
1781 name: "libx",
1782 shared_libs: ["libz"],
1783 system_shared_libs: [],
1784 stl: "none",
1785 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001786 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001787 }
1788
1789 cc_library {
1790 name: "libz",
1791 system_shared_libs: [],
1792 stl: "none",
1793 stubs: {
1794 versions: ["29", "R"],
1795 },
1796 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001797 `,
1798 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1799 variables.Platform_version_active_codenames = []string{"R"}
1800 }),
1801 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001802
1803 expectLink := func(from, from_variant, to, to_variant string) {
1804 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1805 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1806 }
1807 expectNoLink := func(from, from_variant, to, to_variant string) {
1808 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1809 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1810 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001811 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1812 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001813 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1814 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001815}
1816
Jooyung Han4c4da062021-06-23 10:23:16 +09001817func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1818 testApex(t, `
1819 apex {
1820 name: "myapex",
1821 key: "myapex.key",
1822 java_libs: ["libx"],
1823 min_sdk_version: "S",
1824 }
1825
1826 apex_key {
1827 name: "myapex.key",
1828 public_key: "testkey.avbpubkey",
1829 private_key: "testkey.pem",
1830 }
1831
1832 java_library {
1833 name: "libx",
1834 srcs: ["a.java"],
1835 apex_available: [ "myapex" ],
1836 sdk_version: "current",
1837 min_sdk_version: "S", // should be okay
1838 }
1839 `,
1840 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1841 variables.Platform_version_active_codenames = []string{"S"}
1842 variables.Platform_sdk_codename = proptools.StringPtr("S")
1843 }),
1844 )
1845}
1846
Jooyung Han749dc692020-04-15 11:03:39 +09001847func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001848 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001849 apex {
1850 name: "myapex",
1851 key: "myapex.key",
1852 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001853 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001854 }
1855
1856 apex_key {
1857 name: "myapex.key",
1858 public_key: "testkey.avbpubkey",
1859 private_key: "testkey.pem",
1860 }
1861
1862 cc_library {
1863 name: "libx",
1864 shared_libs: ["libz"],
1865 system_shared_libs: [],
1866 stl: "none",
1867 apex_available: [ "myapex" ],
1868 }
1869
1870 cc_library {
1871 name: "libz",
1872 system_shared_libs: [],
1873 stl: "none",
1874 stubs: {
1875 versions: ["1", "2"],
1876 },
1877 }
1878 `)
1879
1880 expectLink := func(from, from_variant, to, to_variant string) {
1881 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1882 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1883 }
1884 expectNoLink := func(from, from_variant, to, to_variant string) {
1885 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1886 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1887 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001888 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001889 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001890 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001891 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001892}
1893
Jiyong Park5df7bd32021-08-25 16:18:46 +09001894func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1895 ctx := testApex(t, `
1896 apex {
1897 name: "myapex",
1898 key: "myapex.key",
1899 native_shared_libs: ["mylib"],
1900 updatable: false,
1901 vendor: true,
1902 min_sdk_version: "29",
1903 }
1904
1905 apex_key {
1906 name: "myapex.key",
1907 public_key: "testkey.avbpubkey",
1908 private_key: "testkey.pem",
1909 }
1910
1911 cc_library {
1912 name: "mylib",
1913 vendor_available: true,
1914 system_shared_libs: [],
1915 stl: "none",
1916 apex_available: [ "myapex" ],
1917 min_sdk_version: "29",
1918 }
1919 `)
1920
1921 vendorVariant := "android_vendor.29_arm64_armv8-a"
1922
1923 // First check that the correct variant of crtbegin_so is used.
1924 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1925 crtBegin := names(ldRule.Args["crtBegin"])
1926 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1927
1928 // Ensure that the crtbegin_so used by the APEX is targeting 29
1929 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1930 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1931}
1932
Jooyung Han03b51852020-02-26 22:45:42 +09001933func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001934 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001935 apex {
1936 name: "myapex",
1937 key: "myapex.key",
1938 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001939 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001940 }
1941
1942 apex_key {
1943 name: "myapex.key",
1944 public_key: "testkey.avbpubkey",
1945 private_key: "testkey.pem",
1946 }
1947
1948 cc_library {
1949 name: "libx",
1950 system_shared_libs: [],
1951 stl: "none",
1952 apex_available: [ "myapex" ],
1953 stubs: {
1954 versions: ["1", "2"],
1955 },
1956 }
1957
1958 cc_library {
1959 name: "libz",
1960 shared_libs: ["libx"],
1961 system_shared_libs: [],
1962 stl: "none",
1963 }
1964 `)
1965
1966 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001967 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001968 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1969 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1970 }
1971 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001972 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001973 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1974 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1975 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001976 expectLink("libz", "shared", "libx", "shared_current")
1977 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001978 expectNoLink("libz", "shared", "libz", "shared_1")
1979 expectNoLink("libz", "shared", "libz", "shared")
1980}
1981
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001982var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1983 func(variables android.FixtureProductVariables) {
1984 variables.SanitizeDevice = []string{"hwaddress"}
1985 },
1986)
1987
Jooyung Han75568392020-03-20 04:29:24 +09001988func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001989 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001990 apex {
1991 name: "myapex",
1992 key: "myapex.key",
1993 native_shared_libs: ["libx"],
1994 min_sdk_version: "29",
1995 }
1996
1997 apex_key {
1998 name: "myapex.key",
1999 public_key: "testkey.avbpubkey",
2000 private_key: "testkey.pem",
2001 }
2002
2003 cc_library {
2004 name: "libx",
2005 shared_libs: ["libbar"],
2006 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002007 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002008 }
2009
2010 cc_library {
2011 name: "libbar",
2012 stubs: {
2013 versions: ["29", "30"],
2014 },
2015 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002016 `,
2017 prepareForTestWithSantitizeHwaddress,
2018 )
Jooyung Han03b51852020-02-26 22:45:42 +09002019 expectLink := func(from, from_variant, to, to_variant string) {
2020 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2021 libFlags := ld.Args["libFlags"]
2022 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2023 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002024 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002025}
2026
Jooyung Han75568392020-03-20 04:29:24 +09002027func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002028 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002029 apex {
2030 name: "myapex",
2031 key: "myapex.key",
2032 native_shared_libs: ["libx"],
2033 min_sdk_version: "29",
2034 }
2035
2036 apex_key {
2037 name: "myapex.key",
2038 public_key: "testkey.avbpubkey",
2039 private_key: "testkey.pem",
2040 }
2041
2042 cc_library {
2043 name: "libx",
2044 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002045 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002046 }
Jooyung Han75568392020-03-20 04:29:24 +09002047 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002048
2049 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002050 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002051 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002052 // note that platform variant is not.
2053 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002054 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002055}
2056
Jooyung Han749dc692020-04-15 11:03:39 +09002057func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2058 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002059 apex {
2060 name: "myapex",
2061 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002062 native_shared_libs: ["mylib"],
2063 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002064 }
2065
2066 apex_key {
2067 name: "myapex.key",
2068 public_key: "testkey.avbpubkey",
2069 private_key: "testkey.pem",
2070 }
Jooyung Han749dc692020-04-15 11:03:39 +09002071
2072 cc_library {
2073 name: "mylib",
2074 srcs: ["mylib.cpp"],
2075 system_shared_libs: [],
2076 stl: "none",
2077 apex_available: [
2078 "myapex",
2079 ],
2080 min_sdk_version: "30",
2081 }
2082 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002083
2084 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2085 apex {
2086 name: "myapex",
2087 key: "myapex.key",
2088 native_shared_libs: ["libfoo.ffi"],
2089 min_sdk_version: "29",
2090 }
2091
2092 apex_key {
2093 name: "myapex.key",
2094 public_key: "testkey.avbpubkey",
2095 private_key: "testkey.pem",
2096 }
2097
2098 rust_ffi_shared {
2099 name: "libfoo.ffi",
2100 srcs: ["foo.rs"],
2101 crate_name: "foo",
2102 apex_available: [
2103 "myapex",
2104 ],
2105 min_sdk_version: "30",
2106 }
2107 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002108
2109 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2110 apex {
2111 name: "myapex",
2112 key: "myapex.key",
2113 java_libs: ["libfoo"],
2114 min_sdk_version: "29",
2115 }
2116
2117 apex_key {
2118 name: "myapex.key",
2119 public_key: "testkey.avbpubkey",
2120 private_key: "testkey.pem",
2121 }
2122
2123 java_import {
2124 name: "libfoo",
2125 jars: ["libfoo.jar"],
2126 apex_available: [
2127 "myapex",
2128 ],
2129 min_sdk_version: "30",
2130 }
2131 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002132}
2133
2134func TestApexMinSdkVersion_Okay(t *testing.T) {
2135 testApex(t, `
2136 apex {
2137 name: "myapex",
2138 key: "myapex.key",
2139 native_shared_libs: ["libfoo"],
2140 java_libs: ["libbar"],
2141 min_sdk_version: "29",
2142 }
2143
2144 apex_key {
2145 name: "myapex.key",
2146 public_key: "testkey.avbpubkey",
2147 private_key: "testkey.pem",
2148 }
2149
2150 cc_library {
2151 name: "libfoo",
2152 srcs: ["mylib.cpp"],
2153 shared_libs: ["libfoo_dep"],
2154 apex_available: ["myapex"],
2155 min_sdk_version: "29",
2156 }
2157
2158 cc_library {
2159 name: "libfoo_dep",
2160 srcs: ["mylib.cpp"],
2161 apex_available: ["myapex"],
2162 min_sdk_version: "29",
2163 }
2164
2165 java_library {
2166 name: "libbar",
2167 sdk_version: "current",
2168 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002169 static_libs: [
2170 "libbar_dep",
2171 "libbar_import_dep",
2172 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002173 apex_available: ["myapex"],
2174 min_sdk_version: "29",
2175 }
2176
2177 java_library {
2178 name: "libbar_dep",
2179 sdk_version: "current",
2180 srcs: ["a.java"],
2181 apex_available: ["myapex"],
2182 min_sdk_version: "29",
2183 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002184
2185 java_import {
2186 name: "libbar_import_dep",
2187 jars: ["libbar.jar"],
2188 apex_available: ["myapex"],
2189 min_sdk_version: "29",
2190 }
Jooyung Han03b51852020-02-26 22:45:42 +09002191 `)
2192}
2193
Colin Cross8ca61c12022-10-06 21:00:14 -07002194func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2195 // Tests that an apex dependency with min_sdk_version higher than the
2196 // min_sdk_version of the apex is allowed as long as the dependency's
2197 // min_sdk_version is less than or equal to the api level that the
2198 // architecture was introduced in. In this case, arm64 didn't exist
2199 // until api level 21, so the arm64 code will never need to run on
2200 // an api level 20 device, even if other architectures of the apex
2201 // will.
2202 testApex(t, `
2203 apex {
2204 name: "myapex",
2205 key: "myapex.key",
2206 native_shared_libs: ["libfoo"],
2207 min_sdk_version: "20",
2208 }
2209
2210 apex_key {
2211 name: "myapex.key",
2212 public_key: "testkey.avbpubkey",
2213 private_key: "testkey.pem",
2214 }
2215
2216 cc_library {
2217 name: "libfoo",
2218 srcs: ["mylib.cpp"],
2219 apex_available: ["myapex"],
2220 min_sdk_version: "21",
2221 stl: "none",
2222 }
2223 `)
2224}
2225
Artur Satayev8cf899a2020-04-15 17:29:42 +01002226func TestJavaStableSdkVersion(t *testing.T) {
2227 testCases := []struct {
2228 name string
2229 expectedError string
2230 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002231 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002232 }{
2233 {
2234 name: "Non-updatable apex with non-stable dep",
2235 bp: `
2236 apex {
2237 name: "myapex",
2238 java_libs: ["myjar"],
2239 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002240 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002241 }
2242 apex_key {
2243 name: "myapex.key",
2244 public_key: "testkey.avbpubkey",
2245 private_key: "testkey.pem",
2246 }
2247 java_library {
2248 name: "myjar",
2249 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002250 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002251 apex_available: ["myapex"],
2252 }
2253 `,
2254 },
2255 {
2256 name: "Updatable apex with stable dep",
2257 bp: `
2258 apex {
2259 name: "myapex",
2260 java_libs: ["myjar"],
2261 key: "myapex.key",
2262 updatable: true,
2263 min_sdk_version: "29",
2264 }
2265 apex_key {
2266 name: "myapex.key",
2267 public_key: "testkey.avbpubkey",
2268 private_key: "testkey.pem",
2269 }
2270 java_library {
2271 name: "myjar",
2272 srcs: ["foo/bar/MyClass.java"],
2273 sdk_version: "current",
2274 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002275 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002276 }
2277 `,
2278 },
2279 {
2280 name: "Updatable apex with non-stable dep",
2281 expectedError: "cannot depend on \"myjar\"",
2282 bp: `
2283 apex {
2284 name: "myapex",
2285 java_libs: ["myjar"],
2286 key: "myapex.key",
2287 updatable: true,
2288 }
2289 apex_key {
2290 name: "myapex.key",
2291 public_key: "testkey.avbpubkey",
2292 private_key: "testkey.pem",
2293 }
2294 java_library {
2295 name: "myjar",
2296 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002297 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002298 apex_available: ["myapex"],
2299 }
2300 `,
2301 },
2302 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002303 name: "Updatable apex with non-stable legacy core platform dep",
2304 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2305 bp: `
2306 apex {
2307 name: "myapex",
2308 java_libs: ["myjar-uses-legacy"],
2309 key: "myapex.key",
2310 updatable: true,
2311 }
2312 apex_key {
2313 name: "myapex.key",
2314 public_key: "testkey.avbpubkey",
2315 private_key: "testkey.pem",
2316 }
2317 java_library {
2318 name: "myjar-uses-legacy",
2319 srcs: ["foo/bar/MyClass.java"],
2320 sdk_version: "core_platform",
2321 apex_available: ["myapex"],
2322 }
2323 `,
2324 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2325 },
2326 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002327 name: "Updatable apex with non-stable transitive dep",
2328 // This is not actually detecting that the transitive dependency is unstable, rather it is
2329 // detecting that the transitive dependency is building against a wider API surface than the
2330 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002331 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002332 bp: `
2333 apex {
2334 name: "myapex",
2335 java_libs: ["myjar"],
2336 key: "myapex.key",
2337 updatable: true,
2338 }
2339 apex_key {
2340 name: "myapex.key",
2341 public_key: "testkey.avbpubkey",
2342 private_key: "testkey.pem",
2343 }
2344 java_library {
2345 name: "myjar",
2346 srcs: ["foo/bar/MyClass.java"],
2347 sdk_version: "current",
2348 apex_available: ["myapex"],
2349 static_libs: ["transitive-jar"],
2350 }
2351 java_library {
2352 name: "transitive-jar",
2353 srcs: ["foo/bar/MyClass.java"],
2354 sdk_version: "core_platform",
2355 apex_available: ["myapex"],
2356 }
2357 `,
2358 },
2359 }
2360
2361 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002362 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2363 continue
2364 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002365 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002366 errorHandler := android.FixtureExpectsNoErrors
2367 if test.expectedError != "" {
2368 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002369 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002370 android.GroupFixturePreparers(
2371 java.PrepareForTestWithJavaDefaultModules,
2372 PrepareForTestWithApexBuildComponents,
2373 prepareForTestWithMyapex,
2374 android.OptionalFixturePreparer(test.preparer),
2375 ).
2376 ExtendWithErrorHandler(errorHandler).
2377 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002378 })
2379 }
2380}
2381
Jooyung Han749dc692020-04-15 11:03:39 +09002382func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2383 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2384 apex {
2385 name: "myapex",
2386 key: "myapex.key",
2387 native_shared_libs: ["mylib"],
2388 min_sdk_version: "29",
2389 }
2390
2391 apex_key {
2392 name: "myapex.key",
2393 public_key: "testkey.avbpubkey",
2394 private_key: "testkey.pem",
2395 }
2396
2397 cc_library {
2398 name: "mylib",
2399 srcs: ["mylib.cpp"],
2400 shared_libs: ["mylib2"],
2401 system_shared_libs: [],
2402 stl: "none",
2403 apex_available: [
2404 "myapex",
2405 ],
2406 min_sdk_version: "29",
2407 }
2408
2409 // indirect part of the apex
2410 cc_library {
2411 name: "mylib2",
2412 srcs: ["mylib.cpp"],
2413 system_shared_libs: [],
2414 stl: "none",
2415 apex_available: [
2416 "myapex",
2417 ],
2418 min_sdk_version: "30",
2419 }
2420 `)
2421}
2422
2423func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2424 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2425 apex {
2426 name: "myapex",
2427 key: "myapex.key",
2428 apps: ["AppFoo"],
2429 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002430 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002431 }
2432
2433 apex_key {
2434 name: "myapex.key",
2435 public_key: "testkey.avbpubkey",
2436 private_key: "testkey.pem",
2437 }
2438
2439 android_app {
2440 name: "AppFoo",
2441 srcs: ["foo/bar/MyClass.java"],
2442 sdk_version: "current",
2443 min_sdk_version: "29",
2444 system_modules: "none",
2445 stl: "none",
2446 static_libs: ["bar"],
2447 apex_available: [ "myapex" ],
2448 }
2449
2450 java_library {
2451 name: "bar",
2452 sdk_version: "current",
2453 srcs: ["a.java"],
2454 apex_available: [ "myapex" ],
2455 }
2456 `)
2457}
2458
2459func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002460 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002461 apex {
2462 name: "myapex",
2463 key: "myapex.key",
2464 native_shared_libs: ["mylib"],
2465 min_sdk_version: "29",
2466 }
2467
2468 apex_key {
2469 name: "myapex.key",
2470 public_key: "testkey.avbpubkey",
2471 private_key: "testkey.pem",
2472 }
2473
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002474 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002475 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2476 cc_library {
2477 name: "mylib",
2478 srcs: ["mylib.cpp"],
2479 shared_libs: ["mylib2"],
2480 system_shared_libs: [],
2481 stl: "none",
2482 apex_available: ["myapex", "otherapex"],
2483 min_sdk_version: "29",
2484 }
2485
2486 cc_library {
2487 name: "mylib2",
2488 srcs: ["mylib.cpp"],
2489 system_shared_libs: [],
2490 stl: "none",
2491 apex_available: ["otherapex"],
2492 stubs: { versions: ["29", "30"] },
2493 min_sdk_version: "30",
2494 }
2495
2496 apex {
2497 name: "otherapex",
2498 key: "myapex.key",
2499 native_shared_libs: ["mylib", "mylib2"],
2500 min_sdk_version: "30",
2501 }
2502 `)
2503 expectLink := func(from, from_variant, to, to_variant string) {
2504 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2505 libFlags := ld.Args["libFlags"]
2506 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2507 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002508 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002509 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002510}
2511
Jooyung Haned124c32021-01-26 11:43:46 +09002512func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002513 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2514 func(variables android.FixtureProductVariables) {
2515 variables.Platform_sdk_codename = proptools.StringPtr("S")
2516 variables.Platform_version_active_codenames = []string{"S"}
2517 },
2518 )
Jooyung Haned124c32021-01-26 11:43:46 +09002519 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2520 apex {
2521 name: "myapex",
2522 key: "myapex.key",
2523 native_shared_libs: ["libfoo"],
2524 min_sdk_version: "S",
2525 }
2526 apex_key {
2527 name: "myapex.key",
2528 public_key: "testkey.avbpubkey",
2529 private_key: "testkey.pem",
2530 }
2531 cc_library {
2532 name: "libfoo",
2533 shared_libs: ["libbar"],
2534 apex_available: ["myapex"],
2535 min_sdk_version: "29",
2536 }
2537 cc_library {
2538 name: "libbar",
2539 apex_available: ["myapex"],
2540 }
2541 `, withSAsActiveCodeNames)
2542}
2543
2544func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002545 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2546 variables.Platform_sdk_codename = proptools.StringPtr("S")
2547 variables.Platform_version_active_codenames = []string{"S", "T"}
2548 })
Colin Cross1c460562021-02-16 17:55:47 -08002549 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002550 apex {
2551 name: "myapex",
2552 key: "myapex.key",
2553 native_shared_libs: ["libfoo"],
2554 min_sdk_version: "S",
2555 }
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561 cc_library {
2562 name: "libfoo",
2563 shared_libs: ["libbar"],
2564 apex_available: ["myapex"],
2565 min_sdk_version: "S",
2566 }
2567 cc_library {
2568 name: "libbar",
2569 stubs: {
2570 symbol_file: "libbar.map.txt",
2571 versions: ["30", "S", "T"],
2572 },
2573 }
2574 `, withSAsActiveCodeNames)
2575
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002576 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002577 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2578 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002579 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002580}
2581
Jiyong Park7c2ee712018-12-07 00:42:25 +09002582func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002583 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002584 apex {
2585 name: "myapex",
2586 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002587 native_shared_libs: ["mylib"],
2588 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002589 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002590 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002591 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002592 }
2593
2594 apex_key {
2595 name: "myapex.key",
2596 public_key: "testkey.avbpubkey",
2597 private_key: "testkey.pem",
2598 }
2599
2600 prebuilt_etc {
2601 name: "myetc",
2602 src: "myprebuilt",
2603 sub_dir: "foo/bar",
2604 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002605
2606 cc_library {
2607 name: "mylib",
2608 srcs: ["mylib.cpp"],
2609 relative_install_path: "foo/bar",
2610 system_shared_libs: [],
2611 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002612 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002613 }
2614
2615 cc_binary {
2616 name: "mybin",
2617 srcs: ["mylib.cpp"],
2618 relative_install_path: "foo/bar",
2619 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002620 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002621 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002622 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002623 `)
2624
Sundong Ahnabb64432019-10-22 13:58:29 +09002625 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002626 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002627
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002628 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002629 ensureContains(t, cmd, "/etc ")
2630 ensureContains(t, cmd, "/etc/foo ")
2631 ensureContains(t, cmd, "/etc/foo/bar ")
2632 ensureContains(t, cmd, "/lib64 ")
2633 ensureContains(t, cmd, "/lib64/foo ")
2634 ensureContains(t, cmd, "/lib64/foo/bar ")
2635 ensureContains(t, cmd, "/lib ")
2636 ensureContains(t, cmd, "/lib/foo ")
2637 ensureContains(t, cmd, "/lib/foo/bar ")
2638 ensureContains(t, cmd, "/bin ")
2639 ensureContains(t, cmd, "/bin/foo ")
2640 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002641}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002642
Jooyung Han35155c42020-02-06 17:33:20 +09002643func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002644 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002645 apex {
2646 name: "myapex",
2647 key: "myapex.key",
2648 multilib: {
2649 both: {
2650 native_shared_libs: ["mylib"],
2651 binaries: ["mybin"],
2652 },
2653 },
2654 compile_multilib: "both",
2655 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002656 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002657 }
2658
2659 apex_key {
2660 name: "myapex.key",
2661 public_key: "testkey.avbpubkey",
2662 private_key: "testkey.pem",
2663 }
2664
2665 cc_library {
2666 name: "mylib",
2667 relative_install_path: "foo/bar",
2668 system_shared_libs: [],
2669 stl: "none",
2670 apex_available: [ "myapex" ],
2671 native_bridge_supported: true,
2672 }
2673
2674 cc_binary {
2675 name: "mybin",
2676 relative_install_path: "foo/bar",
2677 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002678 stl: "none",
2679 apex_available: [ "myapex" ],
2680 native_bridge_supported: true,
2681 compile_multilib: "both", // default is "first" for binary
2682 multilib: {
2683 lib64: {
2684 suffix: "64",
2685 },
2686 },
2687 }
2688 `, withNativeBridgeEnabled)
2689 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2690 "bin/foo/bar/mybin",
2691 "bin/foo/bar/mybin64",
2692 "bin/arm/foo/bar/mybin",
2693 "bin/arm64/foo/bar/mybin64",
2694 "lib/foo/bar/mylib.so",
2695 "lib/arm/foo/bar/mylib.so",
2696 "lib64/foo/bar/mylib.so",
2697 "lib64/arm64/foo/bar/mylib.so",
2698 })
2699}
2700
Jooyung Han85d61762020-06-24 23:50:26 +09002701func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002702 result := android.GroupFixturePreparers(
2703 prepareForApexTest,
2704 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2705 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002706 apex {
2707 name: "myapex",
2708 key: "myapex.key",
2709 binaries: ["mybin"],
2710 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002711 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002712 }
2713 apex_key {
2714 name: "myapex.key",
2715 public_key: "testkey.avbpubkey",
2716 private_key: "testkey.pem",
2717 }
2718 cc_binary {
2719 name: "mybin",
2720 vendor: true,
2721 shared_libs: ["libfoo"],
2722 }
2723 cc_library {
2724 name: "libfoo",
2725 proprietary: true,
2726 }
2727 `)
2728
Colin Crossc68db4b2021-11-11 18:59:15 -08002729 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002730 "bin/mybin",
2731 "lib64/libfoo.so",
2732 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2733 "lib64/libc++.so",
2734 })
2735
Colin Crossc68db4b2021-11-11 18:59:15 -08002736 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2737 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002738 name := apexBundle.BaseModuleName()
2739 prefix := "TARGET_"
2740 var builder strings.Builder
2741 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002742 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002743 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002744 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002745
Colin Crossc68db4b2021-11-11 18:59:15 -08002746 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002747 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2748 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002749}
2750
Jooyung Hanc5a96762022-02-04 11:54:50 +09002751func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2752 testApexError(t, `Trying to include a VNDK library`, `
2753 apex {
2754 name: "myapex",
2755 key: "myapex.key",
2756 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2757 vendor: true,
2758 use_vndk_as_stable: true,
2759 updatable: false,
2760 }
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }`)
2766}
2767
Jooyung Handf78e212020-07-22 15:54:47 +09002768func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002769 // myapex myapex2
2770 // | |
2771 // mybin ------. mybin2
2772 // \ \ / |
2773 // (stable) .---\--------` |
2774 // \ / \ |
2775 // \ / \ /
2776 // libvndk libvendor
2777 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002778 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002779 apex {
2780 name: "myapex",
2781 key: "myapex.key",
2782 binaries: ["mybin"],
2783 vendor: true,
2784 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002785 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002786 }
2787 apex_key {
2788 name: "myapex.key",
2789 public_key: "testkey.avbpubkey",
2790 private_key: "testkey.pem",
2791 }
2792 cc_binary {
2793 name: "mybin",
2794 vendor: true,
2795 shared_libs: ["libvndk", "libvendor"],
2796 }
2797 cc_library {
2798 name: "libvndk",
2799 vndk: {
2800 enabled: true,
2801 },
2802 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002803 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002804 }
2805 cc_library {
2806 name: "libvendor",
2807 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002808 stl: "none",
2809 }
2810 apex {
2811 name: "myapex2",
2812 key: "myapex.key",
2813 binaries: ["mybin2"],
2814 vendor: true,
2815 use_vndk_as_stable: false,
2816 updatable: false,
2817 }
2818 cc_binary {
2819 name: "mybin2",
2820 vendor: true,
2821 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002822 }
2823 `)
2824
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002825 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002826
Jooyung Han91f92032022-02-04 12:36:33 +09002827 for _, tc := range []struct {
2828 name string
2829 apexName string
2830 moduleName string
2831 moduleVariant string
2832 libs []string
2833 contents []string
2834 requireVndkNamespace bool
2835 }{
2836 {
2837 name: "use_vndk_as_stable",
2838 apexName: "myapex",
2839 moduleName: "mybin",
2840 moduleVariant: vendorVariant + "_apex10000",
2841 libs: []string{
2842 // should link with vendor variants of VNDK libs(libvndk/libc++)
2843 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2844 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2845 // unstable Vendor libs as APEX variant
2846 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2847 },
2848 contents: []string{
2849 "bin/mybin",
2850 "lib64/libvendor.so",
2851 // VNDK libs (libvndk/libc++) are not included
2852 },
2853 requireVndkNamespace: true,
2854 },
2855 {
2856 name: "!use_vndk_as_stable",
2857 apexName: "myapex2",
2858 moduleName: "mybin2",
2859 moduleVariant: vendorVariant + "_myapex2",
2860 libs: []string{
2861 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2862 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2863 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2864 // unstable vendor libs have "merged" APEX variants
2865 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2866 },
2867 contents: []string{
2868 "bin/mybin2",
2869 "lib64/libvendor.so",
2870 // VNDK libs are included as well
2871 "lib64/libvndk.so",
2872 "lib64/libc++.so",
2873 },
2874 requireVndkNamespace: false,
2875 },
2876 } {
2877 t.Run(tc.name, func(t *testing.T) {
2878 // Check linked libs
2879 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2880 libs := names(ldRule.Args["libFlags"])
2881 for _, lib := range tc.libs {
2882 ensureListContains(t, libs, lib)
2883 }
2884 // Check apex contents
2885 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002886
Jooyung Han91f92032022-02-04 12:36:33 +09002887 // Check "requireNativeLibs"
2888 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2889 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2890 if tc.requireVndkNamespace {
2891 ensureListContains(t, requireNativeLibs, ":vndk")
2892 } else {
2893 ensureListNotContains(t, requireNativeLibs, ":vndk")
2894 }
2895 })
2896 }
Jooyung Handf78e212020-07-22 15:54:47 +09002897}
2898
Justin Yun13decfb2021-03-08 19:25:55 +09002899func TestProductVariant(t *testing.T) {
2900 ctx := testApex(t, `
2901 apex {
2902 name: "myapex",
2903 key: "myapex.key",
2904 updatable: false,
2905 product_specific: true,
2906 binaries: ["foo"],
2907 }
2908
2909 apex_key {
2910 name: "myapex.key",
2911 public_key: "testkey.avbpubkey",
2912 private_key: "testkey.pem",
2913 }
2914
2915 cc_binary {
2916 name: "foo",
2917 product_available: true,
2918 apex_available: ["myapex"],
2919 srcs: ["foo.cpp"],
2920 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002921 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2922 variables.ProductVndkVersion = proptools.StringPtr("current")
2923 }),
2924 )
Justin Yun13decfb2021-03-08 19:25:55 +09002925
2926 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002927 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002928 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2929 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2930 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2931 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2932}
2933
Jooyung Han8e5685d2020-09-21 11:02:57 +09002934func TestApex_withPrebuiltFirmware(t *testing.T) {
2935 testCases := []struct {
2936 name string
2937 additionalProp string
2938 }{
2939 {"system apex with prebuilt_firmware", ""},
2940 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2941 }
2942 for _, tc := range testCases {
2943 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002944 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002945 apex {
2946 name: "myapex",
2947 key: "myapex.key",
2948 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002949 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002950 `+tc.additionalProp+`
2951 }
2952 apex_key {
2953 name: "myapex.key",
2954 public_key: "testkey.avbpubkey",
2955 private_key: "testkey.pem",
2956 }
2957 prebuilt_firmware {
2958 name: "myfirmware",
2959 src: "myfirmware.bin",
2960 filename_from_src: true,
2961 `+tc.additionalProp+`
2962 }
2963 `)
2964 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2965 "etc/firmware/myfirmware.bin",
2966 })
2967 })
2968 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002969}
2970
Jooyung Hanefb184e2020-06-25 17:14:25 +09002971func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002972 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002973 apex {
2974 name: "myapex",
2975 key: "myapex.key",
2976 vendor: true,
2977 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002978 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002979 }
2980
2981 apex_key {
2982 name: "myapex.key",
2983 public_key: "testkey.avbpubkey",
2984 private_key: "testkey.pem",
2985 }
2986
2987 cc_library {
2988 name: "mylib",
2989 vendor_available: true,
2990 }
2991 `)
2992
2993 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002994 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002995 name := apexBundle.BaseModuleName()
2996 prefix := "TARGET_"
2997 var builder strings.Builder
2998 data.Custom(&builder, name, prefix, "", data)
2999 androidMk := builder.String()
3000 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
3001}
3002
Jooyung Han2ed99d02020-06-24 23:26:26 +09003003func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003004 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003005 apex {
3006 name: "myapex",
3007 key: "myapex.key",
3008 vintf_fragments: ["fragment.xml"],
3009 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003010 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003011 }
3012 apex_key {
3013 name: "myapex.key",
3014 public_key: "testkey.avbpubkey",
3015 private_key: "testkey.pem",
3016 }
3017 cc_binary {
3018 name: "mybin",
3019 }
3020 `)
3021
3022 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003023 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003024 name := apexBundle.BaseModuleName()
3025 prefix := "TARGET_"
3026 var builder strings.Builder
3027 data.Custom(&builder, name, prefix, "", data)
3028 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003029 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003030 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003031}
3032
Jiyong Park16e91a02018-12-20 18:18:08 +09003033func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003034 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003035 apex {
3036 name: "myapex",
3037 key: "myapex.key",
3038 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003039 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003040 }
3041
3042 apex_key {
3043 name: "myapex.key",
3044 public_key: "testkey.avbpubkey",
3045 private_key: "testkey.pem",
3046 }
3047
3048 cc_library {
3049 name: "mylib",
3050 srcs: ["mylib.cpp"],
3051 system_shared_libs: [],
3052 stl: "none",
3053 stubs: {
3054 versions: ["1", "2", "3"],
3055 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003056 apex_available: [
3057 "//apex_available:platform",
3058 "myapex",
3059 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003060 }
3061
3062 cc_binary {
3063 name: "not_in_apex",
3064 srcs: ["mylib.cpp"],
3065 static_libs: ["mylib"],
3066 static_executable: true,
3067 system_shared_libs: [],
3068 stl: "none",
3069 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003070 `)
3071
Colin Cross7113d202019-11-20 16:39:12 -08003072 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003073
3074 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003075 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003076}
Jiyong Park9335a262018-12-24 11:31:58 +09003077
3078func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003079 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003080 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003081 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003082 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003083 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003084 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003085 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003086 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003087 }
3088
3089 cc_library {
3090 name: "mylib",
3091 srcs: ["mylib.cpp"],
3092 system_shared_libs: [],
3093 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003094 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003095 }
3096
3097 apex_key {
3098 name: "myapex.key",
3099 public_key: "testkey.avbpubkey",
3100 private_key: "testkey.pem",
3101 }
3102
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003103 android_app_certificate {
3104 name: "myapex.certificate",
3105 certificate: "testkey",
3106 }
3107
3108 android_app_certificate {
3109 name: "myapex.certificate.override",
3110 certificate: "testkey.override",
3111 }
3112
Jiyong Park9335a262018-12-24 11:31:58 +09003113 `)
3114
3115 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003116 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003117
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003118 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3119 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003120 "vendor/foo/devkeys/testkey.avbpubkey")
3121 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003122 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3123 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003124 "vendor/foo/devkeys/testkey.pem")
3125 }
3126
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003127 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003128 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003129 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003130 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003131 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003132 }
3133}
Jiyong Park58e364a2019-01-19 19:24:06 +09003134
Jooyung Hanf121a652019-12-17 14:30:11 +09003135func TestCertificate(t *testing.T) {
3136 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003137 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003138 apex {
3139 name: "myapex",
3140 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003141 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003142 }
3143 apex_key {
3144 name: "myapex.key",
3145 public_key: "testkey.avbpubkey",
3146 private_key: "testkey.pem",
3147 }`)
3148 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3149 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3150 if actual := rule.Args["certificates"]; actual != expected {
3151 t.Errorf("certificates should be %q, not %q", expected, actual)
3152 }
3153 })
3154 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003155 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003156 apex {
3157 name: "myapex_keytest",
3158 key: "myapex.key",
3159 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003160 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003161 }
3162 apex_key {
3163 name: "myapex.key",
3164 public_key: "testkey.avbpubkey",
3165 private_key: "testkey.pem",
3166 }
3167 android_app_certificate {
3168 name: "myapex.certificate.override",
3169 certificate: "testkey.override",
3170 }`)
3171 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3172 expected := "testkey.override.x509.pem testkey.override.pk8"
3173 if actual := rule.Args["certificates"]; actual != expected {
3174 t.Errorf("certificates should be %q, not %q", expected, actual)
3175 }
3176 })
3177 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003178 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003179 apex {
3180 name: "myapex",
3181 key: "myapex.key",
3182 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003183 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003184 }
3185 apex_key {
3186 name: "myapex.key",
3187 public_key: "testkey.avbpubkey",
3188 private_key: "testkey.pem",
3189 }
3190 android_app_certificate {
3191 name: "myapex.certificate",
3192 certificate: "testkey",
3193 }`)
3194 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3195 expected := "testkey.x509.pem testkey.pk8"
3196 if actual := rule.Args["certificates"]; actual != expected {
3197 t.Errorf("certificates should be %q, not %q", expected, actual)
3198 }
3199 })
3200 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003201 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003202 apex {
3203 name: "myapex_keytest",
3204 key: "myapex.key",
3205 file_contexts: ":myapex-file_contexts",
3206 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003207 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003208 }
3209 apex_key {
3210 name: "myapex.key",
3211 public_key: "testkey.avbpubkey",
3212 private_key: "testkey.pem",
3213 }
3214 android_app_certificate {
3215 name: "myapex.certificate.override",
3216 certificate: "testkey.override",
3217 }`)
3218 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3219 expected := "testkey.override.x509.pem testkey.override.pk8"
3220 if actual := rule.Args["certificates"]; actual != expected {
3221 t.Errorf("certificates should be %q, not %q", expected, actual)
3222 }
3223 })
3224 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003225 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003226 apex {
3227 name: "myapex",
3228 key: "myapex.key",
3229 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003230 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003231 }
3232 apex_key {
3233 name: "myapex.key",
3234 public_key: "testkey.avbpubkey",
3235 private_key: "testkey.pem",
3236 }`)
3237 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3238 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3239 if actual := rule.Args["certificates"]; actual != expected {
3240 t.Errorf("certificates should be %q, not %q", expected, actual)
3241 }
3242 })
3243 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003244 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003245 apex {
3246 name: "myapex_keytest",
3247 key: "myapex.key",
3248 file_contexts: ":myapex-file_contexts",
3249 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003250 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003251 }
3252 apex_key {
3253 name: "myapex.key",
3254 public_key: "testkey.avbpubkey",
3255 private_key: "testkey.pem",
3256 }
3257 android_app_certificate {
3258 name: "myapex.certificate.override",
3259 certificate: "testkey.override",
3260 }`)
3261 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3262 expected := "testkey.override.x509.pem testkey.override.pk8"
3263 if actual := rule.Args["certificates"]; actual != expected {
3264 t.Errorf("certificates should be %q, not %q", expected, actual)
3265 }
3266 })
3267}
3268
Jiyong Park58e364a2019-01-19 19:24:06 +09003269func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003270 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003271 apex {
3272 name: "myapex",
3273 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003274 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003275 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003276 }
3277
3278 apex {
3279 name: "otherapex",
3280 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003281 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003282 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003283 }
3284
3285 apex_key {
3286 name: "myapex.key",
3287 public_key: "testkey.avbpubkey",
3288 private_key: "testkey.pem",
3289 }
3290
3291 cc_library {
3292 name: "mylib",
3293 srcs: ["mylib.cpp"],
3294 system_shared_libs: [],
3295 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003296 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003297 "myapex",
3298 "otherapex",
3299 ],
Jooyung Han24282772020-03-21 23:20:55 +09003300 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003301 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003302 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003303 cc_library {
3304 name: "mylib2",
3305 srcs: ["mylib.cpp"],
3306 system_shared_libs: [],
3307 stl: "none",
3308 apex_available: [
3309 "myapex",
3310 "otherapex",
3311 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003312 static_libs: ["mylib3"],
3313 recovery_available: true,
3314 min_sdk_version: "29",
3315 }
3316 cc_library {
3317 name: "mylib3",
3318 srcs: ["mylib.cpp"],
3319 system_shared_libs: [],
3320 stl: "none",
3321 apex_available: [
3322 "myapex",
3323 "otherapex",
3324 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003325 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003326 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003327 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003328 `)
3329
Jooyung Hanc87a0592020-03-02 17:44:33 +09003330 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003331 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003332 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003333 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003334
Jooyung Hanccce2f22020-03-07 03:45:53 +09003335 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003336 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003337 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003338 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003339
Jooyung Hanccce2f22020-03-07 03:45:53 +09003340 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003341 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003342 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003343 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jiyong Park58e364a2019-01-19 19:24:06 +09003344
Colin Crossaede88c2020-08-11 12:17:01 -07003345 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3346 // each variant defines additional macros to distinguish which apex variant it is built for
3347
3348 // non-APEX variant does not have __ANDROID_APEX__ defined
3349 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3350 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3351
Dan Albertb19953d2020-11-17 15:29:36 -08003352 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003353 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3354 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003355 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003356
Jooyung Hanc87a0592020-03-02 17:44:33 +09003357 // non-APEX variant does not have __ANDROID_APEX__ defined
3358 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3359 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3360
Dan Albertb19953d2020-11-17 15:29:36 -08003361 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003362 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003363 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003364 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003365}
Jiyong Park7e636d02019-01-28 16:16:54 +09003366
3367func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003368 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003369 apex {
3370 name: "myapex",
3371 key: "myapex.key",
3372 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003373 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003374 }
3375
3376 apex_key {
3377 name: "myapex.key",
3378 public_key: "testkey.avbpubkey",
3379 private_key: "testkey.pem",
3380 }
3381
3382 cc_library_headers {
3383 name: "mylib_headers",
3384 export_include_dirs: ["my_include"],
3385 system_shared_libs: [],
3386 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003387 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003388 }
3389
3390 cc_library {
3391 name: "mylib",
3392 srcs: ["mylib.cpp"],
3393 system_shared_libs: [],
3394 stl: "none",
3395 header_libs: ["mylib_headers"],
3396 export_header_lib_headers: ["mylib_headers"],
3397 stubs: {
3398 versions: ["1", "2", "3"],
3399 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003400 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003401 }
3402
3403 cc_library {
3404 name: "otherlib",
3405 srcs: ["mylib.cpp"],
3406 system_shared_libs: [],
3407 stl: "none",
3408 shared_libs: ["mylib"],
3409 }
3410 `)
3411
Colin Cross7113d202019-11-20 16:39:12 -08003412 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003413
3414 // Ensure that the include path of the header lib is exported to 'otherlib'
3415 ensureContains(t, cFlags, "-Imy_include")
3416}
Alex Light9670d332019-01-29 18:07:33 -08003417
Jiyong Park7cd10e32020-01-14 09:22:18 +09003418type fileInApex struct {
3419 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003420 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003421 isLink bool
3422}
3423
Jooyung Hana57af4a2020-01-23 05:36:59 +00003424func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003425 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003426 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003427 copyCmds := apexRule.Args["copy_commands"]
3428 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003429 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003430 for _, cmd := range strings.Split(copyCmds, "&&") {
3431 cmd = strings.TrimSpace(cmd)
3432 if cmd == "" {
3433 continue
3434 }
3435 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003436 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003437 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003438 switch terms[0] {
3439 case "mkdir":
3440 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003441 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003442 t.Fatal("copyCmds contains invalid cp command", cmd)
3443 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003444 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003445 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003446 isLink = false
3447 case "ln":
3448 if len(terms) != 3 && len(terms) != 4 {
3449 // ln LINK TARGET or ln -s LINK TARGET
3450 t.Fatal("copyCmds contains invalid ln command", cmd)
3451 }
3452 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003453 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003454 isLink = true
3455 default:
3456 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3457 }
3458 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003459 index := strings.Index(dst, imageApexDir)
3460 if index == -1 {
3461 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3462 }
3463 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003464 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003465 }
3466 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003467 return ret
3468}
3469
Jooyung Hana57af4a2020-01-23 05:36:59 +00003470func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3471 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003472 var failed bool
3473 var surplus []string
3474 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003475 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003476 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003477 for _, expected := range files {
3478 if matched, _ := path.Match(expected, file.path); matched {
3479 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003480 mactchFound = true
3481 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003482 }
3483 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003484 if !mactchFound {
3485 surplus = append(surplus, file.path)
3486 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003487 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003488
Jooyung Han31c470b2019-10-18 16:26:59 +09003489 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003490 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003491 t.Log("surplus files", surplus)
3492 failed = true
3493 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003494
3495 if len(files) > len(filesMatched) {
3496 var missing []string
3497 for _, expected := range files {
3498 if !filesMatched[expected] {
3499 missing = append(missing, expected)
3500 }
3501 }
3502 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003503 t.Log("missing files", missing)
3504 failed = true
3505 }
3506 if failed {
3507 t.Fail()
3508 }
3509}
3510
Jooyung Han344d5432019-08-23 11:17:39 +09003511func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003512 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003513 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003514 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003515 "etc/llndk.libraries.29.txt",
3516 "etc/vndkcore.libraries.29.txt",
3517 "etc/vndksp.libraries.29.txt",
3518 "etc/vndkprivate.libraries.29.txt",
3519 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003520 }
3521 testCases := []struct {
3522 vndkVersion string
3523 expectedFiles []string
3524 }{
3525 {
3526 vndkVersion: "current",
3527 expectedFiles: append(commonFiles,
3528 "lib/libvndk.so",
3529 "lib/libvndksp.so",
3530 "lib64/libvndk.so",
3531 "lib64/libvndksp.so"),
3532 },
3533 {
3534 vndkVersion: "",
3535 expectedFiles: append(commonFiles,
3536 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3537 "lib/libvndksp.so",
3538 "lib64/libvndksp.so"),
3539 },
3540 }
3541 for _, tc := range testCases {
3542 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3543 ctx := testApex(t, `
3544 apex_vndk {
3545 name: "com.android.vndk.current",
3546 key: "com.android.vndk.current.key",
3547 updatable: false,
3548 }
3549
3550 apex_key {
3551 name: "com.android.vndk.current.key",
3552 public_key: "testkey.avbpubkey",
3553 private_key: "testkey.pem",
3554 }
3555
3556 cc_library {
3557 name: "libvndk",
3558 srcs: ["mylib.cpp"],
3559 vendor_available: true,
3560 product_available: true,
3561 vndk: {
3562 enabled: true,
3563 },
3564 system_shared_libs: [],
3565 stl: "none",
3566 apex_available: [ "com.android.vndk.current" ],
3567 }
3568
3569 cc_library {
3570 name: "libvndksp",
3571 srcs: ["mylib.cpp"],
3572 vendor_available: true,
3573 product_available: true,
3574 vndk: {
3575 enabled: true,
3576 support_system_process: true,
3577 },
3578 system_shared_libs: [],
3579 stl: "none",
3580 apex_available: [ "com.android.vndk.current" ],
3581 }
3582
3583 // VNDK-Ext should not cause any problems
3584
3585 cc_library {
3586 name: "libvndk.ext",
3587 srcs: ["mylib2.cpp"],
3588 vendor: true,
3589 vndk: {
3590 enabled: true,
3591 extends: "libvndk",
3592 },
3593 system_shared_libs: [],
3594 stl: "none",
3595 }
3596
3597 cc_library {
3598 name: "libvndksp.ext",
3599 srcs: ["mylib2.cpp"],
3600 vendor: true,
3601 vndk: {
3602 enabled: true,
3603 support_system_process: true,
3604 extends: "libvndksp",
3605 },
3606 system_shared_libs: [],
3607 stl: "none",
3608 }
3609 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3610 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3611 }))
3612 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3613 })
3614 }
Jooyung Han344d5432019-08-23 11:17:39 +09003615}
3616
3617func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003618 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003619 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003620 name: "com.android.vndk.current",
3621 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003622 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003623 }
3624
3625 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003626 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003627 public_key: "testkey.avbpubkey",
3628 private_key: "testkey.pem",
3629 }
3630
3631 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003632 name: "libvndk",
3633 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003634 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003635 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003636 vndk: {
3637 enabled: true,
3638 },
3639 system_shared_libs: [],
3640 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003641 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003642 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003643
3644 cc_prebuilt_library_shared {
3645 name: "libvndk.arm",
3646 srcs: ["libvndk.arm.so"],
3647 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003648 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003649 vndk: {
3650 enabled: true,
3651 },
3652 enabled: false,
3653 arch: {
3654 arm: {
3655 enabled: true,
3656 },
3657 },
3658 system_shared_libs: [],
3659 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003660 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003661 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003662 `+vndkLibrariesTxtFiles("current"),
3663 withFiles(map[string][]byte{
3664 "libvndk.so": nil,
3665 "libvndk.arm.so": nil,
3666 }))
Colin Cross2807f002021-03-02 10:15:29 -08003667 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003668 "lib/libvndk.so",
3669 "lib/libvndk.arm.so",
3670 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003671 "lib/libc++.so",
3672 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003673 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003674 })
Jooyung Han344d5432019-08-23 11:17:39 +09003675}
3676
Jooyung Han39edb6c2019-11-06 16:53:07 +09003677func vndkLibrariesTxtFiles(vers ...string) (result string) {
3678 for _, v := range vers {
3679 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003680 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003681 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003682 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003683 name: "` + txt + `.libraries.txt",
3684 }
3685 `
3686 }
3687 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003688 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003689 result += `
3690 prebuilt_etc {
3691 name: "` + txt + `.libraries.` + v + `.txt",
3692 src: "dummy.txt",
3693 }
3694 `
3695 }
3696 }
3697 }
3698 return
3699}
3700
Jooyung Han344d5432019-08-23 11:17:39 +09003701func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003702 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003703 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003704 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003705 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003706 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003707 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003708 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003709 }
3710
3711 apex_key {
3712 name: "myapex.key",
3713 public_key: "testkey.avbpubkey",
3714 private_key: "testkey.pem",
3715 }
3716
Jooyung Han31c470b2019-10-18 16:26:59 +09003717 vndk_prebuilt_shared {
3718 name: "libvndk27",
3719 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003720 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003721 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003722 vndk: {
3723 enabled: true,
3724 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003725 target_arch: "arm64",
3726 arch: {
3727 arm: {
3728 srcs: ["libvndk27_arm.so"],
3729 },
3730 arm64: {
3731 srcs: ["libvndk27_arm64.so"],
3732 },
3733 },
Colin Cross2807f002021-03-02 10:15:29 -08003734 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003735 }
3736
3737 vndk_prebuilt_shared {
3738 name: "libvndk27",
3739 version: "27",
3740 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003741 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003742 vndk: {
3743 enabled: true,
3744 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003745 target_arch: "x86_64",
3746 arch: {
3747 x86: {
3748 srcs: ["libvndk27_x86.so"],
3749 },
3750 x86_64: {
3751 srcs: ["libvndk27_x86_64.so"],
3752 },
3753 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003754 }
3755 `+vndkLibrariesTxtFiles("27"),
3756 withFiles(map[string][]byte{
3757 "libvndk27_arm.so": nil,
3758 "libvndk27_arm64.so": nil,
3759 "libvndk27_x86.so": nil,
3760 "libvndk27_x86_64.so": nil,
3761 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003762
Colin Cross2807f002021-03-02 10:15:29 -08003763 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003764 "lib/libvndk27_arm.so",
3765 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003766 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003767 })
Jooyung Han344d5432019-08-23 11:17:39 +09003768}
3769
Jooyung Han90eee022019-10-01 20:02:42 +09003770func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003771 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003772 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003773 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003774 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003775 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003776 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003777 }
3778 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003779 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003780 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003781 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003782 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003783 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003784 }
3785 apex_key {
3786 name: "myapex.key",
3787 public_key: "testkey.avbpubkey",
3788 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003789 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003790
3791 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003792 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003793 actual := proptools.String(bundle.properties.Apex_name)
3794 if !reflect.DeepEqual(actual, expected) {
3795 t.Errorf("Got '%v', expected '%v'", actual, expected)
3796 }
3797 }
3798
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003799 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003800 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003801}
3802
Jooyung Han344d5432019-08-23 11:17:39 +09003803func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003804 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003805 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003806 name: "com.android.vndk.current",
3807 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003808 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003809 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003810 }
3811
3812 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003813 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003814 public_key: "testkey.avbpubkey",
3815 private_key: "testkey.pem",
3816 }
3817
3818 cc_library {
3819 name: "libvndk",
3820 srcs: ["mylib.cpp"],
3821 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003822 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003823 native_bridge_supported: true,
3824 host_supported: true,
3825 vndk: {
3826 enabled: true,
3827 },
3828 system_shared_libs: [],
3829 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003830 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003831 }
Colin Cross2807f002021-03-02 10:15:29 -08003832 `+vndkLibrariesTxtFiles("current"),
3833 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003834
Colin Cross2807f002021-03-02 10:15:29 -08003835 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003836 "lib/libvndk.so",
3837 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003838 "lib/libc++.so",
3839 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003840 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003841 })
Jooyung Han344d5432019-08-23 11:17:39 +09003842}
3843
3844func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003845 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
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",
Jooyung Han344d5432019-08-23 11:17:39 +09003850 native_bridge_supported: true,
3851 }
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",
3871 }
3872 `)
3873}
3874
Jooyung Han31c470b2019-10-18 16:26:59 +09003875func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003876 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003877 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003878 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003879 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003880 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003881 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003882 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003883 }
3884
3885 apex_key {
3886 name: "myapex.key",
3887 public_key: "testkey.avbpubkey",
3888 private_key: "testkey.pem",
3889 }
3890
3891 vndk_prebuilt_shared {
3892 name: "libvndk27",
3893 version: "27",
3894 target_arch: "arm",
3895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003896 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003897 vndk: {
3898 enabled: true,
3899 },
3900 arch: {
3901 arm: {
3902 srcs: ["libvndk27.so"],
3903 }
3904 },
3905 }
3906
3907 vndk_prebuilt_shared {
3908 name: "libvndk27",
3909 version: "27",
3910 target_arch: "arm",
3911 binder32bit: true,
3912 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003913 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003914 vndk: {
3915 enabled: true,
3916 },
3917 arch: {
3918 arm: {
3919 srcs: ["libvndk27binder32.so"],
3920 }
3921 },
Colin Cross2807f002021-03-02 10:15:29 -08003922 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003923 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003924 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003925 withFiles(map[string][]byte{
3926 "libvndk27.so": nil,
3927 "libvndk27binder32.so": nil,
3928 }),
3929 withBinder32bit,
3930 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003931 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003932 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3933 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003934 },
3935 }),
3936 )
3937
Colin Cross2807f002021-03-02 10:15:29 -08003938 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003939 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003940 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003941 })
3942}
3943
Jooyung Han45a96772020-06-15 14:59:42 +09003944func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003945 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003946 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003947 name: "com.android.vndk.current",
3948 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003949 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003950 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003951 }
3952
3953 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003954 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003955 public_key: "testkey.avbpubkey",
3956 private_key: "testkey.pem",
3957 }
3958
3959 cc_library {
3960 name: "libz",
3961 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003962 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003963 vndk: {
3964 enabled: true,
3965 },
3966 stubs: {
3967 symbol_file: "libz.map.txt",
3968 versions: ["30"],
3969 }
3970 }
3971 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3972 "libz.map.txt": nil,
3973 }))
3974
Colin Cross2807f002021-03-02 10:15:29 -08003975 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003976 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3977 ensureListEmpty(t, provideNativeLibs)
3978}
3979
Jooyung Hane1633032019-08-01 17:41:43 +09003980func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003981 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003982 apex {
3983 name: "myapex_nodep",
3984 key: "myapex.key",
3985 native_shared_libs: ["lib_nodep"],
3986 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003987 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003988 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003989 }
3990
3991 apex {
3992 name: "myapex_dep",
3993 key: "myapex.key",
3994 native_shared_libs: ["lib_dep"],
3995 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003996 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003997 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003998 }
3999
4000 apex {
4001 name: "myapex_provider",
4002 key: "myapex.key",
4003 native_shared_libs: ["libfoo"],
4004 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004005 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004006 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004007 }
4008
4009 apex {
4010 name: "myapex_selfcontained",
4011 key: "myapex.key",
4012 native_shared_libs: ["lib_dep", "libfoo"],
4013 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004014 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004015 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004016 }
4017
4018 apex_key {
4019 name: "myapex.key",
4020 public_key: "testkey.avbpubkey",
4021 private_key: "testkey.pem",
4022 }
4023
4024 cc_library {
4025 name: "lib_nodep",
4026 srcs: ["mylib.cpp"],
4027 system_shared_libs: [],
4028 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004029 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004030 }
4031
4032 cc_library {
4033 name: "lib_dep",
4034 srcs: ["mylib.cpp"],
4035 shared_libs: ["libfoo"],
4036 system_shared_libs: [],
4037 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004038 apex_available: [
4039 "myapex_dep",
4040 "myapex_provider",
4041 "myapex_selfcontained",
4042 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004043 }
4044
4045 cc_library {
4046 name: "libfoo",
4047 srcs: ["mytest.cpp"],
4048 stubs: {
4049 versions: ["1"],
4050 },
4051 system_shared_libs: [],
4052 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004053 apex_available: [
4054 "myapex_provider",
4055 "myapex_selfcontained",
4056 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004057 }
4058 `)
4059
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004060 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004061 var provideNativeLibs, requireNativeLibs []string
4062
Sundong Ahnabb64432019-10-22 13:58:29 +09004063 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004064 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4065 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004066 ensureListEmpty(t, provideNativeLibs)
4067 ensureListEmpty(t, requireNativeLibs)
4068
Sundong Ahnabb64432019-10-22 13:58:29 +09004069 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004070 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4071 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004072 ensureListEmpty(t, provideNativeLibs)
4073 ensureListContains(t, requireNativeLibs, "libfoo.so")
4074
Sundong Ahnabb64432019-10-22 13:58:29 +09004075 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004076 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4077 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004078 ensureListContains(t, provideNativeLibs, "libfoo.so")
4079 ensureListEmpty(t, requireNativeLibs)
4080
Sundong Ahnabb64432019-10-22 13:58:29 +09004081 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004082 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4083 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004084 ensureListContains(t, provideNativeLibs, "libfoo.so")
4085 ensureListEmpty(t, requireNativeLibs)
4086}
4087
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004088func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004089 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004090 apex {
4091 name: "myapex",
4092 key: "myapex.key",
4093 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004094 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004095 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004096 }
4097
4098 apex_key {
4099 name: "myapex.key",
4100 public_key: "testkey.avbpubkey",
4101 private_key: "testkey.pem",
4102 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004103
4104 cc_library {
4105 name: "mylib",
4106 srcs: ["mylib.cpp"],
4107 system_shared_libs: [],
4108 stl: "none",
4109 apex_available: [
4110 "//apex_available:platform",
4111 "myapex",
4112 ],
4113 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004114 `)
4115
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004116 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004117 apexManifestRule := module.Rule("apexManifestRule")
4118 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4119 apexRule := module.Rule("apexRule")
4120 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004121
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004122 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004123 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004124 name := apexBundle.BaseModuleName()
4125 prefix := "TARGET_"
4126 var builder strings.Builder
4127 data.Custom(&builder, name, prefix, "", data)
4128 androidMk := builder.String()
4129 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4130 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004131}
4132
Alex Light0851b882019-02-07 13:20:53 -08004133func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004134 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004135 apex {
4136 name: "myapex",
4137 key: "myapex.key",
4138 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004139 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004140 }
4141
4142 apex_key {
4143 name: "myapex.key",
4144 public_key: "testkey.avbpubkey",
4145 private_key: "testkey.pem",
4146 }
4147
4148 cc_library {
4149 name: "mylib_common",
4150 srcs: ["mylib.cpp"],
4151 system_shared_libs: [],
4152 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004153 apex_available: [
4154 "//apex_available:platform",
4155 "myapex",
4156 ],
Alex Light0851b882019-02-07 13:20:53 -08004157 }
4158 `)
4159
Sundong Ahnabb64432019-10-22 13:58:29 +09004160 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004161 apexRule := module.Rule("apexRule")
4162 copyCmds := apexRule.Args["copy_commands"]
4163
4164 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4165 t.Log("Apex was a test apex!")
4166 t.Fail()
4167 }
4168 // Ensure that main rule creates an output
4169 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4170
4171 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004172 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004173
4174 // Ensure that both direct and indirect deps are copied into apex
4175 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4176
Colin Cross7113d202019-11-20 16:39:12 -08004177 // Ensure that the platform variant ends with _shared
4178 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004179
Colin Cross56a83212020-09-15 18:30:11 -07004180 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004181 t.Log("Found mylib_common not in any apex!")
4182 t.Fail()
4183 }
4184}
4185
4186func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004187 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004188 apex_test {
4189 name: "myapex",
4190 key: "myapex.key",
4191 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004192 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004193 }
4194
4195 apex_key {
4196 name: "myapex.key",
4197 public_key: "testkey.avbpubkey",
4198 private_key: "testkey.pem",
4199 }
4200
4201 cc_library {
4202 name: "mylib_common_test",
4203 srcs: ["mylib.cpp"],
4204 system_shared_libs: [],
4205 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004206 // TODO: remove //apex_available:platform
4207 apex_available: [
4208 "//apex_available:platform",
4209 "myapex",
4210 ],
Alex Light0851b882019-02-07 13:20:53 -08004211 }
4212 `)
4213
Sundong Ahnabb64432019-10-22 13:58:29 +09004214 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004215 apexRule := module.Rule("apexRule")
4216 copyCmds := apexRule.Args["copy_commands"]
4217
4218 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4219 t.Log("Apex was not a test apex!")
4220 t.Fail()
4221 }
4222 // Ensure that main rule creates an output
4223 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4224
4225 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004226 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004227
4228 // Ensure that both direct and indirect deps are copied into apex
4229 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4230
Colin Cross7113d202019-11-20 16:39:12 -08004231 // Ensure that the platform variant ends with _shared
4232 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004233}
4234
Alex Light9670d332019-01-29 18:07:33 -08004235func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004236 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004237 apex {
4238 name: "myapex",
4239 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004240 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004241 multilib: {
4242 first: {
4243 native_shared_libs: ["mylib_common"],
4244 }
4245 },
4246 target: {
4247 android: {
4248 multilib: {
4249 first: {
4250 native_shared_libs: ["mylib"],
4251 }
4252 }
4253 },
4254 host: {
4255 multilib: {
4256 first: {
4257 native_shared_libs: ["mylib2"],
4258 }
4259 }
4260 }
4261 }
4262 }
4263
4264 apex_key {
4265 name: "myapex.key",
4266 public_key: "testkey.avbpubkey",
4267 private_key: "testkey.pem",
4268 }
4269
4270 cc_library {
4271 name: "mylib",
4272 srcs: ["mylib.cpp"],
4273 system_shared_libs: [],
4274 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004275 // TODO: remove //apex_available:platform
4276 apex_available: [
4277 "//apex_available:platform",
4278 "myapex",
4279 ],
Alex Light9670d332019-01-29 18:07:33 -08004280 }
4281
4282 cc_library {
4283 name: "mylib_common",
4284 srcs: ["mylib.cpp"],
4285 system_shared_libs: [],
4286 stl: "none",
4287 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004288 // TODO: remove //apex_available:platform
4289 apex_available: [
4290 "//apex_available:platform",
4291 "myapex",
4292 ],
Alex Light9670d332019-01-29 18:07:33 -08004293 }
4294
4295 cc_library {
4296 name: "mylib2",
4297 srcs: ["mylib.cpp"],
4298 system_shared_libs: [],
4299 stl: "none",
4300 compile_multilib: "first",
4301 }
4302 `)
4303
Sundong Ahnabb64432019-10-22 13:58:29 +09004304 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004305 copyCmds := apexRule.Args["copy_commands"]
4306
4307 // Ensure that main rule creates an output
4308 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4309
4310 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004311 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4312 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4313 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004314
4315 // Ensure that both direct and indirect deps are copied into apex
4316 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4317 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4318 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4319
Colin Cross7113d202019-11-20 16:39:12 -08004320 // Ensure that the platform variant ends with _shared
4321 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4322 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004324}
Jiyong Park04480cf2019-02-06 00:16:29 +09004325
Jiyong Park59140302020-12-14 18:44:04 +09004326func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004327 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004328 apex {
4329 name: "myapex",
4330 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004331 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004332 arch: {
4333 arm64: {
4334 native_shared_libs: ["mylib.arm64"],
4335 },
4336 x86_64: {
4337 native_shared_libs: ["mylib.x64"],
4338 },
4339 }
4340 }
4341
4342 apex_key {
4343 name: "myapex.key",
4344 public_key: "testkey.avbpubkey",
4345 private_key: "testkey.pem",
4346 }
4347
4348 cc_library {
4349 name: "mylib.arm64",
4350 srcs: ["mylib.cpp"],
4351 system_shared_libs: [],
4352 stl: "none",
4353 // TODO: remove //apex_available:platform
4354 apex_available: [
4355 "//apex_available:platform",
4356 "myapex",
4357 ],
4358 }
4359
4360 cc_library {
4361 name: "mylib.x64",
4362 srcs: ["mylib.cpp"],
4363 system_shared_libs: [],
4364 stl: "none",
4365 // TODO: remove //apex_available:platform
4366 apex_available: [
4367 "//apex_available:platform",
4368 "myapex",
4369 ],
4370 }
4371 `)
4372
4373 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4374 copyCmds := apexRule.Args["copy_commands"]
4375
4376 // Ensure that apex variant is created for the direct dep
4377 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4378 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4379
4380 // Ensure that both direct and indirect deps are copied into apex
4381 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4382 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4383}
4384
Jiyong Park04480cf2019-02-06 00:16:29 +09004385func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004386 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004387 apex {
4388 name: "myapex",
4389 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004390 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004391 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004392 }
4393
4394 apex_key {
4395 name: "myapex.key",
4396 public_key: "testkey.avbpubkey",
4397 private_key: "testkey.pem",
4398 }
4399
4400 sh_binary {
4401 name: "myscript",
4402 src: "mylib.cpp",
4403 filename: "myscript.sh",
4404 sub_dir: "script",
4405 }
4406 `)
4407
Sundong Ahnabb64432019-10-22 13:58:29 +09004408 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004409 copyCmds := apexRule.Args["copy_commands"]
4410
4411 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4412}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004413
Jooyung Han91df2082019-11-20 01:49:42 +09004414func TestApexInVariousPartition(t *testing.T) {
4415 testcases := []struct {
4416 propName, parition, flattenedPartition string
4417 }{
4418 {"", "system", "system_ext"},
4419 {"product_specific: true", "product", "product"},
4420 {"soc_specific: true", "vendor", "vendor"},
4421 {"proprietary: true", "vendor", "vendor"},
4422 {"vendor: true", "vendor", "vendor"},
4423 {"system_ext_specific: true", "system_ext", "system_ext"},
4424 }
4425 for _, tc := range testcases {
4426 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004427 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004428 apex {
4429 name: "myapex",
4430 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004431 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004432 `+tc.propName+`
4433 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004434
Jooyung Han91df2082019-11-20 01:49:42 +09004435 apex_key {
4436 name: "myapex.key",
4437 public_key: "testkey.avbpubkey",
4438 private_key: "testkey.pem",
4439 }
4440 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004441
Jooyung Han91df2082019-11-20 01:49:42 +09004442 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004443 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4444 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004445 if actual != expected {
4446 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4447 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004448
Jooyung Han91df2082019-11-20 01:49:42 +09004449 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004450 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4451 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004452 if actual != expected {
4453 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4454 }
4455 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004456 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004457}
Jiyong Park67882562019-03-21 01:11:21 +09004458
Jooyung Han580eb4f2020-06-24 19:33:06 +09004459func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004460 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004461 apex {
4462 name: "myapex",
4463 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004464 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004465 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004466
Jooyung Han580eb4f2020-06-24 19:33:06 +09004467 apex_key {
4468 name: "myapex.key",
4469 public_key: "testkey.avbpubkey",
4470 private_key: "testkey.pem",
4471 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004472 `)
4473 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004474 rule := module.Output("file_contexts")
4475 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4476}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004477
Jooyung Han580eb4f2020-06-24 19:33:06 +09004478func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004479 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004480 apex {
4481 name: "myapex",
4482 key: "myapex.key",
4483 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004484 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004485 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004486
Jooyung Han580eb4f2020-06-24 19:33:06 +09004487 apex_key {
4488 name: "myapex.key",
4489 public_key: "testkey.avbpubkey",
4490 private_key: "testkey.pem",
4491 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004492 `, withFiles(map[string][]byte{
4493 "my_own_file_contexts": nil,
4494 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004495}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004496
Jooyung Han580eb4f2020-06-24 19:33:06 +09004497func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004498 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004499 apex {
4500 name: "myapex",
4501 key: "myapex.key",
4502 product_specific: true,
4503 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004504 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004505 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004506
Jooyung Han580eb4f2020-06-24 19:33:06 +09004507 apex_key {
4508 name: "myapex.key",
4509 public_key: "testkey.avbpubkey",
4510 private_key: "testkey.pem",
4511 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004512 `)
4513
Colin Cross1c460562021-02-16 17:55:47 -08004514 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004515 apex {
4516 name: "myapex",
4517 key: "myapex.key",
4518 product_specific: true,
4519 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004520 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004521 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004522
Jooyung Han580eb4f2020-06-24 19:33:06 +09004523 apex_key {
4524 name: "myapex.key",
4525 public_key: "testkey.avbpubkey",
4526 private_key: "testkey.pem",
4527 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004528 `, withFiles(map[string][]byte{
4529 "product_specific_file_contexts": nil,
4530 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004531 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4532 rule := module.Output("file_contexts")
4533 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4534}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004535
Jooyung Han580eb4f2020-06-24 19:33:06 +09004536func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004537 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004538 apex {
4539 name: "myapex",
4540 key: "myapex.key",
4541 product_specific: true,
4542 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004543 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004544 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004545
Jooyung Han580eb4f2020-06-24 19:33:06 +09004546 apex_key {
4547 name: "myapex.key",
4548 public_key: "testkey.avbpubkey",
4549 private_key: "testkey.pem",
4550 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004551
Jooyung Han580eb4f2020-06-24 19:33:06 +09004552 filegroup {
4553 name: "my-file-contexts",
4554 srcs: ["product_specific_file_contexts"],
4555 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004556 `, withFiles(map[string][]byte{
4557 "product_specific_file_contexts": nil,
4558 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004559 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4560 rule := module.Output("file_contexts")
4561 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004562}
4563
Jiyong Park67882562019-03-21 01:11:21 +09004564func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004565 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004566 apex_key {
4567 name: "myapex.key",
4568 public_key: ":my.avbpubkey",
4569 private_key: ":my.pem",
4570 product_specific: true,
4571 }
4572
4573 filegroup {
4574 name: "my.avbpubkey",
4575 srcs: ["testkey2.avbpubkey"],
4576 }
4577
4578 filegroup {
4579 name: "my.pem",
4580 srcs: ["testkey2.pem"],
4581 }
4582 `)
4583
4584 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4585 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004586 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004587 if actual_pubkey != expected_pubkey {
4588 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4589 }
4590 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004591 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004592 if actual_privkey != expected_privkey {
4593 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4594 }
4595}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004596
4597func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004598 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004599 prebuilt_apex {
4600 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004601 arch: {
4602 arm64: {
4603 src: "myapex-arm64.apex",
4604 },
4605 arm: {
4606 src: "myapex-arm.apex",
4607 },
4608 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004609 }
4610 `)
4611
Wei Li340ee8e2022-03-18 17:33:24 -07004612 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4613 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004614
Jiyong Parkc95714e2019-03-29 14:23:10 +09004615 expectedInput := "myapex-arm64.apex"
4616 if prebuilt.inputApex.String() != expectedInput {
4617 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4618 }
Wei Li340ee8e2022-03-18 17:33:24 -07004619 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4620 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4621 rule := testingModule.Rule("genProvenanceMetaData")
4622 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4623 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4624 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4625 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004626}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004627
Paul Duffinc0609c62021-03-01 17:27:16 +00004628func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004629 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004630 prebuilt_apex {
4631 name: "myapex",
4632 }
4633 `)
4634}
4635
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004636func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004637 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004638 prebuilt_apex {
4639 name: "myapex",
4640 src: "myapex-arm.apex",
4641 filename: "notmyapex.apex",
4642 }
4643 `)
4644
Wei Li340ee8e2022-03-18 17:33:24 -07004645 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4646 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004647
4648 expected := "notmyapex.apex"
4649 if p.installFilename != expected {
4650 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4651 }
Wei Li340ee8e2022-03-18 17:33:24 -07004652 rule := testingModule.Rule("genProvenanceMetaData")
4653 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4654 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4655 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4656 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004657}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004658
Samiul Islam7c02e262021-09-08 17:48:28 +01004659func TestApexSetFilenameOverride(t *testing.T) {
4660 testApex(t, `
4661 apex_set {
4662 name: "com.company.android.myapex",
4663 apex_name: "com.android.myapex",
4664 set: "company-myapex.apks",
4665 filename: "com.company.android.myapex.apex"
4666 }
4667 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4668
4669 testApex(t, `
4670 apex_set {
4671 name: "com.company.android.myapex",
4672 apex_name: "com.android.myapex",
4673 set: "company-myapex.apks",
4674 filename: "com.company.android.myapex.capex"
4675 }
4676 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4677
4678 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4679 apex_set {
4680 name: "com.company.android.myapex",
4681 apex_name: "com.android.myapex",
4682 set: "company-myapex.apks",
4683 filename: "some-random-suffix"
4684 }
4685 `)
4686}
4687
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004688func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004689 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004690 prebuilt_apex {
4691 name: "myapex.prebuilt",
4692 src: "myapex-arm.apex",
4693 overrides: [
4694 "myapex",
4695 ],
4696 }
4697 `)
4698
Wei Li340ee8e2022-03-18 17:33:24 -07004699 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4700 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004701
4702 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004703 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004704 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004705 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004706 }
Wei Li340ee8e2022-03-18 17:33:24 -07004707 rule := testingModule.Rule("genProvenanceMetaData")
4708 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4709 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4710 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4711 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004712}
4713
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004714func TestPrebuiltApexName(t *testing.T) {
4715 testApex(t, `
4716 prebuilt_apex {
4717 name: "com.company.android.myapex",
4718 apex_name: "com.android.myapex",
4719 src: "company-myapex-arm.apex",
4720 }
4721 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4722
4723 testApex(t, `
4724 apex_set {
4725 name: "com.company.android.myapex",
4726 apex_name: "com.android.myapex",
4727 set: "company-myapex.apks",
4728 }
4729 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4730}
4731
4732func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4733 _ = android.GroupFixturePreparers(
4734 java.PrepareForTestWithJavaDefaultModules,
4735 PrepareForTestWithApexBuildComponents,
4736 android.FixtureWithRootAndroidBp(`
4737 platform_bootclasspath {
4738 name: "platform-bootclasspath",
4739 fragments: [
4740 {
4741 apex: "com.android.art",
4742 module: "art-bootclasspath-fragment",
4743 },
4744 ],
4745 }
4746
4747 prebuilt_apex {
4748 name: "com.company.android.art",
4749 apex_name: "com.android.art",
4750 src: "com.company.android.art-arm.apex",
4751 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4752 }
4753
4754 prebuilt_bootclasspath_fragment {
4755 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004756 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004757 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004758 hidden_api: {
4759 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4760 metadata: "my-bootclasspath-fragment/metadata.csv",
4761 index: "my-bootclasspath-fragment/index.csv",
4762 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4763 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4764 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004765 }
4766
4767 java_import {
4768 name: "core-oj",
4769 jars: ["prebuilt.jar"],
4770 }
4771 `),
4772 ).RunTest(t)
4773}
4774
Paul Duffin092153d2021-01-26 11:42:39 +00004775// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4776// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004777func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004778 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004779
Paul Duffin89886cb2021-02-05 16:44:03 +00004780 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004781 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004782 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004783 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004784 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004785 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004786 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4787 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4788 android.NormalizePathForTesting(dexJarBuildPath))
4789 }
4790
4791 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004792 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004793 // Make sure the import has been given the correct path to the dex jar.
4794 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4795 dexJarBuildPath := p.DexJarInstallPath()
4796 stem := android.RemoveOptionalPrebuiltPrefix(name)
4797 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4798 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4799 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004800 }
4801
Paul Duffin39853512021-02-26 11:09:39 +00004802 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004803 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004804 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004805 android.AssertArrayString(t, "Check if there is no source variant",
4806 []string{"android_common"},
4807 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004808 }
4809
4810 t.Run("prebuilt only", func(t *testing.T) {
4811 bp := `
4812 prebuilt_apex {
4813 name: "myapex",
4814 arch: {
4815 arm64: {
4816 src: "myapex-arm64.apex",
4817 },
4818 arm: {
4819 src: "myapex-arm.apex",
4820 },
4821 },
Paul Duffin39853512021-02-26 11:09:39 +00004822 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004823 }
4824
4825 java_import {
4826 name: "libfoo",
4827 jars: ["libfoo.jar"],
4828 }
Paul Duffin39853512021-02-26 11:09:39 +00004829
4830 java_sdk_library_import {
4831 name: "libbar",
4832 public: {
4833 jars: ["libbar.jar"],
4834 },
4835 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004836 `
4837
4838 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4839 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4840
Martin Stjernholm44825602021-09-17 01:44:12 +01004841 deapexerName := deapexerModuleName("myapex")
4842 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4843
Paul Duffinf6932af2021-02-26 18:21:56 +00004844 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004845 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004846 rule := deapexer.Rule("deapexer")
4847 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4848 t.Errorf("expected: %q, found: %q", expected, actual)
4849 }
4850
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004851 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004852 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004853 rule = prebuiltApex.Rule("android/soong/android.Cp")
4854 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4855 t.Errorf("expected: %q, found: %q", expected, actual)
4856 }
4857
Paul Duffin89886cb2021-02-05 16:44:03 +00004858 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004859 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004860
4861 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004862 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004863 })
4864
4865 t.Run("prebuilt with source preferred", func(t *testing.T) {
4866
4867 bp := `
4868 prebuilt_apex {
4869 name: "myapex",
4870 arch: {
4871 arm64: {
4872 src: "myapex-arm64.apex",
4873 },
4874 arm: {
4875 src: "myapex-arm.apex",
4876 },
4877 },
Paul Duffin39853512021-02-26 11:09:39 +00004878 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004879 }
4880
4881 java_import {
4882 name: "libfoo",
4883 jars: ["libfoo.jar"],
4884 }
4885
4886 java_library {
4887 name: "libfoo",
4888 }
Paul Duffin39853512021-02-26 11:09:39 +00004889
4890 java_sdk_library_import {
4891 name: "libbar",
4892 public: {
4893 jars: ["libbar.jar"],
4894 },
4895 }
4896
4897 java_sdk_library {
4898 name: "libbar",
4899 srcs: ["foo/bar/MyClass.java"],
4900 unsafe_ignore_missing_latest_api: true,
4901 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004902 `
4903
4904 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4905 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4906
Paul Duffin89886cb2021-02-05 16:44:03 +00004907 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004908 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004909 ensureNoSourceVariant(t, ctx, "libfoo")
4910
4911 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004912 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004913 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004914 })
4915
4916 t.Run("prebuilt preferred with source", func(t *testing.T) {
4917 bp := `
4918 prebuilt_apex {
4919 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004920 arch: {
4921 arm64: {
4922 src: "myapex-arm64.apex",
4923 },
4924 arm: {
4925 src: "myapex-arm.apex",
4926 },
4927 },
Paul Duffin39853512021-02-26 11:09:39 +00004928 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004929 }
4930
4931 java_import {
4932 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004933 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004934 jars: ["libfoo.jar"],
4935 }
4936
4937 java_library {
4938 name: "libfoo",
4939 }
Paul Duffin39853512021-02-26 11:09:39 +00004940
4941 java_sdk_library_import {
4942 name: "libbar",
4943 prefer: true,
4944 public: {
4945 jars: ["libbar.jar"],
4946 },
4947 }
4948
4949 java_sdk_library {
4950 name: "libbar",
4951 srcs: ["foo/bar/MyClass.java"],
4952 unsafe_ignore_missing_latest_api: true,
4953 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004954 `
4955
4956 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4957 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4958
Paul Duffin89886cb2021-02-05 16:44:03 +00004959 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004960 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004961 ensureNoSourceVariant(t, ctx, "libfoo")
4962
4963 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004964 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004965 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004966 })
4967}
4968
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004969func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004970 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004971 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004972 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4973 // is disabled.
4974 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4975 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004976
Paul Duffin37856732021-02-26 14:24:15 +00004977 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4978 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004979 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004980 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004981 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004982 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004983 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004984 foundLibfooJar = true
4985 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004986 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004987 }
4988 }
4989 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02004990 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 +00004991 }
4992 }
4993
Paul Duffin40a3f652021-07-19 13:11:24 +01004994 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004995 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004996 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004997 var rule android.TestingBuildParams
4998
4999 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5000 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005001 }
5002
Paul Duffin40a3f652021-07-19 13:11:24 +01005003 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5004 t.Helper()
5005 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5006 var rule android.TestingBuildParams
5007
5008 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5009 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5010 }
5011
Paul Duffin89f570a2021-06-16 01:42:33 +01005012 fragment := java.ApexVariantReference{
5013 Apex: proptools.StringPtr("myapex"),
5014 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5015 }
5016
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005017 t.Run("prebuilt only", func(t *testing.T) {
5018 bp := `
5019 prebuilt_apex {
5020 name: "myapex",
5021 arch: {
5022 arm64: {
5023 src: "myapex-arm64.apex",
5024 },
5025 arm: {
5026 src: "myapex-arm.apex",
5027 },
5028 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005029 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5030 }
5031
5032 prebuilt_bootclasspath_fragment {
5033 name: "my-bootclasspath-fragment",
5034 contents: ["libfoo", "libbar"],
5035 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005036 hidden_api: {
5037 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5038 metadata: "my-bootclasspath-fragment/metadata.csv",
5039 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005040 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5041 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5042 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005043 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005044 }
5045
5046 java_import {
5047 name: "libfoo",
5048 jars: ["libfoo.jar"],
5049 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005050 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005051 }
Paul Duffin37856732021-02-26 14:24:15 +00005052
5053 java_sdk_library_import {
5054 name: "libbar",
5055 public: {
5056 jars: ["libbar.jar"],
5057 },
5058 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005059 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005060 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005061 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005062 `
5063
Paul Duffin89f570a2021-06-16 01:42:33 +01005064 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005065 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5066 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005067
Paul Duffin537ea3d2021-05-14 10:38:00 +01005068 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005069 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005070 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005071 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005072 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5073 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005074 })
5075
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005076 t.Run("apex_set only", func(t *testing.T) {
5077 bp := `
5078 apex_set {
5079 name: "myapex",
5080 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005081 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5082 }
5083
5084 prebuilt_bootclasspath_fragment {
5085 name: "my-bootclasspath-fragment",
5086 contents: ["libfoo", "libbar"],
5087 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005088 hidden_api: {
5089 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5090 metadata: "my-bootclasspath-fragment/metadata.csv",
5091 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005092 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5093 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5094 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005095 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005096 }
5097
5098 java_import {
5099 name: "libfoo",
5100 jars: ["libfoo.jar"],
5101 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005102 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005103 }
5104
5105 java_sdk_library_import {
5106 name: "libbar",
5107 public: {
5108 jars: ["libbar.jar"],
5109 },
5110 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005111 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005112 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005113 }
5114 `
5115
Paul Duffin89f570a2021-06-16 01:42:33 +01005116 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005117 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5118 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5119
Paul Duffin537ea3d2021-05-14 10:38:00 +01005120 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005121 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005122 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005123 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005124 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5125 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005126 })
5127
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005128 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5129 bp := `
5130 prebuilt_apex {
5131 name: "myapex",
5132 arch: {
5133 arm64: {
5134 src: "myapex-arm64.apex",
5135 },
5136 arm: {
5137 src: "myapex-arm.apex",
5138 },
5139 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005140 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5141 }
5142
5143 prebuilt_bootclasspath_fragment {
5144 name: "my-bootclasspath-fragment",
5145 contents: ["libfoo", "libbar"],
5146 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005147 hidden_api: {
5148 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5149 metadata: "my-bootclasspath-fragment/metadata.csv",
5150 index: "my-bootclasspath-fragment/index.csv",
5151 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5152 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5153 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005154 }
5155
5156 java_import {
5157 name: "libfoo",
5158 jars: ["libfoo.jar"],
5159 apex_available: ["myapex"],
5160 }
5161
5162 java_library {
5163 name: "libfoo",
5164 srcs: ["foo/bar/MyClass.java"],
5165 apex_available: ["myapex"],
5166 }
Paul Duffin37856732021-02-26 14:24:15 +00005167
5168 java_sdk_library_import {
5169 name: "libbar",
5170 public: {
5171 jars: ["libbar.jar"],
5172 },
5173 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005174 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005175 }
5176
5177 java_sdk_library {
5178 name: "libbar",
5179 srcs: ["foo/bar/MyClass.java"],
5180 unsafe_ignore_missing_latest_api: true,
5181 apex_available: ["myapex"],
5182 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005183 `
5184
5185 // In this test the source (java_library) libfoo is active since the
5186 // prebuilt (java_import) defaults to prefer:false. However the
5187 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5188 // find the dex boot jar in it. We either need to disable the source libfoo
5189 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005190 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005191 // dexbootjar check is skipped if AllowMissingDependencies is true
5192 preparerAllowMissingDeps := android.GroupFixturePreparers(
5193 preparer,
5194 android.PrepareForTestWithAllowMissingDependencies,
5195 )
5196 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005197 })
5198
5199 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5200 bp := `
5201 prebuilt_apex {
5202 name: "myapex",
5203 arch: {
5204 arm64: {
5205 src: "myapex-arm64.apex",
5206 },
5207 arm: {
5208 src: "myapex-arm.apex",
5209 },
5210 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005211 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5212 }
5213
5214 prebuilt_bootclasspath_fragment {
5215 name: "my-bootclasspath-fragment",
5216 contents: ["libfoo", "libbar"],
5217 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005218 hidden_api: {
5219 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5220 metadata: "my-bootclasspath-fragment/metadata.csv",
5221 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005222 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5223 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5224 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005225 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005226 }
5227
5228 java_import {
5229 name: "libfoo",
5230 prefer: true,
5231 jars: ["libfoo.jar"],
5232 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005233 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005234 }
5235
5236 java_library {
5237 name: "libfoo",
5238 srcs: ["foo/bar/MyClass.java"],
5239 apex_available: ["myapex"],
5240 }
Paul Duffin37856732021-02-26 14:24:15 +00005241
5242 java_sdk_library_import {
5243 name: "libbar",
5244 prefer: true,
5245 public: {
5246 jars: ["libbar.jar"],
5247 },
5248 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005249 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005250 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005251 }
5252
5253 java_sdk_library {
5254 name: "libbar",
5255 srcs: ["foo/bar/MyClass.java"],
5256 unsafe_ignore_missing_latest_api: true,
5257 apex_available: ["myapex"],
5258 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005259 `
5260
Paul Duffin89f570a2021-06-16 01:42:33 +01005261 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005262 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5263 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005264
Paul Duffin537ea3d2021-05-14 10:38:00 +01005265 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005266 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005267 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005268 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005269 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5270 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005271 })
5272
5273 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5274 bp := `
5275 apex {
5276 name: "myapex",
5277 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005278 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005279 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005280 }
5281
5282 apex_key {
5283 name: "myapex.key",
5284 public_key: "testkey.avbpubkey",
5285 private_key: "testkey.pem",
5286 }
5287
5288 prebuilt_apex {
5289 name: "myapex",
5290 arch: {
5291 arm64: {
5292 src: "myapex-arm64.apex",
5293 },
5294 arm: {
5295 src: "myapex-arm.apex",
5296 },
5297 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005298 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5299 }
5300
5301 prebuilt_bootclasspath_fragment {
5302 name: "my-bootclasspath-fragment",
5303 contents: ["libfoo", "libbar"],
5304 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005305 hidden_api: {
5306 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5307 metadata: "my-bootclasspath-fragment/metadata.csv",
5308 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005309 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5310 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5311 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005312 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005313 }
5314
5315 java_import {
5316 name: "libfoo",
5317 jars: ["libfoo.jar"],
5318 apex_available: ["myapex"],
5319 }
5320
5321 java_library {
5322 name: "libfoo",
5323 srcs: ["foo/bar/MyClass.java"],
5324 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005325 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005326 }
Paul Duffin37856732021-02-26 14:24:15 +00005327
5328 java_sdk_library_import {
5329 name: "libbar",
5330 public: {
5331 jars: ["libbar.jar"],
5332 },
5333 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005334 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005335 }
5336
5337 java_sdk_library {
5338 name: "libbar",
5339 srcs: ["foo/bar/MyClass.java"],
5340 unsafe_ignore_missing_latest_api: true,
5341 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005342 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005343 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005344 `
5345
Paul Duffin89f570a2021-06-16 01:42:33 +01005346 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005347 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5348 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005349
Paul Duffin537ea3d2021-05-14 10:38:00 +01005350 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005351 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005352 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005353 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005354 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5355 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005356 })
5357
5358 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5359 bp := `
5360 apex {
5361 name: "myapex",
5362 enabled: false,
5363 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005364 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005365 }
5366
5367 apex_key {
5368 name: "myapex.key",
5369 public_key: "testkey.avbpubkey",
5370 private_key: "testkey.pem",
5371 }
5372
5373 prebuilt_apex {
5374 name: "myapex",
5375 arch: {
5376 arm64: {
5377 src: "myapex-arm64.apex",
5378 },
5379 arm: {
5380 src: "myapex-arm.apex",
5381 },
5382 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005383 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5384 }
5385
5386 prebuilt_bootclasspath_fragment {
5387 name: "my-bootclasspath-fragment",
5388 contents: ["libfoo", "libbar"],
5389 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005390 hidden_api: {
5391 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5392 metadata: "my-bootclasspath-fragment/metadata.csv",
5393 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005394 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5395 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5396 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005397 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005398 }
5399
5400 java_import {
5401 name: "libfoo",
5402 prefer: true,
5403 jars: ["libfoo.jar"],
5404 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005405 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005406 }
5407
5408 java_library {
5409 name: "libfoo",
5410 srcs: ["foo/bar/MyClass.java"],
5411 apex_available: ["myapex"],
5412 }
Paul Duffin37856732021-02-26 14:24:15 +00005413
5414 java_sdk_library_import {
5415 name: "libbar",
5416 prefer: true,
5417 public: {
5418 jars: ["libbar.jar"],
5419 },
5420 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005421 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005422 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005423 }
5424
5425 java_sdk_library {
5426 name: "libbar",
5427 srcs: ["foo/bar/MyClass.java"],
5428 unsafe_ignore_missing_latest_api: true,
5429 apex_available: ["myapex"],
5430 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005431 `
5432
Paul Duffin89f570a2021-06-16 01:42:33 +01005433 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005434 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5435 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005436
Paul Duffin537ea3d2021-05-14 10:38:00 +01005437 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005438 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005439 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005440 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005441 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5442 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005443 })
5444}
5445
Roland Levillain630846d2019-06-26 12:48:34 +01005446func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005447 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005448 apex_test {
5449 name: "myapex",
5450 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005451 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005452 tests: [
5453 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005454 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005455 ],
5456 }
5457
5458 apex_key {
5459 name: "myapex.key",
5460 public_key: "testkey.avbpubkey",
5461 private_key: "testkey.pem",
5462 }
5463
Liz Kammer1c14a212020-05-12 15:26:55 -07005464 filegroup {
5465 name: "fg",
5466 srcs: [
5467 "baz",
5468 "bar/baz"
5469 ],
5470 }
5471
Roland Levillain630846d2019-06-26 12:48:34 +01005472 cc_test {
5473 name: "mytest",
5474 gtest: false,
5475 srcs: ["mytest.cpp"],
5476 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005477 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005478 system_shared_libs: [],
5479 static_executable: true,
5480 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005481 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005482 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005483
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005484 cc_library {
5485 name: "mylib",
5486 srcs: ["mylib.cpp"],
5487 system_shared_libs: [],
5488 stl: "none",
5489 }
5490
Liz Kammer5bd365f2020-05-27 15:15:11 -07005491 filegroup {
5492 name: "fg2",
5493 srcs: [
5494 "testdata/baz"
5495 ],
5496 }
5497
Roland Levillain9b5fde92019-06-28 15:41:19 +01005498 cc_test {
5499 name: "mytests",
5500 gtest: false,
5501 srcs: [
5502 "mytest1.cpp",
5503 "mytest2.cpp",
5504 "mytest3.cpp",
5505 ],
5506 test_per_src: true,
5507 relative_install_path: "test",
5508 system_shared_libs: [],
5509 static_executable: true,
5510 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005511 data: [
5512 ":fg",
5513 ":fg2",
5514 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005515 }
Roland Levillain630846d2019-06-26 12:48:34 +01005516 `)
5517
Sundong Ahnabb64432019-10-22 13:58:29 +09005518 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005519 copyCmds := apexRule.Args["copy_commands"]
5520
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005521 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005522 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005523 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005524
Liz Kammer1c14a212020-05-12 15:26:55 -07005525 //Ensure that test data are copied into apex.
5526 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5527 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5528
Roland Levillain9b5fde92019-06-28 15:41:19 +01005529 // Ensure that test deps built with `test_per_src` are copied into apex.
5530 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5531 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5532 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005533
5534 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005535 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005536 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005537 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005538 prefix := "TARGET_"
5539 var builder strings.Builder
5540 data.Custom(&builder, name, prefix, "", data)
5541 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005542 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5543 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5544 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5545 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005546 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005547 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005548 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005549
5550 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005551 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005552 data.Custom(&builder, name, prefix, "", data)
5553 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005554 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5555 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005556}
5557
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005558func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005559 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005560 apex {
5561 name: "myapex",
5562 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005563 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005564 }
5565 apex_key {
5566 name: "myapex.key",
5567 public_key: "testkey.avbpubkey",
5568 private_key: "testkey.pem",
5569 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005570 `,
5571 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5572 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5573 }),
5574 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005575 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005576 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005577 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005578 var builder strings.Builder
5579 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5580 androidMk := builder.String()
5581 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5582}
5583
Jooyung Hand48f3c32019-08-23 11:18:57 +09005584func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5585 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5586 apex {
5587 name: "myapex",
5588 key: "myapex.key",
5589 native_shared_libs: ["libfoo"],
5590 }
5591
5592 apex_key {
5593 name: "myapex.key",
5594 public_key: "testkey.avbpubkey",
5595 private_key: "testkey.pem",
5596 }
5597
5598 cc_library {
5599 name: "libfoo",
5600 stl: "none",
5601 system_shared_libs: [],
5602 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005603 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005604 }
5605 `)
5606 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5607 apex {
5608 name: "myapex",
5609 key: "myapex.key",
5610 java_libs: ["myjar"],
5611 }
5612
5613 apex_key {
5614 name: "myapex.key",
5615 public_key: "testkey.avbpubkey",
5616 private_key: "testkey.pem",
5617 }
5618
5619 java_library {
5620 name: "myjar",
5621 srcs: ["foo/bar/MyClass.java"],
5622 sdk_version: "none",
5623 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005624 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005625 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005626 }
5627 `)
5628}
5629
Bill Peckhama41a6962021-01-11 10:58:54 -08005630func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005631 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005632 apex {
5633 name: "myapex",
5634 key: "myapex.key",
5635 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005636 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005637 }
5638
5639 apex_key {
5640 name: "myapex.key",
5641 public_key: "testkey.avbpubkey",
5642 private_key: "testkey.pem",
5643 }
5644
5645 java_import {
5646 name: "myjavaimport",
5647 apex_available: ["myapex"],
5648 jars: ["my.jar"],
5649 compile_dex: true,
5650 }
5651 `)
5652
5653 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5654 apexRule := module.Rule("apexRule")
5655 copyCmds := apexRule.Args["copy_commands"]
5656 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5657}
5658
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005659func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005660 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005661 apex {
5662 name: "myapex",
5663 key: "myapex.key",
5664 apps: [
5665 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005666 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005667 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005668 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005669 }
5670
5671 apex_key {
5672 name: "myapex.key",
5673 public_key: "testkey.avbpubkey",
5674 private_key: "testkey.pem",
5675 }
5676
5677 android_app {
5678 name: "AppFoo",
5679 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005680 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005681 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005682 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005683 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005684 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005685 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005686
5687 android_app {
5688 name: "AppFooPriv",
5689 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005690 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005691 system_modules: "none",
5692 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005693 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005694 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005695 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005696
5697 cc_library_shared {
5698 name: "libjni",
5699 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005700 shared_libs: ["libfoo"],
5701 stl: "none",
5702 system_shared_libs: [],
5703 apex_available: [ "myapex" ],
5704 sdk_version: "current",
5705 }
5706
5707 cc_library_shared {
5708 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005709 stl: "none",
5710 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005711 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005712 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005713 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005714 `)
5715
Sundong Ahnabb64432019-10-22 13:58:29 +09005716 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005717 apexRule := module.Rule("apexRule")
5718 copyCmds := apexRule.Args["copy_commands"]
5719
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005720 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5721 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005722
Colin Crossaede88c2020-08-11 12:17:01 -07005723 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005724 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005725 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005726 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005727 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005728 // JNI libraries including transitive deps are
5729 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005730 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005731 // ... embedded inside APK (jnilibs.zip)
5732 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5733 // ... and not directly inside the APEX
5734 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5735 }
Dario Frenicde2a032019-10-27 00:29:22 +01005736}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005737
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005738func TestApexWithAppImportBuildId(t *testing.T) {
5739 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5740 for _, id := range invalidBuildIds {
5741 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5742 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5743 variables.BuildId = proptools.StringPtr(id)
5744 })
5745 testApexError(t, message, `apex {
5746 name: "myapex",
5747 key: "myapex.key",
5748 apps: ["AppFooPrebuilt"],
5749 updatable: false,
5750 }
5751
5752 apex_key {
5753 name: "myapex.key",
5754 public_key: "testkey.avbpubkey",
5755 private_key: "testkey.pem",
5756 }
5757
5758 android_app_import {
5759 name: "AppFooPrebuilt",
5760 apk: "PrebuiltAppFoo.apk",
5761 presigned: true,
5762 apex_available: ["myapex"],
5763 }
5764 `, fixture)
5765 }
5766}
5767
Dario Frenicde2a032019-10-27 00:29:22 +01005768func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005769 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005770 apex {
5771 name: "myapex",
5772 key: "myapex.key",
5773 apps: [
5774 "AppFooPrebuilt",
5775 "AppFooPrivPrebuilt",
5776 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005777 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005778 }
5779
5780 apex_key {
5781 name: "myapex.key",
5782 public_key: "testkey.avbpubkey",
5783 private_key: "testkey.pem",
5784 }
5785
5786 android_app_import {
5787 name: "AppFooPrebuilt",
5788 apk: "PrebuiltAppFoo.apk",
5789 presigned: true,
5790 dex_preopt: {
5791 enabled: false,
5792 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005793 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005794 }
5795
5796 android_app_import {
5797 name: "AppFooPrivPrebuilt",
5798 apk: "PrebuiltAppFooPriv.apk",
5799 privileged: true,
5800 presigned: true,
5801 dex_preopt: {
5802 enabled: false,
5803 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005804 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005805 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005806 }
5807 `)
5808
Sundong Ahnabb64432019-10-22 13:58:29 +09005809 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005810 apexRule := module.Rule("apexRule")
5811 copyCmds := apexRule.Args["copy_commands"]
5812
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005813 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5814 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005815}
5816
5817func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005818 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005819 apex {
5820 name: "myapex",
5821 key: "myapex.key",
5822 apps: [
5823 "AppFoo",
5824 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005825 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005826 }
5827
5828 apex_key {
5829 name: "myapex.key",
5830 public_key: "testkey.avbpubkey",
5831 private_key: "testkey.pem",
5832 }
5833
5834 android_app {
5835 name: "AppFoo",
5836 srcs: ["foo/bar/MyClass.java"],
5837 sdk_version: "none",
5838 system_modules: "none",
5839 apex_available: [ "myapex" ],
5840 }
5841
5842 android_app_import {
5843 name: "AppFoo",
5844 apk: "AppFooPrebuilt.apk",
5845 filename: "AppFooPrebuilt.apk",
5846 presigned: true,
5847 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005848 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005849 }
5850 `, withFiles(map[string][]byte{
5851 "AppFooPrebuilt.apk": nil,
5852 }))
5853
5854 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005855 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005856 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005857}
5858
Dario Freni6f3937c2019-12-20 22:58:03 +00005859func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005860 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005861 apex {
5862 name: "myapex",
5863 key: "myapex.key",
5864 apps: [
5865 "TesterHelpAppFoo",
5866 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005867 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005868 }
5869
5870 apex_key {
5871 name: "myapex.key",
5872 public_key: "testkey.avbpubkey",
5873 private_key: "testkey.pem",
5874 }
5875
5876 android_test_helper_app {
5877 name: "TesterHelpAppFoo",
5878 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005879 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005880 }
5881
5882 `)
5883
5884 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5885 apexRule := module.Rule("apexRule")
5886 copyCmds := apexRule.Args["copy_commands"]
5887
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005888 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00005889}
5890
Jooyung Han18020ea2019-11-13 10:50:48 +09005891func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5892 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005893 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005894 apex {
5895 name: "myapex",
5896 key: "myapex.key",
5897 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005898 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005899 }
5900
5901 apex_key {
5902 name: "myapex.key",
5903 public_key: "testkey.avbpubkey",
5904 private_key: "testkey.pem",
5905 }
5906
5907 apex {
5908 name: "otherapex",
5909 key: "myapex.key",
5910 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005911 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005912 }
5913
5914 cc_defaults {
5915 name: "libfoo-defaults",
5916 apex_available: ["otherapex"],
5917 }
5918
5919 cc_library {
5920 name: "libfoo",
5921 defaults: ["libfoo-defaults"],
5922 stl: "none",
5923 system_shared_libs: [],
5924 }`)
5925}
5926
Paul Duffine52e66f2020-03-30 17:54:29 +01005927func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005928 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005929 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005930 apex {
5931 name: "myapex",
5932 key: "myapex.key",
5933 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005934 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005935 }
5936
5937 apex_key {
5938 name: "myapex.key",
5939 public_key: "testkey.avbpubkey",
5940 private_key: "testkey.pem",
5941 }
5942
5943 apex {
5944 name: "otherapex",
5945 key: "otherapex.key",
5946 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005947 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005948 }
5949
5950 apex_key {
5951 name: "otherapex.key",
5952 public_key: "testkey.avbpubkey",
5953 private_key: "testkey.pem",
5954 }
5955
5956 cc_library {
5957 name: "libfoo",
5958 stl: "none",
5959 system_shared_libs: [],
5960 apex_available: ["otherapex"],
5961 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005962}
Jiyong Park127b40b2019-09-30 16:04:35 +09005963
Paul Duffine52e66f2020-03-30 17:54:29 +01005964func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005965 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005966 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00005967.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01005968.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005969.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005970.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005971.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005972.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005973 apex {
5974 name: "myapex",
5975 key: "myapex.key",
5976 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005977 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005978 }
5979
5980 apex_key {
5981 name: "myapex.key",
5982 public_key: "testkey.avbpubkey",
5983 private_key: "testkey.pem",
5984 }
5985
Jiyong Park127b40b2019-09-30 16:04:35 +09005986 cc_library {
5987 name: "libfoo",
5988 stl: "none",
5989 shared_libs: ["libbar"],
5990 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005991 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005992 }
5993
5994 cc_library {
5995 name: "libbar",
5996 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005997 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005998 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005999 apex_available: ["myapex"],
6000 }
6001
6002 cc_library {
6003 name: "libbaz",
6004 stl: "none",
6005 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006006 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006007}
Jiyong Park127b40b2019-09-30 16:04:35 +09006008
Paul Duffine52e66f2020-03-30 17:54:29 +01006009func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006010 testApexError(t, "\"otherapex\" is not a valid module name", `
6011 apex {
6012 name: "myapex",
6013 key: "myapex.key",
6014 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006015 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006016 }
6017
6018 apex_key {
6019 name: "myapex.key",
6020 public_key: "testkey.avbpubkey",
6021 private_key: "testkey.pem",
6022 }
6023
6024 cc_library {
6025 name: "libfoo",
6026 stl: "none",
6027 system_shared_libs: [],
6028 apex_available: ["otherapex"],
6029 }`)
6030
Paul Duffine52e66f2020-03-30 17:54:29 +01006031 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006032 apex {
6033 name: "myapex",
6034 key: "myapex.key",
6035 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006036 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006037 }
6038
6039 apex_key {
6040 name: "myapex.key",
6041 public_key: "testkey.avbpubkey",
6042 private_key: "testkey.pem",
6043 }
6044
6045 cc_library {
6046 name: "libfoo",
6047 stl: "none",
6048 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006049 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006050 apex_available: ["myapex"],
6051 }
6052
6053 cc_library {
6054 name: "libbar",
6055 stl: "none",
6056 system_shared_libs: [],
6057 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006058 }
6059
6060 cc_library {
6061 name: "libbaz",
6062 stl: "none",
6063 system_shared_libs: [],
6064 stubs: {
6065 versions: ["10", "20", "30"],
6066 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006067 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006068}
Jiyong Park127b40b2019-09-30 16:04:35 +09006069
Jiyong Park89e850a2020-04-07 16:37:39 +09006070func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006071 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006072 apex {
6073 name: "myapex",
6074 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006075 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006076 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006077 }
6078
6079 apex_key {
6080 name: "myapex.key",
6081 public_key: "testkey.avbpubkey",
6082 private_key: "testkey.pem",
6083 }
6084
6085 cc_library {
6086 name: "libfoo",
6087 stl: "none",
6088 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006089 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006090 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006091 }
6092
6093 cc_library {
6094 name: "libfoo2",
6095 stl: "none",
6096 system_shared_libs: [],
6097 shared_libs: ["libbaz"],
6098 apex_available: ["//apex_available:platform"],
6099 }
6100
6101 cc_library {
6102 name: "libbar",
6103 stl: "none",
6104 system_shared_libs: [],
6105 apex_available: ["myapex"],
6106 }
6107
6108 cc_library {
6109 name: "libbaz",
6110 stl: "none",
6111 system_shared_libs: [],
6112 apex_available: ["myapex"],
6113 stubs: {
6114 versions: ["1"],
6115 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006116 }`)
6117
Jiyong Park89e850a2020-04-07 16:37:39 +09006118 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6119 // because it depends on libbar which isn't available to platform
6120 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6121 if libfoo.NotAvailableForPlatform() != true {
6122 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6123 }
6124
6125 // libfoo2 however can be available to platform because it depends on libbaz which provides
6126 // stubs
6127 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6128 if libfoo2.NotAvailableForPlatform() == true {
6129 t.Errorf("%q should be available to platform", libfoo2.String())
6130 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006131}
Jiyong Parka90ca002019-10-07 15:47:24 +09006132
Paul Duffine52e66f2020-03-30 17:54:29 +01006133func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006134 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006135 apex {
6136 name: "myapex",
6137 key: "myapex.key",
6138 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006139 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006140 }
6141
6142 apex_key {
6143 name: "myapex.key",
6144 public_key: "testkey.avbpubkey",
6145 private_key: "testkey.pem",
6146 }
6147
6148 cc_library {
6149 name: "libfoo",
6150 stl: "none",
6151 system_shared_libs: [],
6152 apex_available: ["myapex"],
6153 static: {
6154 apex_available: ["//apex_available:platform"],
6155 },
6156 }`)
6157
Jiyong Park89e850a2020-04-07 16:37:39 +09006158 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6159 if libfooShared.NotAvailableForPlatform() != true {
6160 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6161 }
6162 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6163 if libfooStatic.NotAvailableForPlatform() != false {
6164 t.Errorf("%q should be available to platform", libfooStatic.String())
6165 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006166}
6167
Jiyong Park5d790c32019-11-15 18:40:32 +09006168func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006169 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006170 apex {
6171 name: "myapex",
6172 key: "myapex.key",
6173 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006174 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006175 prebuilts: ["myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006176 bootclasspath_fragments: ["mybootclasspath_fragment"],
6177 systemserverclasspath_fragments: ["mysystemserverclasspath_fragment"],
6178 java_libs: ["myjava_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006179 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006180 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006181 }
6182
6183 override_apex {
6184 name: "override_myapex",
6185 base: "myapex",
6186 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006187 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006188 prebuilts: ["override_myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006189 bootclasspath_fragments: ["override_bootclasspath_fragment"],
6190 systemserverclasspath_fragments: ["override_systemserverclasspath_fragment"],
6191 java_libs: ["override_java_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006192 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006193 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006194 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006195 key: "mynewapex.key",
6196 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006197 }
6198
6199 apex_key {
6200 name: "myapex.key",
6201 public_key: "testkey.avbpubkey",
6202 private_key: "testkey.pem",
6203 }
6204
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006205 apex_key {
6206 name: "mynewapex.key",
6207 public_key: "testkey2.avbpubkey",
6208 private_key: "testkey2.pem",
6209 }
6210
6211 android_app_certificate {
6212 name: "myapex.certificate",
6213 certificate: "testkey",
6214 }
6215
Jiyong Park5d790c32019-11-15 18:40:32 +09006216 android_app {
6217 name: "app",
6218 srcs: ["foo/bar/MyClass.java"],
6219 package_name: "foo",
6220 sdk_version: "none",
6221 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006222 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006223 }
6224
6225 override_android_app {
6226 name: "override_app",
6227 base: "app",
6228 package_name: "bar",
6229 }
markchien7c803b82021-08-26 22:10:06 +08006230
6231 bpf {
6232 name: "bpf",
6233 srcs: ["bpf.c"],
6234 }
6235
6236 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006237 name: "overrideBpf",
6238 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006239 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006240
6241 prebuilt_etc {
6242 name: "myetc",
6243 src: "myprebuilt",
6244 }
6245
6246 prebuilt_etc {
6247 name: "override_myetc",
6248 src: "override_myprebuilt",
6249 }
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006250
6251 java_library {
6252 name: "bcplib",
6253 srcs: ["a.java"],
6254 compile_dex: true,
6255 apex_available: ["myapex"],
6256 permitted_packages: ["bcp.lib"],
6257 }
6258
6259 bootclasspath_fragment {
6260 name: "mybootclasspath_fragment",
6261 contents: ["bcplib"],
6262 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01006263 hidden_api: {
6264 split_packages: ["*"],
6265 },
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006266 }
6267
6268 java_library {
6269 name: "override_bcplib",
6270 srcs: ["a.java"],
6271 compile_dex: true,
6272 apex_available: ["myapex"],
6273 permitted_packages: ["override.bcp.lib"],
6274 }
6275
6276 bootclasspath_fragment {
6277 name: "override_bootclasspath_fragment",
6278 contents: ["override_bcplib"],
6279 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01006280 hidden_api: {
6281 split_packages: ["*"],
6282 },
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006283 }
6284
6285 java_library {
6286 name: "systemserverlib",
6287 srcs: ["a.java"],
6288 apex_available: ["myapex"],
6289 }
6290
6291 systemserverclasspath_fragment {
6292 name: "mysystemserverclasspath_fragment",
6293 standalone_contents: ["systemserverlib"],
6294 apex_available: ["myapex"],
6295 }
6296
6297 java_library {
6298 name: "override_systemserverlib",
6299 srcs: ["a.java"],
6300 apex_available: ["myapex"],
6301 }
6302
6303 systemserverclasspath_fragment {
6304 name: "override_systemserverclasspath_fragment",
6305 standalone_contents: ["override_systemserverlib"],
6306 apex_available: ["myapex"],
6307 }
6308
6309 java_library {
6310 name: "myjava_library",
6311 srcs: ["a.java"],
6312 compile_dex: true,
6313 apex_available: ["myapex"],
6314 }
6315
6316 java_library {
6317 name: "override_java_library",
6318 srcs: ["a.java"],
6319 compile_dex: true,
6320 apex_available: ["myapex"],
6321 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006322 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006323
Jiyong Park317645e2019-12-05 13:20:58 +09006324 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6325 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6326 if originalVariant.GetOverriddenBy() != "" {
6327 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6328 }
6329 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6330 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6331 }
6332
Jiyong Park5d790c32019-11-15 18:40:32 +09006333 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6334 apexRule := module.Rule("apexRule")
6335 copyCmds := apexRule.Args["copy_commands"]
6336
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006337 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6338 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006339
markchien7c803b82021-08-26 22:10:06 +08006340 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006341 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006342
Daniel Norman5a3ce132021-08-26 15:44:43 -07006343 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6344 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6345
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006346 apexBundle := module.Module().(*apexBundle)
6347 name := apexBundle.Name()
6348 if name != "override_myapex" {
6349 t.Errorf("name should be \"override_myapex\", but was %q", name)
6350 }
6351
Baligh Uddin004d7172020-02-19 21:29:28 -08006352 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6353 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6354 }
6355
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006356 android.AssertArrayString(t, "Bootclasspath_fragments does not match",
6357 []string{"override_bootclasspath_fragment"}, apexBundle.overridableProperties.Bootclasspath_fragments)
6358 android.AssertArrayString(t, "Systemserverclasspath_fragments does not match",
6359 []string{"override_systemserverclasspath_fragment"}, apexBundle.overridableProperties.Systemserverclasspath_fragments)
6360 android.AssertArrayString(t, "Java_libs does not match",
6361 []string{"override_java_library"}, apexBundle.overridableProperties.Java_libs)
6362
Jiyong Park20bacab2020-03-03 11:45:41 +09006363 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006364 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006365 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6366
6367 signApkRule := module.Rule("signapk")
6368 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006369
Colin Crossaa255532020-07-03 13:18:24 -07006370 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006371 var builder strings.Builder
6372 data.Custom(&builder, name, "TARGET_", "", data)
6373 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006374 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Ken Chen5372a242022-07-07 17:48:06 +08006375 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006376 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006377 ensureContains(t, androidMk, "LOCAL_MODULE := override_bcplib.override_myapex")
6378 ensureContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.override_myapex")
6379 ensureContains(t, androidMk, "LOCAL_MODULE := override_java_library.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006380 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006381 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006382 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006383 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006384 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006385 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006386 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_bcplib.myapex")
6387 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.myapex")
6388 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_java_library.pb.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006389 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006390}
6391
Albert Martineefabcf2022-03-21 20:11:16 +00006392func TestMinSdkVersionOverride(t *testing.T) {
6393 // Override from 29 to 31
6394 minSdkOverride31 := "31"
6395 ctx := testApex(t, `
6396 apex {
6397 name: "myapex",
6398 key: "myapex.key",
6399 native_shared_libs: ["mylib"],
6400 updatable: true,
6401 min_sdk_version: "29"
6402 }
6403
6404 override_apex {
6405 name: "override_myapex",
6406 base: "myapex",
6407 logging_parent: "com.foo.bar",
6408 package_name: "test.overridden.package"
6409 }
6410
6411 apex_key {
6412 name: "myapex.key",
6413 public_key: "testkey.avbpubkey",
6414 private_key: "testkey.pem",
6415 }
6416
6417 cc_library {
6418 name: "mylib",
6419 srcs: ["mylib.cpp"],
6420 runtime_libs: ["libbar"],
6421 system_shared_libs: [],
6422 stl: "none",
6423 apex_available: [ "myapex" ],
6424 min_sdk_version: "apex_inherit"
6425 }
6426
6427 cc_library {
6428 name: "libbar",
6429 srcs: ["mylib.cpp"],
6430 system_shared_libs: [],
6431 stl: "none",
6432 apex_available: [ "myapex" ],
6433 min_sdk_version: "apex_inherit"
6434 }
6435
6436 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6437
6438 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6439 copyCmds := apexRule.Args["copy_commands"]
6440
6441 // Ensure that direct non-stubs dep is always included
6442 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6443
6444 // Ensure that runtime_libs dep in included
6445 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6446
6447 // Ensure libraries target overridden min_sdk_version value
6448 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6449}
6450
6451func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6452 // Attempt to override from 31 to 29, should be a NOOP
6453 minSdkOverride29 := "29"
6454 ctx := testApex(t, `
6455 apex {
6456 name: "myapex",
6457 key: "myapex.key",
6458 native_shared_libs: ["mylib"],
6459 updatable: true,
6460 min_sdk_version: "31"
6461 }
6462
6463 override_apex {
6464 name: "override_myapex",
6465 base: "myapex",
6466 logging_parent: "com.foo.bar",
6467 package_name: "test.overridden.package"
6468 }
6469
6470 apex_key {
6471 name: "myapex.key",
6472 public_key: "testkey.avbpubkey",
6473 private_key: "testkey.pem",
6474 }
6475
6476 cc_library {
6477 name: "mylib",
6478 srcs: ["mylib.cpp"],
6479 runtime_libs: ["libbar"],
6480 system_shared_libs: [],
6481 stl: "none",
6482 apex_available: [ "myapex" ],
6483 min_sdk_version: "apex_inherit"
6484 }
6485
6486 cc_library {
6487 name: "libbar",
6488 srcs: ["mylib.cpp"],
6489 system_shared_libs: [],
6490 stl: "none",
6491 apex_available: [ "myapex" ],
6492 min_sdk_version: "apex_inherit"
6493 }
6494
6495 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6496
6497 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6498 copyCmds := apexRule.Args["copy_commands"]
6499
6500 // Ensure that direct non-stubs dep is always included
6501 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6502
6503 // Ensure that runtime_libs dep in included
6504 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6505
6506 // Ensure libraries target the original min_sdk_version value rather than the overridden
6507 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6508}
6509
Jooyung Han214bf372019-11-12 13:03:50 +09006510func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006511 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006512 apex {
6513 name: "myapex",
6514 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006515 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006516 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006517 }
6518
6519 apex_key {
6520 name: "myapex.key",
6521 public_key: "testkey.avbpubkey",
6522 private_key: "testkey.pem",
6523 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006524
6525 cc_library {
6526 name: "mylib",
6527 srcs: ["mylib.cpp"],
6528 stl: "libc++",
6529 system_shared_libs: [],
6530 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006531 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006532 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006533 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006534
6535 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6536 args := module.Rule("apexRule").Args
6537 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006538 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006539
6540 // The copies of the libraries in the apex should have one more dependency than
6541 // the ones outside the apex, namely the unwinder. Ideally we should check
6542 // the dependency names directly here but for some reason the names are blank in
6543 // this test.
6544 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006545 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006546 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6547 if len(apexImplicits) != len(nonApexImplicits)+1 {
6548 t.Errorf("%q missing unwinder dep", lib)
6549 }
6550 }
Jooyung Han214bf372019-11-12 13:03:50 +09006551}
6552
Paul Duffine05480a2021-03-08 15:07:14 +00006553var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006554 "api/current.txt": nil,
6555 "api/removed.txt": nil,
6556 "api/system-current.txt": nil,
6557 "api/system-removed.txt": nil,
6558 "api/test-current.txt": nil,
6559 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006560
Anton Hanssondff2c782020-12-21 17:10:01 +00006561 "100/public/api/foo.txt": nil,
6562 "100/public/api/foo-removed.txt": nil,
6563 "100/system/api/foo.txt": nil,
6564 "100/system/api/foo-removed.txt": nil,
6565
Paul Duffineedc5d52020-06-12 17:46:39 +01006566 // For java_sdk_library_import
6567 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006568}
6569
Jooyung Han58f26ab2019-12-18 15:34:32 +09006570func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006571 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006572 apex {
6573 name: "myapex",
6574 key: "myapex.key",
6575 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006576 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006577 }
6578
6579 apex_key {
6580 name: "myapex.key",
6581 public_key: "testkey.avbpubkey",
6582 private_key: "testkey.pem",
6583 }
6584
6585 java_sdk_library {
6586 name: "foo",
6587 srcs: ["a.java"],
6588 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006589 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006590 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006591
6592 prebuilt_apis {
6593 name: "sdk",
6594 api_dirs: ["100"],
6595 }
Paul Duffin9b879592020-05-26 13:21:35 +01006596 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006597
6598 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006599 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006600 "javalib/foo.jar",
6601 "etc/permissions/foo.xml",
6602 })
6603 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006604 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006605 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 +09006606}
6607
Paul Duffin9b879592020-05-26 13:21:35 +01006608func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006609 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006610 apex {
6611 name: "myapex",
6612 key: "myapex.key",
6613 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006614 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006615 }
6616
6617 apex_key {
6618 name: "myapex.key",
6619 public_key: "testkey.avbpubkey",
6620 private_key: "testkey.pem",
6621 }
6622
6623 java_sdk_library {
6624 name: "foo",
6625 srcs: ["a.java"],
6626 api_packages: ["foo"],
6627 apex_available: ["myapex"],
6628 sdk_version: "none",
6629 system_modules: "none",
6630 }
6631
6632 java_library {
6633 name: "bar",
6634 srcs: ["a.java"],
6635 libs: ["foo"],
6636 apex_available: ["myapex"],
6637 sdk_version: "none",
6638 system_modules: "none",
6639 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006640
6641 prebuilt_apis {
6642 name: "sdk",
6643 api_dirs: ["100"],
6644 }
Paul Duffin9b879592020-05-26 13:21:35 +01006645 `, withFiles(filesForSdkLibrary))
6646
6647 // java_sdk_library installs both impl jar and permission XML
6648 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6649 "javalib/bar.jar",
6650 "javalib/foo.jar",
6651 "etc/permissions/foo.xml",
6652 })
6653
6654 // The bar library should depend on the implementation jar.
6655 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006656 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006657 t.Errorf("expected %q, found %#q", expected, actual)
6658 }
6659}
6660
6661func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006662 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006663 apex {
6664 name: "myapex",
6665 key: "myapex.key",
6666 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006667 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006668 }
6669
6670 apex_key {
6671 name: "myapex.key",
6672 public_key: "testkey.avbpubkey",
6673 private_key: "testkey.pem",
6674 }
6675
6676 java_sdk_library {
6677 name: "foo",
6678 srcs: ["a.java"],
6679 api_packages: ["foo"],
6680 apex_available: ["myapex"],
6681 sdk_version: "none",
6682 system_modules: "none",
6683 }
6684
6685 java_library {
6686 name: "bar",
6687 srcs: ["a.java"],
6688 libs: ["foo"],
6689 sdk_version: "none",
6690 system_modules: "none",
6691 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006692
6693 prebuilt_apis {
6694 name: "sdk",
6695 api_dirs: ["100"],
6696 }
Paul Duffin9b879592020-05-26 13:21:35 +01006697 `, withFiles(filesForSdkLibrary))
6698
6699 // java_sdk_library installs both impl jar and permission XML
6700 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6701 "javalib/foo.jar",
6702 "etc/permissions/foo.xml",
6703 })
6704
6705 // The bar library should depend on the stubs jar.
6706 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006707 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006708 t.Errorf("expected %q, found %#q", expected, actual)
6709 }
6710}
6711
Paul Duffineedc5d52020-06-12 17:46:39 +01006712func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006713 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006714 prebuilt_apis {
6715 name: "sdk",
6716 api_dirs: ["100"],
6717 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006718 withFiles(map[string][]byte{
6719 "apex/a.java": nil,
6720 "apex/apex_manifest.json": nil,
6721 "apex/Android.bp": []byte(`
6722 package {
6723 default_visibility: ["//visibility:private"],
6724 }
6725
6726 apex {
6727 name: "myapex",
6728 key: "myapex.key",
6729 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006730 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006731 }
6732
6733 apex_key {
6734 name: "myapex.key",
6735 public_key: "testkey.avbpubkey",
6736 private_key: "testkey.pem",
6737 }
6738
6739 java_library {
6740 name: "bar",
6741 srcs: ["a.java"],
6742 libs: ["foo"],
6743 apex_available: ["myapex"],
6744 sdk_version: "none",
6745 system_modules: "none",
6746 }
6747`),
6748 "source/a.java": nil,
6749 "source/api/current.txt": nil,
6750 "source/api/removed.txt": nil,
6751 "source/Android.bp": []byte(`
6752 package {
6753 default_visibility: ["//visibility:private"],
6754 }
6755
6756 java_sdk_library {
6757 name: "foo",
6758 visibility: ["//apex"],
6759 srcs: ["a.java"],
6760 api_packages: ["foo"],
6761 apex_available: ["myapex"],
6762 sdk_version: "none",
6763 system_modules: "none",
6764 public: {
6765 enabled: true,
6766 },
6767 }
6768`),
6769 "prebuilt/a.jar": nil,
6770 "prebuilt/Android.bp": []byte(`
6771 package {
6772 default_visibility: ["//visibility:private"],
6773 }
6774
6775 java_sdk_library_import {
6776 name: "foo",
6777 visibility: ["//apex", "//source"],
6778 apex_available: ["myapex"],
6779 prefer: true,
6780 public: {
6781 jars: ["a.jar"],
6782 },
6783 }
6784`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006785 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006786 )
6787
6788 // java_sdk_library installs both impl jar and permission XML
6789 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6790 "javalib/bar.jar",
6791 "javalib/foo.jar",
6792 "etc/permissions/foo.xml",
6793 })
6794
6795 // The bar library should depend on the implementation jar.
6796 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006797 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006798 t.Errorf("expected %q, found %#q", expected, actual)
6799 }
6800}
6801
6802func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6803 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6804 apex {
6805 name: "myapex",
6806 key: "myapex.key",
6807 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006808 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006809 }
6810
6811 apex_key {
6812 name: "myapex.key",
6813 public_key: "testkey.avbpubkey",
6814 private_key: "testkey.pem",
6815 }
6816
6817 java_sdk_library_import {
6818 name: "foo",
6819 apex_available: ["myapex"],
6820 prefer: true,
6821 public: {
6822 jars: ["a.jar"],
6823 },
6824 }
6825
6826 `, withFiles(filesForSdkLibrary))
6827}
6828
atrost6e126252020-01-27 17:01:16 +00006829func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006830 result := android.GroupFixturePreparers(
6831 prepareForApexTest,
6832 java.PrepareForTestWithPlatformCompatConfig,
6833 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006834 apex {
6835 name: "myapex",
6836 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006837 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006838 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006839 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006840 }
6841
6842 apex_key {
6843 name: "myapex.key",
6844 public_key: "testkey.avbpubkey",
6845 private_key: "testkey.pem",
6846 }
6847
6848 platform_compat_config {
6849 name: "myjar-platform-compat-config",
6850 src: ":myjar",
6851 }
6852
6853 java_library {
6854 name: "myjar",
6855 srcs: ["foo/bar/MyClass.java"],
6856 sdk_version: "none",
6857 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006858 apex_available: [ "myapex" ],
6859 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006860
6861 // Make sure that a preferred prebuilt does not affect the apex contents.
6862 prebuilt_platform_compat_config {
6863 name: "myjar-platform-compat-config",
6864 metadata: "compat-config/metadata.xml",
6865 prefer: true,
6866 }
atrost6e126252020-01-27 17:01:16 +00006867 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006868 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006869 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6870 "etc/compatconfig/myjar-platform-compat-config.xml",
6871 "javalib/myjar.jar",
6872 })
6873}
6874
Jiyong Park479321d2019-12-16 11:47:12 +09006875func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6876 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6877 apex {
6878 name: "myapex",
6879 key: "myapex.key",
6880 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006881 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006882 }
6883
6884 apex_key {
6885 name: "myapex.key",
6886 public_key: "testkey.avbpubkey",
6887 private_key: "testkey.pem",
6888 }
6889
6890 java_library {
6891 name: "myjar",
6892 srcs: ["foo/bar/MyClass.java"],
6893 sdk_version: "none",
6894 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006895 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006896 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006897 }
6898 `)
6899}
6900
Jiyong Park7afd1072019-12-30 16:56:33 +09006901func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006902 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006903 apex {
6904 name: "myapex",
6905 key: "myapex.key",
6906 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006907 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006908 }
6909
6910 apex_key {
6911 name: "myapex.key",
6912 public_key: "testkey.avbpubkey",
6913 private_key: "testkey.pem",
6914 }
6915
6916 cc_library {
6917 name: "mylib",
6918 srcs: ["mylib.cpp"],
6919 system_shared_libs: [],
6920 stl: "none",
6921 required: ["a", "b"],
6922 host_required: ["c", "d"],
6923 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006924 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006925 }
6926 `)
6927
6928 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006929 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006930 name := apexBundle.BaseModuleName()
6931 prefix := "TARGET_"
6932 var builder strings.Builder
6933 data.Custom(&builder, name, prefix, "", data)
6934 androidMk := builder.String()
6935 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6936 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6937 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6938}
6939
Jiyong Park7cd10e32020-01-14 09:22:18 +09006940func TestSymlinksFromApexToSystem(t *testing.T) {
6941 bp := `
6942 apex {
6943 name: "myapex",
6944 key: "myapex.key",
6945 native_shared_libs: ["mylib"],
6946 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006947 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006948 }
6949
Jiyong Park9d677202020-02-19 16:29:35 +09006950 apex {
6951 name: "myapex.updatable",
6952 key: "myapex.key",
6953 native_shared_libs: ["mylib"],
6954 java_libs: ["myjar"],
6955 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006956 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006957 }
6958
Jiyong Park7cd10e32020-01-14 09:22:18 +09006959 apex_key {
6960 name: "myapex.key",
6961 public_key: "testkey.avbpubkey",
6962 private_key: "testkey.pem",
6963 }
6964
6965 cc_library {
6966 name: "mylib",
6967 srcs: ["mylib.cpp"],
6968 shared_libs: ["myotherlib"],
6969 system_shared_libs: [],
6970 stl: "none",
6971 apex_available: [
6972 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006973 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006974 "//apex_available:platform",
6975 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006976 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006977 }
6978
6979 cc_library {
6980 name: "myotherlib",
6981 srcs: ["mylib.cpp"],
6982 system_shared_libs: [],
6983 stl: "none",
6984 apex_available: [
6985 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006986 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006987 "//apex_available:platform",
6988 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006989 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006990 }
6991
6992 java_library {
6993 name: "myjar",
6994 srcs: ["foo/bar/MyClass.java"],
6995 sdk_version: "none",
6996 system_modules: "none",
6997 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006998 apex_available: [
6999 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007000 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007001 "//apex_available:platform",
7002 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007003 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007004 }
7005
7006 java_library {
7007 name: "myotherjar",
7008 srcs: ["foo/bar/MyClass.java"],
7009 sdk_version: "none",
7010 system_modules: "none",
7011 apex_available: [
7012 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007013 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007014 "//apex_available:platform",
7015 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007016 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007017 }
7018 `
7019
7020 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7021 for _, f := range files {
7022 if f.path == file {
7023 if f.isLink {
7024 t.Errorf("%q is not a real file", file)
7025 }
7026 return
7027 }
7028 }
7029 t.Errorf("%q is not found", file)
7030 }
7031
7032 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
7033 for _, f := range files {
7034 if f.path == file {
7035 if !f.isLink {
7036 t.Errorf("%q is not a symlink", file)
7037 }
7038 return
7039 }
7040 }
7041 t.Errorf("%q is not found", file)
7042 }
7043
Jiyong Park9d677202020-02-19 16:29:35 +09007044 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7045 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007046 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007047 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007048 ensureRealfileExists(t, files, "javalib/myjar.jar")
7049 ensureRealfileExists(t, files, "lib64/mylib.so")
7050 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7051
Jiyong Park9d677202020-02-19 16:29:35 +09007052 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7053 ensureRealfileExists(t, files, "javalib/myjar.jar")
7054 ensureRealfileExists(t, files, "lib64/mylib.so")
7055 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7056
7057 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007058 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007059 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007060 ensureRealfileExists(t, files, "javalib/myjar.jar")
7061 ensureRealfileExists(t, files, "lib64/mylib.so")
7062 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007063
7064 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7065 ensureRealfileExists(t, files, "javalib/myjar.jar")
7066 ensureRealfileExists(t, files, "lib64/mylib.so")
7067 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007068}
7069
Yo Chiange8128052020-07-23 20:09:18 +08007070func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007071 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007072 apex {
7073 name: "myapex",
7074 key: "myapex.key",
7075 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007076 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007077 }
7078
7079 apex_key {
7080 name: "myapex.key",
7081 public_key: "testkey.avbpubkey",
7082 private_key: "testkey.pem",
7083 }
7084
7085 cc_library_shared {
7086 name: "mylib",
7087 srcs: ["mylib.cpp"],
7088 shared_libs: ["myotherlib"],
7089 system_shared_libs: [],
7090 stl: "none",
7091 apex_available: [
7092 "myapex",
7093 "//apex_available:platform",
7094 ],
7095 }
7096
7097 cc_prebuilt_library_shared {
7098 name: "myotherlib",
7099 srcs: ["prebuilt.so"],
7100 system_shared_libs: [],
7101 stl: "none",
7102 apex_available: [
7103 "myapex",
7104 "//apex_available:platform",
7105 ],
7106 }
7107 `)
7108
7109 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007110 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007111 var builder strings.Builder
7112 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7113 androidMk := builder.String()
7114 // `myotherlib` is added to `myapex` as symlink
7115 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
7116 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7117 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7118 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09007119 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 +08007120}
7121
Jooyung Han643adc42020-02-27 13:50:06 +09007122func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007123 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007124 apex {
7125 name: "myapex",
7126 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007127 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007128 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007129 }
7130
7131 apex_key {
7132 name: "myapex.key",
7133 public_key: "testkey.avbpubkey",
7134 private_key: "testkey.pem",
7135 }
7136
7137 cc_library {
7138 name: "mylib",
7139 srcs: ["mylib.cpp"],
7140 shared_libs: ["mylib2"],
7141 system_shared_libs: [],
7142 stl: "none",
7143 apex_available: [ "myapex" ],
7144 }
7145
7146 cc_library {
7147 name: "mylib2",
7148 srcs: ["mylib.cpp"],
7149 system_shared_libs: [],
7150 stl: "none",
7151 apex_available: [ "myapex" ],
7152 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007153
7154 rust_ffi_shared {
7155 name: "libfoo.rust",
7156 crate_name: "foo",
7157 srcs: ["foo.rs"],
7158 shared_libs: ["libfoo.shared_from_rust"],
7159 prefer_rlib: true,
7160 apex_available: ["myapex"],
7161 }
7162
7163 cc_library_shared {
7164 name: "libfoo.shared_from_rust",
7165 srcs: ["mylib.cpp"],
7166 system_shared_libs: [],
7167 stl: "none",
7168 stubs: {
7169 versions: ["10", "11", "12"],
7170 },
7171 }
7172
Jooyung Han643adc42020-02-27 13:50:06 +09007173 `)
7174
7175 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7176 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007177 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007178 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7179 "lib64/mylib.so",
7180 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007181 "lib64/libfoo.rust.so",
7182 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7183 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007184 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007185
7186 // b/220397949
7187 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007188}
7189
Jooyung Han49f67012020-04-17 13:43:10 +09007190func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007191 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007192 apex {
7193 name: "myapex",
7194 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007195 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007196 }
7197 apex_key {
7198 name: "myapex.key",
7199 public_key: "testkey.avbpubkey",
7200 private_key: "testkey.pem",
7201 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007202 `,
7203 android.FixtureModifyConfig(func(config android.Config) {
7204 delete(config.Targets, android.Android)
7205 config.AndroidCommonTarget = android.Target{}
7206 }),
7207 )
Jooyung Han49f67012020-04-17 13:43:10 +09007208
7209 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7210 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7211 }
7212}
7213
Jiyong Parkbd159612020-02-28 15:22:21 +09007214func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007215 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007216 apex {
7217 name: "myapex",
7218 key: "myapex.key",
7219 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007220 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007221 }
7222
7223 apex_key {
7224 name: "myapex.key",
7225 public_key: "testkey.avbpubkey",
7226 private_key: "testkey.pem",
7227 }
7228
7229 android_app {
7230 name: "AppFoo",
7231 srcs: ["foo/bar/MyClass.java"],
7232 sdk_version: "none",
7233 system_modules: "none",
7234 apex_available: [ "myapex" ],
7235 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007236 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007237
Colin Crosscf371cc2020-11-13 11:48:42 -08007238 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007239 content := bundleConfigRule.Args["content"]
7240
7241 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007242 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 +09007243}
7244
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007245func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007246 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007247 apex {
7248 name: "myapex",
7249 key: "myapex.key",
7250 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007251 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007252 }
7253
7254 apex_key {
7255 name: "myapex.key",
7256 public_key: "testkey.avbpubkey",
7257 private_key: "testkey.pem",
7258 }
7259
7260 android_app_set {
7261 name: "AppSet",
7262 set: "AppSet.apks",
7263 }`)
7264 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007265 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007266 content := bundleConfigRule.Args["content"]
7267 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7268 s := mod.Rule("apexRule").Args["copy_commands"]
7269 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007270 if len(copyCmds) != 4 {
7271 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007272 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007273 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7274 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007275 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7276 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007277
7278 // Ensure that canned_fs_config has an entry for the app set zip file
7279 generateFsRule := mod.Rule("generateFsConfig")
7280 cmd := generateFsRule.RuleParams.Command
7281 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007282}
7283
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007284func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007285 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007286 apex_set {
7287 name: "myapex",
7288 filename: "foo_v2.apex",
7289 sanitized: {
7290 none: { set: "myapex.apks", },
7291 hwaddress: { set: "myapex.hwasan.apks", },
7292 },
Paul Duffin24704672021-04-06 16:09:30 +01007293 }
7294 `
7295 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007296
Paul Duffin24704672021-04-06 16:09:30 +01007297 // Check that the extractor produces the correct output file from the correct input file.
7298 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007299
Paul Duffin24704672021-04-06 16:09:30 +01007300 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7301 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007302
Paul Duffin24704672021-04-06 16:09:30 +01007303 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7304
7305 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007306 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7307 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007308
7309 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007310}
7311
Pranav Guptaeba03b02022-09-27 00:27:08 +00007312func TestApexSetApksModuleAssignment(t *testing.T) {
7313 ctx := testApex(t, `
7314 apex_set {
7315 name: "myapex",
7316 set: ":myapex_apks_file",
7317 }
7318
7319 filegroup {
7320 name: "myapex_apks_file",
7321 srcs: ["myapex.apks"],
7322 }
7323 `)
7324
7325 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7326
7327 // Check that the extractor produces the correct apks file from the input module
7328 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7329 extractedApex := m.Output(extractorOutput)
7330
7331 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7332}
7333
Paul Duffin89f570a2021-06-16 01:42:33 +01007334func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007335 t.Helper()
7336
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007337 bp := `
7338 java_library {
7339 name: "some-updatable-apex-lib",
7340 srcs: ["a.java"],
7341 sdk_version: "current",
7342 apex_available: [
7343 "some-updatable-apex",
7344 ],
satayevabcd5972021-08-06 17:49:46 +01007345 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007346 }
7347
7348 java_library {
7349 name: "some-non-updatable-apex-lib",
7350 srcs: ["a.java"],
7351 apex_available: [
7352 "some-non-updatable-apex",
7353 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007354 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007355 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007356 }
7357
7358 bootclasspath_fragment {
7359 name: "some-non-updatable-fragment",
7360 contents: ["some-non-updatable-apex-lib"],
7361 apex_available: [
7362 "some-non-updatable-apex",
7363 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007364 hidden_api: {
7365 split_packages: ["*"],
7366 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007367 }
7368
7369 java_library {
7370 name: "some-platform-lib",
7371 srcs: ["a.java"],
7372 sdk_version: "current",
7373 installable: true,
7374 }
7375
7376 java_library {
7377 name: "some-art-lib",
7378 srcs: ["a.java"],
7379 sdk_version: "current",
7380 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007381 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007382 ],
7383 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007384 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007385 }
7386
7387 apex {
7388 name: "some-updatable-apex",
7389 key: "some-updatable-apex.key",
7390 java_libs: ["some-updatable-apex-lib"],
7391 updatable: true,
7392 min_sdk_version: "current",
7393 }
7394
7395 apex {
7396 name: "some-non-updatable-apex",
7397 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007398 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007399 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007400 }
7401
7402 apex_key {
7403 name: "some-updatable-apex.key",
7404 }
7405
7406 apex_key {
7407 name: "some-non-updatable-apex.key",
7408 }
7409
7410 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007411 name: "com.android.art.debug",
7412 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007413 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007414 updatable: true,
7415 min_sdk_version: "current",
7416 }
7417
Paul Duffinf23bc472021-04-27 12:42:20 +01007418 bootclasspath_fragment {
7419 name: "art-bootclasspath-fragment",
7420 image_name: "art",
7421 contents: ["some-art-lib"],
7422 apex_available: [
7423 "com.android.art.debug",
7424 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007425 hidden_api: {
7426 split_packages: ["*"],
7427 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007428 }
7429
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007430 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007431 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007432 }
7433
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007434 filegroup {
7435 name: "some-updatable-apex-file_contexts",
7436 srcs: [
7437 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7438 ],
7439 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007440
7441 filegroup {
7442 name: "some-non-updatable-apex-file_contexts",
7443 srcs: [
7444 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7445 ],
7446 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007447 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007448
Paul Duffin89f570a2021-06-16 01:42:33 +01007449 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007450}
7451
Paul Duffin89f570a2021-06-16 01:42:33 +01007452func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007453 t.Helper()
7454
Paul Duffin55607122021-03-30 23:32:51 +01007455 fs := android.MockFS{
7456 "a.java": nil,
7457 "a.jar": nil,
7458 "apex_manifest.json": nil,
7459 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007460 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007461 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7462 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7463 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007464 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007465 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007466
Paul Duffin55607122021-03-30 23:32:51 +01007467 errorHandler := android.FixtureExpectsNoErrors
7468 if errmsg != "" {
7469 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007470 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007471
Paul Duffin55607122021-03-30 23:32:51 +01007472 result := android.GroupFixturePreparers(
7473 cc.PrepareForTestWithCcDefaultModules,
7474 java.PrepareForTestWithHiddenApiBuildComponents,
7475 java.PrepareForTestWithJavaDefaultModules,
7476 java.PrepareForTestWithJavaSdkLibraryFiles,
7477 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007478 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007479 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007480 android.FixtureModifyMockFS(func(fs android.MockFS) {
7481 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7482 insert := ""
7483 for _, fragment := range fragments {
7484 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7485 }
7486 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7487 platform_bootclasspath {
7488 name: "platform-bootclasspath",
7489 fragments: [
7490 %s
7491 ],
7492 }
7493 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007494 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007495 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007496 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007497 ).
7498 ExtendWithErrorHandler(errorHandler).
7499 RunTestWithBp(t, bp)
7500
7501 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007502}
7503
Paul Duffin5556c5f2022-06-09 17:32:21 +00007504func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007505 preparers := android.GroupFixturePreparers(
7506 java.PrepareForTestWithJavaDefaultModules,
7507 PrepareForTestWithApexBuildComponents,
7508 ).
7509 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7510 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7511
7512 bpBase := `
7513 apex_set {
7514 name: "com.android.myapex",
7515 installable: true,
7516 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7517 set: "myapex.apks",
7518 }
7519
7520 apex_set {
7521 name: "com.mycompany.android.myapex",
7522 apex_name: "com.android.myapex",
7523 installable: true,
7524 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7525 set: "company-myapex.apks",
7526 }
7527
7528 prebuilt_bootclasspath_fragment {
7529 name: "my-bootclasspath-fragment",
7530 apex_available: ["com.android.myapex"],
7531 %s
7532 }
7533 `
7534
7535 t.Run("java_import", func(t *testing.T) {
7536 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7537 java_import {
7538 name: "libfoo",
7539 jars: ["libfoo.jar"],
7540 apex_available: ["com.android.myapex"],
7541 }
7542 `)
7543 })
7544
7545 t.Run("java_sdk_library_import", func(t *testing.T) {
7546 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7547 java_sdk_library_import {
7548 name: "libfoo",
7549 public: {
7550 jars: ["libbar.jar"],
7551 },
7552 apex_available: ["com.android.myapex"],
7553 }
7554 `)
7555 })
7556
7557 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7558 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7559 image_name: "art",
7560 contents: ["libfoo"],
7561 `)+`
7562 java_sdk_library_import {
7563 name: "libfoo",
7564 public: {
7565 jars: ["libbar.jar"],
7566 },
7567 apex_available: ["com.android.myapex"],
7568 }
7569 `)
7570 })
7571}
7572
Paul Duffin5556c5f2022-06-09 17:32:21 +00007573func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7574 preparers := android.GroupFixturePreparers(
7575 java.PrepareForTestWithJavaDefaultModules,
7576 PrepareForTestWithApexBuildComponents,
7577 )
7578
7579 bpBase := `
7580 apex_set {
7581 name: "com.android.myapex",
7582 installable: true,
7583 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7584 set: "myapex.apks",
7585 }
7586
7587 apex_set {
7588 name: "com.android.myapex_compressed",
7589 apex_name: "com.android.myapex",
7590 installable: true,
7591 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7592 set: "myapex_compressed.apks",
7593 }
7594
7595 prebuilt_bootclasspath_fragment {
7596 name: "my-bootclasspath-fragment",
7597 apex_available: [
7598 "com.android.myapex",
7599 "com.android.myapex_compressed",
7600 ],
7601 hidden_api: {
7602 annotation_flags: "annotation-flags.csv",
7603 metadata: "metadata.csv",
7604 index: "index.csv",
7605 signature_patterns: "signature_patterns.csv",
7606 },
7607 %s
7608 }
7609 `
7610
7611 t.Run("java_import", func(t *testing.T) {
7612 result := preparers.RunTestWithBp(t,
7613 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7614 java_import {
7615 name: "libfoo",
7616 jars: ["libfoo.jar"],
7617 apex_available: [
7618 "com.android.myapex",
7619 "com.android.myapex_compressed",
7620 ],
7621 }
7622 `)
7623
7624 module := result.Module("libfoo", "android_common_com.android.myapex")
7625 usesLibraryDep := module.(java.UsesLibraryDependency)
7626 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7627 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7628 usesLibraryDep.DexJarBuildPath().Path())
7629 })
7630
7631 t.Run("java_sdk_library_import", func(t *testing.T) {
7632 result := preparers.RunTestWithBp(t,
7633 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7634 java_sdk_library_import {
7635 name: "libfoo",
7636 public: {
7637 jars: ["libbar.jar"],
7638 },
7639 apex_available: [
7640 "com.android.myapex",
7641 "com.android.myapex_compressed",
7642 ],
7643 compile_dex: true,
7644 }
7645 `)
7646
7647 module := result.Module("libfoo", "android_common_com.android.myapex")
7648 usesLibraryDep := module.(java.UsesLibraryDependency)
7649 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7650 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7651 usesLibraryDep.DexJarBuildPath().Path())
7652 })
7653
7654 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7655 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7656 image_name: "art",
7657 contents: ["libfoo"],
7658 `)+`
7659 java_sdk_library_import {
7660 name: "libfoo",
7661 public: {
7662 jars: ["libbar.jar"],
7663 },
7664 apex_available: [
7665 "com.android.myapex",
7666 "com.android.myapex_compressed",
7667 ],
7668 compile_dex: true,
7669 }
7670 `)
7671 })
7672}
7673
Jooyung Han548640b2020-04-27 12:10:30 +09007674func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7675 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7676 apex {
7677 name: "myapex",
7678 key: "myapex.key",
7679 updatable: true,
7680 }
7681
7682 apex_key {
7683 name: "myapex.key",
7684 public_key: "testkey.avbpubkey",
7685 private_key: "testkey.pem",
7686 }
7687 `)
7688}
7689
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007690func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7691 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7692 apex {
7693 name: "myapex",
7694 key: "myapex.key",
7695 }
7696
7697 apex_key {
7698 name: "myapex.key",
7699 public_key: "testkey.avbpubkey",
7700 private_key: "testkey.pem",
7701 }
7702 `)
7703}
7704
Daniel Norman69109112021-12-02 12:52:42 -08007705func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7706 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7707 apex {
7708 name: "myapex",
7709 key: "myapex.key",
7710 updatable: true,
7711 soc_specific: true,
7712 }
7713
7714 apex_key {
7715 name: "myapex.key",
7716 public_key: "testkey.avbpubkey",
7717 private_key: "testkey.pem",
7718 }
7719 `)
7720}
7721
satayevb98371c2021-06-15 16:49:50 +01007722func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7723 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7724 apex {
7725 name: "myapex",
7726 key: "myapex.key",
7727 systemserverclasspath_fragments: [
7728 "mysystemserverclasspathfragment",
7729 ],
7730 min_sdk_version: "29",
7731 updatable: true,
7732 }
7733
7734 apex_key {
7735 name: "myapex.key",
7736 public_key: "testkey.avbpubkey",
7737 private_key: "testkey.pem",
7738 }
7739
7740 java_library {
7741 name: "foo",
7742 srcs: ["b.java"],
7743 min_sdk_version: "29",
7744 installable: true,
7745 apex_available: [
7746 "myapex",
7747 ],
7748 }
7749
7750 systemserverclasspath_fragment {
7751 name: "mysystemserverclasspathfragment",
7752 generate_classpaths_proto: false,
7753 contents: [
7754 "foo",
7755 ],
7756 apex_available: [
7757 "myapex",
7758 ],
7759 }
satayevabcd5972021-08-06 17:49:46 +01007760 `,
7761 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7762 )
satayevb98371c2021-06-15 16:49:50 +01007763}
7764
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007765func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007766 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7767 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7768 // modules to be included in the BootJars.
7769 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7770 return android.GroupFixturePreparers(
7771 dexpreopt.FixtureSetBootJars(bootJars...),
7772 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7773 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7774 }),
7775 )
7776 }
7777
7778 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7779 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7780 // specified in the ArtApexJars configuration.
7781 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7782 return android.GroupFixturePreparers(
7783 dexpreopt.FixtureSetArtBootJars(bootJars...),
7784 dexpreopt.FixtureSetBootJars(bootJars...),
7785 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7786 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7787 }),
7788 )
7789 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007790
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007791 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007792 preparer := android.GroupFixturePreparers(
7793 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7794 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7795 )
7796 fragments := []java.ApexVariantReference{
7797 {
7798 Apex: proptools.StringPtr("com.android.art.debug"),
7799 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7800 },
7801 {
7802 Apex: proptools.StringPtr("some-non-updatable-apex"),
7803 Module: proptools.StringPtr("some-non-updatable-fragment"),
7804 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007805 }
satayevabcd5972021-08-06 17:49:46 +01007806 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007807 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007808
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007809 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007810 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7811 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007812 preparer := android.GroupFixturePreparers(
7813 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7814 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7815 )
Paul Duffin60264a02021-04-12 20:02:36 +01007816 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007817 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007818
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007819 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 +01007820 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 +01007821 // Update the dexpreopt ArtApexJars directly.
7822 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7823 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007824 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007825
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007826 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 +01007827 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 +01007828 // Update the dexpreopt ArtApexJars directly.
7829 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7830 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007831 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007832
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007833 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 +01007834 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 +01007835 preparer := android.GroupFixturePreparers(
7836 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7837 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7838 )
Paul Duffin60264a02021-04-12 20:02:36 +01007839 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007840 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007841
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007842 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 +01007843 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007844 fragment := java.ApexVariantReference{
7845 Apex: proptools.StringPtr("some-non-updatable-apex"),
7846 Module: proptools.StringPtr("some-non-updatable-fragment"),
7847 }
7848 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007849 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007850
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007851 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007852 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007853 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7854 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007855 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007856
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007857 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007858 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007859 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7860 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007861 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007862
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007863 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007864 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007865 // Update the dexpreopt ArtApexJars directly.
7866 preparer := prepareSetArtJars("platform:some-platform-lib")
7867 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007868 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007869
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007870 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007871 preparer := android.GroupFixturePreparers(
7872 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7873 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7874 )
7875 fragments := []java.ApexVariantReference{
7876 {
7877 Apex: proptools.StringPtr("some-non-updatable-apex"),
7878 Module: proptools.StringPtr("some-non-updatable-fragment"),
7879 },
7880 }
7881 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007882 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007883}
7884
7885func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007886 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007887 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007888 fragment := java.ApexVariantReference{
7889 Apex: proptools.StringPtr("myapex"),
7890 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7891 }
7892
Paul Duffin064b70c2020-11-02 17:32:38 +00007893 testDexpreoptWithApexes(t, `
7894 prebuilt_apex {
7895 name: "myapex" ,
7896 arch: {
7897 arm64: {
7898 src: "myapex-arm64.apex",
7899 },
7900 arm: {
7901 src: "myapex-arm.apex",
7902 },
7903 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007904 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7905 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007906
Paul Duffin89f570a2021-06-16 01:42:33 +01007907 prebuilt_bootclasspath_fragment {
7908 name: "my-bootclasspath-fragment",
7909 contents: ["libfoo"],
7910 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007911 hidden_api: {
7912 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7913 metadata: "my-bootclasspath-fragment/metadata.csv",
7914 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01007915 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
7916 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
7917 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01007918 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007919 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007920
Paul Duffin89f570a2021-06-16 01:42:33 +01007921 java_import {
7922 name: "libfoo",
7923 jars: ["libfoo.jar"],
7924 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007925 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007926 }
7927 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007928 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007929}
7930
Spandan Dasf14e2542021-11-12 00:01:37 +00007931func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007932 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007933 bp += `
7934 apex_key {
7935 name: "myapex.key",
7936 public_key: "testkey.avbpubkey",
7937 private_key: "testkey.pem",
7938 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007939 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007940 "lib1/src/A.java": nil,
7941 "lib2/src/B.java": nil,
7942 "system/sepolicy/apex/myapex-file_contexts": nil,
7943 }
7944
Paul Duffin45338f02021-03-30 23:07:52 +01007945 errorHandler := android.FixtureExpectsNoErrors
7946 if errmsg != "" {
7947 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007948 }
Colin Crossae8600b2020-10-29 17:09:13 -07007949
Paul Duffin45338f02021-03-30 23:07:52 +01007950 android.GroupFixturePreparers(
7951 android.PrepareForTestWithAndroidBuildComponents,
7952 java.PrepareForTestWithJavaBuildComponents,
7953 PrepareForTestWithApexBuildComponents,
7954 android.PrepareForTestWithNeverallowRules(rules),
7955 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007956 apexBootJars := make([]string, 0, len(bootJars))
7957 for _, apexBootJar := range bootJars {
7958 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007959 }
satayevd604b212021-07-21 14:23:52 +01007960 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007961 }),
7962 fs.AddToFixture(),
7963 ).
7964 ExtendWithErrorHandler(errorHandler).
7965 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007966}
7967
7968func TestApexPermittedPackagesRules(t *testing.T) {
7969 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00007970 name string
7971 expectedError string
7972 bp string
7973 bootJars []string
7974 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01007975 }{
7976
7977 {
7978 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7979 expectedError: "",
7980 bp: `
7981 java_library {
7982 name: "bcp_lib1",
7983 srcs: ["lib1/src/*.java"],
7984 permitted_packages: ["foo.bar"],
7985 apex_available: ["myapex"],
7986 sdk_version: "none",
7987 system_modules: "none",
7988 }
7989 java_library {
7990 name: "nonbcp_lib2",
7991 srcs: ["lib2/src/*.java"],
7992 apex_available: ["myapex"],
7993 permitted_packages: ["a.b"],
7994 sdk_version: "none",
7995 system_modules: "none",
7996 }
7997 apex {
7998 name: "myapex",
7999 key: "myapex.key",
8000 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008001 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008002 }`,
8003 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008004 bcpPermittedPackages: map[string][]string{
8005 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008006 "foo.bar",
8007 },
8008 },
8009 },
8010 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008011 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008012 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 +01008013 bp: `
8014 java_library {
8015 name: "bcp_lib1",
8016 srcs: ["lib1/src/*.java"],
8017 apex_available: ["myapex"],
8018 permitted_packages: ["foo.bar"],
8019 sdk_version: "none",
8020 system_modules: "none",
8021 }
8022 java_library {
8023 name: "bcp_lib2",
8024 srcs: ["lib2/src/*.java"],
8025 apex_available: ["myapex"],
8026 permitted_packages: ["foo.bar", "bar.baz"],
8027 sdk_version: "none",
8028 system_modules: "none",
8029 }
8030 apex {
8031 name: "myapex",
8032 key: "myapex.key",
8033 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008034 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008035 }
8036 `,
8037 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008038 bcpPermittedPackages: map[string][]string{
8039 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008040 "foo.bar",
8041 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008042 "bcp_lib2": []string{
8043 "foo.bar",
8044 },
8045 },
8046 },
8047 {
8048 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8049 expectedError: "",
8050 bp: `
8051 java_library {
8052 name: "bcp_lib_restricted",
8053 srcs: ["lib1/src/*.java"],
8054 apex_available: ["myapex"],
8055 permitted_packages: ["foo.bar"],
8056 sdk_version: "none",
8057 min_sdk_version: "29",
8058 system_modules: "none",
8059 }
8060 java_library {
8061 name: "bcp_lib_unrestricted",
8062 srcs: ["lib2/src/*.java"],
8063 apex_available: ["myapex"],
8064 permitted_packages: ["foo.bar", "bar.baz"],
8065 sdk_version: "none",
8066 min_sdk_version: "29",
8067 system_modules: "none",
8068 }
8069 apex {
8070 name: "myapex",
8071 key: "myapex.key",
8072 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8073 updatable: true,
8074 min_sdk_version: "29",
8075 }
8076 `,
8077 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8078 bcpPermittedPackages: map[string][]string{
8079 "bcp_lib1_non_updateable": []string{
8080 "foo.bar",
8081 },
8082 // 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 +01008083 },
8084 },
8085 }
8086 for _, tc := range testcases {
8087 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008088 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8089 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008090 })
8091 }
8092}
8093
Jiyong Park62304bb2020-04-13 16:19:48 +09008094func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008095 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008096 apex {
8097 name: "myapex",
8098 key: "myapex.key",
8099 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008100 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008101 }
8102
8103 apex_key {
8104 name: "myapex.key",
8105 public_key: "testkey.avbpubkey",
8106 private_key: "testkey.pem",
8107 }
8108
8109 cc_library {
8110 name: "mylib",
8111 srcs: ["mylib.cpp"],
8112 system_shared_libs: [],
8113 stl: "none",
8114 stubs: {
8115 versions: ["1"],
8116 },
8117 apex_available: ["myapex"],
8118 }
8119
8120 cc_library {
8121 name: "myprivlib",
8122 srcs: ["mylib.cpp"],
8123 system_shared_libs: [],
8124 stl: "none",
8125 apex_available: ["myapex"],
8126 }
8127
8128
8129 cc_test {
8130 name: "mytest",
8131 gtest: false,
8132 srcs: ["mylib.cpp"],
8133 system_shared_libs: [],
8134 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008135 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008136 test_for: ["myapex"]
8137 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008138
8139 cc_library {
8140 name: "mytestlib",
8141 srcs: ["mylib.cpp"],
8142 system_shared_libs: [],
8143 shared_libs: ["mylib", "myprivlib"],
8144 stl: "none",
8145 test_for: ["myapex"],
8146 }
8147
8148 cc_benchmark {
8149 name: "mybench",
8150 srcs: ["mylib.cpp"],
8151 system_shared_libs: [],
8152 shared_libs: ["mylib", "myprivlib"],
8153 stl: "none",
8154 test_for: ["myapex"],
8155 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008156 `)
8157
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008158 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008159 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008160 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8161 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8162 }
8163
8164 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008165 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008166 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8167 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8168 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8169}
Jiyong Park46a512f2020-12-04 18:02:13 +09008170
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008171func TestIndirectTestFor(t *testing.T) {
8172 ctx := testApex(t, `
8173 apex {
8174 name: "myapex",
8175 key: "myapex.key",
8176 native_shared_libs: ["mylib", "myprivlib"],
8177 updatable: false,
8178 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008179
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008180 apex_key {
8181 name: "myapex.key",
8182 public_key: "testkey.avbpubkey",
8183 private_key: "testkey.pem",
8184 }
8185
8186 cc_library {
8187 name: "mylib",
8188 srcs: ["mylib.cpp"],
8189 system_shared_libs: [],
8190 stl: "none",
8191 stubs: {
8192 versions: ["1"],
8193 },
8194 apex_available: ["myapex"],
8195 }
8196
8197 cc_library {
8198 name: "myprivlib",
8199 srcs: ["mylib.cpp"],
8200 system_shared_libs: [],
8201 stl: "none",
8202 shared_libs: ["mylib"],
8203 apex_available: ["myapex"],
8204 }
8205
8206 cc_library {
8207 name: "mytestlib",
8208 srcs: ["mylib.cpp"],
8209 system_shared_libs: [],
8210 shared_libs: ["myprivlib"],
8211 stl: "none",
8212 test_for: ["myapex"],
8213 }
8214 `)
8215
8216 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008217 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008218 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8219 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8220 }
8221
8222 // The platform variant of mytestlib links to the platform variant of the
8223 // internal myprivlib.
8224 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8225
8226 // The platform variant of myprivlib links to the platform variant of mylib
8227 // and bypasses its stubs.
8228 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 +09008229}
8230
Martin Stjernholmec009002021-03-27 15:18:31 +00008231func TestTestForForLibInOtherApex(t *testing.T) {
8232 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8233 _ = testApex(t, `
8234 apex {
8235 name: "com.android.art",
8236 key: "myapex.key",
8237 native_shared_libs: ["mylib"],
8238 updatable: false,
8239 }
8240
8241 apex {
8242 name: "com.android.art.debug",
8243 key: "myapex.key",
8244 native_shared_libs: ["mylib", "mytestlib"],
8245 updatable: false,
8246 }
8247
8248 apex_key {
8249 name: "myapex.key",
8250 public_key: "testkey.avbpubkey",
8251 private_key: "testkey.pem",
8252 }
8253
8254 cc_library {
8255 name: "mylib",
8256 srcs: ["mylib.cpp"],
8257 system_shared_libs: [],
8258 stl: "none",
8259 stubs: {
8260 versions: ["1"],
8261 },
8262 apex_available: ["com.android.art", "com.android.art.debug"],
8263 }
8264
8265 cc_library {
8266 name: "mytestlib",
8267 srcs: ["mylib.cpp"],
8268 system_shared_libs: [],
8269 shared_libs: ["mylib"],
8270 stl: "none",
8271 apex_available: ["com.android.art.debug"],
8272 test_for: ["com.android.art"],
8273 }
8274 `,
8275 android.MockFS{
8276 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8277 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8278 }.AddToFixture())
8279}
8280
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008281// TODO(jungjw): Move this to proptools
8282func intPtr(i int) *int {
8283 return &i
8284}
8285
8286func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008287 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008288 apex_set {
8289 name: "myapex",
8290 set: "myapex.apks",
8291 filename: "foo_v2.apex",
8292 overrides: ["foo"],
8293 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008294 `,
8295 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8296 variables.Platform_sdk_version = intPtr(30)
8297 }),
8298 android.FixtureModifyConfig(func(config android.Config) {
8299 config.Targets[android.Android] = []android.Target{
8300 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8301 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8302 }
8303 }),
8304 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008305
Paul Duffin24704672021-04-06 16:09:30 +01008306 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008307
8308 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008309 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008310 actual := extractedApex.Args["abis"]
8311 expected := "ARMEABI_V7A,ARM64_V8A"
8312 if actual != expected {
8313 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8314 }
8315 actual = extractedApex.Args["sdk-version"]
8316 expected = "30"
8317 if actual != expected {
8318 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8319 }
8320
Paul Duffin6717d882021-06-15 19:09:41 +01008321 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008322 a := m.Module().(*ApexSet)
8323 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008324 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008325 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8326 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8327 }
8328}
8329
Jiyong Park7d95a512020-05-10 15:16:24 +09008330func TestNoStaticLinkingToStubsLib(t *testing.T) {
8331 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8332 apex {
8333 name: "myapex",
8334 key: "myapex.key",
8335 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008336 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008337 }
8338
8339 apex_key {
8340 name: "myapex.key",
8341 public_key: "testkey.avbpubkey",
8342 private_key: "testkey.pem",
8343 }
8344
8345 cc_library {
8346 name: "mylib",
8347 srcs: ["mylib.cpp"],
8348 static_libs: ["otherlib"],
8349 system_shared_libs: [],
8350 stl: "none",
8351 apex_available: [ "myapex" ],
8352 }
8353
8354 cc_library {
8355 name: "otherlib",
8356 srcs: ["mylib.cpp"],
8357 system_shared_libs: [],
8358 stl: "none",
8359 stubs: {
8360 versions: ["1", "2", "3"],
8361 },
8362 apex_available: [ "myapex" ],
8363 }
8364 `)
8365}
8366
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008367func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008368 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008369 apex {
8370 name: "myapex",
8371 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008372 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008373 custom_sign_tool: "sign_myapex",
8374 }
8375
8376 apex_key {
8377 name: "myapex.key",
8378 public_key: "testkey.avbpubkey",
8379 private_key: "testkey.pem",
8380 }
8381 `)
8382
8383 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8384 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8385 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"`)
8386}
8387
8388func TestApexKeysTxtOverrides(t *testing.T) {
8389 ctx := testApex(t, `
8390 apex {
8391 name: "myapex",
8392 key: "myapex.key",
8393 updatable: false,
8394 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008395 }
8396
8397 apex_key {
8398 name: "myapex.key",
8399 public_key: "testkey.avbpubkey",
8400 private_key: "testkey.pem",
8401 }
8402
8403 prebuilt_apex {
8404 name: "myapex",
8405 prefer: true,
8406 arch: {
8407 arm64: {
8408 src: "myapex-arm64.apex",
8409 },
8410 arm: {
8411 src: "myapex-arm.apex",
8412 },
8413 },
8414 }
8415
8416 apex_set {
8417 name: "myapex_set",
8418 set: "myapex.apks",
8419 filename: "myapex_set.apex",
8420 overrides: ["myapex"],
8421 }
8422 `)
8423
8424 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8425 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8426 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 +09008427 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 +09008428}
8429
Jooyung Han938b5932020-06-20 12:47:47 +09008430func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008431 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008432 apex {
8433 name: "myapex",
8434 key: "myapex.key",
8435 apps: ["app"],
8436 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008437 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008438 }
8439
8440 apex_key {
8441 name: "myapex.key",
8442 public_key: "testkey.avbpubkey",
8443 private_key: "testkey.pem",
8444 }
8445
8446 android_app {
8447 name: "app",
8448 srcs: ["foo/bar/MyClass.java"],
8449 package_name: "foo",
8450 sdk_version: "none",
8451 system_modules: "none",
8452 apex_available: [ "myapex" ],
8453 }
8454 `, withFiles(map[string][]byte{
8455 "sub/Android.bp": []byte(`
8456 override_apex {
8457 name: "override_myapex",
8458 base: "myapex",
8459 apps: ["override_app"],
8460 allowed_files: ":allowed",
8461 }
8462 // Overridable "path" property should be referenced indirectly
8463 filegroup {
8464 name: "allowed",
8465 srcs: ["allowed.txt"],
8466 }
8467 override_android_app {
8468 name: "override_app",
8469 base: "app",
8470 package_name: "bar",
8471 }
8472 `),
8473 }))
8474
8475 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8476 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8477 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8478 }
8479
8480 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8481 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8482 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8483 }
8484}
8485
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008486func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008487 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008488 apex {
8489 name: "myapex",
8490 key: "myapex.key",
8491 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008492 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008493 }
8494
8495 apex_key {
8496 name: "myapex.key",
8497 public_key: "testkey.avbpubkey",
8498 private_key: "testkey.pem",
8499 }
8500
8501 cc_library {
8502 name: "mylib",
8503 srcs: ["mylib.cpp"],
8504 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008505 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008506 },
8507 apex_available: ["myapex"],
8508 }
8509
8510 cc_prebuilt_library_shared {
8511 name: "mylib",
8512 prefer: false,
8513 srcs: ["prebuilt.so"],
8514 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008515 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008516 },
8517 apex_available: ["myapex"],
8518 }
8519 `)
8520}
8521
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008522func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008523 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008524 apex {
8525 name: "myapex",
8526 key: "myapex.key",
8527 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008528 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008529 }
8530 apex_key {
8531 name: "myapex.key",
8532 public_key: "testkey.avbpubkey",
8533 private_key: "testkey.pem",
8534 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008535 `,
8536 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8537 variables.CompressedApex = proptools.BoolPtr(true)
8538 }),
8539 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008540
8541 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8542 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8543
8544 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8545 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8546
8547 // Make sure output of bundle is .capex
8548 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8549 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8550
8551 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008552 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008553 var builder strings.Builder
8554 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8555 androidMk := builder.String()
8556 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8557}
8558
Martin Stjernholm2856c662020-12-02 15:03:42 +00008559func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008560 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008561 apex {
8562 name: "myapex",
8563 key: "myapex.key",
8564 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008565 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008566 }
8567
8568 apex_key {
8569 name: "myapex.key",
8570 public_key: "testkey.avbpubkey",
8571 private_key: "testkey.pem",
8572 }
8573
8574 cc_library {
8575 name: "mylib",
8576 srcs: ["mylib.cpp"],
8577 apex_available: ["myapex"],
8578 shared_libs: ["otherlib"],
8579 system_shared_libs: [],
8580 }
8581
8582 cc_library {
8583 name: "otherlib",
8584 srcs: ["mylib.cpp"],
8585 stubs: {
8586 versions: ["current"],
8587 },
8588 }
8589
8590 cc_prebuilt_library_shared {
8591 name: "otherlib",
8592 prefer: true,
8593 srcs: ["prebuilt.so"],
8594 stubs: {
8595 versions: ["current"],
8596 },
8597 }
8598 `)
8599
8600 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008601 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008602 var builder strings.Builder
8603 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8604 androidMk := builder.String()
8605
8606 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8607 // a thing there.
8608 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
8609}
8610
Jiyong Parke3867542020-12-03 17:28:25 +09008611func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008612 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008613 apex {
8614 name: "myapex",
8615 key: "myapex.key",
8616 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008617 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008618 }
8619
8620 apex_key {
8621 name: "myapex.key",
8622 public_key: "testkey.avbpubkey",
8623 private_key: "testkey.pem",
8624 }
8625
8626 cc_library {
8627 name: "mylib",
8628 srcs: ["mylib.cpp"],
8629 system_shared_libs: [],
8630 stl: "none",
8631 apex_available: ["myapex"],
8632 shared_libs: ["mylib2"],
8633 target: {
8634 apex: {
8635 exclude_shared_libs: ["mylib2"],
8636 },
8637 },
8638 }
8639
8640 cc_library {
8641 name: "mylib2",
8642 srcs: ["mylib.cpp"],
8643 system_shared_libs: [],
8644 stl: "none",
8645 }
8646 `)
8647
8648 // Check if mylib is linked to mylib2 for the non-apex target
8649 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8650 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8651
8652 // Make sure that the link doesn't occur for the apex target
8653 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8654 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8655
8656 // It shouldn't appear in the copy cmd as well.
8657 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8658 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8659}
8660
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008661func TestPrebuiltStubLibDep(t *testing.T) {
8662 bpBase := `
8663 apex {
8664 name: "myapex",
8665 key: "myapex.key",
8666 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008667 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008668 }
8669 apex_key {
8670 name: "myapex.key",
8671 public_key: "testkey.avbpubkey",
8672 private_key: "testkey.pem",
8673 }
8674 cc_library {
8675 name: "mylib",
8676 srcs: ["mylib.cpp"],
8677 apex_available: ["myapex"],
8678 shared_libs: ["stublib"],
8679 system_shared_libs: [],
8680 }
8681 apex {
8682 name: "otherapex",
8683 enabled: %s,
8684 key: "myapex.key",
8685 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008686 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008687 }
8688 `
8689
8690 stublibSourceBp := `
8691 cc_library {
8692 name: "stublib",
8693 srcs: ["mylib.cpp"],
8694 apex_available: ["otherapex"],
8695 system_shared_libs: [],
8696 stl: "none",
8697 stubs: {
8698 versions: ["1"],
8699 },
8700 }
8701 `
8702
8703 stublibPrebuiltBp := `
8704 cc_prebuilt_library_shared {
8705 name: "stublib",
8706 srcs: ["prebuilt.so"],
8707 apex_available: ["otherapex"],
8708 stubs: {
8709 versions: ["1"],
8710 },
8711 %s
8712 }
8713 `
8714
8715 tests := []struct {
8716 name string
8717 stublibBp string
8718 usePrebuilt bool
8719 modNames []string // Modules to collect AndroidMkEntries for
8720 otherApexEnabled []string
8721 }{
8722 {
8723 name: "only_source",
8724 stublibBp: stublibSourceBp,
8725 usePrebuilt: false,
8726 modNames: []string{"stublib"},
8727 otherApexEnabled: []string{"true", "false"},
8728 },
8729 {
8730 name: "source_preferred",
8731 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8732 usePrebuilt: false,
8733 modNames: []string{"stublib", "prebuilt_stublib"},
8734 otherApexEnabled: []string{"true", "false"},
8735 },
8736 {
8737 name: "prebuilt_preferred",
8738 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8739 usePrebuilt: true,
8740 modNames: []string{"stublib", "prebuilt_stublib"},
8741 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8742 },
8743 {
8744 name: "only_prebuilt",
8745 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8746 usePrebuilt: true,
8747 modNames: []string{"stublib"},
8748 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8749 },
8750 }
8751
8752 for _, test := range tests {
8753 t.Run(test.name, func(t *testing.T) {
8754 for _, otherApexEnabled := range test.otherApexEnabled {
8755 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008756 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008757
8758 type modAndMkEntries struct {
8759 mod *cc.Module
8760 mkEntries android.AndroidMkEntries
8761 }
8762 entries := []*modAndMkEntries{}
8763
8764 // Gather shared lib modules that are installable
8765 for _, modName := range test.modNames {
8766 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8767 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8768 continue
8769 }
8770 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008771 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008772 continue
8773 }
Colin Crossaa255532020-07-03 13:18:24 -07008774 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008775 if ent.Disabled {
8776 continue
8777 }
8778 entries = append(entries, &modAndMkEntries{
8779 mod: mod,
8780 mkEntries: ent,
8781 })
8782 }
8783 }
8784 }
8785
8786 var entry *modAndMkEntries = nil
8787 for _, ent := range entries {
8788 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8789 if entry != nil {
8790 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8791 } else {
8792 entry = ent
8793 }
8794 }
8795 }
8796
8797 if entry == nil {
8798 t.Errorf("AndroidMk entry for \"stublib\" missing")
8799 } else {
8800 isPrebuilt := entry.mod.Prebuilt() != nil
8801 if isPrebuilt != test.usePrebuilt {
8802 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8803 }
8804 if !entry.mod.IsStubs() {
8805 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8806 }
8807 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8808 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8809 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008810 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008811 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008812 if !android.InList(expected, cflags) {
8813 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8814 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008815 }
8816 })
8817 }
8818 })
8819 }
8820}
8821
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008822func TestHostApexInHostOnlyBuild(t *testing.T) {
8823 testApex(t, `
8824 apex {
8825 name: "myapex",
8826 host_supported: true,
8827 key: "myapex.key",
8828 updatable: false,
8829 payload_type: "zip",
8830 }
8831 apex_key {
8832 name: "myapex.key",
8833 public_key: "testkey.avbpubkey",
8834 private_key: "testkey.pem",
8835 }
8836 `,
8837 android.FixtureModifyConfig(func(config android.Config) {
8838 // We may not have device targets in all builds, e.g. in
8839 // prebuilts/build-tools/build-prebuilts.sh
8840 config.Targets[android.Android] = []android.Target{}
8841 }))
8842}
8843
Colin Crossc33e5212021-05-25 18:16:02 -07008844func TestApexJavaCoverage(t *testing.T) {
8845 bp := `
8846 apex {
8847 name: "myapex",
8848 key: "myapex.key",
8849 java_libs: ["mylib"],
8850 bootclasspath_fragments: ["mybootclasspathfragment"],
8851 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8852 updatable: false,
8853 }
8854
8855 apex_key {
8856 name: "myapex.key",
8857 public_key: "testkey.avbpubkey",
8858 private_key: "testkey.pem",
8859 }
8860
8861 java_library {
8862 name: "mylib",
8863 srcs: ["mylib.java"],
8864 apex_available: ["myapex"],
8865 compile_dex: true,
8866 }
8867
8868 bootclasspath_fragment {
8869 name: "mybootclasspathfragment",
8870 contents: ["mybootclasspathlib"],
8871 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01008872 hidden_api: {
8873 split_packages: ["*"],
8874 },
Colin Crossc33e5212021-05-25 18:16:02 -07008875 }
8876
8877 java_library {
8878 name: "mybootclasspathlib",
8879 srcs: ["mybootclasspathlib.java"],
8880 apex_available: ["myapex"],
8881 compile_dex: true,
8882 }
8883
8884 systemserverclasspath_fragment {
8885 name: "mysystemserverclasspathfragment",
8886 contents: ["mysystemserverclasspathlib"],
8887 apex_available: ["myapex"],
8888 }
8889
8890 java_library {
8891 name: "mysystemserverclasspathlib",
8892 srcs: ["mysystemserverclasspathlib.java"],
8893 apex_available: ["myapex"],
8894 compile_dex: true,
8895 }
8896 `
8897
8898 result := android.GroupFixturePreparers(
8899 PrepareForTestWithApexBuildComponents,
8900 prepareForTestWithMyapex,
8901 java.PrepareForTestWithJavaDefaultModules,
8902 android.PrepareForTestWithAndroidBuildComponents,
8903 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008904 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8905 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04008906 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07008907 ).RunTest(t)
8908
8909 // Make sure jacoco ran on both mylib and mybootclasspathlib
8910 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8911 t.Errorf("Failed to find jacoco rule for mylib")
8912 }
8913 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8914 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8915 }
8916 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8917 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8918 }
8919}
8920
Jiyong Park192600a2021-08-03 07:52:17 +00008921func TestProhibitStaticExecutable(t *testing.T) {
8922 testApexError(t, `executable mybin is static`, `
8923 apex {
8924 name: "myapex",
8925 key: "myapex.key",
8926 binaries: ["mybin"],
8927 min_sdk_version: "29",
8928 }
8929
8930 apex_key {
8931 name: "myapex.key",
8932 public_key: "testkey.avbpubkey",
8933 private_key: "testkey.pem",
8934 }
8935
8936 cc_binary {
8937 name: "mybin",
8938 srcs: ["mylib.cpp"],
8939 relative_install_path: "foo/bar",
8940 static_executable: true,
8941 system_shared_libs: [],
8942 stl: "none",
8943 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008944 min_sdk_version: "29",
8945 }
8946 `)
8947
8948 testApexError(t, `executable mybin.rust is static`, `
8949 apex {
8950 name: "myapex",
8951 key: "myapex.key",
8952 binaries: ["mybin.rust"],
8953 min_sdk_version: "29",
8954 }
8955
8956 apex_key {
8957 name: "myapex.key",
8958 public_key: "testkey.avbpubkey",
8959 private_key: "testkey.pem",
8960 }
8961
8962 rust_binary {
8963 name: "mybin.rust",
8964 srcs: ["foo.rs"],
8965 static_executable: true,
8966 apex_available: ["myapex"],
8967 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008968 }
8969 `)
8970}
8971
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008972func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
8973 ctx := testApex(t, `
8974 apex {
8975 name: "myapex",
8976 key: "myapex.key",
8977 updatable: false,
8978 java_libs: ["foo"],
8979 }
8980
8981 apex_key {
8982 name: "myapex.key",
8983 public_key: "testkey.avbpubkey",
8984 private_key: "testkey.pem",
8985 }
8986
8987 java_library {
8988 name: "foo",
8989 srcs: ["foo.java"],
8990 apex_available: ["myapex"],
8991 installable: true,
8992 }
8993 `,
8994 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8995 )
8996
8997 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8998 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8999 var builder strings.Builder
9000 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9001 androidMk := builder.String()
9002 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex")
9003}
9004
9005func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9006 ctx := testApex(t, `
9007 prebuilt_apex {
9008 name: "myapex",
9009 arch: {
9010 arm64: {
9011 src: "myapex-arm64.apex",
9012 },
9013 arm: {
9014 src: "myapex-arm.apex",
9015 },
9016 },
9017 exported_java_libs: ["foo"],
9018 }
9019
9020 java_import {
9021 name: "foo",
9022 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009023 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009024 }
9025 `,
9026 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9027 )
9028
9029 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9030 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9031 mainModuleEntries := entriesList[0]
9032 android.AssertArrayString(t,
9033 "LOCAL_REQUIRED_MODULES",
9034 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9035 []string{
9036 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9037 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9038 })
9039}
9040
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009041func TestAndroidMk_RequiredModules(t *testing.T) {
9042 ctx := testApex(t, `
9043 apex {
9044 name: "myapex",
9045 key: "myapex.key",
9046 updatable: false,
9047 java_libs: ["foo"],
9048 required: ["otherapex"],
9049 }
9050
9051 apex {
9052 name: "otherapex",
9053 key: "myapex.key",
9054 updatable: false,
9055 java_libs: ["foo"],
9056 required: ["otherapex"],
9057 }
9058
9059 apex_key {
9060 name: "myapex.key",
9061 public_key: "testkey.avbpubkey",
9062 private_key: "testkey.pem",
9063 }
9064
9065 java_library {
9066 name: "foo",
9067 srcs: ["foo.java"],
9068 apex_available: ["myapex", "otherapex"],
9069 installable: true,
9070 }
9071 `)
9072
9073 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9074 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9075 var builder strings.Builder
9076 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9077 androidMk := builder.String()
9078 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherapex")
9079}
9080
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009081func TestAndroidMk_RequiredDeps(t *testing.T) {
9082 ctx := testApex(t, `
9083 apex {
9084 name: "myapex",
9085 key: "myapex.key",
9086 updatable: false,
9087 }
9088
9089 apex_key {
9090 name: "myapex.key",
9091 public_key: "testkey.avbpubkey",
9092 private_key: "testkey.pem",
9093 }
9094 `)
9095
9096 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9097 bundle.requiredDeps = append(bundle.requiredDeps, "foo")
9098 data := android.AndroidMkDataForTest(t, ctx, bundle)
9099 var builder strings.Builder
9100 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9101 androidMk := builder.String()
9102 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo")
9103
9104 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
9105 flattenedBundle.requiredDeps = append(flattenedBundle.requiredDeps, "foo")
9106 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9107 var flattenedBuilder strings.Builder
9108 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9109 flattenedAndroidMk := flattenedBuilder.String()
9110 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES += foo")
9111}
9112
Jooyung Hana6d36672022-02-24 13:58:07 +09009113func TestApexOutputFileProducer(t *testing.T) {
9114 for _, tc := range []struct {
9115 name string
9116 ref string
9117 expected_data []string
9118 }{
9119 {
9120 name: "test_using_output",
9121 ref: ":myapex",
9122 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9123 },
9124 {
9125 name: "test_using_apex",
9126 ref: ":myapex{.apex}",
9127 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9128 },
9129 } {
9130 t.Run(tc.name, func(t *testing.T) {
9131 ctx := testApex(t, `
9132 apex {
9133 name: "myapex",
9134 key: "myapex.key",
9135 compressible: true,
9136 updatable: false,
9137 }
9138
9139 apex_key {
9140 name: "myapex.key",
9141 public_key: "testkey.avbpubkey",
9142 private_key: "testkey.pem",
9143 }
9144
9145 java_test {
9146 name: "`+tc.name+`",
9147 srcs: ["a.java"],
9148 data: ["`+tc.ref+`"],
9149 }
9150 `,
9151 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9152 variables.CompressedApex = proptools.BoolPtr(true)
9153 }))
9154 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9155 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9156 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9157 })
9158 }
9159}
9160
satayev758968a2021-12-06 11:42:40 +00009161func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9162 preparer := android.GroupFixturePreparers(
9163 PrepareForTestWithApexBuildComponents,
9164 prepareForTestWithMyapex,
9165 java.PrepareForTestWithJavaSdkLibraryFiles,
9166 java.PrepareForTestWithJavaDefaultModules,
9167 android.PrepareForTestWithAndroidBuildComponents,
9168 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9169 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9170 )
9171
9172 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9173 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9174 preparer.RunTestWithBp(t, `
9175 apex {
9176 name: "myapex",
9177 key: "myapex.key",
9178 bootclasspath_fragments: ["mybootclasspathfragment"],
9179 min_sdk_version: "30",
9180 updatable: false,
9181 }
9182
9183 apex_key {
9184 name: "myapex.key",
9185 public_key: "testkey.avbpubkey",
9186 private_key: "testkey.pem",
9187 }
9188
9189 bootclasspath_fragment {
9190 name: "mybootclasspathfragment",
9191 contents: ["mybootclasspathlib"],
9192 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009193 hidden_api: {
9194 split_packages: ["*"],
9195 },
satayev758968a2021-12-06 11:42:40 +00009196 }
9197
9198 java_sdk_library {
9199 name: "mybootclasspathlib",
9200 srcs: ["mybootclasspathlib.java"],
9201 apex_available: ["myapex"],
9202 compile_dex: true,
9203 unsafe_ignore_missing_latest_api: true,
9204 min_sdk_version: "31",
9205 static_libs: ["util"],
9206 }
9207
9208 java_library {
9209 name: "util",
9210 srcs: ["a.java"],
9211 apex_available: ["myapex"],
9212 min_sdk_version: "31",
9213 static_libs: ["another_util"],
9214 }
9215
9216 java_library {
9217 name: "another_util",
9218 srcs: ["a.java"],
9219 min_sdk_version: "31",
9220 apex_available: ["myapex"],
9221 }
9222 `)
9223 })
9224
9225 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9226 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9227 preparer.RunTestWithBp(t, `
9228 apex {
9229 name: "myapex",
9230 key: "myapex.key",
9231 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9232 min_sdk_version: "30",
9233 updatable: false,
9234 }
9235
9236 apex_key {
9237 name: "myapex.key",
9238 public_key: "testkey.avbpubkey",
9239 private_key: "testkey.pem",
9240 }
9241
9242 systemserverclasspath_fragment {
9243 name: "mysystemserverclasspathfragment",
9244 contents: ["mysystemserverclasspathlib"],
9245 apex_available: ["myapex"],
9246 }
9247
9248 java_sdk_library {
9249 name: "mysystemserverclasspathlib",
9250 srcs: ["mysystemserverclasspathlib.java"],
9251 apex_available: ["myapex"],
9252 compile_dex: true,
9253 min_sdk_version: "32",
9254 unsafe_ignore_missing_latest_api: true,
9255 static_libs: ["util"],
9256 }
9257
9258 java_library {
9259 name: "util",
9260 srcs: ["a.java"],
9261 apex_available: ["myapex"],
9262 min_sdk_version: "31",
9263 static_libs: ["another_util"],
9264 }
9265
9266 java_library {
9267 name: "another_util",
9268 srcs: ["a.java"],
9269 min_sdk_version: "31",
9270 apex_available: ["myapex"],
9271 }
9272 `)
9273 })
9274
9275 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9276 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9277 RunTestWithBp(t, `
9278 apex {
9279 name: "myapex",
9280 key: "myapex.key",
9281 bootclasspath_fragments: ["mybootclasspathfragment"],
9282 min_sdk_version: "30",
9283 updatable: false,
9284 }
9285
9286 apex_key {
9287 name: "myapex.key",
9288 public_key: "testkey.avbpubkey",
9289 private_key: "testkey.pem",
9290 }
9291
9292 bootclasspath_fragment {
9293 name: "mybootclasspathfragment",
9294 contents: ["mybootclasspathlib"],
9295 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009296 hidden_api: {
9297 split_packages: ["*"],
9298 },
satayev758968a2021-12-06 11:42:40 +00009299 }
9300
9301 java_sdk_library {
9302 name: "mybootclasspathlib",
9303 srcs: ["mybootclasspathlib.java"],
9304 apex_available: ["myapex"],
9305 compile_dex: true,
9306 unsafe_ignore_missing_latest_api: true,
9307 }
9308 `)
9309 })
9310
9311 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9312 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9313 RunTestWithBp(t, `
9314 apex {
9315 name: "myapex",
9316 key: "myapex.key",
9317 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9318 min_sdk_version: "30",
9319 updatable: false,
9320 }
9321
9322 apex_key {
9323 name: "myapex.key",
9324 public_key: "testkey.avbpubkey",
9325 private_key: "testkey.pem",
9326 }
9327
9328 systemserverclasspath_fragment {
9329 name: "mysystemserverclasspathfragment",
9330 contents: ["mysystemserverclasspathlib"],
9331 apex_available: ["myapex"],
9332 }
9333
9334 java_sdk_library {
9335 name: "mysystemserverclasspathlib",
9336 srcs: ["mysystemserverclasspathlib.java"],
9337 apex_available: ["myapex"],
9338 compile_dex: true,
9339 unsafe_ignore_missing_latest_api: true,
9340 }
9341 `)
9342 })
9343}
9344
Jiakai Zhang6decef92022-01-12 17:56:19 +00009345// Verifies that the APEX depends on all the Make modules in the list.
9346func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9347 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9348 for _, dep := range deps {
9349 android.AssertStringListContains(t, "", a.requiredDeps, dep)
9350 }
9351}
9352
9353// Verifies that the APEX does not depend on any of the Make modules in the list.
9354func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9355 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9356 for _, dep := range deps {
9357 android.AssertStringListDoesNotContain(t, "", a.requiredDeps, dep)
9358 }
9359}
9360
Spandan Das66773252022-01-15 00:23:18 +00009361func TestApexStrictUpdtabilityLint(t *testing.T) {
9362 bpTemplate := `
9363 apex {
9364 name: "myapex",
9365 key: "myapex.key",
9366 java_libs: ["myjavalib"],
9367 updatable: %v,
9368 min_sdk_version: "29",
9369 }
9370 apex_key {
9371 name: "myapex.key",
9372 }
9373 java_library {
9374 name: "myjavalib",
9375 srcs: ["MyClass.java"],
9376 apex_available: [ "myapex" ],
9377 lint: {
9378 strict_updatability_linting: %v,
9379 },
9380 sdk_version: "current",
9381 min_sdk_version: "29",
9382 }
9383 `
9384 fs := android.MockFS{
9385 "lint-baseline.xml": nil,
9386 }
9387
9388 testCases := []struct {
9389 testCaseName string
9390 apexUpdatable bool
9391 javaStrictUpdtabilityLint bool
9392 lintFileExists bool
9393 disallowedFlagExpected bool
9394 }{
9395 {
9396 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9397 apexUpdatable: true,
9398 javaStrictUpdtabilityLint: true,
9399 lintFileExists: false,
9400 disallowedFlagExpected: false,
9401 },
9402 {
9403 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9404 apexUpdatable: false,
9405 javaStrictUpdtabilityLint: false,
9406 lintFileExists: true,
9407 disallowedFlagExpected: false,
9408 },
9409 {
9410 testCaseName: "non-updatable apex respects strict updatability of javalib",
9411 apexUpdatable: false,
9412 javaStrictUpdtabilityLint: true,
9413 lintFileExists: true,
9414 disallowedFlagExpected: true,
9415 },
9416 {
9417 testCaseName: "updatable apex sets strict updatability of javalib to true",
9418 apexUpdatable: true,
9419 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9420 lintFileExists: true,
9421 disallowedFlagExpected: true,
9422 },
9423 }
9424
9425 for _, testCase := range testCases {
9426 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9427 fixtures := []android.FixturePreparer{}
9428 if testCase.lintFileExists {
9429 fixtures = append(fixtures, fs.AddToFixture())
9430 }
9431
9432 result := testApex(t, bp, fixtures...)
9433 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9434 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9435 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9436
9437 if disallowedFlagActual != testCase.disallowedFlagExpected {
9438 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9439 }
9440 }
9441}
9442
Spandan Dasd9c23ab2022-02-10 02:34:13 +00009443func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9444 bp := `
9445 apex {
9446 name: "myapex",
9447 key: "myapex.key",
9448 java_libs: ["myjavalib"],
9449 updatable: true,
9450 min_sdk_version: "29",
9451 }
9452 apex_key {
9453 name: "myapex.key",
9454 }
9455 java_library {
9456 name: "myjavalib",
9457 srcs: ["MyClass.java"],
9458 apex_available: [ "myapex" ],
9459 sdk_version: "current",
9460 min_sdk_version: "29",
9461 }
9462 `
9463
9464 testCases := []struct {
9465 testCaseName string
9466 moduleDirectory string
9467 disallowedFlagExpected bool
9468 }{
9469 {
9470 testCaseName: "lintable module defined outside libcore",
9471 moduleDirectory: "",
9472 disallowedFlagExpected: true,
9473 },
9474 {
9475 testCaseName: "lintable module defined in libcore root directory",
9476 moduleDirectory: "libcore/",
9477 disallowedFlagExpected: false,
9478 },
9479 {
9480 testCaseName: "lintable module defined in libcore child directory",
9481 moduleDirectory: "libcore/childdir/",
9482 disallowedFlagExpected: true,
9483 },
9484 }
9485
9486 for _, testCase := range testCases {
9487 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9488 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9489 result := testApex(t, "", lintFileCreator, bpFileCreator)
9490 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9491 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9492 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9493 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9494
9495 if disallowedFlagActual != testCase.disallowedFlagExpected {
9496 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9497 }
9498 }
9499}
9500
Spandan Das66773252022-01-15 00:23:18 +00009501// checks transtive deps of an apex coming from bootclasspath_fragment
9502func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9503 bp := `
9504 apex {
9505 name: "myapex",
9506 key: "myapex.key",
9507 bootclasspath_fragments: ["mybootclasspathfragment"],
9508 updatable: true,
9509 min_sdk_version: "29",
9510 }
9511 apex_key {
9512 name: "myapex.key",
9513 }
9514 bootclasspath_fragment {
9515 name: "mybootclasspathfragment",
9516 contents: ["myjavalib"],
9517 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009518 hidden_api: {
9519 split_packages: ["*"],
9520 },
Spandan Das66773252022-01-15 00:23:18 +00009521 }
9522 java_library {
9523 name: "myjavalib",
9524 srcs: ["MyClass.java"],
9525 apex_available: [ "myapex" ],
9526 sdk_version: "current",
9527 min_sdk_version: "29",
9528 compile_dex: true,
9529 }
9530 `
9531 fs := android.MockFS{
9532 "lint-baseline.xml": nil,
9533 }
9534
9535 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9536 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9537 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9538 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9539 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9540 }
9541}
9542
Spandan Das42e89502022-05-06 22:12:55 +00009543// updatable apexes should propagate updatable=true to its apps
9544func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9545 bp := `
9546 apex {
9547 name: "myapex",
9548 key: "myapex.key",
9549 updatable: %v,
9550 apps: [
9551 "myapp",
9552 ],
9553 min_sdk_version: "30",
9554 }
9555 apex_key {
9556 name: "myapex.key",
9557 }
9558 android_app {
9559 name: "myapp",
9560 updatable: %v,
9561 apex_available: [
9562 "myapex",
9563 ],
9564 sdk_version: "current",
9565 min_sdk_version: "30",
9566 }
9567 `
9568 testCases := []struct {
9569 name string
9570 apex_is_updatable_bp bool
9571 app_is_updatable_bp bool
9572 app_is_updatable_expected bool
9573 }{
9574 {
9575 name: "Non-updatable apex respects updatable property of non-updatable app",
9576 apex_is_updatable_bp: false,
9577 app_is_updatable_bp: false,
9578 app_is_updatable_expected: false,
9579 },
9580 {
9581 name: "Non-updatable apex respects updatable property of updatable app",
9582 apex_is_updatable_bp: false,
9583 app_is_updatable_bp: true,
9584 app_is_updatable_expected: true,
9585 },
9586 {
9587 name: "Updatable apex respects updatable property of updatable app",
9588 apex_is_updatable_bp: true,
9589 app_is_updatable_bp: true,
9590 app_is_updatable_expected: true,
9591 },
9592 {
9593 name: "Updatable apex sets updatable=true on non-updatable app",
9594 apex_is_updatable_bp: true,
9595 app_is_updatable_bp: false,
9596 app_is_updatable_expected: true,
9597 },
9598 }
9599 for _, testCase := range testCases {
9600 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9601 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9602 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9603 }
9604}
9605
Kiyoung Kim487689e2022-07-26 09:48:22 +09009606func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9607 bp := `
9608 apex {
9609 name: "myapex",
9610 key: "myapex.key",
9611 native_shared_libs: ["libfoo"],
9612 min_sdk_version: "29",
9613 }
9614 apex_key {
9615 name: "myapex.key",
9616 }
9617 cc_library {
9618 name: "libfoo",
9619 shared_libs: ["libc"],
9620 apex_available: ["myapex"],
9621 min_sdk_version: "29",
9622 }
9623 cc_api_library {
9624 name: "libc",
9625 src: "libc.so",
9626 min_sdk_version: "29",
9627 recovery_available: true,
9628 }
9629 api_imports {
9630 name: "api_imports",
9631 shared_libs: [
9632 "libc",
9633 ],
9634 header_libs: [],
9635 }
9636 `
9637 result := testApex(t, bp)
9638
9639 hasDep := func(m android.Module, wantDep android.Module) bool {
9640 t.Helper()
9641 var found bool
9642 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9643 if dep == wantDep {
9644 found = true
9645 }
9646 })
9647 return found
9648 }
9649
9650 libfooApexVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex29").Module()
9651 libcApexVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared_apex29").Module()
9652
9653 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(libfooApexVariant, libcApexVariant))
9654
9655 // libfoo core variant should be buildable in the same inner tree since
9656 // certain mcombo files might build system and apexes in the same inner tree
9657 // libfoo core variant should link against source libc
9658 libfooCoreVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
9659 libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
9660 android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
9661}
9662
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07009663func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01009664 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07009665}