blob: 9c440939dd6e3e5a3e02d8a1640a4f3e732d4198 [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
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "build/make/core/proguard.flags": nil,
206 "build/make/core/proguard_basic_keeps.flags": nil,
207 "dummy.txt": nil,
208 "baz": nil,
209 "bar/baz": nil,
210 "testdata/baz": nil,
211 "AppSet.apks": nil,
212 "foo.rs": nil,
213 "libfoo.jar": nil,
214 "libbar.jar": nil,
215 },
216 ),
217
218 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
219 variables.DeviceVndkVersion = proptools.StringPtr("current")
220 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
221 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
222 variables.Platform_sdk_codename = proptools.StringPtr("Q")
223 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000224 // "Tiramisu" needs to be in the next line for compatibility with soong code,
225 // not because of these tests specifically (it's not used by the tests)
226 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900227 variables.Platform_vndk_version = proptools.StringPtr("29")
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 Park52818fc2019-03-18 12:01:38 +0900447 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900448 static_libs: ["libstatic"],
449 // TODO: remove //apex_available:platform
450 apex_available: [
451 "//apex_available:platform",
452 "myapex",
453 ],
454 }
455
Paul Duffindddd5462020-04-07 15:25:44 +0100456 cc_prebuilt_library_shared {
457 name: "mylib2",
458 srcs: ["prebuilt.so"],
459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
464 }
465
Jiyong Park9918e1a2020-03-17 19:16:40 +0900466 cc_library_static {
467 name: "libstatic",
468 srcs: ["mylib.cpp"],
469 system_shared_libs: [],
470 stl: "none",
471 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000472 // TODO: remove //apex_available:platform
473 apex_available: [
474 "//apex_available:platform",
475 "myapex",
476 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478
479 java_library {
480 name: "myjar",
481 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900482 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 sdk_version: "none",
484 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900486 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000487 // TODO: remove //apex_available:platform
488 apex_available: [
489 "//apex_available:platform",
490 "myapex",
491 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 }
493
Jiyong Park77acec62020-06-01 21:39:15 +0900494 dex_import {
495 name: "myjar_dex",
496 jars: ["prebuilt.jar"],
497 apex_available: [
498 "//apex_available:platform",
499 "myapex",
500 ],
501 }
502
Jiyong Park7f7766d2019-07-25 22:02:35 +0900503 java_library {
504 name: "myotherjar",
505 srcs: ["foo/bar/MyClass.java"],
506 sdk_version: "none",
507 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900508 // TODO: remove //apex_available:platform
509 apex_available: [
510 "//apex_available:platform",
511 "myapex",
512 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900513 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900514
515 java_library {
516 name: "mysharedjar",
517 srcs: ["foo/bar/MyClass.java"],
518 sdk_version: "none",
519 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900520 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900521 `)
522
Paul Duffina71a67a2021-03-29 00:42:57 +0100523 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900524
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 // Make sure that Android.mk is created
526 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700527 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900528 var builder strings.Builder
529 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
530
531 androidMk := builder.String()
532 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
533 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
534
Jiyong Park42cca6c2019-04-01 11:15:50 +0900535 optFlags := apexRule.Args["opt_flags"]
536 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700537 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100538 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900539
Jiyong Park25fc6a92018-11-18 18:02:45 +0900540 copyCmds := apexRule.Args["copy_commands"]
541
542 // Ensure that main rule creates an output
543 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
544
545 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551
552 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700553 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
554 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
556 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800561 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
562 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900564 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900566 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
567 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900568 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 // .. but not for java libs
570 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800572
Colin Cross7113d202019-11-20 16:39:12 -0800573 // Ensure that the platform variant ends with _shared or _common
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
575 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900576 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
577 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900578 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
579
580 // Ensure that dynamic dependency to java libs are not included
581 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800582
583 // Ensure that all symlinks are present.
584 found_foo_link_64 := false
585 found_foo := false
586 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900587 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800588 if strings.HasSuffix(cmd, "bin/foo") {
589 found_foo = true
590 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
591 found_foo_link_64 = true
592 }
593 }
594 }
595 good := found_foo && found_foo_link_64
596 if !good {
597 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
598 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900599
Artur Satayeva8bd1132020-04-27 18:07:06 +0100600 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
603 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
604 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100605
606 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100607 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100608 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
609 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
610 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800611}
612
Jooyung Hanf21c7972019-12-16 22:32:06 +0900613func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800614 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615 apex_defaults {
616 name: "myapex-defaults",
617 key: "myapex.key",
618 prebuilts: ["myetc"],
619 native_shared_libs: ["mylib"],
620 java_libs: ["myjar"],
621 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900622 rros: ["rro"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800623 bpfs: ["bpf", "netd_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000624 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900625 }
626
627 prebuilt_etc {
628 name: "myetc",
629 src: "myprebuilt",
630 }
631
632 apex {
633 name: "myapex",
634 defaults: ["myapex-defaults"],
635 }
636
637 apex_key {
638 name: "myapex.key",
639 public_key: "testkey.avbpubkey",
640 private_key: "testkey.pem",
641 }
642
643 cc_library {
644 name: "mylib",
645 system_shared_libs: [],
646 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000647 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900648 }
649
650 java_library {
651 name: "myjar",
652 srcs: ["foo/bar/MyClass.java"],
653 sdk_version: "none",
654 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000655 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900656 }
657
658 android_app {
659 name: "AppFoo",
660 srcs: ["foo/bar/MyClass.java"],
661 sdk_version: "none",
662 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000663 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900664 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900665
666 runtime_resource_overlay {
667 name: "rro",
668 theme: "blue",
669 }
670
markchien2f59ec92020-09-02 16:23:38 +0800671 bpf {
672 name: "bpf",
673 srcs: ["bpf.c", "bpf2.c"],
674 }
675
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 bpf {
677 name: "netd_test",
678 srcs: ["netd_test.c"],
679 sub_dir: "netd",
680 }
681
Jooyung Hanf21c7972019-12-16 22:32:06 +0900682 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000683 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900684 "etc/myetc",
685 "javalib/myjar.jar",
686 "lib64/mylib.so",
687 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900688 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800689 "etc/bpf/bpf.o",
690 "etc/bpf/bpf2.o",
Ken Chenfad7f9d2021-11-10 22:02:57 +0800691 "etc/bpf/netd/netd_test.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900692 })
693}
694
Jooyung Han01a3ee22019-11-02 02:52:25 +0900695func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800696 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900697 apex {
698 name: "myapex",
699 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000700 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900701 }
702
703 apex_key {
704 name: "myapex.key",
705 public_key: "testkey.avbpubkey",
706 private_key: "testkey.pem",
707 }
708 `)
709
710 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900711 args := module.Rule("apexRule").Args
712 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
713 t.Error("manifest should be apex_manifest.pb, but " + manifest)
714 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900715}
716
Liz Kammer4854a7d2021-05-27 14:28:27 -0400717func TestApexManifestMinSdkVersion(t *testing.T) {
718 ctx := testApex(t, `
719 apex_defaults {
720 name: "my_defaults",
721 key: "myapex.key",
722 product_specific: true,
723 file_contexts: ":my-file-contexts",
724 updatable: false,
725 }
726 apex {
727 name: "myapex_30",
728 min_sdk_version: "30",
729 defaults: ["my_defaults"],
730 }
731
732 apex {
733 name: "myapex_current",
734 min_sdk_version: "current",
735 defaults: ["my_defaults"],
736 }
737
738 apex {
739 name: "myapex_none",
740 defaults: ["my_defaults"],
741 }
742
743 apex_key {
744 name: "myapex.key",
745 public_key: "testkey.avbpubkey",
746 private_key: "testkey.pem",
747 }
748
749 filegroup {
750 name: "my-file-contexts",
751 srcs: ["product_specific_file_contexts"],
752 }
753 `, withFiles(map[string][]byte{
754 "product_specific_file_contexts": nil,
755 }), android.FixtureModifyProductVariables(
756 func(variables android.FixtureProductVariables) {
757 variables.Unbundled_build = proptools.BoolPtr(true)
758 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
759 }), android.FixtureMergeEnv(map[string]string{
760 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
761 }))
762
763 testCases := []struct {
764 module string
765 minSdkVersion string
766 }{
767 {
768 module: "myapex_30",
769 minSdkVersion: "30",
770 },
771 {
772 module: "myapex_current",
773 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
774 },
775 {
776 module: "myapex_none",
777 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
778 },
779 }
780 for _, tc := range testCases {
781 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
782 args := module.Rule("apexRule").Args
783 optFlags := args["opt_flags"]
784 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
785 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
786 }
787 }
788}
789
Alex Light5098a612018-11-29 17:12:15 -0800790func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800791 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800792 apex {
793 name: "myapex",
794 key: "myapex.key",
795 payload_type: "zip",
796 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000797 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800798 }
799
800 apex_key {
801 name: "myapex.key",
802 public_key: "testkey.avbpubkey",
803 private_key: "testkey.pem",
804 }
805
806 cc_library {
807 name: "mylib",
808 srcs: ["mylib.cpp"],
809 shared_libs: ["mylib2"],
810 system_shared_libs: [],
811 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000812 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800813 }
814
815 cc_library {
816 name: "mylib2",
817 srcs: ["mylib.cpp"],
818 system_shared_libs: [],
819 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800821 }
822 `)
823
Sundong Ahnabb64432019-10-22 13:58:29 +0900824 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800825 copyCmds := zipApexRule.Args["copy_commands"]
826
827 // Ensure that main rule creates an output
828 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
829
830 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700831 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800832
833 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700834 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800835
836 // Ensure that both direct and indirect deps are copied into apex
837 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
838 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900839}
840
841func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800842 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900843 apex {
844 name: "myapex",
845 key: "myapex.key",
846 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900847 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000848 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900849 }
850
851 apex_key {
852 name: "myapex.key",
853 public_key: "testkey.avbpubkey",
854 private_key: "testkey.pem",
855 }
856
857 cc_library {
858 name: "mylib",
859 srcs: ["mylib.cpp"],
860 shared_libs: ["mylib2", "mylib3"],
861 system_shared_libs: [],
862 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000863 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900864 }
865
866 cc_library {
867 name: "mylib2",
868 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900869 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900870 system_shared_libs: [],
871 stl: "none",
872 stubs: {
873 versions: ["1", "2", "3"],
874 },
875 }
876
877 cc_library {
878 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900879 srcs: ["mylib.cpp"],
880 shared_libs: ["mylib4"],
881 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900882 stl: "none",
883 stubs: {
884 versions: ["10", "11", "12"],
885 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000886 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900887 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900888
889 cc_library {
890 name: "mylib4",
891 srcs: ["mylib.cpp"],
892 system_shared_libs: [],
893 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000894 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900895 }
Jiyong Park105dc322021-06-11 17:22:09 +0900896
897 rust_binary {
898 name: "foo.rust",
899 srcs: ["foo.rs"],
900 shared_libs: ["libfoo.shared_from_rust"],
901 prefer_rlib: true,
902 apex_available: ["myapex"],
903 }
904
905 cc_library_shared {
906 name: "libfoo.shared_from_rust",
907 srcs: ["mylib.cpp"],
908 system_shared_libs: [],
909 stl: "none",
910 stubs: {
911 versions: ["10", "11", "12"],
912 },
913 }
914
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915 `)
916
Sundong Ahnabb64432019-10-22 13:58:29 +0900917 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900918 copyCmds := apexRule.Args["copy_commands"]
919
920 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800921 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922
923 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800924 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925
926 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800927 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928
Colin Crossaede88c2020-08-11 12:17:01 -0700929 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900930
931 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900932 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900934 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935
936 // 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 -0700937 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900938 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700939 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900940
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700941 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
942 // is replaced by sharing of "cFlags" in cc/builder.go.
943 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
944 // module variable representing "cflags". So it was not detected by ensureNotContains.
945 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
946 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
947 // including the original cflags's "-include mylib.h".
948 //
Jiyong Park64379952018-12-13 18:37:29 +0900949 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700950 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
951 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900952
953 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700954 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
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
1138 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001139 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
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
Artur Satayev8cf899a2020-04-15 17:29:42 +01002194func TestJavaStableSdkVersion(t *testing.T) {
2195 testCases := []struct {
2196 name string
2197 expectedError string
2198 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002199 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002200 }{
2201 {
2202 name: "Non-updatable apex with non-stable dep",
2203 bp: `
2204 apex {
2205 name: "myapex",
2206 java_libs: ["myjar"],
2207 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002208 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002209 }
2210 apex_key {
2211 name: "myapex.key",
2212 public_key: "testkey.avbpubkey",
2213 private_key: "testkey.pem",
2214 }
2215 java_library {
2216 name: "myjar",
2217 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002218 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002219 apex_available: ["myapex"],
2220 }
2221 `,
2222 },
2223 {
2224 name: "Updatable apex with stable dep",
2225 bp: `
2226 apex {
2227 name: "myapex",
2228 java_libs: ["myjar"],
2229 key: "myapex.key",
2230 updatable: true,
2231 min_sdk_version: "29",
2232 }
2233 apex_key {
2234 name: "myapex.key",
2235 public_key: "testkey.avbpubkey",
2236 private_key: "testkey.pem",
2237 }
2238 java_library {
2239 name: "myjar",
2240 srcs: ["foo/bar/MyClass.java"],
2241 sdk_version: "current",
2242 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002243 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002244 }
2245 `,
2246 },
2247 {
2248 name: "Updatable apex with non-stable dep",
2249 expectedError: "cannot depend on \"myjar\"",
2250 bp: `
2251 apex {
2252 name: "myapex",
2253 java_libs: ["myjar"],
2254 key: "myapex.key",
2255 updatable: true,
2256 }
2257 apex_key {
2258 name: "myapex.key",
2259 public_key: "testkey.avbpubkey",
2260 private_key: "testkey.pem",
2261 }
2262 java_library {
2263 name: "myjar",
2264 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002265 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002266 apex_available: ["myapex"],
2267 }
2268 `,
2269 },
2270 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002271 name: "Updatable apex with non-stable legacy core platform dep",
2272 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2273 bp: `
2274 apex {
2275 name: "myapex",
2276 java_libs: ["myjar-uses-legacy"],
2277 key: "myapex.key",
2278 updatable: true,
2279 }
2280 apex_key {
2281 name: "myapex.key",
2282 public_key: "testkey.avbpubkey",
2283 private_key: "testkey.pem",
2284 }
2285 java_library {
2286 name: "myjar-uses-legacy",
2287 srcs: ["foo/bar/MyClass.java"],
2288 sdk_version: "core_platform",
2289 apex_available: ["myapex"],
2290 }
2291 `,
2292 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2293 },
2294 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002295 name: "Updatable apex with non-stable transitive dep",
2296 // This is not actually detecting that the transitive dependency is unstable, rather it is
2297 // detecting that the transitive dependency is building against a wider API surface than the
2298 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002299 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002300 bp: `
2301 apex {
2302 name: "myapex",
2303 java_libs: ["myjar"],
2304 key: "myapex.key",
2305 updatable: true,
2306 }
2307 apex_key {
2308 name: "myapex.key",
2309 public_key: "testkey.avbpubkey",
2310 private_key: "testkey.pem",
2311 }
2312 java_library {
2313 name: "myjar",
2314 srcs: ["foo/bar/MyClass.java"],
2315 sdk_version: "current",
2316 apex_available: ["myapex"],
2317 static_libs: ["transitive-jar"],
2318 }
2319 java_library {
2320 name: "transitive-jar",
2321 srcs: ["foo/bar/MyClass.java"],
2322 sdk_version: "core_platform",
2323 apex_available: ["myapex"],
2324 }
2325 `,
2326 },
2327 }
2328
2329 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002330 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2331 continue
2332 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002333 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002334 errorHandler := android.FixtureExpectsNoErrors
2335 if test.expectedError != "" {
2336 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002337 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002338 android.GroupFixturePreparers(
2339 java.PrepareForTestWithJavaDefaultModules,
2340 PrepareForTestWithApexBuildComponents,
2341 prepareForTestWithMyapex,
2342 android.OptionalFixturePreparer(test.preparer),
2343 ).
2344 ExtendWithErrorHandler(errorHandler).
2345 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002346 })
2347 }
2348}
2349
Jooyung Han749dc692020-04-15 11:03:39 +09002350func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2351 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2352 apex {
2353 name: "myapex",
2354 key: "myapex.key",
2355 native_shared_libs: ["mylib"],
2356 min_sdk_version: "29",
2357 }
2358
2359 apex_key {
2360 name: "myapex.key",
2361 public_key: "testkey.avbpubkey",
2362 private_key: "testkey.pem",
2363 }
2364
2365 cc_library {
2366 name: "mylib",
2367 srcs: ["mylib.cpp"],
2368 shared_libs: ["mylib2"],
2369 system_shared_libs: [],
2370 stl: "none",
2371 apex_available: [
2372 "myapex",
2373 ],
2374 min_sdk_version: "29",
2375 }
2376
2377 // indirect part of the apex
2378 cc_library {
2379 name: "mylib2",
2380 srcs: ["mylib.cpp"],
2381 system_shared_libs: [],
2382 stl: "none",
2383 apex_available: [
2384 "myapex",
2385 ],
2386 min_sdk_version: "30",
2387 }
2388 `)
2389}
2390
2391func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2392 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2393 apex {
2394 name: "myapex",
2395 key: "myapex.key",
2396 apps: ["AppFoo"],
2397 min_sdk_version: "29",
2398 }
2399
2400 apex_key {
2401 name: "myapex.key",
2402 public_key: "testkey.avbpubkey",
2403 private_key: "testkey.pem",
2404 }
2405
2406 android_app {
2407 name: "AppFoo",
2408 srcs: ["foo/bar/MyClass.java"],
2409 sdk_version: "current",
2410 min_sdk_version: "29",
2411 system_modules: "none",
2412 stl: "none",
2413 static_libs: ["bar"],
2414 apex_available: [ "myapex" ],
2415 }
2416
2417 java_library {
2418 name: "bar",
2419 sdk_version: "current",
2420 srcs: ["a.java"],
2421 apex_available: [ "myapex" ],
2422 }
2423 `)
2424}
2425
2426func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002427 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002428 apex {
2429 name: "myapex",
2430 key: "myapex.key",
2431 native_shared_libs: ["mylib"],
2432 min_sdk_version: "29",
2433 }
2434
2435 apex_key {
2436 name: "myapex.key",
2437 public_key: "testkey.avbpubkey",
2438 private_key: "testkey.pem",
2439 }
2440
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002441 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002442 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2443 cc_library {
2444 name: "mylib",
2445 srcs: ["mylib.cpp"],
2446 shared_libs: ["mylib2"],
2447 system_shared_libs: [],
2448 stl: "none",
2449 apex_available: ["myapex", "otherapex"],
2450 min_sdk_version: "29",
2451 }
2452
2453 cc_library {
2454 name: "mylib2",
2455 srcs: ["mylib.cpp"],
2456 system_shared_libs: [],
2457 stl: "none",
2458 apex_available: ["otherapex"],
2459 stubs: { versions: ["29", "30"] },
2460 min_sdk_version: "30",
2461 }
2462
2463 apex {
2464 name: "otherapex",
2465 key: "myapex.key",
2466 native_shared_libs: ["mylib", "mylib2"],
2467 min_sdk_version: "30",
2468 }
2469 `)
2470 expectLink := func(from, from_variant, to, to_variant string) {
2471 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2472 libFlags := ld.Args["libFlags"]
2473 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2474 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002475 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002476 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002477}
2478
Jooyung Haned124c32021-01-26 11:43:46 +09002479func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002480 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2481 func(variables android.FixtureProductVariables) {
2482 variables.Platform_sdk_codename = proptools.StringPtr("S")
2483 variables.Platform_version_active_codenames = []string{"S"}
2484 },
2485 )
Jooyung Haned124c32021-01-26 11:43:46 +09002486 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2487 apex {
2488 name: "myapex",
2489 key: "myapex.key",
2490 native_shared_libs: ["libfoo"],
2491 min_sdk_version: "S",
2492 }
2493 apex_key {
2494 name: "myapex.key",
2495 public_key: "testkey.avbpubkey",
2496 private_key: "testkey.pem",
2497 }
2498 cc_library {
2499 name: "libfoo",
2500 shared_libs: ["libbar"],
2501 apex_available: ["myapex"],
2502 min_sdk_version: "29",
2503 }
2504 cc_library {
2505 name: "libbar",
2506 apex_available: ["myapex"],
2507 }
2508 `, withSAsActiveCodeNames)
2509}
2510
2511func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002512 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2513 variables.Platform_sdk_codename = proptools.StringPtr("S")
2514 variables.Platform_version_active_codenames = []string{"S", "T"}
2515 })
Colin Cross1c460562021-02-16 17:55:47 -08002516 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002517 apex {
2518 name: "myapex",
2519 key: "myapex.key",
2520 native_shared_libs: ["libfoo"],
2521 min_sdk_version: "S",
2522 }
2523 apex_key {
2524 name: "myapex.key",
2525 public_key: "testkey.avbpubkey",
2526 private_key: "testkey.pem",
2527 }
2528 cc_library {
2529 name: "libfoo",
2530 shared_libs: ["libbar"],
2531 apex_available: ["myapex"],
2532 min_sdk_version: "S",
2533 }
2534 cc_library {
2535 name: "libbar",
2536 stubs: {
2537 symbol_file: "libbar.map.txt",
2538 versions: ["30", "S", "T"],
2539 },
2540 }
2541 `, withSAsActiveCodeNames)
2542
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002543 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002544 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2545 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002546 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002547}
2548
Jiyong Park7c2ee712018-12-07 00:42:25 +09002549func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002550 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002551 apex {
2552 name: "myapex",
2553 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002554 native_shared_libs: ["mylib"],
2555 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002556 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002557 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002558 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002559 }
2560
2561 apex_key {
2562 name: "myapex.key",
2563 public_key: "testkey.avbpubkey",
2564 private_key: "testkey.pem",
2565 }
2566
2567 prebuilt_etc {
2568 name: "myetc",
2569 src: "myprebuilt",
2570 sub_dir: "foo/bar",
2571 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002572
2573 cc_library {
2574 name: "mylib",
2575 srcs: ["mylib.cpp"],
2576 relative_install_path: "foo/bar",
2577 system_shared_libs: [],
2578 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002579 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002580 }
2581
2582 cc_binary {
2583 name: "mybin",
2584 srcs: ["mylib.cpp"],
2585 relative_install_path: "foo/bar",
2586 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002587 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002588 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002589 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002590 `)
2591
Sundong Ahnabb64432019-10-22 13:58:29 +09002592 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002593 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002594
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002595 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002596 ensureContains(t, cmd, "/etc ")
2597 ensureContains(t, cmd, "/etc/foo ")
2598 ensureContains(t, cmd, "/etc/foo/bar ")
2599 ensureContains(t, cmd, "/lib64 ")
2600 ensureContains(t, cmd, "/lib64/foo ")
2601 ensureContains(t, cmd, "/lib64/foo/bar ")
2602 ensureContains(t, cmd, "/lib ")
2603 ensureContains(t, cmd, "/lib/foo ")
2604 ensureContains(t, cmd, "/lib/foo/bar ")
2605 ensureContains(t, cmd, "/bin ")
2606 ensureContains(t, cmd, "/bin/foo ")
2607 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002608}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002609
Jooyung Han35155c42020-02-06 17:33:20 +09002610func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002611 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002612 apex {
2613 name: "myapex",
2614 key: "myapex.key",
2615 multilib: {
2616 both: {
2617 native_shared_libs: ["mylib"],
2618 binaries: ["mybin"],
2619 },
2620 },
2621 compile_multilib: "both",
2622 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002623 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002624 }
2625
2626 apex_key {
2627 name: "myapex.key",
2628 public_key: "testkey.avbpubkey",
2629 private_key: "testkey.pem",
2630 }
2631
2632 cc_library {
2633 name: "mylib",
2634 relative_install_path: "foo/bar",
2635 system_shared_libs: [],
2636 stl: "none",
2637 apex_available: [ "myapex" ],
2638 native_bridge_supported: true,
2639 }
2640
2641 cc_binary {
2642 name: "mybin",
2643 relative_install_path: "foo/bar",
2644 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002645 stl: "none",
2646 apex_available: [ "myapex" ],
2647 native_bridge_supported: true,
2648 compile_multilib: "both", // default is "first" for binary
2649 multilib: {
2650 lib64: {
2651 suffix: "64",
2652 },
2653 },
2654 }
2655 `, withNativeBridgeEnabled)
2656 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2657 "bin/foo/bar/mybin",
2658 "bin/foo/bar/mybin64",
2659 "bin/arm/foo/bar/mybin",
2660 "bin/arm64/foo/bar/mybin64",
2661 "lib/foo/bar/mylib.so",
2662 "lib/arm/foo/bar/mylib.so",
2663 "lib64/foo/bar/mylib.so",
2664 "lib64/arm64/foo/bar/mylib.so",
2665 })
2666}
2667
Jooyung Han85d61762020-06-24 23:50:26 +09002668func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002669 result := android.GroupFixturePreparers(
2670 prepareForApexTest,
2671 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2672 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002673 apex {
2674 name: "myapex",
2675 key: "myapex.key",
2676 binaries: ["mybin"],
2677 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002678 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002679 }
2680 apex_key {
2681 name: "myapex.key",
2682 public_key: "testkey.avbpubkey",
2683 private_key: "testkey.pem",
2684 }
2685 cc_binary {
2686 name: "mybin",
2687 vendor: true,
2688 shared_libs: ["libfoo"],
2689 }
2690 cc_library {
2691 name: "libfoo",
2692 proprietary: true,
2693 }
2694 `)
2695
Colin Crossc68db4b2021-11-11 18:59:15 -08002696 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002697 "bin/mybin",
2698 "lib64/libfoo.so",
2699 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2700 "lib64/libc++.so",
2701 })
2702
Colin Crossc68db4b2021-11-11 18:59:15 -08002703 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2704 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002705 name := apexBundle.BaseModuleName()
2706 prefix := "TARGET_"
2707 var builder strings.Builder
2708 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002709 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002710 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002711 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002712
Colin Crossc68db4b2021-11-11 18:59:15 -08002713 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002714 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2715 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002716}
2717
Jooyung Hanc5a96762022-02-04 11:54:50 +09002718func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2719 testApexError(t, `Trying to include a VNDK library`, `
2720 apex {
2721 name: "myapex",
2722 key: "myapex.key",
2723 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2724 vendor: true,
2725 use_vndk_as_stable: true,
2726 updatable: false,
2727 }
2728 apex_key {
2729 name: "myapex.key",
2730 public_key: "testkey.avbpubkey",
2731 private_key: "testkey.pem",
2732 }`)
2733}
2734
Jooyung Handf78e212020-07-22 15:54:47 +09002735func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002736 // myapex myapex2
2737 // | |
2738 // mybin ------. mybin2
2739 // \ \ / |
2740 // (stable) .---\--------` |
2741 // \ / \ |
2742 // \ / \ /
2743 // libvndk libvendor
2744 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002745 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002746 apex {
2747 name: "myapex",
2748 key: "myapex.key",
2749 binaries: ["mybin"],
2750 vendor: true,
2751 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002752 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002753 }
2754 apex_key {
2755 name: "myapex.key",
2756 public_key: "testkey.avbpubkey",
2757 private_key: "testkey.pem",
2758 }
2759 cc_binary {
2760 name: "mybin",
2761 vendor: true,
2762 shared_libs: ["libvndk", "libvendor"],
2763 }
2764 cc_library {
2765 name: "libvndk",
2766 vndk: {
2767 enabled: true,
2768 },
2769 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002770 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002771 }
2772 cc_library {
2773 name: "libvendor",
2774 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002775 stl: "none",
2776 }
2777 apex {
2778 name: "myapex2",
2779 key: "myapex.key",
2780 binaries: ["mybin2"],
2781 vendor: true,
2782 use_vndk_as_stable: false,
2783 updatable: false,
2784 }
2785 cc_binary {
2786 name: "mybin2",
2787 vendor: true,
2788 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002789 }
2790 `)
2791
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002792 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002793
Jooyung Han91f92032022-02-04 12:36:33 +09002794 for _, tc := range []struct {
2795 name string
2796 apexName string
2797 moduleName string
2798 moduleVariant string
2799 libs []string
2800 contents []string
2801 requireVndkNamespace bool
2802 }{
2803 {
2804 name: "use_vndk_as_stable",
2805 apexName: "myapex",
2806 moduleName: "mybin",
2807 moduleVariant: vendorVariant + "_apex10000",
2808 libs: []string{
2809 // should link with vendor variants of VNDK libs(libvndk/libc++)
2810 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2811 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2812 // unstable Vendor libs as APEX variant
2813 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2814 },
2815 contents: []string{
2816 "bin/mybin",
2817 "lib64/libvendor.so",
2818 // VNDK libs (libvndk/libc++) are not included
2819 },
2820 requireVndkNamespace: true,
2821 },
2822 {
2823 name: "!use_vndk_as_stable",
2824 apexName: "myapex2",
2825 moduleName: "mybin2",
2826 moduleVariant: vendorVariant + "_myapex2",
2827 libs: []string{
2828 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2829 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2830 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2831 // unstable vendor libs have "merged" APEX variants
2832 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2833 },
2834 contents: []string{
2835 "bin/mybin2",
2836 "lib64/libvendor.so",
2837 // VNDK libs are included as well
2838 "lib64/libvndk.so",
2839 "lib64/libc++.so",
2840 },
2841 requireVndkNamespace: false,
2842 },
2843 } {
2844 t.Run(tc.name, func(t *testing.T) {
2845 // Check linked libs
2846 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2847 libs := names(ldRule.Args["libFlags"])
2848 for _, lib := range tc.libs {
2849 ensureListContains(t, libs, lib)
2850 }
2851 // Check apex contents
2852 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002853
Jooyung Han91f92032022-02-04 12:36:33 +09002854 // Check "requireNativeLibs"
2855 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2856 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2857 if tc.requireVndkNamespace {
2858 ensureListContains(t, requireNativeLibs, ":vndk")
2859 } else {
2860 ensureListNotContains(t, requireNativeLibs, ":vndk")
2861 }
2862 })
2863 }
Jooyung Handf78e212020-07-22 15:54:47 +09002864}
2865
Justin Yun13decfb2021-03-08 19:25:55 +09002866func TestProductVariant(t *testing.T) {
2867 ctx := testApex(t, `
2868 apex {
2869 name: "myapex",
2870 key: "myapex.key",
2871 updatable: false,
2872 product_specific: true,
2873 binaries: ["foo"],
2874 }
2875
2876 apex_key {
2877 name: "myapex.key",
2878 public_key: "testkey.avbpubkey",
2879 private_key: "testkey.pem",
2880 }
2881
2882 cc_binary {
2883 name: "foo",
2884 product_available: true,
2885 apex_available: ["myapex"],
2886 srcs: ["foo.cpp"],
2887 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002888 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2889 variables.ProductVndkVersion = proptools.StringPtr("current")
2890 }),
2891 )
Justin Yun13decfb2021-03-08 19:25:55 +09002892
2893 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002894 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002895 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2896 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2897 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2898 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2899}
2900
Jooyung Han8e5685d2020-09-21 11:02:57 +09002901func TestApex_withPrebuiltFirmware(t *testing.T) {
2902 testCases := []struct {
2903 name string
2904 additionalProp string
2905 }{
2906 {"system apex with prebuilt_firmware", ""},
2907 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2908 }
2909 for _, tc := range testCases {
2910 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002911 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002912 apex {
2913 name: "myapex",
2914 key: "myapex.key",
2915 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002916 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002917 `+tc.additionalProp+`
2918 }
2919 apex_key {
2920 name: "myapex.key",
2921 public_key: "testkey.avbpubkey",
2922 private_key: "testkey.pem",
2923 }
2924 prebuilt_firmware {
2925 name: "myfirmware",
2926 src: "myfirmware.bin",
2927 filename_from_src: true,
2928 `+tc.additionalProp+`
2929 }
2930 `)
2931 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2932 "etc/firmware/myfirmware.bin",
2933 })
2934 })
2935 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002936}
2937
Jooyung Hanefb184e2020-06-25 17:14:25 +09002938func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002939 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002940 apex {
2941 name: "myapex",
2942 key: "myapex.key",
2943 vendor: true,
2944 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002945 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002946 }
2947
2948 apex_key {
2949 name: "myapex.key",
2950 public_key: "testkey.avbpubkey",
2951 private_key: "testkey.pem",
2952 }
2953
2954 cc_library {
2955 name: "mylib",
2956 vendor_available: true,
2957 }
2958 `)
2959
2960 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002961 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002962 name := apexBundle.BaseModuleName()
2963 prefix := "TARGET_"
2964 var builder strings.Builder
2965 data.Custom(&builder, name, prefix, "", data)
2966 androidMk := builder.String()
2967 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2968}
2969
Jooyung Han2ed99d02020-06-24 23:26:26 +09002970func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002971 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002972 apex {
2973 name: "myapex",
2974 key: "myapex.key",
2975 vintf_fragments: ["fragment.xml"],
2976 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002977 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002978 }
2979 apex_key {
2980 name: "myapex.key",
2981 public_key: "testkey.avbpubkey",
2982 private_key: "testkey.pem",
2983 }
2984 cc_binary {
2985 name: "mybin",
2986 }
2987 `)
2988
2989 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002990 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002991 name := apexBundle.BaseModuleName()
2992 prefix := "TARGET_"
2993 var builder strings.Builder
2994 data.Custom(&builder, name, prefix, "", data)
2995 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002996 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002997 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002998}
2999
Jiyong Park16e91a02018-12-20 18:18:08 +09003000func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003001 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003002 apex {
3003 name: "myapex",
3004 key: "myapex.key",
3005 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003006 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003007 }
3008
3009 apex_key {
3010 name: "myapex.key",
3011 public_key: "testkey.avbpubkey",
3012 private_key: "testkey.pem",
3013 }
3014
3015 cc_library {
3016 name: "mylib",
3017 srcs: ["mylib.cpp"],
3018 system_shared_libs: [],
3019 stl: "none",
3020 stubs: {
3021 versions: ["1", "2", "3"],
3022 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003023 apex_available: [
3024 "//apex_available:platform",
3025 "myapex",
3026 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003027 }
3028
3029 cc_binary {
3030 name: "not_in_apex",
3031 srcs: ["mylib.cpp"],
3032 static_libs: ["mylib"],
3033 static_executable: true,
3034 system_shared_libs: [],
3035 stl: "none",
3036 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003037 `)
3038
Colin Cross7113d202019-11-20 16:39:12 -08003039 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003040
3041 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003042 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003043}
Jiyong Park9335a262018-12-24 11:31:58 +09003044
3045func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003046 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003047 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003048 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003049 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003050 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003051 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003052 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003053 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003054 }
3055
3056 cc_library {
3057 name: "mylib",
3058 srcs: ["mylib.cpp"],
3059 system_shared_libs: [],
3060 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003061 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003062 }
3063
3064 apex_key {
3065 name: "myapex.key",
3066 public_key: "testkey.avbpubkey",
3067 private_key: "testkey.pem",
3068 }
3069
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003070 android_app_certificate {
3071 name: "myapex.certificate",
3072 certificate: "testkey",
3073 }
3074
3075 android_app_certificate {
3076 name: "myapex.certificate.override",
3077 certificate: "testkey.override",
3078 }
3079
Jiyong Park9335a262018-12-24 11:31:58 +09003080 `)
3081
3082 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003083 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003084
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003085 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3086 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003087 "vendor/foo/devkeys/testkey.avbpubkey")
3088 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003089 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3090 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003091 "vendor/foo/devkeys/testkey.pem")
3092 }
3093
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003094 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003095 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003096 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003097 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003098 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003099 }
3100}
Jiyong Park58e364a2019-01-19 19:24:06 +09003101
Jooyung Hanf121a652019-12-17 14:30:11 +09003102func TestCertificate(t *testing.T) {
3103 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003104 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003105 apex {
3106 name: "myapex",
3107 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003108 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003109 }
3110 apex_key {
3111 name: "myapex.key",
3112 public_key: "testkey.avbpubkey",
3113 private_key: "testkey.pem",
3114 }`)
3115 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3116 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3117 if actual := rule.Args["certificates"]; actual != expected {
3118 t.Errorf("certificates should be %q, not %q", expected, actual)
3119 }
3120 })
3121 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003122 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003123 apex {
3124 name: "myapex_keytest",
3125 key: "myapex.key",
3126 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003127 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003128 }
3129 apex_key {
3130 name: "myapex.key",
3131 public_key: "testkey.avbpubkey",
3132 private_key: "testkey.pem",
3133 }
3134 android_app_certificate {
3135 name: "myapex.certificate.override",
3136 certificate: "testkey.override",
3137 }`)
3138 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3139 expected := "testkey.override.x509.pem testkey.override.pk8"
3140 if actual := rule.Args["certificates"]; actual != expected {
3141 t.Errorf("certificates should be %q, not %q", expected, actual)
3142 }
3143 })
3144 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003145 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003146 apex {
3147 name: "myapex",
3148 key: "myapex.key",
3149 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003150 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003151 }
3152 apex_key {
3153 name: "myapex.key",
3154 public_key: "testkey.avbpubkey",
3155 private_key: "testkey.pem",
3156 }
3157 android_app_certificate {
3158 name: "myapex.certificate",
3159 certificate: "testkey",
3160 }`)
3161 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3162 expected := "testkey.x509.pem testkey.pk8"
3163 if actual := rule.Args["certificates"]; actual != expected {
3164 t.Errorf("certificates should be %q, not %q", expected, actual)
3165 }
3166 })
3167 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003168 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003169 apex {
3170 name: "myapex_keytest",
3171 key: "myapex.key",
3172 file_contexts: ":myapex-file_contexts",
3173 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003174 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003175 }
3176 apex_key {
3177 name: "myapex.key",
3178 public_key: "testkey.avbpubkey",
3179 private_key: "testkey.pem",
3180 }
3181 android_app_certificate {
3182 name: "myapex.certificate.override",
3183 certificate: "testkey.override",
3184 }`)
3185 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3186 expected := "testkey.override.x509.pem testkey.override.pk8"
3187 if actual := rule.Args["certificates"]; actual != expected {
3188 t.Errorf("certificates should be %q, not %q", expected, actual)
3189 }
3190 })
3191 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003192 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003193 apex {
3194 name: "myapex",
3195 key: "myapex.key",
3196 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003197 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003198 }
3199 apex_key {
3200 name: "myapex.key",
3201 public_key: "testkey.avbpubkey",
3202 private_key: "testkey.pem",
3203 }`)
3204 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3205 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3206 if actual := rule.Args["certificates"]; actual != expected {
3207 t.Errorf("certificates should be %q, not %q", expected, actual)
3208 }
3209 })
3210 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003211 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003212 apex {
3213 name: "myapex_keytest",
3214 key: "myapex.key",
3215 file_contexts: ":myapex-file_contexts",
3216 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003217 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003218 }
3219 apex_key {
3220 name: "myapex.key",
3221 public_key: "testkey.avbpubkey",
3222 private_key: "testkey.pem",
3223 }
3224 android_app_certificate {
3225 name: "myapex.certificate.override",
3226 certificate: "testkey.override",
3227 }`)
3228 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3229 expected := "testkey.override.x509.pem testkey.override.pk8"
3230 if actual := rule.Args["certificates"]; actual != expected {
3231 t.Errorf("certificates should be %q, not %q", expected, actual)
3232 }
3233 })
3234}
3235
Jiyong Park58e364a2019-01-19 19:24:06 +09003236func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003237 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003238 apex {
3239 name: "myapex",
3240 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003241 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003242 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003243 }
3244
3245 apex {
3246 name: "otherapex",
3247 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003248 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003249 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003250 }
3251
3252 apex_key {
3253 name: "myapex.key",
3254 public_key: "testkey.avbpubkey",
3255 private_key: "testkey.pem",
3256 }
3257
3258 cc_library {
3259 name: "mylib",
3260 srcs: ["mylib.cpp"],
3261 system_shared_libs: [],
3262 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003263 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003264 "myapex",
3265 "otherapex",
3266 ],
Jooyung Han24282772020-03-21 23:20:55 +09003267 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003268 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003269 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003270 cc_library {
3271 name: "mylib2",
3272 srcs: ["mylib.cpp"],
3273 system_shared_libs: [],
3274 stl: "none",
3275 apex_available: [
3276 "myapex",
3277 "otherapex",
3278 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003279 static_libs: ["mylib3"],
3280 recovery_available: true,
3281 min_sdk_version: "29",
3282 }
3283 cc_library {
3284 name: "mylib3",
3285 srcs: ["mylib.cpp"],
3286 system_shared_libs: [],
3287 stl: "none",
3288 apex_available: [
3289 "myapex",
3290 "otherapex",
3291 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003292 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003293 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003294 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003295 `)
3296
Jooyung Hanc87a0592020-03-02 17:44:33 +09003297 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003298 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003299 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003300 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003301
Jooyung Hanccce2f22020-03-07 03:45:53 +09003302 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003303 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003304 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003305 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003306
Jooyung Hanccce2f22020-03-07 03:45:53 +09003307 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003308 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003309 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003310 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jiyong Park58e364a2019-01-19 19:24:06 +09003311
Colin Crossaede88c2020-08-11 12:17:01 -07003312 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3313 // each variant defines additional macros to distinguish which apex variant it is built for
3314
3315 // non-APEX variant does not have __ANDROID_APEX__ defined
3316 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3317 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3318
Dan Albertb19953d2020-11-17 15:29:36 -08003319 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003320 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3321 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003322 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003323
Jooyung Hanc87a0592020-03-02 17:44:33 +09003324 // non-APEX variant does not have __ANDROID_APEX__ defined
3325 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3326 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3327
Dan Albertb19953d2020-11-17 15:29:36 -08003328 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003329 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003330 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003331 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003332}
Jiyong Park7e636d02019-01-28 16:16:54 +09003333
3334func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003335 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003336 apex {
3337 name: "myapex",
3338 key: "myapex.key",
3339 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003340 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003341 }
3342
3343 apex_key {
3344 name: "myapex.key",
3345 public_key: "testkey.avbpubkey",
3346 private_key: "testkey.pem",
3347 }
3348
3349 cc_library_headers {
3350 name: "mylib_headers",
3351 export_include_dirs: ["my_include"],
3352 system_shared_libs: [],
3353 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003354 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003355 }
3356
3357 cc_library {
3358 name: "mylib",
3359 srcs: ["mylib.cpp"],
3360 system_shared_libs: [],
3361 stl: "none",
3362 header_libs: ["mylib_headers"],
3363 export_header_lib_headers: ["mylib_headers"],
3364 stubs: {
3365 versions: ["1", "2", "3"],
3366 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003367 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003368 }
3369
3370 cc_library {
3371 name: "otherlib",
3372 srcs: ["mylib.cpp"],
3373 system_shared_libs: [],
3374 stl: "none",
3375 shared_libs: ["mylib"],
3376 }
3377 `)
3378
Colin Cross7113d202019-11-20 16:39:12 -08003379 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003380
3381 // Ensure that the include path of the header lib is exported to 'otherlib'
3382 ensureContains(t, cFlags, "-Imy_include")
3383}
Alex Light9670d332019-01-29 18:07:33 -08003384
Jiyong Park7cd10e32020-01-14 09:22:18 +09003385type fileInApex struct {
3386 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003387 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003388 isLink bool
3389}
3390
Jooyung Hana57af4a2020-01-23 05:36:59 +00003391func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003392 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003393 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003394 copyCmds := apexRule.Args["copy_commands"]
3395 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003396 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 for _, cmd := range strings.Split(copyCmds, "&&") {
3398 cmd = strings.TrimSpace(cmd)
3399 if cmd == "" {
3400 continue
3401 }
3402 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003403 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003404 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003405 switch terms[0] {
3406 case "mkdir":
3407 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003408 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003409 t.Fatal("copyCmds contains invalid cp command", cmd)
3410 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003411 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003412 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003413 isLink = false
3414 case "ln":
3415 if len(terms) != 3 && len(terms) != 4 {
3416 // ln LINK TARGET or ln -s LINK TARGET
3417 t.Fatal("copyCmds contains invalid ln command", cmd)
3418 }
3419 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003420 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003421 isLink = true
3422 default:
3423 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3424 }
3425 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003426 index := strings.Index(dst, imageApexDir)
3427 if index == -1 {
3428 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3429 }
3430 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003431 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003432 }
3433 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003434 return ret
3435}
3436
Jooyung Hana57af4a2020-01-23 05:36:59 +00003437func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3438 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003439 var failed bool
3440 var surplus []string
3441 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003442 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003443 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003444 for _, expected := range files {
3445 if matched, _ := path.Match(expected, file.path); matched {
3446 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003447 mactchFound = true
3448 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003449 }
3450 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003451 if !mactchFound {
3452 surplus = append(surplus, file.path)
3453 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003454 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003455
Jooyung Han31c470b2019-10-18 16:26:59 +09003456 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003457 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003458 t.Log("surplus files", surplus)
3459 failed = true
3460 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003461
3462 if len(files) > len(filesMatched) {
3463 var missing []string
3464 for _, expected := range files {
3465 if !filesMatched[expected] {
3466 missing = append(missing, expected)
3467 }
3468 }
3469 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003470 t.Log("missing files", missing)
3471 failed = true
3472 }
3473 if failed {
3474 t.Fail()
3475 }
3476}
3477
Jooyung Han344d5432019-08-23 11:17:39 +09003478func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003479 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003480 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003481 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003482 "etc/llndk.libraries.29.txt",
3483 "etc/vndkcore.libraries.29.txt",
3484 "etc/vndksp.libraries.29.txt",
3485 "etc/vndkprivate.libraries.29.txt",
3486 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003487 }
3488 testCases := []struct {
3489 vndkVersion string
3490 expectedFiles []string
3491 }{
3492 {
3493 vndkVersion: "current",
3494 expectedFiles: append(commonFiles,
3495 "lib/libvndk.so",
3496 "lib/libvndksp.so",
3497 "lib64/libvndk.so",
3498 "lib64/libvndksp.so"),
3499 },
3500 {
3501 vndkVersion: "",
3502 expectedFiles: append(commonFiles,
3503 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3504 "lib/libvndksp.so",
3505 "lib64/libvndksp.so"),
3506 },
3507 }
3508 for _, tc := range testCases {
3509 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3510 ctx := testApex(t, `
3511 apex_vndk {
3512 name: "com.android.vndk.current",
3513 key: "com.android.vndk.current.key",
3514 updatable: false,
3515 }
3516
3517 apex_key {
3518 name: "com.android.vndk.current.key",
3519 public_key: "testkey.avbpubkey",
3520 private_key: "testkey.pem",
3521 }
3522
3523 cc_library {
3524 name: "libvndk",
3525 srcs: ["mylib.cpp"],
3526 vendor_available: true,
3527 product_available: true,
3528 vndk: {
3529 enabled: true,
3530 },
3531 system_shared_libs: [],
3532 stl: "none",
3533 apex_available: [ "com.android.vndk.current" ],
3534 }
3535
3536 cc_library {
3537 name: "libvndksp",
3538 srcs: ["mylib.cpp"],
3539 vendor_available: true,
3540 product_available: true,
3541 vndk: {
3542 enabled: true,
3543 support_system_process: true,
3544 },
3545 system_shared_libs: [],
3546 stl: "none",
3547 apex_available: [ "com.android.vndk.current" ],
3548 }
3549
3550 // VNDK-Ext should not cause any problems
3551
3552 cc_library {
3553 name: "libvndk.ext",
3554 srcs: ["mylib2.cpp"],
3555 vendor: true,
3556 vndk: {
3557 enabled: true,
3558 extends: "libvndk",
3559 },
3560 system_shared_libs: [],
3561 stl: "none",
3562 }
3563
3564 cc_library {
3565 name: "libvndksp.ext",
3566 srcs: ["mylib2.cpp"],
3567 vendor: true,
3568 vndk: {
3569 enabled: true,
3570 support_system_process: true,
3571 extends: "libvndksp",
3572 },
3573 system_shared_libs: [],
3574 stl: "none",
3575 }
3576 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3577 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3578 }))
3579 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3580 })
3581 }
Jooyung Han344d5432019-08-23 11:17:39 +09003582}
3583
3584func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003585 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003586 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003587 name: "com.android.vndk.current",
3588 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003589 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003590 }
3591
3592 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003593 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003594 public_key: "testkey.avbpubkey",
3595 private_key: "testkey.pem",
3596 }
3597
3598 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003599 name: "libvndk",
3600 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003601 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003602 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003603 vndk: {
3604 enabled: true,
3605 },
3606 system_shared_libs: [],
3607 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003608 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003609 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003610
3611 cc_prebuilt_library_shared {
3612 name: "libvndk.arm",
3613 srcs: ["libvndk.arm.so"],
3614 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003615 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003616 vndk: {
3617 enabled: true,
3618 },
3619 enabled: false,
3620 arch: {
3621 arm: {
3622 enabled: true,
3623 },
3624 },
3625 system_shared_libs: [],
3626 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003627 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003628 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003629 `+vndkLibrariesTxtFiles("current"),
3630 withFiles(map[string][]byte{
3631 "libvndk.so": nil,
3632 "libvndk.arm.so": nil,
3633 }))
Colin Cross2807f002021-03-02 10:15:29 -08003634 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003635 "lib/libvndk.so",
3636 "lib/libvndk.arm.so",
3637 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003638 "lib/libc++.so",
3639 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003640 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003641 })
Jooyung Han344d5432019-08-23 11:17:39 +09003642}
3643
Jooyung Han39edb6c2019-11-06 16:53:07 +09003644func vndkLibrariesTxtFiles(vers ...string) (result string) {
3645 for _, v := range vers {
3646 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003647 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003648 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003649 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003650 name: "` + txt + `.libraries.txt",
3651 }
3652 `
3653 }
3654 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003655 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003656 result += `
3657 prebuilt_etc {
3658 name: "` + txt + `.libraries.` + v + `.txt",
3659 src: "dummy.txt",
3660 }
3661 `
3662 }
3663 }
3664 }
3665 return
3666}
3667
Jooyung Han344d5432019-08-23 11:17:39 +09003668func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003669 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003670 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003671 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003672 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003673 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003674 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003675 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003676 }
3677
3678 apex_key {
3679 name: "myapex.key",
3680 public_key: "testkey.avbpubkey",
3681 private_key: "testkey.pem",
3682 }
3683
Jooyung Han31c470b2019-10-18 16:26:59 +09003684 vndk_prebuilt_shared {
3685 name: "libvndk27",
3686 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003687 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003688 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003689 vndk: {
3690 enabled: true,
3691 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003692 target_arch: "arm64",
3693 arch: {
3694 arm: {
3695 srcs: ["libvndk27_arm.so"],
3696 },
3697 arm64: {
3698 srcs: ["libvndk27_arm64.so"],
3699 },
3700 },
Colin Cross2807f002021-03-02 10:15:29 -08003701 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003702 }
3703
3704 vndk_prebuilt_shared {
3705 name: "libvndk27",
3706 version: "27",
3707 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003708 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003709 vndk: {
3710 enabled: true,
3711 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003712 target_arch: "x86_64",
3713 arch: {
3714 x86: {
3715 srcs: ["libvndk27_x86.so"],
3716 },
3717 x86_64: {
3718 srcs: ["libvndk27_x86_64.so"],
3719 },
3720 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003721 }
3722 `+vndkLibrariesTxtFiles("27"),
3723 withFiles(map[string][]byte{
3724 "libvndk27_arm.so": nil,
3725 "libvndk27_arm64.so": nil,
3726 "libvndk27_x86.so": nil,
3727 "libvndk27_x86_64.so": nil,
3728 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003729
Colin Cross2807f002021-03-02 10:15:29 -08003730 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003731 "lib/libvndk27_arm.so",
3732 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003733 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003734 })
Jooyung Han344d5432019-08-23 11:17:39 +09003735}
3736
Jooyung Han90eee022019-10-01 20:02:42 +09003737func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003738 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003739 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003740 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003741 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003742 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003743 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003744 }
3745 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003746 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003747 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003748 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003749 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003750 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003751 }
3752 apex_key {
3753 name: "myapex.key",
3754 public_key: "testkey.avbpubkey",
3755 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003756 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003757
3758 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003759 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003760 actual := proptools.String(bundle.properties.Apex_name)
3761 if !reflect.DeepEqual(actual, expected) {
3762 t.Errorf("Got '%v', expected '%v'", actual, expected)
3763 }
3764 }
3765
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003766 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003767 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003768}
3769
Jooyung Han344d5432019-08-23 11:17:39 +09003770func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003771 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003772 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003773 name: "com.android.vndk.current",
3774 key: "com.android.vndk.current.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 Han344d5432019-08-23 11:17:39 +09003777 }
3778
3779 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003780 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003781 public_key: "testkey.avbpubkey",
3782 private_key: "testkey.pem",
3783 }
3784
3785 cc_library {
3786 name: "libvndk",
3787 srcs: ["mylib.cpp"],
3788 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003789 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003790 native_bridge_supported: true,
3791 host_supported: true,
3792 vndk: {
3793 enabled: true,
3794 },
3795 system_shared_libs: [],
3796 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003797 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003798 }
Colin Cross2807f002021-03-02 10:15:29 -08003799 `+vndkLibrariesTxtFiles("current"),
3800 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003801
Colin Cross2807f002021-03-02 10:15:29 -08003802 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003803 "lib/libvndk.so",
3804 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003805 "lib/libc++.so",
3806 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003807 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003808 })
Jooyung Han344d5432019-08-23 11:17:39 +09003809}
3810
3811func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003812 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003813 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003814 name: "com.android.vndk.current",
3815 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003816 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003817 native_bridge_supported: true,
3818 }
3819
3820 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003821 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003822 public_key: "testkey.avbpubkey",
3823 private_key: "testkey.pem",
3824 }
3825
3826 cc_library {
3827 name: "libvndk",
3828 srcs: ["mylib.cpp"],
3829 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003830 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003831 native_bridge_supported: true,
3832 host_supported: true,
3833 vndk: {
3834 enabled: true,
3835 },
3836 system_shared_libs: [],
3837 stl: "none",
3838 }
3839 `)
3840}
3841
Jooyung Han31c470b2019-10-18 16:26:59 +09003842func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003843 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003844 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003845 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003846 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003847 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003848 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003849 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003850 }
3851
3852 apex_key {
3853 name: "myapex.key",
3854 public_key: "testkey.avbpubkey",
3855 private_key: "testkey.pem",
3856 }
3857
3858 vndk_prebuilt_shared {
3859 name: "libvndk27",
3860 version: "27",
3861 target_arch: "arm",
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003864 vndk: {
3865 enabled: true,
3866 },
3867 arch: {
3868 arm: {
3869 srcs: ["libvndk27.so"],
3870 }
3871 },
3872 }
3873
3874 vndk_prebuilt_shared {
3875 name: "libvndk27",
3876 version: "27",
3877 target_arch: "arm",
3878 binder32bit: true,
3879 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003880 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003881 vndk: {
3882 enabled: true,
3883 },
3884 arch: {
3885 arm: {
3886 srcs: ["libvndk27binder32.so"],
3887 }
3888 },
Colin Cross2807f002021-03-02 10:15:29 -08003889 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003890 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003891 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003892 withFiles(map[string][]byte{
3893 "libvndk27.so": nil,
3894 "libvndk27binder32.so": nil,
3895 }),
3896 withBinder32bit,
3897 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003898 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003899 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3900 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003901 },
3902 }),
3903 )
3904
Colin Cross2807f002021-03-02 10:15:29 -08003905 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003906 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003907 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003908 })
3909}
3910
Jooyung Han45a96772020-06-15 14:59:42 +09003911func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003912 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003913 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003914 name: "com.android.vndk.current",
3915 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003916 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003917 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003918 }
3919
3920 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003921 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003922 public_key: "testkey.avbpubkey",
3923 private_key: "testkey.pem",
3924 }
3925
3926 cc_library {
3927 name: "libz",
3928 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003929 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003930 vndk: {
3931 enabled: true,
3932 },
3933 stubs: {
3934 symbol_file: "libz.map.txt",
3935 versions: ["30"],
3936 }
3937 }
3938 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3939 "libz.map.txt": nil,
3940 }))
3941
Colin Cross2807f002021-03-02 10:15:29 -08003942 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003943 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3944 ensureListEmpty(t, provideNativeLibs)
3945}
3946
Jooyung Hane1633032019-08-01 17:41:43 +09003947func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003948 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003949 apex {
3950 name: "myapex_nodep",
3951 key: "myapex.key",
3952 native_shared_libs: ["lib_nodep"],
3953 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003954 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003955 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003956 }
3957
3958 apex {
3959 name: "myapex_dep",
3960 key: "myapex.key",
3961 native_shared_libs: ["lib_dep"],
3962 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003963 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003964 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003965 }
3966
3967 apex {
3968 name: "myapex_provider",
3969 key: "myapex.key",
3970 native_shared_libs: ["libfoo"],
3971 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003972 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003973 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003974 }
3975
3976 apex {
3977 name: "myapex_selfcontained",
3978 key: "myapex.key",
3979 native_shared_libs: ["lib_dep", "libfoo"],
3980 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003981 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003982 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003983 }
3984
3985 apex_key {
3986 name: "myapex.key",
3987 public_key: "testkey.avbpubkey",
3988 private_key: "testkey.pem",
3989 }
3990
3991 cc_library {
3992 name: "lib_nodep",
3993 srcs: ["mylib.cpp"],
3994 system_shared_libs: [],
3995 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003996 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003997 }
3998
3999 cc_library {
4000 name: "lib_dep",
4001 srcs: ["mylib.cpp"],
4002 shared_libs: ["libfoo"],
4003 system_shared_libs: [],
4004 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004005 apex_available: [
4006 "myapex_dep",
4007 "myapex_provider",
4008 "myapex_selfcontained",
4009 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004010 }
4011
4012 cc_library {
4013 name: "libfoo",
4014 srcs: ["mytest.cpp"],
4015 stubs: {
4016 versions: ["1"],
4017 },
4018 system_shared_libs: [],
4019 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004020 apex_available: [
4021 "myapex_provider",
4022 "myapex_selfcontained",
4023 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004024 }
4025 `)
4026
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004027 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004028 var provideNativeLibs, requireNativeLibs []string
4029
Sundong Ahnabb64432019-10-22 13:58:29 +09004030 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004031 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4032 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004033 ensureListEmpty(t, provideNativeLibs)
4034 ensureListEmpty(t, requireNativeLibs)
4035
Sundong Ahnabb64432019-10-22 13:58:29 +09004036 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004037 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4038 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004039 ensureListEmpty(t, provideNativeLibs)
4040 ensureListContains(t, requireNativeLibs, "libfoo.so")
4041
Sundong Ahnabb64432019-10-22 13:58:29 +09004042 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004043 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4044 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004045 ensureListContains(t, provideNativeLibs, "libfoo.so")
4046 ensureListEmpty(t, requireNativeLibs)
4047
Sundong Ahnabb64432019-10-22 13:58:29 +09004048 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004049 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4050 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004051 ensureListContains(t, provideNativeLibs, "libfoo.so")
4052 ensureListEmpty(t, requireNativeLibs)
4053}
4054
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004055func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004056 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004057 apex {
4058 name: "myapex",
4059 key: "myapex.key",
4060 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004061 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004062 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004063 }
4064
4065 apex_key {
4066 name: "myapex.key",
4067 public_key: "testkey.avbpubkey",
4068 private_key: "testkey.pem",
4069 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004070
4071 cc_library {
4072 name: "mylib",
4073 srcs: ["mylib.cpp"],
4074 system_shared_libs: [],
4075 stl: "none",
4076 apex_available: [
4077 "//apex_available:platform",
4078 "myapex",
4079 ],
4080 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004081 `)
4082
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004083 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004084 apexManifestRule := module.Rule("apexManifestRule")
4085 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4086 apexRule := module.Rule("apexRule")
4087 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004088
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004089 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004090 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004091 name := apexBundle.BaseModuleName()
4092 prefix := "TARGET_"
4093 var builder strings.Builder
4094 data.Custom(&builder, name, prefix, "", data)
4095 androidMk := builder.String()
4096 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4097 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004098}
4099
Alex Light0851b882019-02-07 13:20:53 -08004100func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004101 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004102 apex {
4103 name: "myapex",
4104 key: "myapex.key",
4105 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004106 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004107 }
4108
4109 apex_key {
4110 name: "myapex.key",
4111 public_key: "testkey.avbpubkey",
4112 private_key: "testkey.pem",
4113 }
4114
4115 cc_library {
4116 name: "mylib_common",
4117 srcs: ["mylib.cpp"],
4118 system_shared_libs: [],
4119 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004120 apex_available: [
4121 "//apex_available:platform",
4122 "myapex",
4123 ],
Alex Light0851b882019-02-07 13:20:53 -08004124 }
4125 `)
4126
Sundong Ahnabb64432019-10-22 13:58:29 +09004127 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004128 apexRule := module.Rule("apexRule")
4129 copyCmds := apexRule.Args["copy_commands"]
4130
4131 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4132 t.Log("Apex was a test apex!")
4133 t.Fail()
4134 }
4135 // Ensure that main rule creates an output
4136 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4137
4138 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004139 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004140
4141 // Ensure that both direct and indirect deps are copied into apex
4142 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4143
Colin Cross7113d202019-11-20 16:39:12 -08004144 // Ensure that the platform variant ends with _shared
4145 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004146
Colin Cross56a83212020-09-15 18:30:11 -07004147 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004148 t.Log("Found mylib_common not in any apex!")
4149 t.Fail()
4150 }
4151}
4152
4153func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004154 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004155 apex_test {
4156 name: "myapex",
4157 key: "myapex.key",
4158 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004159 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004160 }
4161
4162 apex_key {
4163 name: "myapex.key",
4164 public_key: "testkey.avbpubkey",
4165 private_key: "testkey.pem",
4166 }
4167
4168 cc_library {
4169 name: "mylib_common_test",
4170 srcs: ["mylib.cpp"],
4171 system_shared_libs: [],
4172 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004173 // TODO: remove //apex_available:platform
4174 apex_available: [
4175 "//apex_available:platform",
4176 "myapex",
4177 ],
Alex Light0851b882019-02-07 13:20:53 -08004178 }
4179 `)
4180
Sundong Ahnabb64432019-10-22 13:58:29 +09004181 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004182 apexRule := module.Rule("apexRule")
4183 copyCmds := apexRule.Args["copy_commands"]
4184
4185 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4186 t.Log("Apex was not a test apex!")
4187 t.Fail()
4188 }
4189 // Ensure that main rule creates an output
4190 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4191
4192 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004193 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004194
4195 // Ensure that both direct and indirect deps are copied into apex
4196 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4197
Colin Cross7113d202019-11-20 16:39:12 -08004198 // Ensure that the platform variant ends with _shared
4199 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004200}
4201
Alex Light9670d332019-01-29 18:07:33 -08004202func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004203 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004204 apex {
4205 name: "myapex",
4206 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004207 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004208 multilib: {
4209 first: {
4210 native_shared_libs: ["mylib_common"],
4211 }
4212 },
4213 target: {
4214 android: {
4215 multilib: {
4216 first: {
4217 native_shared_libs: ["mylib"],
4218 }
4219 }
4220 },
4221 host: {
4222 multilib: {
4223 first: {
4224 native_shared_libs: ["mylib2"],
4225 }
4226 }
4227 }
4228 }
4229 }
4230
4231 apex_key {
4232 name: "myapex.key",
4233 public_key: "testkey.avbpubkey",
4234 private_key: "testkey.pem",
4235 }
4236
4237 cc_library {
4238 name: "mylib",
4239 srcs: ["mylib.cpp"],
4240 system_shared_libs: [],
4241 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004242 // TODO: remove //apex_available:platform
4243 apex_available: [
4244 "//apex_available:platform",
4245 "myapex",
4246 ],
Alex Light9670d332019-01-29 18:07:33 -08004247 }
4248
4249 cc_library {
4250 name: "mylib_common",
4251 srcs: ["mylib.cpp"],
4252 system_shared_libs: [],
4253 stl: "none",
4254 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004255 // TODO: remove //apex_available:platform
4256 apex_available: [
4257 "//apex_available:platform",
4258 "myapex",
4259 ],
Alex Light9670d332019-01-29 18:07:33 -08004260 }
4261
4262 cc_library {
4263 name: "mylib2",
4264 srcs: ["mylib.cpp"],
4265 system_shared_libs: [],
4266 stl: "none",
4267 compile_multilib: "first",
4268 }
4269 `)
4270
Sundong Ahnabb64432019-10-22 13:58:29 +09004271 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004272 copyCmds := apexRule.Args["copy_commands"]
4273
4274 // Ensure that main rule creates an output
4275 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4276
4277 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004278 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4279 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4280 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004281
4282 // Ensure that both direct and indirect deps are copied into apex
4283 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4284 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4285 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4286
Colin Cross7113d202019-11-20 16:39:12 -08004287 // Ensure that the platform variant ends with _shared
4288 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4289 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4290 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004291}
Jiyong Park04480cf2019-02-06 00:16:29 +09004292
Jiyong Park59140302020-12-14 18:44:04 +09004293func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004294 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004295 apex {
4296 name: "myapex",
4297 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004298 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004299 arch: {
4300 arm64: {
4301 native_shared_libs: ["mylib.arm64"],
4302 },
4303 x86_64: {
4304 native_shared_libs: ["mylib.x64"],
4305 },
4306 }
4307 }
4308
4309 apex_key {
4310 name: "myapex.key",
4311 public_key: "testkey.avbpubkey",
4312 private_key: "testkey.pem",
4313 }
4314
4315 cc_library {
4316 name: "mylib.arm64",
4317 srcs: ["mylib.cpp"],
4318 system_shared_libs: [],
4319 stl: "none",
4320 // TODO: remove //apex_available:platform
4321 apex_available: [
4322 "//apex_available:platform",
4323 "myapex",
4324 ],
4325 }
4326
4327 cc_library {
4328 name: "mylib.x64",
4329 srcs: ["mylib.cpp"],
4330 system_shared_libs: [],
4331 stl: "none",
4332 // TODO: remove //apex_available:platform
4333 apex_available: [
4334 "//apex_available:platform",
4335 "myapex",
4336 ],
4337 }
4338 `)
4339
4340 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4341 copyCmds := apexRule.Args["copy_commands"]
4342
4343 // Ensure that apex variant is created for the direct dep
4344 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4345 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4346
4347 // Ensure that both direct and indirect deps are copied into apex
4348 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4349 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4350}
4351
Jiyong Park04480cf2019-02-06 00:16:29 +09004352func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004353 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004354 apex {
4355 name: "myapex",
4356 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004357 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004358 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004359 }
4360
4361 apex_key {
4362 name: "myapex.key",
4363 public_key: "testkey.avbpubkey",
4364 private_key: "testkey.pem",
4365 }
4366
4367 sh_binary {
4368 name: "myscript",
4369 src: "mylib.cpp",
4370 filename: "myscript.sh",
4371 sub_dir: "script",
4372 }
4373 `)
4374
Sundong Ahnabb64432019-10-22 13:58:29 +09004375 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004376 copyCmds := apexRule.Args["copy_commands"]
4377
4378 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4379}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004380
Jooyung Han91df2082019-11-20 01:49:42 +09004381func TestApexInVariousPartition(t *testing.T) {
4382 testcases := []struct {
4383 propName, parition, flattenedPartition string
4384 }{
4385 {"", "system", "system_ext"},
4386 {"product_specific: true", "product", "product"},
4387 {"soc_specific: true", "vendor", "vendor"},
4388 {"proprietary: true", "vendor", "vendor"},
4389 {"vendor: true", "vendor", "vendor"},
4390 {"system_ext_specific: true", "system_ext", "system_ext"},
4391 }
4392 for _, tc := range testcases {
4393 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004394 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004395 apex {
4396 name: "myapex",
4397 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004398 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004399 `+tc.propName+`
4400 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004401
Jooyung Han91df2082019-11-20 01:49:42 +09004402 apex_key {
4403 name: "myapex.key",
4404 public_key: "testkey.avbpubkey",
4405 private_key: "testkey.pem",
4406 }
4407 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004408
Jooyung Han91df2082019-11-20 01:49:42 +09004409 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004410 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4411 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004412 if actual != expected {
4413 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4414 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004415
Jooyung Han91df2082019-11-20 01:49:42 +09004416 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004417 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4418 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004419 if actual != expected {
4420 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4421 }
4422 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004423 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004424}
Jiyong Park67882562019-03-21 01:11:21 +09004425
Jooyung Han580eb4f2020-06-24 19:33:06 +09004426func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004427 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004428 apex {
4429 name: "myapex",
4430 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004431 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004432 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004433
Jooyung Han580eb4f2020-06-24 19:33:06 +09004434 apex_key {
4435 name: "myapex.key",
4436 public_key: "testkey.avbpubkey",
4437 private_key: "testkey.pem",
4438 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004439 `)
4440 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004441 rule := module.Output("file_contexts")
4442 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4443}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004444
Jooyung Han580eb4f2020-06-24 19:33:06 +09004445func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004446 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004447 apex {
4448 name: "myapex",
4449 key: "myapex.key",
4450 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004451 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004452 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004453
Jooyung Han580eb4f2020-06-24 19:33:06 +09004454 apex_key {
4455 name: "myapex.key",
4456 public_key: "testkey.avbpubkey",
4457 private_key: "testkey.pem",
4458 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004459 `, withFiles(map[string][]byte{
4460 "my_own_file_contexts": nil,
4461 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004462}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004463
Jooyung Han580eb4f2020-06-24 19:33:06 +09004464func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004465 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004466 apex {
4467 name: "myapex",
4468 key: "myapex.key",
4469 product_specific: true,
4470 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004471 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004472 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004473
Jooyung Han580eb4f2020-06-24 19:33:06 +09004474 apex_key {
4475 name: "myapex.key",
4476 public_key: "testkey.avbpubkey",
4477 private_key: "testkey.pem",
4478 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004479 `)
4480
Colin Cross1c460562021-02-16 17:55:47 -08004481 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004482 apex {
4483 name: "myapex",
4484 key: "myapex.key",
4485 product_specific: true,
4486 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004487 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004488 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004489
Jooyung Han580eb4f2020-06-24 19:33:06 +09004490 apex_key {
4491 name: "myapex.key",
4492 public_key: "testkey.avbpubkey",
4493 private_key: "testkey.pem",
4494 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004495 `, withFiles(map[string][]byte{
4496 "product_specific_file_contexts": nil,
4497 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004498 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4499 rule := module.Output("file_contexts")
4500 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4501}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004502
Jooyung Han580eb4f2020-06-24 19:33:06 +09004503func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004504 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004505 apex {
4506 name: "myapex",
4507 key: "myapex.key",
4508 product_specific: true,
4509 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004510 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004511 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004512
Jooyung Han580eb4f2020-06-24 19:33:06 +09004513 apex_key {
4514 name: "myapex.key",
4515 public_key: "testkey.avbpubkey",
4516 private_key: "testkey.pem",
4517 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004518
Jooyung Han580eb4f2020-06-24 19:33:06 +09004519 filegroup {
4520 name: "my-file-contexts",
4521 srcs: ["product_specific_file_contexts"],
4522 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004523 `, withFiles(map[string][]byte{
4524 "product_specific_file_contexts": nil,
4525 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004526 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4527 rule := module.Output("file_contexts")
4528 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004529}
4530
Jiyong Park67882562019-03-21 01:11:21 +09004531func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004532 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004533 apex_key {
4534 name: "myapex.key",
4535 public_key: ":my.avbpubkey",
4536 private_key: ":my.pem",
4537 product_specific: true,
4538 }
4539
4540 filegroup {
4541 name: "my.avbpubkey",
4542 srcs: ["testkey2.avbpubkey"],
4543 }
4544
4545 filegroup {
4546 name: "my.pem",
4547 srcs: ["testkey2.pem"],
4548 }
4549 `)
4550
4551 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4552 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004553 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004554 if actual_pubkey != expected_pubkey {
4555 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4556 }
4557 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004558 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004559 if actual_privkey != expected_privkey {
4560 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4561 }
4562}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004563
4564func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004565 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004566 prebuilt_apex {
4567 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004568 arch: {
4569 arm64: {
4570 src: "myapex-arm64.apex",
4571 },
4572 arm: {
4573 src: "myapex-arm.apex",
4574 },
4575 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004576 }
4577 `)
4578
Wei Li340ee8e2022-03-18 17:33:24 -07004579 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4580 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004581
Jiyong Parkc95714e2019-03-29 14:23:10 +09004582 expectedInput := "myapex-arm64.apex"
4583 if prebuilt.inputApex.String() != expectedInput {
4584 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4585 }
Wei Li340ee8e2022-03-18 17:33:24 -07004586 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4587 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4588 rule := testingModule.Rule("genProvenanceMetaData")
4589 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4590 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4591 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4592 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004593}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004594
Paul Duffinc0609c62021-03-01 17:27:16 +00004595func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004596 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004597 prebuilt_apex {
4598 name: "myapex",
4599 }
4600 `)
4601}
4602
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004603func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004604 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004605 prebuilt_apex {
4606 name: "myapex",
4607 src: "myapex-arm.apex",
4608 filename: "notmyapex.apex",
4609 }
4610 `)
4611
Wei Li340ee8e2022-03-18 17:33:24 -07004612 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4613 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004614
4615 expected := "notmyapex.apex"
4616 if p.installFilename != expected {
4617 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4618 }
Wei Li340ee8e2022-03-18 17:33:24 -07004619 rule := testingModule.Rule("genProvenanceMetaData")
4620 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4621 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4622 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4623 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004624}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004625
Samiul Islam7c02e262021-09-08 17:48:28 +01004626func TestApexSetFilenameOverride(t *testing.T) {
4627 testApex(t, `
4628 apex_set {
4629 name: "com.company.android.myapex",
4630 apex_name: "com.android.myapex",
4631 set: "company-myapex.apks",
4632 filename: "com.company.android.myapex.apex"
4633 }
4634 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4635
4636 testApex(t, `
4637 apex_set {
4638 name: "com.company.android.myapex",
4639 apex_name: "com.android.myapex",
4640 set: "company-myapex.apks",
4641 filename: "com.company.android.myapex.capex"
4642 }
4643 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4644
4645 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4646 apex_set {
4647 name: "com.company.android.myapex",
4648 apex_name: "com.android.myapex",
4649 set: "company-myapex.apks",
4650 filename: "some-random-suffix"
4651 }
4652 `)
4653}
4654
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004655func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004656 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004657 prebuilt_apex {
4658 name: "myapex.prebuilt",
4659 src: "myapex-arm.apex",
4660 overrides: [
4661 "myapex",
4662 ],
4663 }
4664 `)
4665
Wei Li340ee8e2022-03-18 17:33:24 -07004666 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4667 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004668
4669 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004670 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004671 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004672 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004673 }
Wei Li340ee8e2022-03-18 17:33:24 -07004674 rule := testingModule.Rule("genProvenanceMetaData")
4675 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4676 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4677 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4678 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004679}
4680
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004681func TestPrebuiltApexName(t *testing.T) {
4682 testApex(t, `
4683 prebuilt_apex {
4684 name: "com.company.android.myapex",
4685 apex_name: "com.android.myapex",
4686 src: "company-myapex-arm.apex",
4687 }
4688 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4689
4690 testApex(t, `
4691 apex_set {
4692 name: "com.company.android.myapex",
4693 apex_name: "com.android.myapex",
4694 set: "company-myapex.apks",
4695 }
4696 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4697}
4698
4699func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4700 _ = android.GroupFixturePreparers(
4701 java.PrepareForTestWithJavaDefaultModules,
4702 PrepareForTestWithApexBuildComponents,
4703 android.FixtureWithRootAndroidBp(`
4704 platform_bootclasspath {
4705 name: "platform-bootclasspath",
4706 fragments: [
4707 {
4708 apex: "com.android.art",
4709 module: "art-bootclasspath-fragment",
4710 },
4711 ],
4712 }
4713
4714 prebuilt_apex {
4715 name: "com.company.android.art",
4716 apex_name: "com.android.art",
4717 src: "com.company.android.art-arm.apex",
4718 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4719 }
4720
4721 prebuilt_bootclasspath_fragment {
4722 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004723 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004724 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004725 hidden_api: {
4726 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4727 metadata: "my-bootclasspath-fragment/metadata.csv",
4728 index: "my-bootclasspath-fragment/index.csv",
4729 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4730 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4731 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004732 }
4733
4734 java_import {
4735 name: "core-oj",
4736 jars: ["prebuilt.jar"],
4737 }
4738 `),
4739 ).RunTest(t)
4740}
4741
Paul Duffin092153d2021-01-26 11:42:39 +00004742// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4743// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004744func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004745 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004746
Paul Duffin89886cb2021-02-05 16:44:03 +00004747 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004748 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004749 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004750 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004751 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004752 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004753 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4754 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4755 android.NormalizePathForTesting(dexJarBuildPath))
4756 }
4757
4758 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004759 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004760 // Make sure the import has been given the correct path to the dex jar.
4761 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4762 dexJarBuildPath := p.DexJarInstallPath()
4763 stem := android.RemoveOptionalPrebuiltPrefix(name)
4764 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4765 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4766 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004767 }
4768
Paul Duffin39853512021-02-26 11:09:39 +00004769 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004770 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004771 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004772 android.AssertArrayString(t, "Check if there is no source variant",
4773 []string{"android_common"},
4774 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004775 }
4776
4777 t.Run("prebuilt only", func(t *testing.T) {
4778 bp := `
4779 prebuilt_apex {
4780 name: "myapex",
4781 arch: {
4782 arm64: {
4783 src: "myapex-arm64.apex",
4784 },
4785 arm: {
4786 src: "myapex-arm.apex",
4787 },
4788 },
Paul Duffin39853512021-02-26 11:09:39 +00004789 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004790 }
4791
4792 java_import {
4793 name: "libfoo",
4794 jars: ["libfoo.jar"],
4795 }
Paul Duffin39853512021-02-26 11:09:39 +00004796
4797 java_sdk_library_import {
4798 name: "libbar",
4799 public: {
4800 jars: ["libbar.jar"],
4801 },
4802 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004803 `
4804
4805 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4806 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4807
Martin Stjernholm44825602021-09-17 01:44:12 +01004808 deapexerName := deapexerModuleName("myapex")
4809 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
4810
Paul Duffinf6932af2021-02-26 18:21:56 +00004811 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004812 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004813 rule := deapexer.Rule("deapexer")
4814 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4815 t.Errorf("expected: %q, found: %q", expected, actual)
4816 }
4817
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004818 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004819 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004820 rule = prebuiltApex.Rule("android/soong/android.Cp")
4821 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4822 t.Errorf("expected: %q, found: %q", expected, actual)
4823 }
4824
Paul Duffin89886cb2021-02-05 16:44:03 +00004825 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004826 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004827
4828 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004829 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004830 })
4831
4832 t.Run("prebuilt with source preferred", func(t *testing.T) {
4833
4834 bp := `
4835 prebuilt_apex {
4836 name: "myapex",
4837 arch: {
4838 arm64: {
4839 src: "myapex-arm64.apex",
4840 },
4841 arm: {
4842 src: "myapex-arm.apex",
4843 },
4844 },
Paul Duffin39853512021-02-26 11:09:39 +00004845 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004846 }
4847
4848 java_import {
4849 name: "libfoo",
4850 jars: ["libfoo.jar"],
4851 }
4852
4853 java_library {
4854 name: "libfoo",
4855 }
Paul Duffin39853512021-02-26 11:09:39 +00004856
4857 java_sdk_library_import {
4858 name: "libbar",
4859 public: {
4860 jars: ["libbar.jar"],
4861 },
4862 }
4863
4864 java_sdk_library {
4865 name: "libbar",
4866 srcs: ["foo/bar/MyClass.java"],
4867 unsafe_ignore_missing_latest_api: true,
4868 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004869 `
4870
4871 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4872 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4873
Paul Duffin89886cb2021-02-05 16:44:03 +00004874 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004875 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004876 ensureNoSourceVariant(t, ctx, "libfoo")
4877
4878 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004879 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004880 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004881 })
4882
4883 t.Run("prebuilt preferred with source", func(t *testing.T) {
4884 bp := `
4885 prebuilt_apex {
4886 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004887 arch: {
4888 arm64: {
4889 src: "myapex-arm64.apex",
4890 },
4891 arm: {
4892 src: "myapex-arm.apex",
4893 },
4894 },
Paul Duffin39853512021-02-26 11:09:39 +00004895 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004896 }
4897
4898 java_import {
4899 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004900 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004901 jars: ["libfoo.jar"],
4902 }
4903
4904 java_library {
4905 name: "libfoo",
4906 }
Paul Duffin39853512021-02-26 11:09:39 +00004907
4908 java_sdk_library_import {
4909 name: "libbar",
4910 prefer: true,
4911 public: {
4912 jars: ["libbar.jar"],
4913 },
4914 }
4915
4916 java_sdk_library {
4917 name: "libbar",
4918 srcs: ["foo/bar/MyClass.java"],
4919 unsafe_ignore_missing_latest_api: true,
4920 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004921 `
4922
4923 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4924 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4925
Paul Duffin89886cb2021-02-05 16:44:03 +00004926 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004927 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004928 ensureNoSourceVariant(t, ctx, "libfoo")
4929
4930 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004931 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004932 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004933 })
4934}
4935
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004936func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004937 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004938 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004939 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4940 // is disabled.
4941 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4942 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004943
Paul Duffin37856732021-02-26 14:24:15 +00004944 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4945 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004946 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004947 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004948 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004949 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004950 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004951 foundLibfooJar = true
4952 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004953 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004954 }
4955 }
4956 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02004957 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 +00004958 }
4959 }
4960
Paul Duffin40a3f652021-07-19 13:11:24 +01004961 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004962 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004963 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004964 var rule android.TestingBuildParams
4965
4966 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4967 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004968 }
4969
Paul Duffin40a3f652021-07-19 13:11:24 +01004970 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
4971 t.Helper()
4972 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4973 var rule android.TestingBuildParams
4974
4975 rule = platformBootclasspath.Output("hiddenapi-index.csv")
4976 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
4977 }
4978
Paul Duffin89f570a2021-06-16 01:42:33 +01004979 fragment := java.ApexVariantReference{
4980 Apex: proptools.StringPtr("myapex"),
4981 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4982 }
4983
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004984 t.Run("prebuilt only", func(t *testing.T) {
4985 bp := `
4986 prebuilt_apex {
4987 name: "myapex",
4988 arch: {
4989 arm64: {
4990 src: "myapex-arm64.apex",
4991 },
4992 arm: {
4993 src: "myapex-arm.apex",
4994 },
4995 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004996 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4997 }
4998
4999 prebuilt_bootclasspath_fragment {
5000 name: "my-bootclasspath-fragment",
5001 contents: ["libfoo", "libbar"],
5002 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005003 hidden_api: {
5004 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5005 metadata: "my-bootclasspath-fragment/metadata.csv",
5006 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005007 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5008 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5009 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005010 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005011 }
5012
5013 java_import {
5014 name: "libfoo",
5015 jars: ["libfoo.jar"],
5016 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005017 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005018 }
Paul Duffin37856732021-02-26 14:24:15 +00005019
5020 java_sdk_library_import {
5021 name: "libbar",
5022 public: {
5023 jars: ["libbar.jar"],
5024 },
5025 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005026 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005027 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005028 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005029 `
5030
Paul Duffin89f570a2021-06-16 01:42:33 +01005031 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005032 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5033 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005034
Paul Duffin537ea3d2021-05-14 10:38:00 +01005035 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005036 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005037 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005038 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005039 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5040 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005041 })
5042
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005043 t.Run("apex_set only", func(t *testing.T) {
5044 bp := `
5045 apex_set {
5046 name: "myapex",
5047 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005048 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5049 }
5050
5051 prebuilt_bootclasspath_fragment {
5052 name: "my-bootclasspath-fragment",
5053 contents: ["libfoo", "libbar"],
5054 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005055 hidden_api: {
5056 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5057 metadata: "my-bootclasspath-fragment/metadata.csv",
5058 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005059 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5060 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5061 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005062 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005063 }
5064
5065 java_import {
5066 name: "libfoo",
5067 jars: ["libfoo.jar"],
5068 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005069 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005070 }
5071
5072 java_sdk_library_import {
5073 name: "libbar",
5074 public: {
5075 jars: ["libbar.jar"],
5076 },
5077 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005078 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005079 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005080 }
5081 `
5082
Paul Duffin89f570a2021-06-16 01:42:33 +01005083 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005084 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5085 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5086
Paul Duffin537ea3d2021-05-14 10:38:00 +01005087 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005088 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005089 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005090 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005091 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5092 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005093 })
5094
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005095 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5096 bp := `
5097 prebuilt_apex {
5098 name: "myapex",
5099 arch: {
5100 arm64: {
5101 src: "myapex-arm64.apex",
5102 },
5103 arm: {
5104 src: "myapex-arm.apex",
5105 },
5106 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005107 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5108 }
5109
5110 prebuilt_bootclasspath_fragment {
5111 name: "my-bootclasspath-fragment",
5112 contents: ["libfoo", "libbar"],
5113 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005114 hidden_api: {
5115 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5116 metadata: "my-bootclasspath-fragment/metadata.csv",
5117 index: "my-bootclasspath-fragment/index.csv",
5118 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5119 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5120 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005121 }
5122
5123 java_import {
5124 name: "libfoo",
5125 jars: ["libfoo.jar"],
5126 apex_available: ["myapex"],
5127 }
5128
5129 java_library {
5130 name: "libfoo",
5131 srcs: ["foo/bar/MyClass.java"],
5132 apex_available: ["myapex"],
5133 }
Paul Duffin37856732021-02-26 14:24:15 +00005134
5135 java_sdk_library_import {
5136 name: "libbar",
5137 public: {
5138 jars: ["libbar.jar"],
5139 },
5140 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005141 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005142 }
5143
5144 java_sdk_library {
5145 name: "libbar",
5146 srcs: ["foo/bar/MyClass.java"],
5147 unsafe_ignore_missing_latest_api: true,
5148 apex_available: ["myapex"],
5149 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005150 `
5151
5152 // In this test the source (java_library) libfoo is active since the
5153 // prebuilt (java_import) defaults to prefer:false. However the
5154 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5155 // find the dex boot jar in it. We either need to disable the source libfoo
5156 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005157 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005158 // dexbootjar check is skipped if AllowMissingDependencies is true
5159 preparerAllowMissingDeps := android.GroupFixturePreparers(
5160 preparer,
5161 android.PrepareForTestWithAllowMissingDependencies,
5162 )
5163 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005164 })
5165
5166 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5167 bp := `
5168 prebuilt_apex {
5169 name: "myapex",
5170 arch: {
5171 arm64: {
5172 src: "myapex-arm64.apex",
5173 },
5174 arm: {
5175 src: "myapex-arm.apex",
5176 },
5177 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005178 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5179 }
5180
5181 prebuilt_bootclasspath_fragment {
5182 name: "my-bootclasspath-fragment",
5183 contents: ["libfoo", "libbar"],
5184 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005185 hidden_api: {
5186 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5187 metadata: "my-bootclasspath-fragment/metadata.csv",
5188 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005189 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5190 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5191 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005192 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005193 }
5194
5195 java_import {
5196 name: "libfoo",
5197 prefer: true,
5198 jars: ["libfoo.jar"],
5199 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005200 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005201 }
5202
5203 java_library {
5204 name: "libfoo",
5205 srcs: ["foo/bar/MyClass.java"],
5206 apex_available: ["myapex"],
5207 }
Paul Duffin37856732021-02-26 14:24:15 +00005208
5209 java_sdk_library_import {
5210 name: "libbar",
5211 prefer: true,
5212 public: {
5213 jars: ["libbar.jar"],
5214 },
5215 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005216 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005217 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005218 }
5219
5220 java_sdk_library {
5221 name: "libbar",
5222 srcs: ["foo/bar/MyClass.java"],
5223 unsafe_ignore_missing_latest_api: true,
5224 apex_available: ["myapex"],
5225 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005226 `
5227
Paul Duffin89f570a2021-06-16 01:42:33 +01005228 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005229 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5230 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005231
Paul Duffin537ea3d2021-05-14 10:38:00 +01005232 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005233 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005234 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005235 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005236 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5237 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005238 })
5239
5240 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5241 bp := `
5242 apex {
5243 name: "myapex",
5244 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005245 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005246 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005247 }
5248
5249 apex_key {
5250 name: "myapex.key",
5251 public_key: "testkey.avbpubkey",
5252 private_key: "testkey.pem",
5253 }
5254
5255 prebuilt_apex {
5256 name: "myapex",
5257 arch: {
5258 arm64: {
5259 src: "myapex-arm64.apex",
5260 },
5261 arm: {
5262 src: "myapex-arm.apex",
5263 },
5264 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005265 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5266 }
5267
5268 prebuilt_bootclasspath_fragment {
5269 name: "my-bootclasspath-fragment",
5270 contents: ["libfoo", "libbar"],
5271 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005272 hidden_api: {
5273 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5274 metadata: "my-bootclasspath-fragment/metadata.csv",
5275 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005276 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5277 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5278 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005279 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005280 }
5281
5282 java_import {
5283 name: "libfoo",
5284 jars: ["libfoo.jar"],
5285 apex_available: ["myapex"],
5286 }
5287
5288 java_library {
5289 name: "libfoo",
5290 srcs: ["foo/bar/MyClass.java"],
5291 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005292 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005293 }
Paul Duffin37856732021-02-26 14:24:15 +00005294
5295 java_sdk_library_import {
5296 name: "libbar",
5297 public: {
5298 jars: ["libbar.jar"],
5299 },
5300 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005301 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005302 }
5303
5304 java_sdk_library {
5305 name: "libbar",
5306 srcs: ["foo/bar/MyClass.java"],
5307 unsafe_ignore_missing_latest_api: true,
5308 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005309 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005310 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005311 `
5312
Paul Duffin89f570a2021-06-16 01:42:33 +01005313 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005314 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5315 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005316
Paul Duffin537ea3d2021-05-14 10:38:00 +01005317 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005318 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005319 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005320 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005321 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5322 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005323 })
5324
5325 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5326 bp := `
5327 apex {
5328 name: "myapex",
5329 enabled: false,
5330 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005331 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005332 }
5333
5334 apex_key {
5335 name: "myapex.key",
5336 public_key: "testkey.avbpubkey",
5337 private_key: "testkey.pem",
5338 }
5339
5340 prebuilt_apex {
5341 name: "myapex",
5342 arch: {
5343 arm64: {
5344 src: "myapex-arm64.apex",
5345 },
5346 arm: {
5347 src: "myapex-arm.apex",
5348 },
5349 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005350 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5351 }
5352
5353 prebuilt_bootclasspath_fragment {
5354 name: "my-bootclasspath-fragment",
5355 contents: ["libfoo", "libbar"],
5356 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005357 hidden_api: {
5358 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5359 metadata: "my-bootclasspath-fragment/metadata.csv",
5360 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005361 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5362 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5363 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005364 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005365 }
5366
5367 java_import {
5368 name: "libfoo",
5369 prefer: true,
5370 jars: ["libfoo.jar"],
5371 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005372 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005373 }
5374
5375 java_library {
5376 name: "libfoo",
5377 srcs: ["foo/bar/MyClass.java"],
5378 apex_available: ["myapex"],
5379 }
Paul Duffin37856732021-02-26 14:24:15 +00005380
5381 java_sdk_library_import {
5382 name: "libbar",
5383 prefer: true,
5384 public: {
5385 jars: ["libbar.jar"],
5386 },
5387 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005388 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005389 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005390 }
5391
5392 java_sdk_library {
5393 name: "libbar",
5394 srcs: ["foo/bar/MyClass.java"],
5395 unsafe_ignore_missing_latest_api: true,
5396 apex_available: ["myapex"],
5397 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005398 `
5399
Paul Duffin89f570a2021-06-16 01:42:33 +01005400 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005401 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5402 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005403
Paul Duffin537ea3d2021-05-14 10:38:00 +01005404 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005405 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005406 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005407 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005408 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5409 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005410 })
5411}
5412
Roland Levillain630846d2019-06-26 12:48:34 +01005413func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005414 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005415 apex_test {
5416 name: "myapex",
5417 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005418 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005419 tests: [
5420 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005421 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005422 ],
5423 }
5424
5425 apex_key {
5426 name: "myapex.key",
5427 public_key: "testkey.avbpubkey",
5428 private_key: "testkey.pem",
5429 }
5430
Liz Kammer1c14a212020-05-12 15:26:55 -07005431 filegroup {
5432 name: "fg",
5433 srcs: [
5434 "baz",
5435 "bar/baz"
5436 ],
5437 }
5438
Roland Levillain630846d2019-06-26 12:48:34 +01005439 cc_test {
5440 name: "mytest",
5441 gtest: false,
5442 srcs: ["mytest.cpp"],
5443 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005444 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005445 system_shared_libs: [],
5446 static_executable: true,
5447 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005448 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005449 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005450
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005451 cc_library {
5452 name: "mylib",
5453 srcs: ["mylib.cpp"],
5454 system_shared_libs: [],
5455 stl: "none",
5456 }
5457
Liz Kammer5bd365f2020-05-27 15:15:11 -07005458 filegroup {
5459 name: "fg2",
5460 srcs: [
5461 "testdata/baz"
5462 ],
5463 }
5464
Roland Levillain9b5fde92019-06-28 15:41:19 +01005465 cc_test {
5466 name: "mytests",
5467 gtest: false,
5468 srcs: [
5469 "mytest1.cpp",
5470 "mytest2.cpp",
5471 "mytest3.cpp",
5472 ],
5473 test_per_src: true,
5474 relative_install_path: "test",
5475 system_shared_libs: [],
5476 static_executable: true,
5477 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005478 data: [
5479 ":fg",
5480 ":fg2",
5481 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005482 }
Roland Levillain630846d2019-06-26 12:48:34 +01005483 `)
5484
Sundong Ahnabb64432019-10-22 13:58:29 +09005485 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005486 copyCmds := apexRule.Args["copy_commands"]
5487
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005488 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005489 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005490 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005491
Liz Kammer1c14a212020-05-12 15:26:55 -07005492 //Ensure that test data are copied into apex.
5493 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5494 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5495
Roland Levillain9b5fde92019-06-28 15:41:19 +01005496 // Ensure that test deps built with `test_per_src` are copied into apex.
5497 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5498 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5499 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005500
5501 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005502 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005503 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005504 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005505 prefix := "TARGET_"
5506 var builder strings.Builder
5507 data.Custom(&builder, name, prefix, "", data)
5508 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005509 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5510 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5511 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5512 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005513 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005514 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005515 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005516
5517 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005518 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005519 data.Custom(&builder, name, prefix, "", data)
5520 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005521 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5522 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005523}
5524
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005525func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005526 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005527 apex {
5528 name: "myapex",
5529 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005530 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005531 }
5532 apex_key {
5533 name: "myapex.key",
5534 public_key: "testkey.avbpubkey",
5535 private_key: "testkey.pem",
5536 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005537 `,
5538 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5539 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5540 }),
5541 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005542 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005543 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005544 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005545 var builder strings.Builder
5546 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5547 androidMk := builder.String()
5548 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5549}
5550
Jooyung Hand48f3c32019-08-23 11:18:57 +09005551func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5552 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5553 apex {
5554 name: "myapex",
5555 key: "myapex.key",
5556 native_shared_libs: ["libfoo"],
5557 }
5558
5559 apex_key {
5560 name: "myapex.key",
5561 public_key: "testkey.avbpubkey",
5562 private_key: "testkey.pem",
5563 }
5564
5565 cc_library {
5566 name: "libfoo",
5567 stl: "none",
5568 system_shared_libs: [],
5569 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005570 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005571 }
5572 `)
5573 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5574 apex {
5575 name: "myapex",
5576 key: "myapex.key",
5577 java_libs: ["myjar"],
5578 }
5579
5580 apex_key {
5581 name: "myapex.key",
5582 public_key: "testkey.avbpubkey",
5583 private_key: "testkey.pem",
5584 }
5585
5586 java_library {
5587 name: "myjar",
5588 srcs: ["foo/bar/MyClass.java"],
5589 sdk_version: "none",
5590 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005591 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005592 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005593 }
5594 `)
5595}
5596
Bill Peckhama41a6962021-01-11 10:58:54 -08005597func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005598 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005599 apex {
5600 name: "myapex",
5601 key: "myapex.key",
5602 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005603 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005604 }
5605
5606 apex_key {
5607 name: "myapex.key",
5608 public_key: "testkey.avbpubkey",
5609 private_key: "testkey.pem",
5610 }
5611
5612 java_import {
5613 name: "myjavaimport",
5614 apex_available: ["myapex"],
5615 jars: ["my.jar"],
5616 compile_dex: true,
5617 }
5618 `)
5619
5620 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5621 apexRule := module.Rule("apexRule")
5622 copyCmds := apexRule.Args["copy_commands"]
5623 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5624}
5625
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005626func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005627 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005628 apex {
5629 name: "myapex",
5630 key: "myapex.key",
5631 apps: [
5632 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005633 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005634 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005635 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005636 }
5637
5638 apex_key {
5639 name: "myapex.key",
5640 public_key: "testkey.avbpubkey",
5641 private_key: "testkey.pem",
5642 }
5643
5644 android_app {
5645 name: "AppFoo",
5646 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005647 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005648 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005649 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005650 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005651 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005652 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005653
5654 android_app {
5655 name: "AppFooPriv",
5656 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005657 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005658 system_modules: "none",
5659 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005660 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005661 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005662 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005663
5664 cc_library_shared {
5665 name: "libjni",
5666 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005667 shared_libs: ["libfoo"],
5668 stl: "none",
5669 system_shared_libs: [],
5670 apex_available: [ "myapex" ],
5671 sdk_version: "current",
5672 }
5673
5674 cc_library_shared {
5675 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005676 stl: "none",
5677 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005678 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005679 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005680 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005681 `)
5682
Sundong Ahnabb64432019-10-22 13:58:29 +09005683 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005684 apexRule := module.Rule("apexRule")
5685 copyCmds := apexRule.Args["copy_commands"]
5686
5687 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005688 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005689
Colin Crossaede88c2020-08-11 12:17:01 -07005690 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005691 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005692 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005693 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005694 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005695 // JNI libraries including transitive deps are
5696 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005697 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005698 // ... embedded inside APK (jnilibs.zip)
5699 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5700 // ... and not directly inside the APEX
5701 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5702 }
Dario Frenicde2a032019-10-27 00:29:22 +01005703}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005704
Dario Frenicde2a032019-10-27 00:29:22 +01005705func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005706 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005707 apex {
5708 name: "myapex",
5709 key: "myapex.key",
5710 apps: [
5711 "AppFooPrebuilt",
5712 "AppFooPrivPrebuilt",
5713 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005714 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005715 }
5716
5717 apex_key {
5718 name: "myapex.key",
5719 public_key: "testkey.avbpubkey",
5720 private_key: "testkey.pem",
5721 }
5722
5723 android_app_import {
5724 name: "AppFooPrebuilt",
5725 apk: "PrebuiltAppFoo.apk",
5726 presigned: true,
5727 dex_preopt: {
5728 enabled: false,
5729 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005730 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005731 }
5732
5733 android_app_import {
5734 name: "AppFooPrivPrebuilt",
5735 apk: "PrebuiltAppFooPriv.apk",
5736 privileged: true,
5737 presigned: true,
5738 dex_preopt: {
5739 enabled: false,
5740 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005741 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005742 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005743 }
5744 `)
5745
Sundong Ahnabb64432019-10-22 13:58:29 +09005746 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005747 apexRule := module.Rule("apexRule")
5748 copyCmds := apexRule.Args["copy_commands"]
5749
5750 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005751 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5752}
5753
5754func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005755 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005756 apex {
5757 name: "myapex",
5758 key: "myapex.key",
5759 apps: [
5760 "AppFoo",
5761 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005762 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005763 }
5764
5765 apex_key {
5766 name: "myapex.key",
5767 public_key: "testkey.avbpubkey",
5768 private_key: "testkey.pem",
5769 }
5770
5771 android_app {
5772 name: "AppFoo",
5773 srcs: ["foo/bar/MyClass.java"],
5774 sdk_version: "none",
5775 system_modules: "none",
5776 apex_available: [ "myapex" ],
5777 }
5778
5779 android_app_import {
5780 name: "AppFoo",
5781 apk: "AppFooPrebuilt.apk",
5782 filename: "AppFooPrebuilt.apk",
5783 presigned: true,
5784 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005785 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005786 }
5787 `, withFiles(map[string][]byte{
5788 "AppFooPrebuilt.apk": nil,
5789 }))
5790
5791 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005792 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005793 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005794}
5795
Dario Freni6f3937c2019-12-20 22:58:03 +00005796func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005797 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005798 apex {
5799 name: "myapex",
5800 key: "myapex.key",
5801 apps: [
5802 "TesterHelpAppFoo",
5803 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005804 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005805 }
5806
5807 apex_key {
5808 name: "myapex.key",
5809 public_key: "testkey.avbpubkey",
5810 private_key: "testkey.pem",
5811 }
5812
5813 android_test_helper_app {
5814 name: "TesterHelpAppFoo",
5815 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005816 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005817 }
5818
5819 `)
5820
5821 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5822 apexRule := module.Rule("apexRule")
5823 copyCmds := apexRule.Args["copy_commands"]
5824
5825 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5826}
5827
Jooyung Han18020ea2019-11-13 10:50:48 +09005828func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5829 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005830 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005831 apex {
5832 name: "myapex",
5833 key: "myapex.key",
5834 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005835 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005836 }
5837
5838 apex_key {
5839 name: "myapex.key",
5840 public_key: "testkey.avbpubkey",
5841 private_key: "testkey.pem",
5842 }
5843
5844 apex {
5845 name: "otherapex",
5846 key: "myapex.key",
5847 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005848 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005849 }
5850
5851 cc_defaults {
5852 name: "libfoo-defaults",
5853 apex_available: ["otherapex"],
5854 }
5855
5856 cc_library {
5857 name: "libfoo",
5858 defaults: ["libfoo-defaults"],
5859 stl: "none",
5860 system_shared_libs: [],
5861 }`)
5862}
5863
Paul Duffine52e66f2020-03-30 17:54:29 +01005864func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005865 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005866 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005867 apex {
5868 name: "myapex",
5869 key: "myapex.key",
5870 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005871 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005872 }
5873
5874 apex_key {
5875 name: "myapex.key",
5876 public_key: "testkey.avbpubkey",
5877 private_key: "testkey.pem",
5878 }
5879
5880 apex {
5881 name: "otherapex",
5882 key: "otherapex.key",
5883 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005884 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005885 }
5886
5887 apex_key {
5888 name: "otherapex.key",
5889 public_key: "testkey.avbpubkey",
5890 private_key: "testkey.pem",
5891 }
5892
5893 cc_library {
5894 name: "libfoo",
5895 stl: "none",
5896 system_shared_libs: [],
5897 apex_available: ["otherapex"],
5898 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005899}
Jiyong Park127b40b2019-09-30 16:04:35 +09005900
Paul Duffine52e66f2020-03-30 17:54:29 +01005901func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005902 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005903 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005904.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005905.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005906.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005907.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005908.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005909.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005910 apex {
5911 name: "myapex",
5912 key: "myapex.key",
5913 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005914 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005915 }
5916
5917 apex_key {
5918 name: "myapex.key",
5919 public_key: "testkey.avbpubkey",
5920 private_key: "testkey.pem",
5921 }
5922
Jiyong Park127b40b2019-09-30 16:04:35 +09005923 cc_library {
5924 name: "libfoo",
5925 stl: "none",
5926 shared_libs: ["libbar"],
5927 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005928 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005929 }
5930
5931 cc_library {
5932 name: "libbar",
5933 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005934 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005935 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005936 apex_available: ["myapex"],
5937 }
5938
5939 cc_library {
5940 name: "libbaz",
5941 stl: "none",
5942 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005943 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005944}
Jiyong Park127b40b2019-09-30 16:04:35 +09005945
Paul Duffine52e66f2020-03-30 17:54:29 +01005946func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005947 testApexError(t, "\"otherapex\" is not a valid module name", `
5948 apex {
5949 name: "myapex",
5950 key: "myapex.key",
5951 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005952 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005953 }
5954
5955 apex_key {
5956 name: "myapex.key",
5957 public_key: "testkey.avbpubkey",
5958 private_key: "testkey.pem",
5959 }
5960
5961 cc_library {
5962 name: "libfoo",
5963 stl: "none",
5964 system_shared_libs: [],
5965 apex_available: ["otherapex"],
5966 }`)
5967
Paul Duffine52e66f2020-03-30 17:54:29 +01005968 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005969 apex {
5970 name: "myapex",
5971 key: "myapex.key",
5972 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005973 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005974 }
5975
5976 apex_key {
5977 name: "myapex.key",
5978 public_key: "testkey.avbpubkey",
5979 private_key: "testkey.pem",
5980 }
5981
5982 cc_library {
5983 name: "libfoo",
5984 stl: "none",
5985 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005986 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005987 apex_available: ["myapex"],
5988 }
5989
5990 cc_library {
5991 name: "libbar",
5992 stl: "none",
5993 system_shared_libs: [],
5994 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005995 }
5996
5997 cc_library {
5998 name: "libbaz",
5999 stl: "none",
6000 system_shared_libs: [],
6001 stubs: {
6002 versions: ["10", "20", "30"],
6003 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006004 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006005}
Jiyong Park127b40b2019-09-30 16:04:35 +09006006
Jiyong Park89e850a2020-04-07 16:37:39 +09006007func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006008 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006009 apex {
6010 name: "myapex",
6011 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006012 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006013 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006014 }
6015
6016 apex_key {
6017 name: "myapex.key",
6018 public_key: "testkey.avbpubkey",
6019 private_key: "testkey.pem",
6020 }
6021
6022 cc_library {
6023 name: "libfoo",
6024 stl: "none",
6025 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006026 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006027 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006028 }
6029
6030 cc_library {
6031 name: "libfoo2",
6032 stl: "none",
6033 system_shared_libs: [],
6034 shared_libs: ["libbaz"],
6035 apex_available: ["//apex_available:platform"],
6036 }
6037
6038 cc_library {
6039 name: "libbar",
6040 stl: "none",
6041 system_shared_libs: [],
6042 apex_available: ["myapex"],
6043 }
6044
6045 cc_library {
6046 name: "libbaz",
6047 stl: "none",
6048 system_shared_libs: [],
6049 apex_available: ["myapex"],
6050 stubs: {
6051 versions: ["1"],
6052 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006053 }`)
6054
Jiyong Park89e850a2020-04-07 16:37:39 +09006055 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6056 // because it depends on libbar which isn't available to platform
6057 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6058 if libfoo.NotAvailableForPlatform() != true {
6059 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6060 }
6061
6062 // libfoo2 however can be available to platform because it depends on libbaz which provides
6063 // stubs
6064 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6065 if libfoo2.NotAvailableForPlatform() == true {
6066 t.Errorf("%q should be available to platform", libfoo2.String())
6067 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006068}
Jiyong Parka90ca002019-10-07 15:47:24 +09006069
Paul Duffine52e66f2020-03-30 17:54:29 +01006070func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006071 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006072 apex {
6073 name: "myapex",
6074 key: "myapex.key",
6075 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006076 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +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: [],
6089 apex_available: ["myapex"],
6090 static: {
6091 apex_available: ["//apex_available:platform"],
6092 },
6093 }`)
6094
Jiyong Park89e850a2020-04-07 16:37:39 +09006095 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6096 if libfooShared.NotAvailableForPlatform() != true {
6097 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6098 }
6099 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6100 if libfooStatic.NotAvailableForPlatform() != false {
6101 t.Errorf("%q should be available to platform", libfooStatic.String())
6102 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006103}
6104
Jiyong Park5d790c32019-11-15 18:40:32 +09006105func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006106 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006107 apex {
6108 name: "myapex",
6109 key: "myapex.key",
6110 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006111 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006112 prebuilts: ["myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006113 bootclasspath_fragments: ["mybootclasspath_fragment"],
6114 systemserverclasspath_fragments: ["mysystemserverclasspath_fragment"],
6115 java_libs: ["myjava_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006116 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006117 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006118 }
6119
6120 override_apex {
6121 name: "override_myapex",
6122 base: "myapex",
6123 apps: ["override_app"],
markchien7c803b82021-08-26 22:10:06 +08006124 bpfs: ["override_bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006125 prebuilts: ["override_myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006126 bootclasspath_fragments: ["override_bootclasspath_fragment"],
6127 systemserverclasspath_fragments: ["override_systemserverclasspath_fragment"],
6128 java_libs: ["override_java_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006129 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006130 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006131 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006132 key: "mynewapex.key",
6133 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006134 }
6135
6136 apex_key {
6137 name: "myapex.key",
6138 public_key: "testkey.avbpubkey",
6139 private_key: "testkey.pem",
6140 }
6141
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006142 apex_key {
6143 name: "mynewapex.key",
6144 public_key: "testkey2.avbpubkey",
6145 private_key: "testkey2.pem",
6146 }
6147
6148 android_app_certificate {
6149 name: "myapex.certificate",
6150 certificate: "testkey",
6151 }
6152
Jiyong Park5d790c32019-11-15 18:40:32 +09006153 android_app {
6154 name: "app",
6155 srcs: ["foo/bar/MyClass.java"],
6156 package_name: "foo",
6157 sdk_version: "none",
6158 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006159 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006160 }
6161
6162 override_android_app {
6163 name: "override_app",
6164 base: "app",
6165 package_name: "bar",
6166 }
markchien7c803b82021-08-26 22:10:06 +08006167
6168 bpf {
6169 name: "bpf",
6170 srcs: ["bpf.c"],
6171 }
6172
6173 bpf {
6174 name: "override_bpf",
6175 srcs: ["override_bpf.c"],
6176 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006177
6178 prebuilt_etc {
6179 name: "myetc",
6180 src: "myprebuilt",
6181 }
6182
6183 prebuilt_etc {
6184 name: "override_myetc",
6185 src: "override_myprebuilt",
6186 }
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006187
6188 java_library {
6189 name: "bcplib",
6190 srcs: ["a.java"],
6191 compile_dex: true,
6192 apex_available: ["myapex"],
6193 permitted_packages: ["bcp.lib"],
6194 }
6195
6196 bootclasspath_fragment {
6197 name: "mybootclasspath_fragment",
6198 contents: ["bcplib"],
6199 apex_available: ["myapex"],
6200 }
6201
6202 java_library {
6203 name: "override_bcplib",
6204 srcs: ["a.java"],
6205 compile_dex: true,
6206 apex_available: ["myapex"],
6207 permitted_packages: ["override.bcp.lib"],
6208 }
6209
6210 bootclasspath_fragment {
6211 name: "override_bootclasspath_fragment",
6212 contents: ["override_bcplib"],
6213 apex_available: ["myapex"],
6214 }
6215
6216 java_library {
6217 name: "systemserverlib",
6218 srcs: ["a.java"],
6219 apex_available: ["myapex"],
6220 }
6221
6222 systemserverclasspath_fragment {
6223 name: "mysystemserverclasspath_fragment",
6224 standalone_contents: ["systemserverlib"],
6225 apex_available: ["myapex"],
6226 }
6227
6228 java_library {
6229 name: "override_systemserverlib",
6230 srcs: ["a.java"],
6231 apex_available: ["myapex"],
6232 }
6233
6234 systemserverclasspath_fragment {
6235 name: "override_systemserverclasspath_fragment",
6236 standalone_contents: ["override_systemserverlib"],
6237 apex_available: ["myapex"],
6238 }
6239
6240 java_library {
6241 name: "myjava_library",
6242 srcs: ["a.java"],
6243 compile_dex: true,
6244 apex_available: ["myapex"],
6245 }
6246
6247 java_library {
6248 name: "override_java_library",
6249 srcs: ["a.java"],
6250 compile_dex: true,
6251 apex_available: ["myapex"],
6252 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006253 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006254
Jiyong Park317645e2019-12-05 13:20:58 +09006255 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6256 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6257 if originalVariant.GetOverriddenBy() != "" {
6258 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6259 }
6260 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6261 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6262 }
6263
Jiyong Park5d790c32019-11-15 18:40:32 +09006264 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6265 apexRule := module.Rule("apexRule")
6266 copyCmds := apexRule.Args["copy_commands"]
6267
6268 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006269 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006270
markchien7c803b82021-08-26 22:10:06 +08006271 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
6272 ensureContains(t, copyCmds, "image.apex/etc/bpf/override_bpf.o")
6273
Daniel Norman5a3ce132021-08-26 15:44:43 -07006274 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6275 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6276
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006277 apexBundle := module.Module().(*apexBundle)
6278 name := apexBundle.Name()
6279 if name != "override_myapex" {
6280 t.Errorf("name should be \"override_myapex\", but was %q", name)
6281 }
6282
Baligh Uddin004d7172020-02-19 21:29:28 -08006283 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6284 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6285 }
6286
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006287 android.AssertArrayString(t, "Bootclasspath_fragments does not match",
6288 []string{"override_bootclasspath_fragment"}, apexBundle.overridableProperties.Bootclasspath_fragments)
6289 android.AssertArrayString(t, "Systemserverclasspath_fragments does not match",
6290 []string{"override_systemserverclasspath_fragment"}, apexBundle.overridableProperties.Systemserverclasspath_fragments)
6291 android.AssertArrayString(t, "Java_libs does not match",
6292 []string{"override_java_library"}, apexBundle.overridableProperties.Java_libs)
6293
Jiyong Park20bacab2020-03-03 11:45:41 +09006294 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006295 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006296 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6297
6298 signApkRule := module.Rule("signapk")
6299 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006300
Colin Crossaa255532020-07-03 13:18:24 -07006301 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006302 var builder strings.Builder
6303 data.Custom(&builder, name, "TARGET_", "", data)
6304 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006305 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
markchien7c803b82021-08-26 22:10:06 +08006306 ensureContains(t, androidMk, "LOCAL_MODULE := override_bpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006307 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006308 ensureContains(t, androidMk, "LOCAL_MODULE := override_bcplib.override_myapex")
6309 ensureContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.override_myapex")
6310 ensureContains(t, androidMk, "LOCAL_MODULE := override_java_library.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006311 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006312 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006313 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006314 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006315 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006316 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006317 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_bcplib.myapex")
6318 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.myapex")
6319 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_java_library.pb.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006320 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006321}
6322
Albert Martineefabcf2022-03-21 20:11:16 +00006323func TestMinSdkVersionOverride(t *testing.T) {
6324 // Override from 29 to 31
6325 minSdkOverride31 := "31"
6326 ctx := testApex(t, `
6327 apex {
6328 name: "myapex",
6329 key: "myapex.key",
6330 native_shared_libs: ["mylib"],
6331 updatable: true,
6332 min_sdk_version: "29"
6333 }
6334
6335 override_apex {
6336 name: "override_myapex",
6337 base: "myapex",
6338 logging_parent: "com.foo.bar",
6339 package_name: "test.overridden.package"
6340 }
6341
6342 apex_key {
6343 name: "myapex.key",
6344 public_key: "testkey.avbpubkey",
6345 private_key: "testkey.pem",
6346 }
6347
6348 cc_library {
6349 name: "mylib",
6350 srcs: ["mylib.cpp"],
6351 runtime_libs: ["libbar"],
6352 system_shared_libs: [],
6353 stl: "none",
6354 apex_available: [ "myapex" ],
6355 min_sdk_version: "apex_inherit"
6356 }
6357
6358 cc_library {
6359 name: "libbar",
6360 srcs: ["mylib.cpp"],
6361 system_shared_libs: [],
6362 stl: "none",
6363 apex_available: [ "myapex" ],
6364 min_sdk_version: "apex_inherit"
6365 }
6366
6367 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6368
6369 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6370 copyCmds := apexRule.Args["copy_commands"]
6371
6372 // Ensure that direct non-stubs dep is always included
6373 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6374
6375 // Ensure that runtime_libs dep in included
6376 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6377
6378 // Ensure libraries target overridden min_sdk_version value
6379 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6380}
6381
6382func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6383 // Attempt to override from 31 to 29, should be a NOOP
6384 minSdkOverride29 := "29"
6385 ctx := testApex(t, `
6386 apex {
6387 name: "myapex",
6388 key: "myapex.key",
6389 native_shared_libs: ["mylib"],
6390 updatable: true,
6391 min_sdk_version: "31"
6392 }
6393
6394 override_apex {
6395 name: "override_myapex",
6396 base: "myapex",
6397 logging_parent: "com.foo.bar",
6398 package_name: "test.overridden.package"
6399 }
6400
6401 apex_key {
6402 name: "myapex.key",
6403 public_key: "testkey.avbpubkey",
6404 private_key: "testkey.pem",
6405 }
6406
6407 cc_library {
6408 name: "mylib",
6409 srcs: ["mylib.cpp"],
6410 runtime_libs: ["libbar"],
6411 system_shared_libs: [],
6412 stl: "none",
6413 apex_available: [ "myapex" ],
6414 min_sdk_version: "apex_inherit"
6415 }
6416
6417 cc_library {
6418 name: "libbar",
6419 srcs: ["mylib.cpp"],
6420 system_shared_libs: [],
6421 stl: "none",
6422 apex_available: [ "myapex" ],
6423 min_sdk_version: "apex_inherit"
6424 }
6425
6426 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6427
6428 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6429 copyCmds := apexRule.Args["copy_commands"]
6430
6431 // Ensure that direct non-stubs dep is always included
6432 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6433
6434 // Ensure that runtime_libs dep in included
6435 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6436
6437 // Ensure libraries target the original min_sdk_version value rather than the overridden
6438 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6439}
6440
Jooyung Han214bf372019-11-12 13:03:50 +09006441func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006442 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006443 apex {
6444 name: "myapex",
6445 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006446 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006447 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006448 }
6449
6450 apex_key {
6451 name: "myapex.key",
6452 public_key: "testkey.avbpubkey",
6453 private_key: "testkey.pem",
6454 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006455
6456 cc_library {
6457 name: "mylib",
6458 srcs: ["mylib.cpp"],
6459 stl: "libc++",
6460 system_shared_libs: [],
6461 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006462 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006463 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006464 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006465
6466 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6467 args := module.Rule("apexRule").Args
6468 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006469 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006470
6471 // The copies of the libraries in the apex should have one more dependency than
6472 // the ones outside the apex, namely the unwinder. Ideally we should check
6473 // the dependency names directly here but for some reason the names are blank in
6474 // this test.
6475 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006476 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006477 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6478 if len(apexImplicits) != len(nonApexImplicits)+1 {
6479 t.Errorf("%q missing unwinder dep", lib)
6480 }
6481 }
Jooyung Han214bf372019-11-12 13:03:50 +09006482}
6483
Paul Duffine05480a2021-03-08 15:07:14 +00006484var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006485 "api/current.txt": nil,
6486 "api/removed.txt": nil,
6487 "api/system-current.txt": nil,
6488 "api/system-removed.txt": nil,
6489 "api/test-current.txt": nil,
6490 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006491
Anton Hanssondff2c782020-12-21 17:10:01 +00006492 "100/public/api/foo.txt": nil,
6493 "100/public/api/foo-removed.txt": nil,
6494 "100/system/api/foo.txt": nil,
6495 "100/system/api/foo-removed.txt": nil,
6496
Paul Duffineedc5d52020-06-12 17:46:39 +01006497 // For java_sdk_library_import
6498 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006499}
6500
Jooyung Han58f26ab2019-12-18 15:34:32 +09006501func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006502 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006503 apex {
6504 name: "myapex",
6505 key: "myapex.key",
6506 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006507 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006508 }
6509
6510 apex_key {
6511 name: "myapex.key",
6512 public_key: "testkey.avbpubkey",
6513 private_key: "testkey.pem",
6514 }
6515
6516 java_sdk_library {
6517 name: "foo",
6518 srcs: ["a.java"],
6519 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006520 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006521 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006522
6523 prebuilt_apis {
6524 name: "sdk",
6525 api_dirs: ["100"],
6526 }
Paul Duffin9b879592020-05-26 13:21:35 +01006527 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006528
6529 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006530 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006531 "javalib/foo.jar",
6532 "etc/permissions/foo.xml",
6533 })
6534 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006535 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006536 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 +09006537}
6538
Paul Duffin9b879592020-05-26 13:21:35 +01006539func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006540 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006541 apex {
6542 name: "myapex",
6543 key: "myapex.key",
6544 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006545 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006546 }
6547
6548 apex_key {
6549 name: "myapex.key",
6550 public_key: "testkey.avbpubkey",
6551 private_key: "testkey.pem",
6552 }
6553
6554 java_sdk_library {
6555 name: "foo",
6556 srcs: ["a.java"],
6557 api_packages: ["foo"],
6558 apex_available: ["myapex"],
6559 sdk_version: "none",
6560 system_modules: "none",
6561 }
6562
6563 java_library {
6564 name: "bar",
6565 srcs: ["a.java"],
6566 libs: ["foo"],
6567 apex_available: ["myapex"],
6568 sdk_version: "none",
6569 system_modules: "none",
6570 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006571
6572 prebuilt_apis {
6573 name: "sdk",
6574 api_dirs: ["100"],
6575 }
Paul Duffin9b879592020-05-26 13:21:35 +01006576 `, withFiles(filesForSdkLibrary))
6577
6578 // java_sdk_library installs both impl jar and permission XML
6579 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6580 "javalib/bar.jar",
6581 "javalib/foo.jar",
6582 "etc/permissions/foo.xml",
6583 })
6584
6585 // The bar library should depend on the implementation jar.
6586 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006587 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006588 t.Errorf("expected %q, found %#q", expected, actual)
6589 }
6590}
6591
6592func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006593 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006594 apex {
6595 name: "myapex",
6596 key: "myapex.key",
6597 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006598 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006599 }
6600
6601 apex_key {
6602 name: "myapex.key",
6603 public_key: "testkey.avbpubkey",
6604 private_key: "testkey.pem",
6605 }
6606
6607 java_sdk_library {
6608 name: "foo",
6609 srcs: ["a.java"],
6610 api_packages: ["foo"],
6611 apex_available: ["myapex"],
6612 sdk_version: "none",
6613 system_modules: "none",
6614 }
6615
6616 java_library {
6617 name: "bar",
6618 srcs: ["a.java"],
6619 libs: ["foo"],
6620 sdk_version: "none",
6621 system_modules: "none",
6622 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006623
6624 prebuilt_apis {
6625 name: "sdk",
6626 api_dirs: ["100"],
6627 }
Paul Duffin9b879592020-05-26 13:21:35 +01006628 `, withFiles(filesForSdkLibrary))
6629
6630 // java_sdk_library installs both impl jar and permission XML
6631 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6632 "javalib/foo.jar",
6633 "etc/permissions/foo.xml",
6634 })
6635
6636 // The bar library should depend on the stubs jar.
6637 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006638 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006639 t.Errorf("expected %q, found %#q", expected, actual)
6640 }
6641}
6642
Paul Duffineedc5d52020-06-12 17:46:39 +01006643func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006644 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006645 prebuilt_apis {
6646 name: "sdk",
6647 api_dirs: ["100"],
6648 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006649 withFiles(map[string][]byte{
6650 "apex/a.java": nil,
6651 "apex/apex_manifest.json": nil,
6652 "apex/Android.bp": []byte(`
6653 package {
6654 default_visibility: ["//visibility:private"],
6655 }
6656
6657 apex {
6658 name: "myapex",
6659 key: "myapex.key",
6660 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006661 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006662 }
6663
6664 apex_key {
6665 name: "myapex.key",
6666 public_key: "testkey.avbpubkey",
6667 private_key: "testkey.pem",
6668 }
6669
6670 java_library {
6671 name: "bar",
6672 srcs: ["a.java"],
6673 libs: ["foo"],
6674 apex_available: ["myapex"],
6675 sdk_version: "none",
6676 system_modules: "none",
6677 }
6678`),
6679 "source/a.java": nil,
6680 "source/api/current.txt": nil,
6681 "source/api/removed.txt": nil,
6682 "source/Android.bp": []byte(`
6683 package {
6684 default_visibility: ["//visibility:private"],
6685 }
6686
6687 java_sdk_library {
6688 name: "foo",
6689 visibility: ["//apex"],
6690 srcs: ["a.java"],
6691 api_packages: ["foo"],
6692 apex_available: ["myapex"],
6693 sdk_version: "none",
6694 system_modules: "none",
6695 public: {
6696 enabled: true,
6697 },
6698 }
6699`),
6700 "prebuilt/a.jar": nil,
6701 "prebuilt/Android.bp": []byte(`
6702 package {
6703 default_visibility: ["//visibility:private"],
6704 }
6705
6706 java_sdk_library_import {
6707 name: "foo",
6708 visibility: ["//apex", "//source"],
6709 apex_available: ["myapex"],
6710 prefer: true,
6711 public: {
6712 jars: ["a.jar"],
6713 },
6714 }
6715`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006716 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006717 )
6718
6719 // java_sdk_library installs both impl jar and permission XML
6720 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6721 "javalib/bar.jar",
6722 "javalib/foo.jar",
6723 "etc/permissions/foo.xml",
6724 })
6725
6726 // The bar library should depend on the implementation jar.
6727 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006728 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006729 t.Errorf("expected %q, found %#q", expected, actual)
6730 }
6731}
6732
6733func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6734 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6735 apex {
6736 name: "myapex",
6737 key: "myapex.key",
6738 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006739 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006740 }
6741
6742 apex_key {
6743 name: "myapex.key",
6744 public_key: "testkey.avbpubkey",
6745 private_key: "testkey.pem",
6746 }
6747
6748 java_sdk_library_import {
6749 name: "foo",
6750 apex_available: ["myapex"],
6751 prefer: true,
6752 public: {
6753 jars: ["a.jar"],
6754 },
6755 }
6756
6757 `, withFiles(filesForSdkLibrary))
6758}
6759
atrost6e126252020-01-27 17:01:16 +00006760func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006761 result := android.GroupFixturePreparers(
6762 prepareForApexTest,
6763 java.PrepareForTestWithPlatformCompatConfig,
6764 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006765 apex {
6766 name: "myapex",
6767 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006768 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006769 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006770 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006771 }
6772
6773 apex_key {
6774 name: "myapex.key",
6775 public_key: "testkey.avbpubkey",
6776 private_key: "testkey.pem",
6777 }
6778
6779 platform_compat_config {
6780 name: "myjar-platform-compat-config",
6781 src: ":myjar",
6782 }
6783
6784 java_library {
6785 name: "myjar",
6786 srcs: ["foo/bar/MyClass.java"],
6787 sdk_version: "none",
6788 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006789 apex_available: [ "myapex" ],
6790 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006791
6792 // Make sure that a preferred prebuilt does not affect the apex contents.
6793 prebuilt_platform_compat_config {
6794 name: "myjar-platform-compat-config",
6795 metadata: "compat-config/metadata.xml",
6796 prefer: true,
6797 }
atrost6e126252020-01-27 17:01:16 +00006798 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006799 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006800 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6801 "etc/compatconfig/myjar-platform-compat-config.xml",
6802 "javalib/myjar.jar",
6803 })
6804}
6805
Jiyong Park479321d2019-12-16 11:47:12 +09006806func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6807 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6808 apex {
6809 name: "myapex",
6810 key: "myapex.key",
6811 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006812 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006813 }
6814
6815 apex_key {
6816 name: "myapex.key",
6817 public_key: "testkey.avbpubkey",
6818 private_key: "testkey.pem",
6819 }
6820
6821 java_library {
6822 name: "myjar",
6823 srcs: ["foo/bar/MyClass.java"],
6824 sdk_version: "none",
6825 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006826 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006827 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006828 }
6829 `)
6830}
6831
Jiyong Park7afd1072019-12-30 16:56:33 +09006832func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006833 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006834 apex {
6835 name: "myapex",
6836 key: "myapex.key",
6837 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006838 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006839 }
6840
6841 apex_key {
6842 name: "myapex.key",
6843 public_key: "testkey.avbpubkey",
6844 private_key: "testkey.pem",
6845 }
6846
6847 cc_library {
6848 name: "mylib",
6849 srcs: ["mylib.cpp"],
6850 system_shared_libs: [],
6851 stl: "none",
6852 required: ["a", "b"],
6853 host_required: ["c", "d"],
6854 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006855 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006856 }
6857 `)
6858
6859 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006860 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006861 name := apexBundle.BaseModuleName()
6862 prefix := "TARGET_"
6863 var builder strings.Builder
6864 data.Custom(&builder, name, prefix, "", data)
6865 androidMk := builder.String()
6866 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6867 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6868 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6869}
6870
Jiyong Park7cd10e32020-01-14 09:22:18 +09006871func TestSymlinksFromApexToSystem(t *testing.T) {
6872 bp := `
6873 apex {
6874 name: "myapex",
6875 key: "myapex.key",
6876 native_shared_libs: ["mylib"],
6877 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006878 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006879 }
6880
Jiyong Park9d677202020-02-19 16:29:35 +09006881 apex {
6882 name: "myapex.updatable",
6883 key: "myapex.key",
6884 native_shared_libs: ["mylib"],
6885 java_libs: ["myjar"],
6886 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006887 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006888 }
6889
Jiyong Park7cd10e32020-01-14 09:22:18 +09006890 apex_key {
6891 name: "myapex.key",
6892 public_key: "testkey.avbpubkey",
6893 private_key: "testkey.pem",
6894 }
6895
6896 cc_library {
6897 name: "mylib",
6898 srcs: ["mylib.cpp"],
6899 shared_libs: ["myotherlib"],
6900 system_shared_libs: [],
6901 stl: "none",
6902 apex_available: [
6903 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006904 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006905 "//apex_available:platform",
6906 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006907 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006908 }
6909
6910 cc_library {
6911 name: "myotherlib",
6912 srcs: ["mylib.cpp"],
6913 system_shared_libs: [],
6914 stl: "none",
6915 apex_available: [
6916 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006917 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006918 "//apex_available:platform",
6919 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006920 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006921 }
6922
6923 java_library {
6924 name: "myjar",
6925 srcs: ["foo/bar/MyClass.java"],
6926 sdk_version: "none",
6927 system_modules: "none",
6928 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006929 apex_available: [
6930 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006931 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006932 "//apex_available:platform",
6933 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006934 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006935 }
6936
6937 java_library {
6938 name: "myotherjar",
6939 srcs: ["foo/bar/MyClass.java"],
6940 sdk_version: "none",
6941 system_modules: "none",
6942 apex_available: [
6943 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006944 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006945 "//apex_available:platform",
6946 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006947 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006948 }
6949 `
6950
6951 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6952 for _, f := range files {
6953 if f.path == file {
6954 if f.isLink {
6955 t.Errorf("%q is not a real file", file)
6956 }
6957 return
6958 }
6959 }
6960 t.Errorf("%q is not found", file)
6961 }
6962
6963 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6964 for _, f := range files {
6965 if f.path == file {
6966 if !f.isLink {
6967 t.Errorf("%q is not a symlink", file)
6968 }
6969 return
6970 }
6971 }
6972 t.Errorf("%q is not found", file)
6973 }
6974
Jiyong Park9d677202020-02-19 16:29:35 +09006975 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6976 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006977 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006978 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006979 ensureRealfileExists(t, files, "javalib/myjar.jar")
6980 ensureRealfileExists(t, files, "lib64/mylib.so")
6981 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6982
Jiyong Park9d677202020-02-19 16:29:35 +09006983 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6984 ensureRealfileExists(t, files, "javalib/myjar.jar")
6985 ensureRealfileExists(t, files, "lib64/mylib.so")
6986 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6987
6988 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006989 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006990 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006991 ensureRealfileExists(t, files, "javalib/myjar.jar")
6992 ensureRealfileExists(t, files, "lib64/mylib.so")
6993 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006994
6995 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6996 ensureRealfileExists(t, files, "javalib/myjar.jar")
6997 ensureRealfileExists(t, files, "lib64/mylib.so")
6998 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006999}
7000
Yo Chiange8128052020-07-23 20:09:18 +08007001func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007002 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007003 apex {
7004 name: "myapex",
7005 key: "myapex.key",
7006 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007007 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007008 }
7009
7010 apex_key {
7011 name: "myapex.key",
7012 public_key: "testkey.avbpubkey",
7013 private_key: "testkey.pem",
7014 }
7015
7016 cc_library_shared {
7017 name: "mylib",
7018 srcs: ["mylib.cpp"],
7019 shared_libs: ["myotherlib"],
7020 system_shared_libs: [],
7021 stl: "none",
7022 apex_available: [
7023 "myapex",
7024 "//apex_available:platform",
7025 ],
7026 }
7027
7028 cc_prebuilt_library_shared {
7029 name: "myotherlib",
7030 srcs: ["prebuilt.so"],
7031 system_shared_libs: [],
7032 stl: "none",
7033 apex_available: [
7034 "myapex",
7035 "//apex_available:platform",
7036 ],
7037 }
7038 `)
7039
7040 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007041 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007042 var builder strings.Builder
7043 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7044 androidMk := builder.String()
7045 // `myotherlib` is added to `myapex` as symlink
7046 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
7047 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7048 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7049 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09007050 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 +08007051}
7052
Jooyung Han643adc42020-02-27 13:50:06 +09007053func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007054 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007055 apex {
7056 name: "myapex",
7057 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007058 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007059 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007060 }
7061
7062 apex_key {
7063 name: "myapex.key",
7064 public_key: "testkey.avbpubkey",
7065 private_key: "testkey.pem",
7066 }
7067
7068 cc_library {
7069 name: "mylib",
7070 srcs: ["mylib.cpp"],
7071 shared_libs: ["mylib2"],
7072 system_shared_libs: [],
7073 stl: "none",
7074 apex_available: [ "myapex" ],
7075 }
7076
7077 cc_library {
7078 name: "mylib2",
7079 srcs: ["mylib.cpp"],
7080 system_shared_libs: [],
7081 stl: "none",
7082 apex_available: [ "myapex" ],
7083 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007084
7085 rust_ffi_shared {
7086 name: "libfoo.rust",
7087 crate_name: "foo",
7088 srcs: ["foo.rs"],
7089 shared_libs: ["libfoo.shared_from_rust"],
7090 prefer_rlib: true,
7091 apex_available: ["myapex"],
7092 }
7093
7094 cc_library_shared {
7095 name: "libfoo.shared_from_rust",
7096 srcs: ["mylib.cpp"],
7097 system_shared_libs: [],
7098 stl: "none",
7099 stubs: {
7100 versions: ["10", "11", "12"],
7101 },
7102 }
7103
Jooyung Han643adc42020-02-27 13:50:06 +09007104 `)
7105
7106 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7107 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007108 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007109 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7110 "lib64/mylib.so",
7111 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007112 "lib64/libfoo.rust.so",
7113 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7114 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007115 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007116
7117 // b/220397949
7118 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007119}
7120
Jooyung Han49f67012020-04-17 13:43:10 +09007121func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007122 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007123 apex {
7124 name: "myapex",
7125 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007126 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007127 }
7128 apex_key {
7129 name: "myapex.key",
7130 public_key: "testkey.avbpubkey",
7131 private_key: "testkey.pem",
7132 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007133 `,
7134 android.FixtureModifyConfig(func(config android.Config) {
7135 delete(config.Targets, android.Android)
7136 config.AndroidCommonTarget = android.Target{}
7137 }),
7138 )
Jooyung Han49f67012020-04-17 13:43:10 +09007139
7140 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7141 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7142 }
7143}
7144
Jiyong Parkbd159612020-02-28 15:22:21 +09007145func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007146 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007147 apex {
7148 name: "myapex",
7149 key: "myapex.key",
7150 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007151 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007152 }
7153
7154 apex_key {
7155 name: "myapex.key",
7156 public_key: "testkey.avbpubkey",
7157 private_key: "testkey.pem",
7158 }
7159
7160 android_app {
7161 name: "AppFoo",
7162 srcs: ["foo/bar/MyClass.java"],
7163 sdk_version: "none",
7164 system_modules: "none",
7165 apex_available: [ "myapex" ],
7166 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007167 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007168
Colin Crosscf371cc2020-11-13 11:48:42 -08007169 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007170 content := bundleConfigRule.Args["content"]
7171
7172 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007173 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09007174}
7175
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007176func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007177 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007178 apex {
7179 name: "myapex",
7180 key: "myapex.key",
7181 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007182 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007183 }
7184
7185 apex_key {
7186 name: "myapex.key",
7187 public_key: "testkey.avbpubkey",
7188 private_key: "testkey.pem",
7189 }
7190
7191 android_app_set {
7192 name: "AppSet",
7193 set: "AppSet.apks",
7194 }`)
7195 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007196 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007197 content := bundleConfigRule.Args["content"]
7198 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7199 s := mod.Rule("apexRule").Args["copy_commands"]
7200 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
7201 if len(copyCmds) != 3 {
7202 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
7203 }
7204 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
7205 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
7206 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
7207}
7208
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007209func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007210 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007211 apex_set {
7212 name: "myapex",
7213 filename: "foo_v2.apex",
7214 sanitized: {
7215 none: { set: "myapex.apks", },
7216 hwaddress: { set: "myapex.hwasan.apks", },
7217 },
Paul Duffin24704672021-04-06 16:09:30 +01007218 }
7219 `
7220 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007221
Paul Duffin24704672021-04-06 16:09:30 +01007222 // Check that the extractor produces the correct output file from the correct input file.
7223 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007224
Paul Duffin24704672021-04-06 16:09:30 +01007225 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7226 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007227
Paul Duffin24704672021-04-06 16:09:30 +01007228 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7229
7230 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007231 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7232 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007233
7234 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007235}
7236
Paul Duffin89f570a2021-06-16 01:42:33 +01007237func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007238 t.Helper()
7239
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007240 bp := `
7241 java_library {
7242 name: "some-updatable-apex-lib",
7243 srcs: ["a.java"],
7244 sdk_version: "current",
7245 apex_available: [
7246 "some-updatable-apex",
7247 ],
satayevabcd5972021-08-06 17:49:46 +01007248 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007249 }
7250
7251 java_library {
7252 name: "some-non-updatable-apex-lib",
7253 srcs: ["a.java"],
7254 apex_available: [
7255 "some-non-updatable-apex",
7256 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007257 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007258 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007259 }
7260
7261 bootclasspath_fragment {
7262 name: "some-non-updatable-fragment",
7263 contents: ["some-non-updatable-apex-lib"],
7264 apex_available: [
7265 "some-non-updatable-apex",
7266 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007267 }
7268
7269 java_library {
7270 name: "some-platform-lib",
7271 srcs: ["a.java"],
7272 sdk_version: "current",
7273 installable: true,
7274 }
7275
7276 java_library {
7277 name: "some-art-lib",
7278 srcs: ["a.java"],
7279 sdk_version: "current",
7280 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007281 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007282 ],
7283 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007284 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007285 }
7286
7287 apex {
7288 name: "some-updatable-apex",
7289 key: "some-updatable-apex.key",
7290 java_libs: ["some-updatable-apex-lib"],
7291 updatable: true,
7292 min_sdk_version: "current",
7293 }
7294
7295 apex {
7296 name: "some-non-updatable-apex",
7297 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007298 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007299 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007300 }
7301
7302 apex_key {
7303 name: "some-updatable-apex.key",
7304 }
7305
7306 apex_key {
7307 name: "some-non-updatable-apex.key",
7308 }
7309
7310 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007311 name: "com.android.art.debug",
7312 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007313 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007314 updatable: true,
7315 min_sdk_version: "current",
7316 }
7317
Paul Duffinf23bc472021-04-27 12:42:20 +01007318 bootclasspath_fragment {
7319 name: "art-bootclasspath-fragment",
7320 image_name: "art",
7321 contents: ["some-art-lib"],
7322 apex_available: [
7323 "com.android.art.debug",
7324 ],
7325 }
7326
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007327 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007328 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007329 }
7330
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007331 filegroup {
7332 name: "some-updatable-apex-file_contexts",
7333 srcs: [
7334 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7335 ],
7336 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007337
7338 filegroup {
7339 name: "some-non-updatable-apex-file_contexts",
7340 srcs: [
7341 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7342 ],
7343 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007344 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007345
Paul Duffin89f570a2021-06-16 01:42:33 +01007346 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007347}
7348
Paul Duffin89f570a2021-06-16 01:42:33 +01007349func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007350 t.Helper()
7351
Paul Duffin55607122021-03-30 23:32:51 +01007352 fs := android.MockFS{
7353 "a.java": nil,
7354 "a.jar": nil,
7355 "apex_manifest.json": nil,
7356 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007357 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007358 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7359 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7360 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007361 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007362 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007363
Paul Duffin55607122021-03-30 23:32:51 +01007364 errorHandler := android.FixtureExpectsNoErrors
7365 if errmsg != "" {
7366 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007367 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007368
Paul Duffin55607122021-03-30 23:32:51 +01007369 result := android.GroupFixturePreparers(
7370 cc.PrepareForTestWithCcDefaultModules,
7371 java.PrepareForTestWithHiddenApiBuildComponents,
7372 java.PrepareForTestWithJavaDefaultModules,
7373 java.PrepareForTestWithJavaSdkLibraryFiles,
7374 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007375 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007376 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007377 android.FixtureModifyMockFS(func(fs android.MockFS) {
7378 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7379 insert := ""
7380 for _, fragment := range fragments {
7381 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7382 }
7383 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7384 platform_bootclasspath {
7385 name: "platform-bootclasspath",
7386 fragments: [
7387 %s
7388 ],
7389 }
7390 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007391 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007392 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007393 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007394 ).
7395 ExtendWithErrorHandler(errorHandler).
7396 RunTestWithBp(t, bp)
7397
7398 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007399}
7400
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007401func TestDuplicateDeapexeresFromPrebuiltApexes(t *testing.T) {
7402 preparers := android.GroupFixturePreparers(
7403 java.PrepareForTestWithJavaDefaultModules,
7404 PrepareForTestWithApexBuildComponents,
7405 ).
7406 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7407 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7408
7409 bpBase := `
7410 apex_set {
7411 name: "com.android.myapex",
7412 installable: true,
7413 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7414 set: "myapex.apks",
7415 }
7416
7417 apex_set {
7418 name: "com.mycompany.android.myapex",
7419 apex_name: "com.android.myapex",
7420 installable: true,
7421 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7422 set: "company-myapex.apks",
7423 }
7424
7425 prebuilt_bootclasspath_fragment {
7426 name: "my-bootclasspath-fragment",
7427 apex_available: ["com.android.myapex"],
7428 %s
7429 }
7430 `
7431
7432 t.Run("java_import", func(t *testing.T) {
7433 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7434 java_import {
7435 name: "libfoo",
7436 jars: ["libfoo.jar"],
7437 apex_available: ["com.android.myapex"],
7438 }
7439 `)
7440 })
7441
7442 t.Run("java_sdk_library_import", func(t *testing.T) {
7443 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7444 java_sdk_library_import {
7445 name: "libfoo",
7446 public: {
7447 jars: ["libbar.jar"],
7448 },
7449 apex_available: ["com.android.myapex"],
7450 }
7451 `)
7452 })
7453
7454 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7455 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7456 image_name: "art",
7457 contents: ["libfoo"],
7458 `)+`
7459 java_sdk_library_import {
7460 name: "libfoo",
7461 public: {
7462 jars: ["libbar.jar"],
7463 },
7464 apex_available: ["com.android.myapex"],
7465 }
7466 `)
7467 })
7468}
7469
Jooyung Han548640b2020-04-27 12:10:30 +09007470func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7471 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7472 apex {
7473 name: "myapex",
7474 key: "myapex.key",
7475 updatable: true,
7476 }
7477
7478 apex_key {
7479 name: "myapex.key",
7480 public_key: "testkey.avbpubkey",
7481 private_key: "testkey.pem",
7482 }
7483 `)
7484}
7485
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007486func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7487 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7488 apex {
7489 name: "myapex",
7490 key: "myapex.key",
7491 }
7492
7493 apex_key {
7494 name: "myapex.key",
7495 public_key: "testkey.avbpubkey",
7496 private_key: "testkey.pem",
7497 }
7498 `)
7499}
7500
Daniel Norman69109112021-12-02 12:52:42 -08007501func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7502 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7503 apex {
7504 name: "myapex",
7505 key: "myapex.key",
7506 updatable: true,
7507 soc_specific: true,
7508 }
7509
7510 apex_key {
7511 name: "myapex.key",
7512 public_key: "testkey.avbpubkey",
7513 private_key: "testkey.pem",
7514 }
7515 `)
7516}
7517
satayevb98371c2021-06-15 16:49:50 +01007518func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7519 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7520 apex {
7521 name: "myapex",
7522 key: "myapex.key",
7523 systemserverclasspath_fragments: [
7524 "mysystemserverclasspathfragment",
7525 ],
7526 min_sdk_version: "29",
7527 updatable: true,
7528 }
7529
7530 apex_key {
7531 name: "myapex.key",
7532 public_key: "testkey.avbpubkey",
7533 private_key: "testkey.pem",
7534 }
7535
7536 java_library {
7537 name: "foo",
7538 srcs: ["b.java"],
7539 min_sdk_version: "29",
7540 installable: true,
7541 apex_available: [
7542 "myapex",
7543 ],
7544 }
7545
7546 systemserverclasspath_fragment {
7547 name: "mysystemserverclasspathfragment",
7548 generate_classpaths_proto: false,
7549 contents: [
7550 "foo",
7551 ],
7552 apex_available: [
7553 "myapex",
7554 ],
7555 }
satayevabcd5972021-08-06 17:49:46 +01007556 `,
7557 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7558 )
satayevb98371c2021-06-15 16:49:50 +01007559}
7560
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007561func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007562 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7563 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7564 // modules to be included in the BootJars.
7565 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7566 return android.GroupFixturePreparers(
7567 dexpreopt.FixtureSetBootJars(bootJars...),
7568 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7569 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7570 }),
7571 )
7572 }
7573
7574 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7575 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7576 // specified in the ArtApexJars configuration.
7577 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7578 return android.GroupFixturePreparers(
7579 dexpreopt.FixtureSetArtBootJars(bootJars...),
7580 dexpreopt.FixtureSetBootJars(bootJars...),
7581 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7582 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7583 }),
7584 )
7585 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007586
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007587 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007588 preparer := android.GroupFixturePreparers(
7589 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7590 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7591 )
7592 fragments := []java.ApexVariantReference{
7593 {
7594 Apex: proptools.StringPtr("com.android.art.debug"),
7595 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7596 },
7597 {
7598 Apex: proptools.StringPtr("some-non-updatable-apex"),
7599 Module: proptools.StringPtr("some-non-updatable-fragment"),
7600 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007601 }
satayevabcd5972021-08-06 17:49:46 +01007602 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007603 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007604
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007605 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007606 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7607 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007608 preparer := android.GroupFixturePreparers(
7609 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7610 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7611 )
Paul Duffin60264a02021-04-12 20:02:36 +01007612 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007613 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007614
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007615 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 +01007616 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 +01007617 // Update the dexpreopt ArtApexJars directly.
7618 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7619 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007620 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007621
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007622 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 +01007623 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 +01007624 // Update the dexpreopt ArtApexJars directly.
7625 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7626 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007627 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007628
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007629 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 +01007630 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 +01007631 preparer := android.GroupFixturePreparers(
7632 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7633 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7634 )
Paul Duffin60264a02021-04-12 20:02:36 +01007635 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007636 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007637
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007638 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 +01007639 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007640 fragment := java.ApexVariantReference{
7641 Apex: proptools.StringPtr("some-non-updatable-apex"),
7642 Module: proptools.StringPtr("some-non-updatable-fragment"),
7643 }
7644 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007645 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007646
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007647 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007648 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007649 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7650 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007651 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007652
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007653 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007654 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007655 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7656 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007657 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007658
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007659 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007660 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007661 // Update the dexpreopt ArtApexJars directly.
7662 preparer := prepareSetArtJars("platform:some-platform-lib")
7663 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007664 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007665
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007666 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007667 preparer := android.GroupFixturePreparers(
7668 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7669 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7670 )
7671 fragments := []java.ApexVariantReference{
7672 {
7673 Apex: proptools.StringPtr("some-non-updatable-apex"),
7674 Module: proptools.StringPtr("some-non-updatable-fragment"),
7675 },
7676 }
7677 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007678 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007679}
7680
7681func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007682 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007683 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007684 fragment := java.ApexVariantReference{
7685 Apex: proptools.StringPtr("myapex"),
7686 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7687 }
7688
Paul Duffin064b70c2020-11-02 17:32:38 +00007689 testDexpreoptWithApexes(t, `
7690 prebuilt_apex {
7691 name: "myapex" ,
7692 arch: {
7693 arm64: {
7694 src: "myapex-arm64.apex",
7695 },
7696 arm: {
7697 src: "myapex-arm.apex",
7698 },
7699 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007700 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7701 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007702
Paul Duffin89f570a2021-06-16 01:42:33 +01007703 prebuilt_bootclasspath_fragment {
7704 name: "my-bootclasspath-fragment",
7705 contents: ["libfoo"],
7706 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007707 hidden_api: {
7708 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7709 metadata: "my-bootclasspath-fragment/metadata.csv",
7710 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01007711 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
7712 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
7713 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01007714 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007715 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007716
Paul Duffin89f570a2021-06-16 01:42:33 +01007717 java_import {
7718 name: "libfoo",
7719 jars: ["libfoo.jar"],
7720 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007721 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007722 }
7723 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007724 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007725}
7726
Spandan Dasf14e2542021-11-12 00:01:37 +00007727func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007728 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007729 bp += `
7730 apex_key {
7731 name: "myapex.key",
7732 public_key: "testkey.avbpubkey",
7733 private_key: "testkey.pem",
7734 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007735 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007736 "lib1/src/A.java": nil,
7737 "lib2/src/B.java": nil,
7738 "system/sepolicy/apex/myapex-file_contexts": nil,
7739 }
7740
Paul Duffin45338f02021-03-30 23:07:52 +01007741 errorHandler := android.FixtureExpectsNoErrors
7742 if errmsg != "" {
7743 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007744 }
Colin Crossae8600b2020-10-29 17:09:13 -07007745
Paul Duffin45338f02021-03-30 23:07:52 +01007746 android.GroupFixturePreparers(
7747 android.PrepareForTestWithAndroidBuildComponents,
7748 java.PrepareForTestWithJavaBuildComponents,
7749 PrepareForTestWithApexBuildComponents,
7750 android.PrepareForTestWithNeverallowRules(rules),
7751 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007752 apexBootJars := make([]string, 0, len(bootJars))
7753 for _, apexBootJar := range bootJars {
7754 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007755 }
satayevd604b212021-07-21 14:23:52 +01007756 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007757 }),
7758 fs.AddToFixture(),
7759 ).
7760 ExtendWithErrorHandler(errorHandler).
7761 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007762}
7763
7764func TestApexPermittedPackagesRules(t *testing.T) {
7765 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00007766 name string
7767 expectedError string
7768 bp string
7769 bootJars []string
7770 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01007771 }{
7772
7773 {
7774 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7775 expectedError: "",
7776 bp: `
7777 java_library {
7778 name: "bcp_lib1",
7779 srcs: ["lib1/src/*.java"],
7780 permitted_packages: ["foo.bar"],
7781 apex_available: ["myapex"],
7782 sdk_version: "none",
7783 system_modules: "none",
7784 }
7785 java_library {
7786 name: "nonbcp_lib2",
7787 srcs: ["lib2/src/*.java"],
7788 apex_available: ["myapex"],
7789 permitted_packages: ["a.b"],
7790 sdk_version: "none",
7791 system_modules: "none",
7792 }
7793 apex {
7794 name: "myapex",
7795 key: "myapex.key",
7796 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007797 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007798 }`,
7799 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00007800 bcpPermittedPackages: map[string][]string{
7801 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01007802 "foo.bar",
7803 },
7804 },
7805 },
7806 {
Anton Hanssone1b18362021-12-23 15:05:38 +00007807 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00007808 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 +01007809 bp: `
7810 java_library {
7811 name: "bcp_lib1",
7812 srcs: ["lib1/src/*.java"],
7813 apex_available: ["myapex"],
7814 permitted_packages: ["foo.bar"],
7815 sdk_version: "none",
7816 system_modules: "none",
7817 }
7818 java_library {
7819 name: "bcp_lib2",
7820 srcs: ["lib2/src/*.java"],
7821 apex_available: ["myapex"],
7822 permitted_packages: ["foo.bar", "bar.baz"],
7823 sdk_version: "none",
7824 system_modules: "none",
7825 }
7826 apex {
7827 name: "myapex",
7828 key: "myapex.key",
7829 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007830 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007831 }
7832 `,
7833 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00007834 bcpPermittedPackages: map[string][]string{
7835 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01007836 "foo.bar",
7837 },
Spandan Dasf14e2542021-11-12 00:01:37 +00007838 "bcp_lib2": []string{
7839 "foo.bar",
7840 },
7841 },
7842 },
7843 {
7844 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
7845 expectedError: "",
7846 bp: `
7847 java_library {
7848 name: "bcp_lib_restricted",
7849 srcs: ["lib1/src/*.java"],
7850 apex_available: ["myapex"],
7851 permitted_packages: ["foo.bar"],
7852 sdk_version: "none",
7853 min_sdk_version: "29",
7854 system_modules: "none",
7855 }
7856 java_library {
7857 name: "bcp_lib_unrestricted",
7858 srcs: ["lib2/src/*.java"],
7859 apex_available: ["myapex"],
7860 permitted_packages: ["foo.bar", "bar.baz"],
7861 sdk_version: "none",
7862 min_sdk_version: "29",
7863 system_modules: "none",
7864 }
7865 apex {
7866 name: "myapex",
7867 key: "myapex.key",
7868 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
7869 updatable: true,
7870 min_sdk_version: "29",
7871 }
7872 `,
7873 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7874 bcpPermittedPackages: map[string][]string{
7875 "bcp_lib1_non_updateable": []string{
7876 "foo.bar",
7877 },
7878 // 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 +01007879 },
7880 },
7881 }
7882 for _, tc := range testcases {
7883 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00007884 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
7885 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01007886 })
7887 }
7888}
7889
Jiyong Park62304bb2020-04-13 16:19:48 +09007890func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007891 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007892 apex {
7893 name: "myapex",
7894 key: "myapex.key",
7895 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007896 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007897 }
7898
7899 apex_key {
7900 name: "myapex.key",
7901 public_key: "testkey.avbpubkey",
7902 private_key: "testkey.pem",
7903 }
7904
7905 cc_library {
7906 name: "mylib",
7907 srcs: ["mylib.cpp"],
7908 system_shared_libs: [],
7909 stl: "none",
7910 stubs: {
7911 versions: ["1"],
7912 },
7913 apex_available: ["myapex"],
7914 }
7915
7916 cc_library {
7917 name: "myprivlib",
7918 srcs: ["mylib.cpp"],
7919 system_shared_libs: [],
7920 stl: "none",
7921 apex_available: ["myapex"],
7922 }
7923
7924
7925 cc_test {
7926 name: "mytest",
7927 gtest: false,
7928 srcs: ["mylib.cpp"],
7929 system_shared_libs: [],
7930 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007931 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007932 test_for: ["myapex"]
7933 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007934
7935 cc_library {
7936 name: "mytestlib",
7937 srcs: ["mylib.cpp"],
7938 system_shared_libs: [],
7939 shared_libs: ["mylib", "myprivlib"],
7940 stl: "none",
7941 test_for: ["myapex"],
7942 }
7943
7944 cc_benchmark {
7945 name: "mybench",
7946 srcs: ["mylib.cpp"],
7947 system_shared_libs: [],
7948 shared_libs: ["mylib", "myprivlib"],
7949 stl: "none",
7950 test_for: ["myapex"],
7951 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007952 `)
7953
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007954 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007955 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007956 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7957 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7958 }
7959
7960 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007961 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007962 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7963 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7964 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7965}
Jiyong Park46a512f2020-12-04 18:02:13 +09007966
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007967func TestIndirectTestFor(t *testing.T) {
7968 ctx := testApex(t, `
7969 apex {
7970 name: "myapex",
7971 key: "myapex.key",
7972 native_shared_libs: ["mylib", "myprivlib"],
7973 updatable: false,
7974 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007975
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007976 apex_key {
7977 name: "myapex.key",
7978 public_key: "testkey.avbpubkey",
7979 private_key: "testkey.pem",
7980 }
7981
7982 cc_library {
7983 name: "mylib",
7984 srcs: ["mylib.cpp"],
7985 system_shared_libs: [],
7986 stl: "none",
7987 stubs: {
7988 versions: ["1"],
7989 },
7990 apex_available: ["myapex"],
7991 }
7992
7993 cc_library {
7994 name: "myprivlib",
7995 srcs: ["mylib.cpp"],
7996 system_shared_libs: [],
7997 stl: "none",
7998 shared_libs: ["mylib"],
7999 apex_available: ["myapex"],
8000 }
8001
8002 cc_library {
8003 name: "mytestlib",
8004 srcs: ["mylib.cpp"],
8005 system_shared_libs: [],
8006 shared_libs: ["myprivlib"],
8007 stl: "none",
8008 test_for: ["myapex"],
8009 }
8010 `)
8011
8012 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008013 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008014 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8015 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8016 }
8017
8018 // The platform variant of mytestlib links to the platform variant of the
8019 // internal myprivlib.
8020 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8021
8022 // The platform variant of myprivlib links to the platform variant of mylib
8023 // and bypasses its stubs.
8024 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 +09008025}
8026
Martin Stjernholmec009002021-03-27 15:18:31 +00008027func TestTestForForLibInOtherApex(t *testing.T) {
8028 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8029 _ = testApex(t, `
8030 apex {
8031 name: "com.android.art",
8032 key: "myapex.key",
8033 native_shared_libs: ["mylib"],
8034 updatable: false,
8035 }
8036
8037 apex {
8038 name: "com.android.art.debug",
8039 key: "myapex.key",
8040 native_shared_libs: ["mylib", "mytestlib"],
8041 updatable: false,
8042 }
8043
8044 apex_key {
8045 name: "myapex.key",
8046 public_key: "testkey.avbpubkey",
8047 private_key: "testkey.pem",
8048 }
8049
8050 cc_library {
8051 name: "mylib",
8052 srcs: ["mylib.cpp"],
8053 system_shared_libs: [],
8054 stl: "none",
8055 stubs: {
8056 versions: ["1"],
8057 },
8058 apex_available: ["com.android.art", "com.android.art.debug"],
8059 }
8060
8061 cc_library {
8062 name: "mytestlib",
8063 srcs: ["mylib.cpp"],
8064 system_shared_libs: [],
8065 shared_libs: ["mylib"],
8066 stl: "none",
8067 apex_available: ["com.android.art.debug"],
8068 test_for: ["com.android.art"],
8069 }
8070 `,
8071 android.MockFS{
8072 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8073 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8074 }.AddToFixture())
8075}
8076
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008077// TODO(jungjw): Move this to proptools
8078func intPtr(i int) *int {
8079 return &i
8080}
8081
8082func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008083 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008084 apex_set {
8085 name: "myapex",
8086 set: "myapex.apks",
8087 filename: "foo_v2.apex",
8088 overrides: ["foo"],
8089 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008090 `,
8091 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8092 variables.Platform_sdk_version = intPtr(30)
8093 }),
8094 android.FixtureModifyConfig(func(config android.Config) {
8095 config.Targets[android.Android] = []android.Target{
8096 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8097 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8098 }
8099 }),
8100 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008101
Paul Duffin24704672021-04-06 16:09:30 +01008102 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008103
8104 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008105 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008106 actual := extractedApex.Args["abis"]
8107 expected := "ARMEABI_V7A,ARM64_V8A"
8108 if actual != expected {
8109 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8110 }
8111 actual = extractedApex.Args["sdk-version"]
8112 expected = "30"
8113 if actual != expected {
8114 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8115 }
8116
Paul Duffin6717d882021-06-15 19:09:41 +01008117 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008118 a := m.Module().(*ApexSet)
8119 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008120 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008121 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8122 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8123 }
8124}
8125
Jiyong Park7d95a512020-05-10 15:16:24 +09008126func TestNoStaticLinkingToStubsLib(t *testing.T) {
8127 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8128 apex {
8129 name: "myapex",
8130 key: "myapex.key",
8131 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008132 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008133 }
8134
8135 apex_key {
8136 name: "myapex.key",
8137 public_key: "testkey.avbpubkey",
8138 private_key: "testkey.pem",
8139 }
8140
8141 cc_library {
8142 name: "mylib",
8143 srcs: ["mylib.cpp"],
8144 static_libs: ["otherlib"],
8145 system_shared_libs: [],
8146 stl: "none",
8147 apex_available: [ "myapex" ],
8148 }
8149
8150 cc_library {
8151 name: "otherlib",
8152 srcs: ["mylib.cpp"],
8153 system_shared_libs: [],
8154 stl: "none",
8155 stubs: {
8156 versions: ["1", "2", "3"],
8157 },
8158 apex_available: [ "myapex" ],
8159 }
8160 `)
8161}
8162
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008163func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008164 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008165 apex {
8166 name: "myapex",
8167 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008168 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008169 custom_sign_tool: "sign_myapex",
8170 }
8171
8172 apex_key {
8173 name: "myapex.key",
8174 public_key: "testkey.avbpubkey",
8175 private_key: "testkey.pem",
8176 }
8177 `)
8178
8179 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8180 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8181 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"`)
8182}
8183
8184func TestApexKeysTxtOverrides(t *testing.T) {
8185 ctx := testApex(t, `
8186 apex {
8187 name: "myapex",
8188 key: "myapex.key",
8189 updatable: false,
8190 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008191 }
8192
8193 apex_key {
8194 name: "myapex.key",
8195 public_key: "testkey.avbpubkey",
8196 private_key: "testkey.pem",
8197 }
8198
8199 prebuilt_apex {
8200 name: "myapex",
8201 prefer: true,
8202 arch: {
8203 arm64: {
8204 src: "myapex-arm64.apex",
8205 },
8206 arm: {
8207 src: "myapex-arm.apex",
8208 },
8209 },
8210 }
8211
8212 apex_set {
8213 name: "myapex_set",
8214 set: "myapex.apks",
8215 filename: "myapex_set.apex",
8216 overrides: ["myapex"],
8217 }
8218 `)
8219
8220 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8221 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8222 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 +09008223 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 +09008224}
8225
Jooyung Han938b5932020-06-20 12:47:47 +09008226func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008227 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008228 apex {
8229 name: "myapex",
8230 key: "myapex.key",
8231 apps: ["app"],
8232 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008233 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008234 }
8235
8236 apex_key {
8237 name: "myapex.key",
8238 public_key: "testkey.avbpubkey",
8239 private_key: "testkey.pem",
8240 }
8241
8242 android_app {
8243 name: "app",
8244 srcs: ["foo/bar/MyClass.java"],
8245 package_name: "foo",
8246 sdk_version: "none",
8247 system_modules: "none",
8248 apex_available: [ "myapex" ],
8249 }
8250 `, withFiles(map[string][]byte{
8251 "sub/Android.bp": []byte(`
8252 override_apex {
8253 name: "override_myapex",
8254 base: "myapex",
8255 apps: ["override_app"],
8256 allowed_files: ":allowed",
8257 }
8258 // Overridable "path" property should be referenced indirectly
8259 filegroup {
8260 name: "allowed",
8261 srcs: ["allowed.txt"],
8262 }
8263 override_android_app {
8264 name: "override_app",
8265 base: "app",
8266 package_name: "bar",
8267 }
8268 `),
8269 }))
8270
8271 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8272 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8273 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8274 }
8275
8276 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8277 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8278 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8279 }
8280}
8281
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008282func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008283 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008284 apex {
8285 name: "myapex",
8286 key: "myapex.key",
8287 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008288 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008289 }
8290
8291 apex_key {
8292 name: "myapex.key",
8293 public_key: "testkey.avbpubkey",
8294 private_key: "testkey.pem",
8295 }
8296
8297 cc_library {
8298 name: "mylib",
8299 srcs: ["mylib.cpp"],
8300 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008301 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008302 },
8303 apex_available: ["myapex"],
8304 }
8305
8306 cc_prebuilt_library_shared {
8307 name: "mylib",
8308 prefer: false,
8309 srcs: ["prebuilt.so"],
8310 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008311 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008312 },
8313 apex_available: ["myapex"],
8314 }
8315 `)
8316}
8317
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008318func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008319 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008320 apex {
8321 name: "myapex",
8322 key: "myapex.key",
8323 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008324 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008325 }
8326 apex_key {
8327 name: "myapex.key",
8328 public_key: "testkey.avbpubkey",
8329 private_key: "testkey.pem",
8330 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008331 `,
8332 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8333 variables.CompressedApex = proptools.BoolPtr(true)
8334 }),
8335 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008336
8337 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8338 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8339
8340 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8341 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8342
8343 // Make sure output of bundle is .capex
8344 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8345 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8346
8347 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008348 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008349 var builder strings.Builder
8350 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8351 androidMk := builder.String()
8352 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8353}
8354
Martin Stjernholm2856c662020-12-02 15:03:42 +00008355func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008356 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008357 apex {
8358 name: "myapex",
8359 key: "myapex.key",
8360 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008361 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008362 }
8363
8364 apex_key {
8365 name: "myapex.key",
8366 public_key: "testkey.avbpubkey",
8367 private_key: "testkey.pem",
8368 }
8369
8370 cc_library {
8371 name: "mylib",
8372 srcs: ["mylib.cpp"],
8373 apex_available: ["myapex"],
8374 shared_libs: ["otherlib"],
8375 system_shared_libs: [],
8376 }
8377
8378 cc_library {
8379 name: "otherlib",
8380 srcs: ["mylib.cpp"],
8381 stubs: {
8382 versions: ["current"],
8383 },
8384 }
8385
8386 cc_prebuilt_library_shared {
8387 name: "otherlib",
8388 prefer: true,
8389 srcs: ["prebuilt.so"],
8390 stubs: {
8391 versions: ["current"],
8392 },
8393 }
8394 `)
8395
8396 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008397 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008398 var builder strings.Builder
8399 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8400 androidMk := builder.String()
8401
8402 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8403 // a thing there.
8404 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
8405}
8406
Jiyong Parke3867542020-12-03 17:28:25 +09008407func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008408 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008409 apex {
8410 name: "myapex",
8411 key: "myapex.key",
8412 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008413 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008414 }
8415
8416 apex_key {
8417 name: "myapex.key",
8418 public_key: "testkey.avbpubkey",
8419 private_key: "testkey.pem",
8420 }
8421
8422 cc_library {
8423 name: "mylib",
8424 srcs: ["mylib.cpp"],
8425 system_shared_libs: [],
8426 stl: "none",
8427 apex_available: ["myapex"],
8428 shared_libs: ["mylib2"],
8429 target: {
8430 apex: {
8431 exclude_shared_libs: ["mylib2"],
8432 },
8433 },
8434 }
8435
8436 cc_library {
8437 name: "mylib2",
8438 srcs: ["mylib.cpp"],
8439 system_shared_libs: [],
8440 stl: "none",
8441 }
8442 `)
8443
8444 // Check if mylib is linked to mylib2 for the non-apex target
8445 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8446 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8447
8448 // Make sure that the link doesn't occur for the apex target
8449 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8450 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8451
8452 // It shouldn't appear in the copy cmd as well.
8453 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8454 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8455}
8456
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008457func TestPrebuiltStubLibDep(t *testing.T) {
8458 bpBase := `
8459 apex {
8460 name: "myapex",
8461 key: "myapex.key",
8462 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008463 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008464 }
8465 apex_key {
8466 name: "myapex.key",
8467 public_key: "testkey.avbpubkey",
8468 private_key: "testkey.pem",
8469 }
8470 cc_library {
8471 name: "mylib",
8472 srcs: ["mylib.cpp"],
8473 apex_available: ["myapex"],
8474 shared_libs: ["stublib"],
8475 system_shared_libs: [],
8476 }
8477 apex {
8478 name: "otherapex",
8479 enabled: %s,
8480 key: "myapex.key",
8481 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008482 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008483 }
8484 `
8485
8486 stublibSourceBp := `
8487 cc_library {
8488 name: "stublib",
8489 srcs: ["mylib.cpp"],
8490 apex_available: ["otherapex"],
8491 system_shared_libs: [],
8492 stl: "none",
8493 stubs: {
8494 versions: ["1"],
8495 },
8496 }
8497 `
8498
8499 stublibPrebuiltBp := `
8500 cc_prebuilt_library_shared {
8501 name: "stublib",
8502 srcs: ["prebuilt.so"],
8503 apex_available: ["otherapex"],
8504 stubs: {
8505 versions: ["1"],
8506 },
8507 %s
8508 }
8509 `
8510
8511 tests := []struct {
8512 name string
8513 stublibBp string
8514 usePrebuilt bool
8515 modNames []string // Modules to collect AndroidMkEntries for
8516 otherApexEnabled []string
8517 }{
8518 {
8519 name: "only_source",
8520 stublibBp: stublibSourceBp,
8521 usePrebuilt: false,
8522 modNames: []string{"stublib"},
8523 otherApexEnabled: []string{"true", "false"},
8524 },
8525 {
8526 name: "source_preferred",
8527 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8528 usePrebuilt: false,
8529 modNames: []string{"stublib", "prebuilt_stublib"},
8530 otherApexEnabled: []string{"true", "false"},
8531 },
8532 {
8533 name: "prebuilt_preferred",
8534 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8535 usePrebuilt: true,
8536 modNames: []string{"stublib", "prebuilt_stublib"},
8537 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8538 },
8539 {
8540 name: "only_prebuilt",
8541 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8542 usePrebuilt: true,
8543 modNames: []string{"stublib"},
8544 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8545 },
8546 }
8547
8548 for _, test := range tests {
8549 t.Run(test.name, func(t *testing.T) {
8550 for _, otherApexEnabled := range test.otherApexEnabled {
8551 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008552 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008553
8554 type modAndMkEntries struct {
8555 mod *cc.Module
8556 mkEntries android.AndroidMkEntries
8557 }
8558 entries := []*modAndMkEntries{}
8559
8560 // Gather shared lib modules that are installable
8561 for _, modName := range test.modNames {
8562 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8563 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8564 continue
8565 }
8566 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008567 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008568 continue
8569 }
Colin Crossaa255532020-07-03 13:18:24 -07008570 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008571 if ent.Disabled {
8572 continue
8573 }
8574 entries = append(entries, &modAndMkEntries{
8575 mod: mod,
8576 mkEntries: ent,
8577 })
8578 }
8579 }
8580 }
8581
8582 var entry *modAndMkEntries = nil
8583 for _, ent := range entries {
8584 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8585 if entry != nil {
8586 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8587 } else {
8588 entry = ent
8589 }
8590 }
8591 }
8592
8593 if entry == nil {
8594 t.Errorf("AndroidMk entry for \"stublib\" missing")
8595 } else {
8596 isPrebuilt := entry.mod.Prebuilt() != nil
8597 if isPrebuilt != test.usePrebuilt {
8598 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8599 }
8600 if !entry.mod.IsStubs() {
8601 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8602 }
8603 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8604 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8605 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008606 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008607 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008608 if !android.InList(expected, cflags) {
8609 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8610 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008611 }
8612 })
8613 }
8614 })
8615 }
8616}
8617
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008618func TestHostApexInHostOnlyBuild(t *testing.T) {
8619 testApex(t, `
8620 apex {
8621 name: "myapex",
8622 host_supported: true,
8623 key: "myapex.key",
8624 updatable: false,
8625 payload_type: "zip",
8626 }
8627 apex_key {
8628 name: "myapex.key",
8629 public_key: "testkey.avbpubkey",
8630 private_key: "testkey.pem",
8631 }
8632 `,
8633 android.FixtureModifyConfig(func(config android.Config) {
8634 // We may not have device targets in all builds, e.g. in
8635 // prebuilts/build-tools/build-prebuilts.sh
8636 config.Targets[android.Android] = []android.Target{}
8637 }))
8638}
8639
Colin Crossc33e5212021-05-25 18:16:02 -07008640func TestApexJavaCoverage(t *testing.T) {
8641 bp := `
8642 apex {
8643 name: "myapex",
8644 key: "myapex.key",
8645 java_libs: ["mylib"],
8646 bootclasspath_fragments: ["mybootclasspathfragment"],
8647 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8648 updatable: false,
8649 }
8650
8651 apex_key {
8652 name: "myapex.key",
8653 public_key: "testkey.avbpubkey",
8654 private_key: "testkey.pem",
8655 }
8656
8657 java_library {
8658 name: "mylib",
8659 srcs: ["mylib.java"],
8660 apex_available: ["myapex"],
8661 compile_dex: true,
8662 }
8663
8664 bootclasspath_fragment {
8665 name: "mybootclasspathfragment",
8666 contents: ["mybootclasspathlib"],
8667 apex_available: ["myapex"],
8668 }
8669
8670 java_library {
8671 name: "mybootclasspathlib",
8672 srcs: ["mybootclasspathlib.java"],
8673 apex_available: ["myapex"],
8674 compile_dex: true,
8675 }
8676
8677 systemserverclasspath_fragment {
8678 name: "mysystemserverclasspathfragment",
8679 contents: ["mysystemserverclasspathlib"],
8680 apex_available: ["myapex"],
8681 }
8682
8683 java_library {
8684 name: "mysystemserverclasspathlib",
8685 srcs: ["mysystemserverclasspathlib.java"],
8686 apex_available: ["myapex"],
8687 compile_dex: true,
8688 }
8689 `
8690
8691 result := android.GroupFixturePreparers(
8692 PrepareForTestWithApexBuildComponents,
8693 prepareForTestWithMyapex,
8694 java.PrepareForTestWithJavaDefaultModules,
8695 android.PrepareForTestWithAndroidBuildComponents,
8696 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008697 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8698 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Colin Crossc33e5212021-05-25 18:16:02 -07008699 android.FixtureMergeEnv(map[string]string{
8700 "EMMA_INSTRUMENT": "true",
8701 }),
8702 ).RunTest(t)
8703
8704 // Make sure jacoco ran on both mylib and mybootclasspathlib
8705 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8706 t.Errorf("Failed to find jacoco rule for mylib")
8707 }
8708 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8709 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8710 }
8711 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8712 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8713 }
8714}
8715
Jiyong Park192600a2021-08-03 07:52:17 +00008716func TestProhibitStaticExecutable(t *testing.T) {
8717 testApexError(t, `executable mybin is static`, `
8718 apex {
8719 name: "myapex",
8720 key: "myapex.key",
8721 binaries: ["mybin"],
8722 min_sdk_version: "29",
8723 }
8724
8725 apex_key {
8726 name: "myapex.key",
8727 public_key: "testkey.avbpubkey",
8728 private_key: "testkey.pem",
8729 }
8730
8731 cc_binary {
8732 name: "mybin",
8733 srcs: ["mylib.cpp"],
8734 relative_install_path: "foo/bar",
8735 static_executable: true,
8736 system_shared_libs: [],
8737 stl: "none",
8738 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008739 min_sdk_version: "29",
8740 }
8741 `)
8742
8743 testApexError(t, `executable mybin.rust is static`, `
8744 apex {
8745 name: "myapex",
8746 key: "myapex.key",
8747 binaries: ["mybin.rust"],
8748 min_sdk_version: "29",
8749 }
8750
8751 apex_key {
8752 name: "myapex.key",
8753 public_key: "testkey.avbpubkey",
8754 private_key: "testkey.pem",
8755 }
8756
8757 rust_binary {
8758 name: "mybin.rust",
8759 srcs: ["foo.rs"],
8760 static_executable: true,
8761 apex_available: ["myapex"],
8762 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008763 }
8764 `)
8765}
8766
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008767func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
8768 ctx := testApex(t, `
8769 apex {
8770 name: "myapex",
8771 key: "myapex.key",
8772 updatable: false,
8773 java_libs: ["foo"],
8774 }
8775
8776 apex_key {
8777 name: "myapex.key",
8778 public_key: "testkey.avbpubkey",
8779 private_key: "testkey.pem",
8780 }
8781
8782 java_library {
8783 name: "foo",
8784 srcs: ["foo.java"],
8785 apex_available: ["myapex"],
8786 installable: true,
8787 }
8788 `,
8789 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8790 )
8791
8792 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8793 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8794 var builder strings.Builder
8795 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8796 androidMk := builder.String()
8797 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")
8798}
8799
8800func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
8801 ctx := testApex(t, `
8802 prebuilt_apex {
8803 name: "myapex",
8804 arch: {
8805 arm64: {
8806 src: "myapex-arm64.apex",
8807 },
8808 arm: {
8809 src: "myapex-arm.apex",
8810 },
8811 },
8812 exported_java_libs: ["foo"],
8813 }
8814
8815 java_import {
8816 name: "foo",
8817 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00008818 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00008819 }
8820 `,
8821 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8822 )
8823
8824 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
8825 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
8826 mainModuleEntries := entriesList[0]
8827 android.AssertArrayString(t,
8828 "LOCAL_REQUIRED_MODULES",
8829 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
8830 []string{
8831 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
8832 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
8833 })
8834}
8835
Jiyong Parkcacc4f32021-10-28 14:26:03 +09008836func TestAndroidMk_RequiredModules(t *testing.T) {
8837 ctx := testApex(t, `
8838 apex {
8839 name: "myapex",
8840 key: "myapex.key",
8841 updatable: false,
8842 java_libs: ["foo"],
8843 required: ["otherapex"],
8844 }
8845
8846 apex {
8847 name: "otherapex",
8848 key: "myapex.key",
8849 updatable: false,
8850 java_libs: ["foo"],
8851 required: ["otherapex"],
8852 }
8853
8854 apex_key {
8855 name: "myapex.key",
8856 public_key: "testkey.avbpubkey",
8857 private_key: "testkey.pem",
8858 }
8859
8860 java_library {
8861 name: "foo",
8862 srcs: ["foo.java"],
8863 apex_available: ["myapex", "otherapex"],
8864 installable: true,
8865 }
8866 `)
8867
8868 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8869 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
8870 var builder strings.Builder
8871 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8872 androidMk := builder.String()
8873 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherapex")
8874}
8875
Jiakai Zhangd70dff72022-02-24 15:06:05 +00008876func TestAndroidMk_RequiredDeps(t *testing.T) {
8877 ctx := testApex(t, `
8878 apex {
8879 name: "myapex",
8880 key: "myapex.key",
8881 updatable: false,
8882 }
8883
8884 apex_key {
8885 name: "myapex.key",
8886 public_key: "testkey.avbpubkey",
8887 private_key: "testkey.pem",
8888 }
8889 `)
8890
8891 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8892 bundle.requiredDeps = append(bundle.requiredDeps, "foo")
8893 data := android.AndroidMkDataForTest(t, ctx, bundle)
8894 var builder strings.Builder
8895 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
8896 androidMk := builder.String()
8897 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += foo")
8898
8899 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
8900 flattenedBundle.requiredDeps = append(flattenedBundle.requiredDeps, "foo")
8901 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
8902 var flattenedBuilder strings.Builder
8903 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
8904 flattenedAndroidMk := flattenedBuilder.String()
8905 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES += foo")
8906}
8907
Jooyung Hana6d36672022-02-24 13:58:07 +09008908func TestApexOutputFileProducer(t *testing.T) {
8909 for _, tc := range []struct {
8910 name string
8911 ref string
8912 expected_data []string
8913 }{
8914 {
8915 name: "test_using_output",
8916 ref: ":myapex",
8917 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
8918 },
8919 {
8920 name: "test_using_apex",
8921 ref: ":myapex{.apex}",
8922 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
8923 },
8924 } {
8925 t.Run(tc.name, func(t *testing.T) {
8926 ctx := testApex(t, `
8927 apex {
8928 name: "myapex",
8929 key: "myapex.key",
8930 compressible: true,
8931 updatable: false,
8932 }
8933
8934 apex_key {
8935 name: "myapex.key",
8936 public_key: "testkey.avbpubkey",
8937 private_key: "testkey.pem",
8938 }
8939
8940 java_test {
8941 name: "`+tc.name+`",
8942 srcs: ["a.java"],
8943 data: ["`+tc.ref+`"],
8944 }
8945 `,
8946 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8947 variables.CompressedApex = proptools.BoolPtr(true)
8948 }))
8949 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
8950 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
8951 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
8952 })
8953 }
8954}
8955
satayev758968a2021-12-06 11:42:40 +00008956func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
8957 preparer := android.GroupFixturePreparers(
8958 PrepareForTestWithApexBuildComponents,
8959 prepareForTestWithMyapex,
8960 java.PrepareForTestWithJavaSdkLibraryFiles,
8961 java.PrepareForTestWithJavaDefaultModules,
8962 android.PrepareForTestWithAndroidBuildComponents,
8963 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8964 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
8965 )
8966
8967 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
8968 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
8969 preparer.RunTestWithBp(t, `
8970 apex {
8971 name: "myapex",
8972 key: "myapex.key",
8973 bootclasspath_fragments: ["mybootclasspathfragment"],
8974 min_sdk_version: "30",
8975 updatable: false,
8976 }
8977
8978 apex_key {
8979 name: "myapex.key",
8980 public_key: "testkey.avbpubkey",
8981 private_key: "testkey.pem",
8982 }
8983
8984 bootclasspath_fragment {
8985 name: "mybootclasspathfragment",
8986 contents: ["mybootclasspathlib"],
8987 apex_available: ["myapex"],
8988 }
8989
8990 java_sdk_library {
8991 name: "mybootclasspathlib",
8992 srcs: ["mybootclasspathlib.java"],
8993 apex_available: ["myapex"],
8994 compile_dex: true,
8995 unsafe_ignore_missing_latest_api: true,
8996 min_sdk_version: "31",
8997 static_libs: ["util"],
8998 }
8999
9000 java_library {
9001 name: "util",
9002 srcs: ["a.java"],
9003 apex_available: ["myapex"],
9004 min_sdk_version: "31",
9005 static_libs: ["another_util"],
9006 }
9007
9008 java_library {
9009 name: "another_util",
9010 srcs: ["a.java"],
9011 min_sdk_version: "31",
9012 apex_available: ["myapex"],
9013 }
9014 `)
9015 })
9016
9017 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9018 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9019 preparer.RunTestWithBp(t, `
9020 apex {
9021 name: "myapex",
9022 key: "myapex.key",
9023 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9024 min_sdk_version: "30",
9025 updatable: false,
9026 }
9027
9028 apex_key {
9029 name: "myapex.key",
9030 public_key: "testkey.avbpubkey",
9031 private_key: "testkey.pem",
9032 }
9033
9034 systemserverclasspath_fragment {
9035 name: "mysystemserverclasspathfragment",
9036 contents: ["mysystemserverclasspathlib"],
9037 apex_available: ["myapex"],
9038 }
9039
9040 java_sdk_library {
9041 name: "mysystemserverclasspathlib",
9042 srcs: ["mysystemserverclasspathlib.java"],
9043 apex_available: ["myapex"],
9044 compile_dex: true,
9045 min_sdk_version: "32",
9046 unsafe_ignore_missing_latest_api: true,
9047 static_libs: ["util"],
9048 }
9049
9050 java_library {
9051 name: "util",
9052 srcs: ["a.java"],
9053 apex_available: ["myapex"],
9054 min_sdk_version: "31",
9055 static_libs: ["another_util"],
9056 }
9057
9058 java_library {
9059 name: "another_util",
9060 srcs: ["a.java"],
9061 min_sdk_version: "31",
9062 apex_available: ["myapex"],
9063 }
9064 `)
9065 })
9066
9067 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9068 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9069 RunTestWithBp(t, `
9070 apex {
9071 name: "myapex",
9072 key: "myapex.key",
9073 bootclasspath_fragments: ["mybootclasspathfragment"],
9074 min_sdk_version: "30",
9075 updatable: false,
9076 }
9077
9078 apex_key {
9079 name: "myapex.key",
9080 public_key: "testkey.avbpubkey",
9081 private_key: "testkey.pem",
9082 }
9083
9084 bootclasspath_fragment {
9085 name: "mybootclasspathfragment",
9086 contents: ["mybootclasspathlib"],
9087 apex_available: ["myapex"],
9088 }
9089
9090 java_sdk_library {
9091 name: "mybootclasspathlib",
9092 srcs: ["mybootclasspathlib.java"],
9093 apex_available: ["myapex"],
9094 compile_dex: true,
9095 unsafe_ignore_missing_latest_api: true,
9096 }
9097 `)
9098 })
9099
9100 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9101 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9102 RunTestWithBp(t, `
9103 apex {
9104 name: "myapex",
9105 key: "myapex.key",
9106 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9107 min_sdk_version: "30",
9108 updatable: false,
9109 }
9110
9111 apex_key {
9112 name: "myapex.key",
9113 public_key: "testkey.avbpubkey",
9114 private_key: "testkey.pem",
9115 }
9116
9117 systemserverclasspath_fragment {
9118 name: "mysystemserverclasspathfragment",
9119 contents: ["mysystemserverclasspathlib"],
9120 apex_available: ["myapex"],
9121 }
9122
9123 java_sdk_library {
9124 name: "mysystemserverclasspathlib",
9125 srcs: ["mysystemserverclasspathlib.java"],
9126 apex_available: ["myapex"],
9127 compile_dex: true,
9128 unsafe_ignore_missing_latest_api: true,
9129 }
9130 `)
9131 })
9132}
9133
Jiakai Zhang6decef92022-01-12 17:56:19 +00009134// Verifies that the APEX depends on all the Make modules in the list.
9135func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9136 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9137 for _, dep := range deps {
9138 android.AssertStringListContains(t, "", a.requiredDeps, dep)
9139 }
9140}
9141
9142// Verifies that the APEX does not depend on any of the Make modules in the list.
9143func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9144 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9145 for _, dep := range deps {
9146 android.AssertStringListDoesNotContain(t, "", a.requiredDeps, dep)
9147 }
9148}
9149
Spandan Das66773252022-01-15 00:23:18 +00009150func TestApexStrictUpdtabilityLint(t *testing.T) {
9151 bpTemplate := `
9152 apex {
9153 name: "myapex",
9154 key: "myapex.key",
9155 java_libs: ["myjavalib"],
9156 updatable: %v,
9157 min_sdk_version: "29",
9158 }
9159 apex_key {
9160 name: "myapex.key",
9161 }
9162 java_library {
9163 name: "myjavalib",
9164 srcs: ["MyClass.java"],
9165 apex_available: [ "myapex" ],
9166 lint: {
9167 strict_updatability_linting: %v,
9168 },
9169 sdk_version: "current",
9170 min_sdk_version: "29",
9171 }
9172 `
9173 fs := android.MockFS{
9174 "lint-baseline.xml": nil,
9175 }
9176
9177 testCases := []struct {
9178 testCaseName string
9179 apexUpdatable bool
9180 javaStrictUpdtabilityLint bool
9181 lintFileExists bool
9182 disallowedFlagExpected bool
9183 }{
9184 {
9185 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9186 apexUpdatable: true,
9187 javaStrictUpdtabilityLint: true,
9188 lintFileExists: false,
9189 disallowedFlagExpected: false,
9190 },
9191 {
9192 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9193 apexUpdatable: false,
9194 javaStrictUpdtabilityLint: false,
9195 lintFileExists: true,
9196 disallowedFlagExpected: false,
9197 },
9198 {
9199 testCaseName: "non-updatable apex respects strict updatability of javalib",
9200 apexUpdatable: false,
9201 javaStrictUpdtabilityLint: true,
9202 lintFileExists: true,
9203 disallowedFlagExpected: true,
9204 },
9205 {
9206 testCaseName: "updatable apex sets strict updatability of javalib to true",
9207 apexUpdatable: true,
9208 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9209 lintFileExists: true,
9210 disallowedFlagExpected: true,
9211 },
9212 }
9213
9214 for _, testCase := range testCases {
9215 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9216 fixtures := []android.FixturePreparer{}
9217 if testCase.lintFileExists {
9218 fixtures = append(fixtures, fs.AddToFixture())
9219 }
9220
9221 result := testApex(t, bp, fixtures...)
9222 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9223 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9224 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9225
9226 if disallowedFlagActual != testCase.disallowedFlagExpected {
9227 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9228 }
9229 }
9230}
9231
Spandan Dasd9c23ab2022-02-10 02:34:13 +00009232func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9233 bp := `
9234 apex {
9235 name: "myapex",
9236 key: "myapex.key",
9237 java_libs: ["myjavalib"],
9238 updatable: true,
9239 min_sdk_version: "29",
9240 }
9241 apex_key {
9242 name: "myapex.key",
9243 }
9244 java_library {
9245 name: "myjavalib",
9246 srcs: ["MyClass.java"],
9247 apex_available: [ "myapex" ],
9248 sdk_version: "current",
9249 min_sdk_version: "29",
9250 }
9251 `
9252
9253 testCases := []struct {
9254 testCaseName string
9255 moduleDirectory string
9256 disallowedFlagExpected bool
9257 }{
9258 {
9259 testCaseName: "lintable module defined outside libcore",
9260 moduleDirectory: "",
9261 disallowedFlagExpected: true,
9262 },
9263 {
9264 testCaseName: "lintable module defined in libcore root directory",
9265 moduleDirectory: "libcore/",
9266 disallowedFlagExpected: false,
9267 },
9268 {
9269 testCaseName: "lintable module defined in libcore child directory",
9270 moduleDirectory: "libcore/childdir/",
9271 disallowedFlagExpected: true,
9272 },
9273 }
9274
9275 for _, testCase := range testCases {
9276 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9277 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9278 result := testApex(t, "", lintFileCreator, bpFileCreator)
9279 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9280 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9281 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9282 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9283
9284 if disallowedFlagActual != testCase.disallowedFlagExpected {
9285 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9286 }
9287 }
9288}
9289
Spandan Das66773252022-01-15 00:23:18 +00009290// checks transtive deps of an apex coming from bootclasspath_fragment
9291func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9292 bp := `
9293 apex {
9294 name: "myapex",
9295 key: "myapex.key",
9296 bootclasspath_fragments: ["mybootclasspathfragment"],
9297 updatable: true,
9298 min_sdk_version: "29",
9299 }
9300 apex_key {
9301 name: "myapex.key",
9302 }
9303 bootclasspath_fragment {
9304 name: "mybootclasspathfragment",
9305 contents: ["myjavalib"],
9306 apex_available: ["myapex"],
9307 }
9308 java_library {
9309 name: "myjavalib",
9310 srcs: ["MyClass.java"],
9311 apex_available: [ "myapex" ],
9312 sdk_version: "current",
9313 min_sdk_version: "29",
9314 compile_dex: true,
9315 }
9316 `
9317 fs := android.MockFS{
9318 "lint-baseline.xml": nil,
9319 }
9320
9321 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9322 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9323 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9324 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9325 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9326 }
9327}
9328
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07009329func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01009330 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07009331}