blob: 6f56cbdb889cd3476d809ccee39a26e86fad53a6 [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 Duffin40b62572021-03-20 11:39:01 +000053 apexFixtureFactory.Extend(preparers...).
Paul Duffine05480a2021-03-08 15:07:14 +000054 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000055 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090056}
57
Paul Duffin40b62572021-03-20 11:39:01 +000058func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090059 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000060 factory := apexFixtureFactory.Extend(preparers...)
61 if bp != "" {
62 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
63 }
64 result := factory.RunTest(t)
Paul Duffine05480a2021-03-08 15:07:14 +000065 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090066}
67
Paul Duffin810f33d2021-03-09 14:12:32 +000068func withFiles(files android.MockFS) android.FixturePreparer {
69 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090070}
71
Paul Duffin810f33d2021-03-09 14:12:32 +000072func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
73 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090074 for k, v := range targets {
75 config.Targets[k] = v
76 }
Paul Duffin810f33d2021-03-09 14:12:32 +000077 })
Jooyung Han344d5432019-08-23 11:17:39 +090078}
79
Jooyung Han35155c42020-02-06 17:33:20 +090080// withNativeBridgeTargets sets configuration with targets including:
81// - X86_64 (primary)
82// - X86 (secondary)
83// - Arm64 on X86_64 (native bridge)
84// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000085var withNativeBridgeEnabled = android.FixtureModifyConfig(
86 func(config android.Config) {
87 config.Targets[android.Android] = []android.Target{
88 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
89 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
90 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
91 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
92 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
93 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
94 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
95 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
96 }
97 },
98)
99
100func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
101 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
102 variables.ManifestPackageNameOverrides = specs
103 })
Jooyung Han35155c42020-02-06 17:33:20 +0900104}
105
Paul Duffin810f33d2021-03-09 14:12:32 +0000106var withBinder32bit = android.FixtureModifyProductVariables(
107 func(variables android.FixtureProductVariables) {
108 variables.Binder32bit = proptools.BoolPtr(true)
109 },
110)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900111
Paul Duffin810f33d2021-03-09 14:12:32 +0000112var withUnbundledBuild = android.FixtureModifyProductVariables(
113 func(variables android.FixtureProductVariables) {
114 variables.Unbundled_build = proptools.BoolPtr(true)
115 },
116)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900117
Paul Duffin37aad602021-03-08 09:47:16 +0000118var apexFixtureFactory = android.NewFixtureFactory(
Paul Duffin37ba3442021-03-29 00:21:08 +0100119 nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000120 // General preparers in alphabetical order as test infrastructure will enforce correct
121 // registration order.
122 android.PrepareForTestWithAndroidBuildComponents,
123 bpf.PrepareForTestWithBpf,
124 cc.PrepareForTestWithCcBuildComponents,
125 java.PrepareForTestWithJavaDefaultModules,
126 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
127 rust.PrepareForTestWithRustDefaultModules,
128 sh.PrepareForTestWithShBuildComponents,
129
130 PrepareForTestWithApexBuildComponents,
131
132 // Additional apex test specific preparers.
133 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
134 filegroup {
135 name: "myapex-file_contexts",
136 srcs: [
137 "apex/myapex-file_contexts",
138 ],
139 }
140 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000141 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000142 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000143 "a.java": nil,
144 "PrebuiltAppFoo.apk": nil,
145 "PrebuiltAppFooPriv.apk": nil,
146 "apex_manifest.json": nil,
147 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000148 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
149 "system/sepolicy/apex/myapex2-file_contexts": nil,
150 "system/sepolicy/apex/otherapex-file_contexts": nil,
151 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
152 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
153 "mylib.cpp": nil,
154 "mytest.cpp": nil,
155 "mytest1.cpp": nil,
156 "mytest2.cpp": nil,
157 "mytest3.cpp": nil,
158 "myprebuilt": nil,
159 "my_include": nil,
160 "foo/bar/MyClass.java": nil,
161 "prebuilt.jar": nil,
162 "prebuilt.so": nil,
163 "vendor/foo/devkeys/test.x509.pem": nil,
164 "vendor/foo/devkeys/test.pk8": nil,
165 "testkey.x509.pem": nil,
166 "testkey.pk8": nil,
167 "testkey.override.x509.pem": nil,
168 "testkey.override.pk8": nil,
169 "vendor/foo/devkeys/testkey.avbpubkey": nil,
170 "vendor/foo/devkeys/testkey.pem": nil,
171 "NOTICE": nil,
172 "custom_notice": nil,
173 "custom_notice_for_static_lib": nil,
174 "testkey2.avbpubkey": nil,
175 "testkey2.pem": nil,
176 "myapex-arm64.apex": nil,
177 "myapex-arm.apex": nil,
178 "myapex.apks": nil,
179 "frameworks/base/api/current.txt": nil,
180 "framework/aidl/a.aidl": nil,
181 "build/make/core/proguard.flags": nil,
182 "build/make/core/proguard_basic_keeps.flags": nil,
183 "dummy.txt": nil,
184 "baz": nil,
185 "bar/baz": nil,
186 "testdata/baz": nil,
187 "AppSet.apks": nil,
188 "foo.rs": nil,
189 "libfoo.jar": nil,
190 "libbar.jar": nil,
191 },
192 ),
193
194 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
195 variables.DeviceVndkVersion = proptools.StringPtr("current")
196 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
197 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
198 variables.Platform_sdk_codename = proptools.StringPtr("Q")
199 variables.Platform_sdk_final = proptools.BoolPtr(false)
200 variables.Platform_version_active_codenames = []string{"Q"}
201 variables.Platform_vndk_version = proptools.StringPtr("VER")
202 }),
203)
204
Paul Duffin52bfaa42021-03-23 23:40:12 +0000205var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
206 "system/sepolicy/apex/myapex-file_contexts": nil,
207})
208
Jooyung Han643adc42020-02-27 13:50:06 +0900209// ensure that 'result' equals 'expected'
210func ensureEquals(t *testing.T, result string, expected string) {
211 t.Helper()
212 if result != expected {
213 t.Errorf("%q != %q", expected, result)
214 }
215}
216
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217// ensure that 'result' contains 'expected'
218func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900219 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220 if !strings.Contains(result, expected) {
221 t.Errorf("%q is not found in %q", expected, result)
222 }
223}
224
Liz Kammer5bd365f2020-05-27 15:15:11 -0700225// ensure that 'result' contains 'expected' exactly one time
226func ensureContainsOnce(t *testing.T, result string, expected string) {
227 t.Helper()
228 count := strings.Count(result, expected)
229 if count != 1 {
230 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
231 }
232}
233
Jiyong Park25fc6a92018-11-18 18:02:45 +0900234// ensures that 'result' does not contain 'notExpected'
235func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900236 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237 if strings.Contains(result, notExpected) {
238 t.Errorf("%q is found in %q", notExpected, result)
239 }
240}
241
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700242func ensureMatches(t *testing.T, result string, expectedRex string) {
243 ok, err := regexp.MatchString(expectedRex, result)
244 if err != nil {
245 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
246 return
247 }
248 if !ok {
249 t.Errorf("%s does not match regular expession %s", result, expectedRex)
250 }
251}
252
Jiyong Park25fc6a92018-11-18 18:02:45 +0900253func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if !android.InList(expected, result) {
256 t.Errorf("%q is not found in %v", expected, result)
257 }
258}
259
260func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if android.InList(notExpected, result) {
263 t.Errorf("%q is found in %v", notExpected, result)
264 }
265}
266
Jooyung Hane1633032019-08-01 17:41:43 +0900267func ensureListEmpty(t *testing.T, result []string) {
268 t.Helper()
269 if len(result) > 0 {
270 t.Errorf("%q is expected to be empty", result)
271 }
272}
273
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000274func ensureListNotEmpty(t *testing.T, result []string) {
275 t.Helper()
276 if len(result) == 0 {
277 t.Errorf("%q is expected to be not empty", result)
278 }
279}
280
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281// Minimal test
282func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800283 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900284 apex_defaults {
285 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900286 manifest: ":myapex.manifest",
287 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900289 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900290 native_shared_libs: [
291 "mylib",
292 "libfoo.ffi",
293 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900294 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800295 multilib: {
296 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900297 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800298 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900299 },
Jiyong Park77acec62020-06-01 21:39:15 +0900300 java_libs: [
301 "myjar",
302 "myjar_dex",
303 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000304 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900305 }
306
Jiyong Park30ca9372019-02-07 16:27:23 +0900307 apex {
308 name: "myapex",
309 defaults: ["myapex-defaults"],
310 }
311
Jiyong Park25fc6a92018-11-18 18:02:45 +0900312 apex_key {
313 name: "myapex.key",
314 public_key: "testkey.avbpubkey",
315 private_key: "testkey.pem",
316 }
317
Jiyong Park809bb722019-02-13 21:33:49 +0900318 filegroup {
319 name: "myapex.manifest",
320 srcs: ["apex_manifest.json"],
321 }
322
323 filegroup {
324 name: "myapex.androidmanifest",
325 srcs: ["AndroidManifest.xml"],
326 }
327
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328 cc_library {
329 name: "mylib",
330 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900331 shared_libs: [
332 "mylib2",
333 "libbar.ffi",
334 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 system_shared_libs: [],
336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000337 // TODO: remove //apex_available:platform
338 apex_available: [
339 "//apex_available:platform",
340 "myapex",
341 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 }
343
Alex Light3d673592019-01-18 14:37:31 -0800344 cc_binary {
345 name: "foo",
346 srcs: ["mylib.cpp"],
347 compile_multilib: "both",
348 multilib: {
349 lib32: {
350 suffix: "32",
351 },
352 lib64: {
353 suffix: "64",
354 },
355 },
356 symlinks: ["foo_link_"],
357 symlink_preferred_arch: true,
358 system_shared_libs: [],
359 static_executable: true,
360 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700361 apex_available: [ "myapex", "com.android.gki.*" ],
362 }
363
Jiyong Park99644e92020-11-17 22:21:02 +0900364 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000365 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900366 srcs: ["foo.rs"],
367 rlibs: ["libfoo.rlib.rust"],
368 dylibs: ["libfoo.dylib.rust"],
369 apex_available: ["myapex"],
370 }
371
372 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000373 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900374 srcs: ["foo.rs"],
375 crate_name: "foo",
376 apex_available: ["myapex"],
377 }
378
379 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000380 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900381 srcs: ["foo.rs"],
382 crate_name: "foo",
383 apex_available: ["myapex"],
384 }
385
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900386 rust_ffi_shared {
387 name: "libfoo.ffi",
388 srcs: ["foo.rs"],
389 crate_name: "foo",
390 apex_available: ["myapex"],
391 }
392
393 rust_ffi_shared {
394 name: "libbar.ffi",
395 srcs: ["foo.rs"],
396 crate_name: "bar",
397 apex_available: ["myapex"],
398 }
399
Yifan Hongd22a84a2020-07-28 17:37:46 -0700400 apex {
401 name: "com.android.gki.fake",
402 binaries: ["foo"],
403 key: "myapex.key",
404 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000405 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800406 }
407
Paul Duffindddd5462020-04-07 15:25:44 +0100408 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900409 name: "mylib2",
410 srcs: ["mylib.cpp"],
411 system_shared_libs: [],
412 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900413 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900414 static_libs: ["libstatic"],
415 // TODO: remove //apex_available:platform
416 apex_available: [
417 "//apex_available:platform",
418 "myapex",
419 ],
420 }
421
Paul Duffindddd5462020-04-07 15:25:44 +0100422 cc_prebuilt_library_shared {
423 name: "mylib2",
424 srcs: ["prebuilt.so"],
425 // TODO: remove //apex_available:platform
426 apex_available: [
427 "//apex_available:platform",
428 "myapex",
429 ],
430 }
431
Jiyong Park9918e1a2020-03-17 19:16:40 +0900432 cc_library_static {
433 name: "libstatic",
434 srcs: ["mylib.cpp"],
435 system_shared_libs: [],
436 stl: "none",
437 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000438 // TODO: remove //apex_available:platform
439 apex_available: [
440 "//apex_available:platform",
441 "myapex",
442 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900443 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900444
445 java_library {
446 name: "myjar",
447 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900448 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900449 sdk_version: "none",
450 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900451 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900452 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000453 // TODO: remove //apex_available:platform
454 apex_available: [
455 "//apex_available:platform",
456 "myapex",
457 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900458 }
459
Jiyong Park77acec62020-06-01 21:39:15 +0900460 dex_import {
461 name: "myjar_dex",
462 jars: ["prebuilt.jar"],
463 apex_available: [
464 "//apex_available:platform",
465 "myapex",
466 ],
467 }
468
Jiyong Park7f7766d2019-07-25 22:02:35 +0900469 java_library {
470 name: "myotherjar",
471 srcs: ["foo/bar/MyClass.java"],
472 sdk_version: "none",
473 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900474 // TODO: remove //apex_available:platform
475 apex_available: [
476 "//apex_available:platform",
477 "myapex",
478 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900480
481 java_library {
482 name: "mysharedjar",
483 srcs: ["foo/bar/MyClass.java"],
484 sdk_version: "none",
485 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900486 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900487 `)
488
Paul Duffina71a67a2021-03-29 00:42:57 +0100489 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900490
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900491 // Make sure that Android.mk is created
492 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700493 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900494 var builder strings.Builder
495 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
496
497 androidMk := builder.String()
498 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
499 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
500
Jiyong Park42cca6c2019-04-01 11:15:50 +0900501 optFlags := apexRule.Args["opt_flags"]
502 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700503 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100504 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900505
Jiyong Park25fc6a92018-11-18 18:02:45 +0900506 copyCmds := apexRule.Args["copy_commands"]
507
508 // Ensure that main rule creates an output
509 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
510
511 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700512 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
513 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
514 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900515 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900516 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900517
518 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700519 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
520 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900521 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
522 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900523 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900524
525 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800526 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
527 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900528 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900529 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900530 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900531 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
532 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900533 // .. but not for java libs
534 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900535 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800536
Colin Cross7113d202019-11-20 16:39:12 -0800537 // Ensure that the platform variant ends with _shared or _common
538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
539 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
541 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
543
544 // Ensure that dynamic dependency to java libs are not included
545 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800546
547 // Ensure that all symlinks are present.
548 found_foo_link_64 := false
549 found_foo := false
550 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900551 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800552 if strings.HasSuffix(cmd, "bin/foo") {
553 found_foo = true
554 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
555 found_foo_link_64 = true
556 }
557 }
558 }
559 good := found_foo && found_foo_link_64
560 if !good {
561 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
562 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900563
Sundong Ahnabb64432019-10-22 13:58:29 +0900564 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700565 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900566 if len(noticeInputs) != 3 {
567 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900568 }
569 ensureListContains(t, noticeInputs, "NOTICE")
570 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900571 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900572
Artur Satayeva8bd1132020-04-27 18:07:06 +0100573 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100574 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100575 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
576 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
577 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100578
579 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100580 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100581 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
582 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
583 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800584}
585
Jooyung Hanf21c7972019-12-16 22:32:06 +0900586func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800587 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900588 apex_defaults {
589 name: "myapex-defaults",
590 key: "myapex.key",
591 prebuilts: ["myetc"],
592 native_shared_libs: ["mylib"],
593 java_libs: ["myjar"],
594 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900595 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800596 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000597 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900598 }
599
600 prebuilt_etc {
601 name: "myetc",
602 src: "myprebuilt",
603 }
604
605 apex {
606 name: "myapex",
607 defaults: ["myapex-defaults"],
608 }
609
610 apex_key {
611 name: "myapex.key",
612 public_key: "testkey.avbpubkey",
613 private_key: "testkey.pem",
614 }
615
616 cc_library {
617 name: "mylib",
618 system_shared_libs: [],
619 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000620 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900621 }
622
623 java_library {
624 name: "myjar",
625 srcs: ["foo/bar/MyClass.java"],
626 sdk_version: "none",
627 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000628 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900629 }
630
631 android_app {
632 name: "AppFoo",
633 srcs: ["foo/bar/MyClass.java"],
634 sdk_version: "none",
635 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000636 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900637 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900638
639 runtime_resource_overlay {
640 name: "rro",
641 theme: "blue",
642 }
643
markchien2f59ec92020-09-02 16:23:38 +0800644 bpf {
645 name: "bpf",
646 srcs: ["bpf.c", "bpf2.c"],
647 }
648
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000650 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900651 "etc/myetc",
652 "javalib/myjar.jar",
653 "lib64/mylib.so",
654 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900655 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800656 "etc/bpf/bpf.o",
657 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 })
659}
660
Jooyung Han01a3ee22019-11-02 02:52:25 +0900661func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800662 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900663 apex {
664 name: "myapex",
665 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000666 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900667 }
668
669 apex_key {
670 name: "myapex.key",
671 public_key: "testkey.avbpubkey",
672 private_key: "testkey.pem",
673 }
674 `)
675
676 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900677 args := module.Rule("apexRule").Args
678 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
679 t.Error("manifest should be apex_manifest.pb, but " + manifest)
680 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900681}
682
Alex Light5098a612018-11-29 17:12:15 -0800683func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800684 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800685 apex {
686 name: "myapex",
687 key: "myapex.key",
688 payload_type: "zip",
689 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000690 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800691 }
692
693 apex_key {
694 name: "myapex.key",
695 public_key: "testkey.avbpubkey",
696 private_key: "testkey.pem",
697 }
698
699 cc_library {
700 name: "mylib",
701 srcs: ["mylib.cpp"],
702 shared_libs: ["mylib2"],
703 system_shared_libs: [],
704 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000705 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800706 }
707
708 cc_library {
709 name: "mylib2",
710 srcs: ["mylib.cpp"],
711 system_shared_libs: [],
712 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000713 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800714 }
715 `)
716
Sundong Ahnabb64432019-10-22 13:58:29 +0900717 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800718 copyCmds := zipApexRule.Args["copy_commands"]
719
720 // Ensure that main rule creates an output
721 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
722
723 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700724 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800725
726 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700727 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800728
729 // Ensure that both direct and indirect deps are copied into apex
730 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
731 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900732}
733
734func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800735 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900736 apex {
737 name: "myapex",
738 key: "myapex.key",
739 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000740 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900741 }
742
743 apex_key {
744 name: "myapex.key",
745 public_key: "testkey.avbpubkey",
746 private_key: "testkey.pem",
747 }
748
749 cc_library {
750 name: "mylib",
751 srcs: ["mylib.cpp"],
752 shared_libs: ["mylib2", "mylib3"],
753 system_shared_libs: [],
754 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000755 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900756 }
757
758 cc_library {
759 name: "mylib2",
760 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900761 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900762 system_shared_libs: [],
763 stl: "none",
764 stubs: {
765 versions: ["1", "2", "3"],
766 },
767 }
768
769 cc_library {
770 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900771 srcs: ["mylib.cpp"],
772 shared_libs: ["mylib4"],
773 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774 stl: "none",
775 stubs: {
776 versions: ["10", "11", "12"],
777 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000778 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900779 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900780
781 cc_library {
782 name: "mylib4",
783 srcs: ["mylib.cpp"],
784 system_shared_libs: [],
785 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000786 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900787 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900788 `)
789
Sundong Ahnabb64432019-10-22 13:58:29 +0900790 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900791 copyCmds := apexRule.Args["copy_commands"]
792
793 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800794 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900795
796 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800797 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798
799 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800800 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900801
Colin Crossaede88c2020-08-11 12:17:01 -0700802 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900803
804 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900805 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900806 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900807 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900808
809 // 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 -0700810 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900811 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700812 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900813
814 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900815 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900816 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900817
818 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700819 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900820
Jooyung Hana57af4a2020-01-23 05:36:59 +0000821 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900822 "lib64/mylib.so",
823 "lib64/mylib3.so",
824 "lib64/mylib4.so",
825 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900826}
827
Colin Cross7812fd32020-09-25 12:35:10 -0700828func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
829 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800830 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700831 apex {
832 name: "myapex",
833 key: "myapex.key",
834 native_shared_libs: ["mylib", "mylib3"],
835 min_sdk_version: "29",
836 }
837
838 apex_key {
839 name: "myapex.key",
840 public_key: "testkey.avbpubkey",
841 private_key: "testkey.pem",
842 }
843
844 cc_library {
845 name: "mylib",
846 srcs: ["mylib.cpp"],
847 shared_libs: ["mylib2", "mylib3"],
848 system_shared_libs: [],
849 stl: "none",
850 apex_available: [ "myapex" ],
851 min_sdk_version: "28",
852 }
853
854 cc_library {
855 name: "mylib2",
856 srcs: ["mylib.cpp"],
857 cflags: ["-include mylib.h"],
858 system_shared_libs: [],
859 stl: "none",
860 stubs: {
861 versions: ["28", "29", "30", "current"],
862 },
863 min_sdk_version: "28",
864 }
865
866 cc_library {
867 name: "mylib3",
868 srcs: ["mylib.cpp"],
869 shared_libs: ["mylib4"],
870 system_shared_libs: [],
871 stl: "none",
872 stubs: {
873 versions: ["28", "29", "30", "current"],
874 },
875 apex_available: [ "myapex" ],
876 min_sdk_version: "28",
877 }
878
879 cc_library {
880 name: "mylib4",
881 srcs: ["mylib.cpp"],
882 system_shared_libs: [],
883 stl: "none",
884 apex_available: [ "myapex" ],
885 min_sdk_version: "28",
886 }
887 `)
888
889 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
890 copyCmds := apexRule.Args["copy_commands"]
891
892 // Ensure that direct non-stubs dep is always included
893 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
894
895 // Ensure that indirect stubs dep is not included
896 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
897
898 // Ensure that direct stubs dep is included
899 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
900
901 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
902
Jiyong Park55549df2021-02-26 23:57:23 +0900903 // Ensure that mylib is linking with the latest version of stub for mylib2
904 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700905 // ... and not linking to the non-stub (impl) variant of mylib2
906 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
907
908 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
909 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
910 // .. and not linking to the stubs variant of mylib3
911 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
912
913 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700914 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700915 ensureNotContains(t, mylib2Cflags, "-include ")
916
917 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700918 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700919
920 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
921 "lib64/mylib.so",
922 "lib64/mylib3.so",
923 "lib64/mylib4.so",
924 })
925}
926
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900927func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
928 t.Parallel()
929 // myapex (Z)
930 // mylib -----------------.
931 // |
932 // otherapex (29) |
933 // libstub's versions: 29 Z current
934 // |
935 // <platform> |
936 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800937 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900938 apex {
939 name: "myapex",
940 key: "myapex.key",
941 native_shared_libs: ["mylib"],
942 min_sdk_version: "Z", // non-final
943 }
944
945 cc_library {
946 name: "mylib",
947 srcs: ["mylib.cpp"],
948 shared_libs: ["libstub"],
949 apex_available: ["myapex"],
950 min_sdk_version: "Z",
951 }
952
953 apex_key {
954 name: "myapex.key",
955 public_key: "testkey.avbpubkey",
956 private_key: "testkey.pem",
957 }
958
959 apex {
960 name: "otherapex",
961 key: "myapex.key",
962 native_shared_libs: ["libstub"],
963 min_sdk_version: "29",
964 }
965
966 cc_library {
967 name: "libstub",
968 srcs: ["mylib.cpp"],
969 stubs: {
970 versions: ["29", "Z", "current"],
971 },
972 apex_available: ["otherapex"],
973 min_sdk_version: "29",
974 }
975
976 // platform module depending on libstub from otherapex should use the latest stub("current")
977 cc_library {
978 name: "libplatform",
979 srcs: ["mylib.cpp"],
980 shared_libs: ["libstub"],
981 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +0000982 `,
983 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
984 variables.Platform_sdk_codename = proptools.StringPtr("Z")
985 variables.Platform_sdk_final = proptools.BoolPtr(false)
986 variables.Platform_version_active_codenames = []string{"Z"}
987 }),
988 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900989
Jiyong Park55549df2021-02-26 23:57:23 +0900990 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900991 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +0900992 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900993 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +0900994 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900995
996 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
997 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
998 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
999 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1000 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1001}
1002
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001003func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001004 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001005 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001006 name: "myapex2",
1007 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001008 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001009 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001010 }
1011
1012 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001013 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001014 public_key: "testkey.avbpubkey",
1015 private_key: "testkey.pem",
1016 }
1017
1018 cc_library {
1019 name: "mylib",
1020 srcs: ["mylib.cpp"],
1021 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001022 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001023 system_shared_libs: [],
1024 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001025 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001026 }
1027
1028 cc_library {
1029 name: "libfoo",
1030 srcs: ["mylib.cpp"],
1031 shared_libs: ["libbar"],
1032 system_shared_libs: [],
1033 stl: "none",
1034 stubs: {
1035 versions: ["10", "20", "30"],
1036 },
1037 }
1038
1039 cc_library {
1040 name: "libbar",
1041 srcs: ["mylib.cpp"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 }
1045
Jiyong Park678c8812020-02-07 17:25:49 +09001046 cc_library_static {
1047 name: "libbaz",
1048 srcs: ["mylib.cpp"],
1049 system_shared_libs: [],
1050 stl: "none",
1051 apex_available: [ "myapex2" ],
1052 }
1053
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001054 `)
1055
Jiyong Park83dc74b2020-01-14 18:38:44 +09001056 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001057 copyCmds := apexRule.Args["copy_commands"]
1058
1059 // Ensure that direct non-stubs dep is always included
1060 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1061
1062 // Ensure that indirect stubs dep is not included
1063 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1064
1065 // Ensure that dependency of stubs is not included
1066 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1067
Colin Crossaede88c2020-08-11 12:17:01 -07001068 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001069
1070 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001071 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001072 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001073 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001074
Jiyong Park3ff16992019-12-27 14:11:47 +09001075 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001076
1077 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1078 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001079
Artur Satayeva8bd1132020-04-27 18:07:06 +01001080 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001081 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001082
Artur Satayeva8bd1132020-04-27 18:07:06 +01001083 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001084 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001085}
1086
Jooyung Hand3639552019-08-09 12:57:43 +09001087func TestApexWithRuntimeLibsDependency(t *testing.T) {
1088 /*
1089 myapex
1090 |
1091 v (runtime_libs)
1092 mylib ------+------> libfoo [provides stub]
1093 |
1094 `------> libbar
1095 */
Colin Cross1c460562021-02-16 17:55:47 -08001096 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001097 apex {
1098 name: "myapex",
1099 key: "myapex.key",
1100 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001101 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001102 }
1103
1104 apex_key {
1105 name: "myapex.key",
1106 public_key: "testkey.avbpubkey",
1107 private_key: "testkey.pem",
1108 }
1109
1110 cc_library {
1111 name: "mylib",
1112 srcs: ["mylib.cpp"],
1113 runtime_libs: ["libfoo", "libbar"],
1114 system_shared_libs: [],
1115 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001116 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001117 }
1118
1119 cc_library {
1120 name: "libfoo",
1121 srcs: ["mylib.cpp"],
1122 system_shared_libs: [],
1123 stl: "none",
1124 stubs: {
1125 versions: ["10", "20", "30"],
1126 },
1127 }
1128
1129 cc_library {
1130 name: "libbar",
1131 srcs: ["mylib.cpp"],
1132 system_shared_libs: [],
1133 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001134 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001135 }
1136
1137 `)
1138
Sundong Ahnabb64432019-10-22 13:58:29 +09001139 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001140 copyCmds := apexRule.Args["copy_commands"]
1141
1142 // Ensure that direct non-stubs dep is always included
1143 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1144
1145 // Ensure that indirect stubs dep is not included
1146 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1147
1148 // Ensure that runtime_libs dep in included
1149 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1150
Sundong Ahnabb64432019-10-22 13:58:29 +09001151 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001152 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1153 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001154
1155}
1156
Paul Duffina02cae32021-03-09 01:44:06 +00001157var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1158 cc.PrepareForTestWithCcBuildComponents,
1159 PrepareForTestWithApexBuildComponents,
1160 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001161 apex {
1162 name: "com.android.runtime",
1163 key: "com.android.runtime.key",
1164 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001165 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001166 }
1167
1168 apex_key {
1169 name: "com.android.runtime.key",
1170 public_key: "testkey.avbpubkey",
1171 private_key: "testkey.pem",
1172 }
Paul Duffina02cae32021-03-09 01:44:06 +00001173 `),
1174 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1175)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001176
Paul Duffina02cae32021-03-09 01:44:06 +00001177func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001178 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001179 cc_library {
1180 name: "libc",
1181 no_libcrt: true,
1182 nocrt: true,
1183 stl: "none",
1184 system_shared_libs: [],
1185 stubs: { versions: ["1"] },
1186 apex_available: ["com.android.runtime"],
1187
1188 sanitize: {
1189 hwaddress: true,
1190 }
1191 }
1192
1193 cc_prebuilt_library_shared {
1194 name: "libclang_rt.hwasan-aarch64-android",
1195 no_libcrt: true,
1196 nocrt: true,
1197 stl: "none",
1198 system_shared_libs: [],
1199 srcs: [""],
1200 stubs: { versions: ["1"] },
1201
1202 sanitize: {
1203 never: true,
1204 },
Paul Duffina02cae32021-03-09 01:44:06 +00001205 } `)
1206 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001207
1208 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1209 "lib64/bionic/libc.so",
1210 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1211 })
1212
1213 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1214
1215 installed := hwasan.Description("install libclang_rt.hwasan")
1216 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1217
1218 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1219 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1220 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1221}
1222
1223func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001224 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001225 prepareForTestOfRuntimeApexWithHwasan,
1226 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1227 variables.SanitizeDevice = []string{"hwaddress"}
1228 }),
1229 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001230 cc_library {
1231 name: "libc",
1232 no_libcrt: true,
1233 nocrt: true,
1234 stl: "none",
1235 system_shared_libs: [],
1236 stubs: { versions: ["1"] },
1237 apex_available: ["com.android.runtime"],
1238 }
1239
1240 cc_prebuilt_library_shared {
1241 name: "libclang_rt.hwasan-aarch64-android",
1242 no_libcrt: true,
1243 nocrt: true,
1244 stl: "none",
1245 system_shared_libs: [],
1246 srcs: [""],
1247 stubs: { versions: ["1"] },
1248
1249 sanitize: {
1250 never: true,
1251 },
1252 }
Paul Duffina02cae32021-03-09 01:44:06 +00001253 `)
1254 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001255
1256 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1257 "lib64/bionic/libc.so",
1258 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1259 })
1260
1261 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1262
1263 installed := hwasan.Description("install libclang_rt.hwasan")
1264 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1265
1266 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1267 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1268 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1269}
1270
Jooyung Han61b66e92020-03-21 14:21:46 +00001271func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1272 testcases := []struct {
1273 name string
1274 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001275 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001276 shouldLink string
1277 shouldNotLink []string
1278 }{
1279 {
Jiyong Park55549df2021-02-26 23:57:23 +09001280 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001281 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001282 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001283 shouldLink: "current",
1284 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001285 },
1286 {
Jiyong Park55549df2021-02-26 23:57:23 +09001287 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001288 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001289 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001290 shouldLink: "current",
1291 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001292 },
1293 }
1294 for _, tc := range testcases {
1295 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001296 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001297 apex {
1298 name: "myapex",
1299 key: "myapex.key",
1300 use_vendor: true,
1301 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001302 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001303 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001304 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001305
Jooyung Han61b66e92020-03-21 14:21:46 +00001306 apex_key {
1307 name: "myapex.key",
1308 public_key: "testkey.avbpubkey",
1309 private_key: "testkey.pem",
1310 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001311
Jooyung Han61b66e92020-03-21 14:21:46 +00001312 cc_library {
1313 name: "mylib",
1314 srcs: ["mylib.cpp"],
1315 vendor_available: true,
1316 shared_libs: ["libbar"],
1317 system_shared_libs: [],
1318 stl: "none",
1319 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001320 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001321 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001322
Jooyung Han61b66e92020-03-21 14:21:46 +00001323 cc_library {
1324 name: "libbar",
1325 srcs: ["mylib.cpp"],
1326 system_shared_libs: [],
1327 stl: "none",
1328 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001329 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001330 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001331
Jooyung Han61b66e92020-03-21 14:21:46 +00001332 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001333 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001334 symbol_file: "",
1335 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001336 `,
1337 setUseVendorAllowListForTest([]string{"myapex"}),
1338 withUnbundledBuild,
1339 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001340
Jooyung Han61b66e92020-03-21 14:21:46 +00001341 // Ensure that LLNDK dep is not included
1342 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1343 "lib64/mylib.so",
1344 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001345
Jooyung Han61b66e92020-03-21 14:21:46 +00001346 // Ensure that LLNDK dep is required
1347 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1348 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1349 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001350
Colin Crossaede88c2020-08-11 12:17:01 -07001351 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001352 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001353 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001354 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001355 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001356
Colin Crossaede88c2020-08-11 12:17:01 -07001357 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001358 ver := tc.shouldLink
1359 if tc.shouldLink == "current" {
1360 ver = strconv.Itoa(android.FutureApiLevelInt)
1361 }
1362 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001363 })
1364 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001365}
1366
Jiyong Park25fc6a92018-11-18 18:02:45 +09001367func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001368 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001369 apex {
1370 name: "myapex",
1371 key: "myapex.key",
1372 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001373 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001374 }
1375
1376 apex_key {
1377 name: "myapex.key",
1378 public_key: "testkey.avbpubkey",
1379 private_key: "testkey.pem",
1380 }
1381
1382 cc_library {
1383 name: "mylib",
1384 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001385 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001386 shared_libs: ["libdl#27"],
1387 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001388 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001389 }
1390
1391 cc_library_shared {
1392 name: "mylib_shared",
1393 srcs: ["mylib.cpp"],
1394 shared_libs: ["libdl#27"],
1395 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001396 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001397 }
1398
1399 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001400 name: "libBootstrap",
1401 srcs: ["mylib.cpp"],
1402 stl: "none",
1403 bootstrap: true,
1404 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001405 `)
1406
Sundong Ahnabb64432019-10-22 13:58:29 +09001407 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001408 copyCmds := apexRule.Args["copy_commands"]
1409
1410 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001411 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001412 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1413 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001414
1415 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001416 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001417
Colin Crossaede88c2020-08-11 12:17:01 -07001418 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1419 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1420 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001421
1422 // For dependency to libc
1423 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001424 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001425 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001426 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001427 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001428 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1429 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001430
1431 // For dependency to libm
1432 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001433 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001434 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001435 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001436 // ... and is not compiling with the stub
1437 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1438 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1439
1440 // For dependency to libdl
1441 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001442 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001444 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1445 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001446 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001447 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001448 // ... Cflags from stub is correctly exported to mylib
1449 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1450 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001451
1452 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001453 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1454 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1455 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1456 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001457}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001458
Jooyung Han749dc692020-04-15 11:03:39 +09001459func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001460 // there are three links between liba --> libz.
1461 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001462 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001463 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001464 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001465 apex {
1466 name: "myapex",
1467 key: "myapex.key",
1468 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001469 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001470 }
1471
1472 apex {
1473 name: "otherapex",
1474 key: "myapex.key",
1475 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001476 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001477 }
1478
1479 apex_key {
1480 name: "myapex.key",
1481 public_key: "testkey.avbpubkey",
1482 private_key: "testkey.pem",
1483 }
1484
1485 cc_library {
1486 name: "libx",
1487 shared_libs: ["liba"],
1488 system_shared_libs: [],
1489 stl: "none",
1490 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001491 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001492 }
1493
1494 cc_library {
1495 name: "liby",
1496 shared_libs: ["liba"],
1497 system_shared_libs: [],
1498 stl: "none",
1499 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001500 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001501 }
1502
1503 cc_library {
1504 name: "liba",
1505 shared_libs: ["libz"],
1506 system_shared_libs: [],
1507 stl: "none",
1508 apex_available: [
1509 "//apex_available:anyapex",
1510 "//apex_available:platform",
1511 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001512 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001513 }
1514
1515 cc_library {
1516 name: "libz",
1517 system_shared_libs: [],
1518 stl: "none",
1519 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001520 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001521 },
1522 }
Jooyung Han749dc692020-04-15 11:03:39 +09001523 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001524
1525 expectLink := func(from, from_variant, to, to_variant string) {
1526 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1527 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1528 }
1529 expectNoLink := func(from, from_variant, to, to_variant string) {
1530 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1531 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1532 }
1533 // platform liba is linked to non-stub version
1534 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001535 // liba in myapex is linked to current
1536 expectLink("liba", "shared_apex29", "libz", "shared_current")
1537 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001538 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001539 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001540 // liba in otherapex is linked to current
1541 expectLink("liba", "shared_apex30", "libz", "shared_current")
1542 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001543 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1544 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001545}
1546
Jooyung Hanaed150d2020-04-02 01:41:41 +09001547func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001548 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001549 apex {
1550 name: "myapex",
1551 key: "myapex.key",
1552 native_shared_libs: ["libx"],
1553 min_sdk_version: "R",
1554 }
1555
1556 apex_key {
1557 name: "myapex.key",
1558 public_key: "testkey.avbpubkey",
1559 private_key: "testkey.pem",
1560 }
1561
1562 cc_library {
1563 name: "libx",
1564 shared_libs: ["libz"],
1565 system_shared_libs: [],
1566 stl: "none",
1567 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001568 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001569 }
1570
1571 cc_library {
1572 name: "libz",
1573 system_shared_libs: [],
1574 stl: "none",
1575 stubs: {
1576 versions: ["29", "R"],
1577 },
1578 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001579 `,
1580 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1581 variables.Platform_version_active_codenames = []string{"R"}
1582 }),
1583 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001584
1585 expectLink := func(from, from_variant, to, to_variant string) {
1586 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1587 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1588 }
1589 expectNoLink := func(from, from_variant, to, to_variant string) {
1590 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1591 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1592 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001593 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1594 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001595 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1596 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001597}
1598
Jooyung Han749dc692020-04-15 11:03:39 +09001599func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001600 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001601 apex {
1602 name: "myapex",
1603 key: "myapex.key",
1604 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001605 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001606 }
1607
1608 apex_key {
1609 name: "myapex.key",
1610 public_key: "testkey.avbpubkey",
1611 private_key: "testkey.pem",
1612 }
1613
1614 cc_library {
1615 name: "libx",
1616 shared_libs: ["libz"],
1617 system_shared_libs: [],
1618 stl: "none",
1619 apex_available: [ "myapex" ],
1620 }
1621
1622 cc_library {
1623 name: "libz",
1624 system_shared_libs: [],
1625 stl: "none",
1626 stubs: {
1627 versions: ["1", "2"],
1628 },
1629 }
1630 `)
1631
1632 expectLink := func(from, from_variant, to, to_variant string) {
1633 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1634 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1635 }
1636 expectNoLink := func(from, from_variant, to, to_variant string) {
1637 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1638 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1639 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001640 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001641 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001642 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001643 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001644}
1645
1646func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001647 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001648 apex {
1649 name: "myapex",
1650 key: "myapex.key",
1651 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001652 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001653 }
1654
1655 apex_key {
1656 name: "myapex.key",
1657 public_key: "testkey.avbpubkey",
1658 private_key: "testkey.pem",
1659 }
1660
1661 cc_library {
1662 name: "libx",
1663 system_shared_libs: [],
1664 stl: "none",
1665 apex_available: [ "myapex" ],
1666 stubs: {
1667 versions: ["1", "2"],
1668 },
1669 }
1670
1671 cc_library {
1672 name: "libz",
1673 shared_libs: ["libx"],
1674 system_shared_libs: [],
1675 stl: "none",
1676 }
1677 `)
1678
1679 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001680 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001681 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1682 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1683 }
1684 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001685 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001686 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1687 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1688 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001689 expectLink("libz", "shared", "libx", "shared_current")
1690 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001691 expectNoLink("libz", "shared", "libz", "shared_1")
1692 expectNoLink("libz", "shared", "libz", "shared")
1693}
1694
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001695var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1696 func(variables android.FixtureProductVariables) {
1697 variables.SanitizeDevice = []string{"hwaddress"}
1698 },
1699)
1700
Jooyung Han75568392020-03-20 04:29:24 +09001701func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001702 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001703 apex {
1704 name: "myapex",
1705 key: "myapex.key",
1706 native_shared_libs: ["libx"],
1707 min_sdk_version: "29",
1708 }
1709
1710 apex_key {
1711 name: "myapex.key",
1712 public_key: "testkey.avbpubkey",
1713 private_key: "testkey.pem",
1714 }
1715
1716 cc_library {
1717 name: "libx",
1718 shared_libs: ["libbar"],
1719 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001720 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001721 }
1722
1723 cc_library {
1724 name: "libbar",
1725 stubs: {
1726 versions: ["29", "30"],
1727 },
1728 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001729 `,
1730 prepareForTestWithSantitizeHwaddress,
1731 )
Jooyung Han03b51852020-02-26 22:45:42 +09001732 expectLink := func(from, from_variant, to, to_variant string) {
1733 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1734 libFlags := ld.Args["libFlags"]
1735 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1736 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001737 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001738}
1739
Jooyung Han75568392020-03-20 04:29:24 +09001740func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001741 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001742 apex {
1743 name: "myapex",
1744 key: "myapex.key",
1745 native_shared_libs: ["libx"],
1746 min_sdk_version: "29",
1747 }
1748
1749 apex_key {
1750 name: "myapex.key",
1751 public_key: "testkey.avbpubkey",
1752 private_key: "testkey.pem",
1753 }
1754
1755 cc_library {
1756 name: "libx",
1757 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001758 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001759 }
Jooyung Han75568392020-03-20 04:29:24 +09001760 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001761
1762 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001763 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001764 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001765 // note that platform variant is not.
1766 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001767 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001768}
1769
Jooyung Han749dc692020-04-15 11:03:39 +09001770func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1771 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001772 apex {
1773 name: "myapex",
1774 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001775 native_shared_libs: ["mylib"],
1776 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001777 }
1778
1779 apex_key {
1780 name: "myapex.key",
1781 public_key: "testkey.avbpubkey",
1782 private_key: "testkey.pem",
1783 }
Jooyung Han749dc692020-04-15 11:03:39 +09001784
1785 cc_library {
1786 name: "mylib",
1787 srcs: ["mylib.cpp"],
1788 system_shared_libs: [],
1789 stl: "none",
1790 apex_available: [
1791 "myapex",
1792 ],
1793 min_sdk_version: "30",
1794 }
1795 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001796
1797 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1798 apex {
1799 name: "myapex",
1800 key: "myapex.key",
1801 native_shared_libs: ["libfoo.ffi"],
1802 min_sdk_version: "29",
1803 }
1804
1805 apex_key {
1806 name: "myapex.key",
1807 public_key: "testkey.avbpubkey",
1808 private_key: "testkey.pem",
1809 }
1810
1811 rust_ffi_shared {
1812 name: "libfoo.ffi",
1813 srcs: ["foo.rs"],
1814 crate_name: "foo",
1815 apex_available: [
1816 "myapex",
1817 ],
1818 min_sdk_version: "30",
1819 }
1820 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001821}
1822
1823func TestApexMinSdkVersion_Okay(t *testing.T) {
1824 testApex(t, `
1825 apex {
1826 name: "myapex",
1827 key: "myapex.key",
1828 native_shared_libs: ["libfoo"],
1829 java_libs: ["libbar"],
1830 min_sdk_version: "29",
1831 }
1832
1833 apex_key {
1834 name: "myapex.key",
1835 public_key: "testkey.avbpubkey",
1836 private_key: "testkey.pem",
1837 }
1838
1839 cc_library {
1840 name: "libfoo",
1841 srcs: ["mylib.cpp"],
1842 shared_libs: ["libfoo_dep"],
1843 apex_available: ["myapex"],
1844 min_sdk_version: "29",
1845 }
1846
1847 cc_library {
1848 name: "libfoo_dep",
1849 srcs: ["mylib.cpp"],
1850 apex_available: ["myapex"],
1851 min_sdk_version: "29",
1852 }
1853
1854 java_library {
1855 name: "libbar",
1856 sdk_version: "current",
1857 srcs: ["a.java"],
1858 static_libs: ["libbar_dep"],
1859 apex_available: ["myapex"],
1860 min_sdk_version: "29",
1861 }
1862
1863 java_library {
1864 name: "libbar_dep",
1865 sdk_version: "current",
1866 srcs: ["a.java"],
1867 apex_available: ["myapex"],
1868 min_sdk_version: "29",
1869 }
Jooyung Han03b51852020-02-26 22:45:42 +09001870 `)
1871}
1872
Artur Satayev8cf899a2020-04-15 17:29:42 +01001873func TestJavaStableSdkVersion(t *testing.T) {
1874 testCases := []struct {
1875 name string
1876 expectedError string
1877 bp string
1878 }{
1879 {
1880 name: "Non-updatable apex with non-stable dep",
1881 bp: `
1882 apex {
1883 name: "myapex",
1884 java_libs: ["myjar"],
1885 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001886 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001887 }
1888 apex_key {
1889 name: "myapex.key",
1890 public_key: "testkey.avbpubkey",
1891 private_key: "testkey.pem",
1892 }
1893 java_library {
1894 name: "myjar",
1895 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001896 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001897 apex_available: ["myapex"],
1898 }
1899 `,
1900 },
1901 {
1902 name: "Updatable apex with stable dep",
1903 bp: `
1904 apex {
1905 name: "myapex",
1906 java_libs: ["myjar"],
1907 key: "myapex.key",
1908 updatable: true,
1909 min_sdk_version: "29",
1910 }
1911 apex_key {
1912 name: "myapex.key",
1913 public_key: "testkey.avbpubkey",
1914 private_key: "testkey.pem",
1915 }
1916 java_library {
1917 name: "myjar",
1918 srcs: ["foo/bar/MyClass.java"],
1919 sdk_version: "current",
1920 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001921 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001922 }
1923 `,
1924 },
1925 {
1926 name: "Updatable apex with non-stable dep",
1927 expectedError: "cannot depend on \"myjar\"",
1928 bp: `
1929 apex {
1930 name: "myapex",
1931 java_libs: ["myjar"],
1932 key: "myapex.key",
1933 updatable: true,
1934 }
1935 apex_key {
1936 name: "myapex.key",
1937 public_key: "testkey.avbpubkey",
1938 private_key: "testkey.pem",
1939 }
1940 java_library {
1941 name: "myjar",
1942 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001943 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001944 apex_available: ["myapex"],
1945 }
1946 `,
1947 },
1948 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001949 name: "Updatable apex with non-stable transitive dep",
1950 // This is not actually detecting that the transitive dependency is unstable, rather it is
1951 // detecting that the transitive dependency is building against a wider API surface than the
1952 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001953 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001954 bp: `
1955 apex {
1956 name: "myapex",
1957 java_libs: ["myjar"],
1958 key: "myapex.key",
1959 updatable: true,
1960 }
1961 apex_key {
1962 name: "myapex.key",
1963 public_key: "testkey.avbpubkey",
1964 private_key: "testkey.pem",
1965 }
1966 java_library {
1967 name: "myjar",
1968 srcs: ["foo/bar/MyClass.java"],
1969 sdk_version: "current",
1970 apex_available: ["myapex"],
1971 static_libs: ["transitive-jar"],
1972 }
1973 java_library {
1974 name: "transitive-jar",
1975 srcs: ["foo/bar/MyClass.java"],
1976 sdk_version: "core_platform",
1977 apex_available: ["myapex"],
1978 }
1979 `,
1980 },
1981 }
1982
1983 for _, test := range testCases {
1984 t.Run(test.name, func(t *testing.T) {
1985 if test.expectedError == "" {
1986 testApex(t, test.bp)
1987 } else {
1988 testApexError(t, test.expectedError, test.bp)
1989 }
1990 })
1991 }
1992}
1993
Jooyung Han749dc692020-04-15 11:03:39 +09001994func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1995 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
1996 apex {
1997 name: "myapex",
1998 key: "myapex.key",
1999 native_shared_libs: ["mylib"],
2000 min_sdk_version: "29",
2001 }
2002
2003 apex_key {
2004 name: "myapex.key",
2005 public_key: "testkey.avbpubkey",
2006 private_key: "testkey.pem",
2007 }
2008
2009 cc_library {
2010 name: "mylib",
2011 srcs: ["mylib.cpp"],
2012 shared_libs: ["mylib2"],
2013 system_shared_libs: [],
2014 stl: "none",
2015 apex_available: [
2016 "myapex",
2017 ],
2018 min_sdk_version: "29",
2019 }
2020
2021 // indirect part of the apex
2022 cc_library {
2023 name: "mylib2",
2024 srcs: ["mylib.cpp"],
2025 system_shared_libs: [],
2026 stl: "none",
2027 apex_available: [
2028 "myapex",
2029 ],
2030 min_sdk_version: "30",
2031 }
2032 `)
2033}
2034
2035func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2036 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2037 apex {
2038 name: "myapex",
2039 key: "myapex.key",
2040 apps: ["AppFoo"],
2041 min_sdk_version: "29",
2042 }
2043
2044 apex_key {
2045 name: "myapex.key",
2046 public_key: "testkey.avbpubkey",
2047 private_key: "testkey.pem",
2048 }
2049
2050 android_app {
2051 name: "AppFoo",
2052 srcs: ["foo/bar/MyClass.java"],
2053 sdk_version: "current",
2054 min_sdk_version: "29",
2055 system_modules: "none",
2056 stl: "none",
2057 static_libs: ["bar"],
2058 apex_available: [ "myapex" ],
2059 }
2060
2061 java_library {
2062 name: "bar",
2063 sdk_version: "current",
2064 srcs: ["a.java"],
2065 apex_available: [ "myapex" ],
2066 }
2067 `)
2068}
2069
2070func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002071 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002072 apex {
2073 name: "myapex",
2074 key: "myapex.key",
2075 native_shared_libs: ["mylib"],
2076 min_sdk_version: "29",
2077 }
2078
2079 apex_key {
2080 name: "myapex.key",
2081 public_key: "testkey.avbpubkey",
2082 private_key: "testkey.pem",
2083 }
2084
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002085 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002086 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2087 cc_library {
2088 name: "mylib",
2089 srcs: ["mylib.cpp"],
2090 shared_libs: ["mylib2"],
2091 system_shared_libs: [],
2092 stl: "none",
2093 apex_available: ["myapex", "otherapex"],
2094 min_sdk_version: "29",
2095 }
2096
2097 cc_library {
2098 name: "mylib2",
2099 srcs: ["mylib.cpp"],
2100 system_shared_libs: [],
2101 stl: "none",
2102 apex_available: ["otherapex"],
2103 stubs: { versions: ["29", "30"] },
2104 min_sdk_version: "30",
2105 }
2106
2107 apex {
2108 name: "otherapex",
2109 key: "myapex.key",
2110 native_shared_libs: ["mylib", "mylib2"],
2111 min_sdk_version: "30",
2112 }
2113 `)
2114 expectLink := func(from, from_variant, to, to_variant string) {
2115 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2116 libFlags := ld.Args["libFlags"]
2117 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2118 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002119 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002120 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002121}
2122
Jooyung Haned124c32021-01-26 11:43:46 +09002123func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002124 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2125 func(variables android.FixtureProductVariables) {
2126 variables.Platform_sdk_codename = proptools.StringPtr("S")
2127 variables.Platform_version_active_codenames = []string{"S"}
2128 },
2129 )
Jooyung Haned124c32021-01-26 11:43:46 +09002130 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2131 apex {
2132 name: "myapex",
2133 key: "myapex.key",
2134 native_shared_libs: ["libfoo"],
2135 min_sdk_version: "S",
2136 }
2137 apex_key {
2138 name: "myapex.key",
2139 public_key: "testkey.avbpubkey",
2140 private_key: "testkey.pem",
2141 }
2142 cc_library {
2143 name: "libfoo",
2144 shared_libs: ["libbar"],
2145 apex_available: ["myapex"],
2146 min_sdk_version: "29",
2147 }
2148 cc_library {
2149 name: "libbar",
2150 apex_available: ["myapex"],
2151 }
2152 `, withSAsActiveCodeNames)
2153}
2154
2155func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002156 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2157 variables.Platform_sdk_codename = proptools.StringPtr("S")
2158 variables.Platform_version_active_codenames = []string{"S", "T"}
2159 })
Colin Cross1c460562021-02-16 17:55:47 -08002160 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002161 apex {
2162 name: "myapex",
2163 key: "myapex.key",
2164 native_shared_libs: ["libfoo"],
2165 min_sdk_version: "S",
2166 }
2167 apex_key {
2168 name: "myapex.key",
2169 public_key: "testkey.avbpubkey",
2170 private_key: "testkey.pem",
2171 }
2172 cc_library {
2173 name: "libfoo",
2174 shared_libs: ["libbar"],
2175 apex_available: ["myapex"],
2176 min_sdk_version: "S",
2177 }
2178 cc_library {
2179 name: "libbar",
2180 stubs: {
2181 symbol_file: "libbar.map.txt",
2182 versions: ["30", "S", "T"],
2183 },
2184 }
2185 `, withSAsActiveCodeNames)
2186
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002187 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002188 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2189 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002190 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002191}
2192
Jiyong Park7c2ee712018-12-07 00:42:25 +09002193func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002194 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002195 apex {
2196 name: "myapex",
2197 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002198 native_shared_libs: ["mylib"],
2199 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002200 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002201 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002202 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002203 }
2204
2205 apex_key {
2206 name: "myapex.key",
2207 public_key: "testkey.avbpubkey",
2208 private_key: "testkey.pem",
2209 }
2210
2211 prebuilt_etc {
2212 name: "myetc",
2213 src: "myprebuilt",
2214 sub_dir: "foo/bar",
2215 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002216
2217 cc_library {
2218 name: "mylib",
2219 srcs: ["mylib.cpp"],
2220 relative_install_path: "foo/bar",
2221 system_shared_libs: [],
2222 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002223 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002224 }
2225
2226 cc_binary {
2227 name: "mybin",
2228 srcs: ["mylib.cpp"],
2229 relative_install_path: "foo/bar",
2230 system_shared_libs: [],
2231 static_executable: true,
2232 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002233 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002234 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002235 `)
2236
Sundong Ahnabb64432019-10-22 13:58:29 +09002237 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002238 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2239
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002240 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002241 ensureListContains(t, dirs, "etc")
2242 ensureListContains(t, dirs, "etc/foo")
2243 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002244 ensureListContains(t, dirs, "lib64")
2245 ensureListContains(t, dirs, "lib64/foo")
2246 ensureListContains(t, dirs, "lib64/foo/bar")
2247 ensureListContains(t, dirs, "lib")
2248 ensureListContains(t, dirs, "lib/foo")
2249 ensureListContains(t, dirs, "lib/foo/bar")
2250
Jiyong Parkbd13e442019-03-15 18:10:35 +09002251 ensureListContains(t, dirs, "bin")
2252 ensureListContains(t, dirs, "bin/foo")
2253 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002254}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002255
Jooyung Han35155c42020-02-06 17:33:20 +09002256func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002257 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002258 apex {
2259 name: "myapex",
2260 key: "myapex.key",
2261 multilib: {
2262 both: {
2263 native_shared_libs: ["mylib"],
2264 binaries: ["mybin"],
2265 },
2266 },
2267 compile_multilib: "both",
2268 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002269 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002270 }
2271
2272 apex_key {
2273 name: "myapex.key",
2274 public_key: "testkey.avbpubkey",
2275 private_key: "testkey.pem",
2276 }
2277
2278 cc_library {
2279 name: "mylib",
2280 relative_install_path: "foo/bar",
2281 system_shared_libs: [],
2282 stl: "none",
2283 apex_available: [ "myapex" ],
2284 native_bridge_supported: true,
2285 }
2286
2287 cc_binary {
2288 name: "mybin",
2289 relative_install_path: "foo/bar",
2290 system_shared_libs: [],
2291 static_executable: true,
2292 stl: "none",
2293 apex_available: [ "myapex" ],
2294 native_bridge_supported: true,
2295 compile_multilib: "both", // default is "first" for binary
2296 multilib: {
2297 lib64: {
2298 suffix: "64",
2299 },
2300 },
2301 }
2302 `, withNativeBridgeEnabled)
2303 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2304 "bin/foo/bar/mybin",
2305 "bin/foo/bar/mybin64",
2306 "bin/arm/foo/bar/mybin",
2307 "bin/arm64/foo/bar/mybin64",
2308 "lib/foo/bar/mylib.so",
2309 "lib/arm/foo/bar/mylib.so",
2310 "lib64/foo/bar/mylib.so",
2311 "lib64/arm64/foo/bar/mylib.so",
2312 })
2313}
2314
Jiyong Parkda6eb592018-12-19 17:12:36 +09002315func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002316 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002317 apex {
2318 name: "myapex",
2319 key: "myapex.key",
2320 native_shared_libs: ["mylib"],
2321 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002322 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002323 }
2324
2325 apex_key {
2326 name: "myapex.key",
2327 public_key: "testkey.avbpubkey",
2328 private_key: "testkey.pem",
2329 }
2330
2331 cc_library {
2332 name: "mylib",
2333 srcs: ["mylib.cpp"],
2334 shared_libs: ["mylib2"],
2335 system_shared_libs: [],
2336 vendor_available: true,
2337 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002338 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002339 }
2340
2341 cc_library {
2342 name: "mylib2",
2343 srcs: ["mylib.cpp"],
2344 system_shared_libs: [],
2345 vendor_available: true,
2346 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002347 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002348 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002349 `,
2350 setUseVendorAllowListForTest([]string{"myapex"}),
2351 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002352
2353 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002354 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002355 for _, implicit := range i.Implicits {
2356 inputsList = append(inputsList, implicit.String())
2357 }
2358 }
2359 inputsString := strings.Join(inputsList, " ")
2360
2361 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002362 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2363 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002364
2365 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002366 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2367 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002368}
Jiyong Park16e91a02018-12-20 18:18:08 +09002369
Jooyung Han85d61762020-06-24 23:50:26 +09002370func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002371 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2372 apex {
2373 name: "myapex",
2374 key: "myapex.key",
2375 use_vendor: true,
2376 }
2377 apex_key {
2378 name: "myapex.key",
2379 public_key: "testkey.avbpubkey",
2380 private_key: "testkey.pem",
2381 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002382 `,
2383 setUseVendorAllowListForTest([]string{""}),
2384 )
Colin Cross440e0d02020-06-11 11:32:11 -07002385 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002386 testApex(t, `
2387 apex {
2388 name: "myapex",
2389 key: "myapex.key",
2390 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002391 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002392 }
2393 apex_key {
2394 name: "myapex.key",
2395 public_key: "testkey.avbpubkey",
2396 private_key: "testkey.pem",
2397 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002398 `,
2399 setUseVendorAllowListForTest([]string{"myapex"}),
2400 )
Jooyung Handc782442019-11-01 03:14:38 +09002401}
2402
Jooyung Han5c998b92019-06-27 11:30:33 +09002403func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2404 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2405 apex {
2406 name: "myapex",
2407 key: "myapex.key",
2408 native_shared_libs: ["mylib"],
2409 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002410 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002411 }
2412
2413 apex_key {
2414 name: "myapex.key",
2415 public_key: "testkey.avbpubkey",
2416 private_key: "testkey.pem",
2417 }
2418
2419 cc_library {
2420 name: "mylib",
2421 srcs: ["mylib.cpp"],
2422 system_shared_libs: [],
2423 stl: "none",
2424 }
2425 `)
2426}
2427
Jooyung Han85d61762020-06-24 23:50:26 +09002428func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002429 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002430 apex {
2431 name: "myapex",
2432 key: "myapex.key",
2433 binaries: ["mybin"],
2434 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002435 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002436 }
2437 apex_key {
2438 name: "myapex.key",
2439 public_key: "testkey.avbpubkey",
2440 private_key: "testkey.pem",
2441 }
2442 cc_binary {
2443 name: "mybin",
2444 vendor: true,
2445 shared_libs: ["libfoo"],
2446 }
2447 cc_library {
2448 name: "libfoo",
2449 proprietary: true,
2450 }
2451 `)
2452
2453 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2454 "bin/mybin",
2455 "lib64/libfoo.so",
2456 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2457 "lib64/libc++.so",
2458 })
2459
2460 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002461 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002462 name := apexBundle.BaseModuleName()
2463 prefix := "TARGET_"
2464 var builder strings.Builder
2465 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002466 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2467 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002468 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002469
2470 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2471 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2472 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002473}
2474
Jooyung Handf78e212020-07-22 15:54:47 +09002475func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002476 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002477 apex {
2478 name: "myapex",
2479 key: "myapex.key",
2480 binaries: ["mybin"],
2481 vendor: true,
2482 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002483 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002484 }
2485 apex_key {
2486 name: "myapex.key",
2487 public_key: "testkey.avbpubkey",
2488 private_key: "testkey.pem",
2489 }
2490 cc_binary {
2491 name: "mybin",
2492 vendor: true,
2493 shared_libs: ["libvndk", "libvendor"],
2494 }
2495 cc_library {
2496 name: "libvndk",
2497 vndk: {
2498 enabled: true,
2499 },
2500 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002501 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002502 }
2503 cc_library {
2504 name: "libvendor",
2505 vendor: true,
2506 }
2507 `)
2508
2509 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2510
Paul Duffina71a67a2021-03-29 00:42:57 +01002511 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002512 libs := names(ldRule.Args["libFlags"])
2513 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002514 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2515 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002516 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002517 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002518
2519 // VNDK libs are not included when use_vndk_as_stable: true
2520 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2521 "bin/mybin",
2522 "lib64/libvendor.so",
2523 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002524
2525 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2526 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2527 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002528}
2529
Justin Yun13decfb2021-03-08 19:25:55 +09002530func TestProductVariant(t *testing.T) {
2531 ctx := testApex(t, `
2532 apex {
2533 name: "myapex",
2534 key: "myapex.key",
2535 updatable: false,
2536 product_specific: true,
2537 binaries: ["foo"],
2538 }
2539
2540 apex_key {
2541 name: "myapex.key",
2542 public_key: "testkey.avbpubkey",
2543 private_key: "testkey.pem",
2544 }
2545
2546 cc_binary {
2547 name: "foo",
2548 product_available: true,
2549 apex_available: ["myapex"],
2550 srcs: ["foo.cpp"],
2551 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002552 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2553 variables.ProductVndkVersion = proptools.StringPtr("current")
2554 }),
2555 )
Justin Yun13decfb2021-03-08 19:25:55 +09002556
2557 cflags := strings.Fields(
2558 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2559 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2560 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2561 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2562 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2563}
2564
Jooyung Han8e5685d2020-09-21 11:02:57 +09002565func TestApex_withPrebuiltFirmware(t *testing.T) {
2566 testCases := []struct {
2567 name string
2568 additionalProp string
2569 }{
2570 {"system apex with prebuilt_firmware", ""},
2571 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2572 }
2573 for _, tc := range testCases {
2574 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002575 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002576 apex {
2577 name: "myapex",
2578 key: "myapex.key",
2579 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002580 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002581 `+tc.additionalProp+`
2582 }
2583 apex_key {
2584 name: "myapex.key",
2585 public_key: "testkey.avbpubkey",
2586 private_key: "testkey.pem",
2587 }
2588 prebuilt_firmware {
2589 name: "myfirmware",
2590 src: "myfirmware.bin",
2591 filename_from_src: true,
2592 `+tc.additionalProp+`
2593 }
2594 `)
2595 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2596 "etc/firmware/myfirmware.bin",
2597 })
2598 })
2599 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002600}
2601
Jooyung Hanefb184e2020-06-25 17:14:25 +09002602func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002603 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002604 apex {
2605 name: "myapex",
2606 key: "myapex.key",
2607 use_vendor: true,
2608 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002609 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002610 }
2611
2612 apex_key {
2613 name: "myapex.key",
2614 public_key: "testkey.avbpubkey",
2615 private_key: "testkey.pem",
2616 }
2617
2618 cc_library {
2619 name: "mylib",
2620 vendor_available: true,
2621 apex_available: ["myapex"],
2622 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002623 `,
2624 setUseVendorAllowListForTest([]string{"myapex"}),
2625 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002626
2627 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002628 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002629 name := apexBundle.BaseModuleName()
2630 prefix := "TARGET_"
2631 var builder strings.Builder
2632 data.Custom(&builder, name, prefix, "", data)
2633 androidMk := builder.String()
2634 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2635}
2636
2637func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002638 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002639 apex {
2640 name: "myapex",
2641 key: "myapex.key",
2642 vendor: true,
2643 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002644 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002645 }
2646
2647 apex_key {
2648 name: "myapex.key",
2649 public_key: "testkey.avbpubkey",
2650 private_key: "testkey.pem",
2651 }
2652
2653 cc_library {
2654 name: "mylib",
2655 vendor_available: true,
2656 }
2657 `)
2658
2659 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002660 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002661 name := apexBundle.BaseModuleName()
2662 prefix := "TARGET_"
2663 var builder strings.Builder
2664 data.Custom(&builder, name, prefix, "", data)
2665 androidMk := builder.String()
2666 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2667}
2668
Jooyung Han2ed99d02020-06-24 23:26:26 +09002669func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002670 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002671 apex {
2672 name: "myapex",
2673 key: "myapex.key",
2674 vintf_fragments: ["fragment.xml"],
2675 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002676 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002677 }
2678 apex_key {
2679 name: "myapex.key",
2680 public_key: "testkey.avbpubkey",
2681 private_key: "testkey.pem",
2682 }
2683 cc_binary {
2684 name: "mybin",
2685 }
2686 `)
2687
2688 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002689 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002690 name := apexBundle.BaseModuleName()
2691 prefix := "TARGET_"
2692 var builder strings.Builder
2693 data.Custom(&builder, name, prefix, "", data)
2694 androidMk := builder.String()
2695 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2696 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2697}
2698
Jiyong Park16e91a02018-12-20 18:18:08 +09002699func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002700 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002701 apex {
2702 name: "myapex",
2703 key: "myapex.key",
2704 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002705 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002706 }
2707
2708 apex_key {
2709 name: "myapex.key",
2710 public_key: "testkey.avbpubkey",
2711 private_key: "testkey.pem",
2712 }
2713
2714 cc_library {
2715 name: "mylib",
2716 srcs: ["mylib.cpp"],
2717 system_shared_libs: [],
2718 stl: "none",
2719 stubs: {
2720 versions: ["1", "2", "3"],
2721 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002722 apex_available: [
2723 "//apex_available:platform",
2724 "myapex",
2725 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002726 }
2727
2728 cc_binary {
2729 name: "not_in_apex",
2730 srcs: ["mylib.cpp"],
2731 static_libs: ["mylib"],
2732 static_executable: true,
2733 system_shared_libs: [],
2734 stl: "none",
2735 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002736 `)
2737
Colin Cross7113d202019-11-20 16:39:12 -08002738 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002739
2740 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002741 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002742}
Jiyong Park9335a262018-12-24 11:31:58 +09002743
2744func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002745 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002746 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002747 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002748 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002749 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002750 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002751 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002752 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002753 }
2754
2755 cc_library {
2756 name: "mylib",
2757 srcs: ["mylib.cpp"],
2758 system_shared_libs: [],
2759 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002760 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002761 }
2762
2763 apex_key {
2764 name: "myapex.key",
2765 public_key: "testkey.avbpubkey",
2766 private_key: "testkey.pem",
2767 }
2768
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002769 android_app_certificate {
2770 name: "myapex.certificate",
2771 certificate: "testkey",
2772 }
2773
2774 android_app_certificate {
2775 name: "myapex.certificate.override",
2776 certificate: "testkey.override",
2777 }
2778
Jiyong Park9335a262018-12-24 11:31:58 +09002779 `)
2780
2781 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002782 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002783
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002784 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2785 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002786 "vendor/foo/devkeys/testkey.avbpubkey")
2787 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002788 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2789 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002790 "vendor/foo/devkeys/testkey.pem")
2791 }
2792
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002793 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002794 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002795 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002796 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002797 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002798 }
2799}
Jiyong Park58e364a2019-01-19 19:24:06 +09002800
Jooyung Hanf121a652019-12-17 14:30:11 +09002801func TestCertificate(t *testing.T) {
2802 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002803 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002804 apex {
2805 name: "myapex",
2806 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002807 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002808 }
2809 apex_key {
2810 name: "myapex.key",
2811 public_key: "testkey.avbpubkey",
2812 private_key: "testkey.pem",
2813 }`)
2814 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2815 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2816 if actual := rule.Args["certificates"]; actual != expected {
2817 t.Errorf("certificates should be %q, not %q", expected, actual)
2818 }
2819 })
2820 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002821 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002822 apex {
2823 name: "myapex_keytest",
2824 key: "myapex.key",
2825 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002826 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002827 }
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833 android_app_certificate {
2834 name: "myapex.certificate.override",
2835 certificate: "testkey.override",
2836 }`)
2837 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2838 expected := "testkey.override.x509.pem testkey.override.pk8"
2839 if actual := rule.Args["certificates"]; actual != expected {
2840 t.Errorf("certificates should be %q, not %q", expected, actual)
2841 }
2842 })
2843 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002844 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002845 apex {
2846 name: "myapex",
2847 key: "myapex.key",
2848 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002849 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002850 }
2851 apex_key {
2852 name: "myapex.key",
2853 public_key: "testkey.avbpubkey",
2854 private_key: "testkey.pem",
2855 }
2856 android_app_certificate {
2857 name: "myapex.certificate",
2858 certificate: "testkey",
2859 }`)
2860 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2861 expected := "testkey.x509.pem testkey.pk8"
2862 if actual := rule.Args["certificates"]; actual != expected {
2863 t.Errorf("certificates should be %q, not %q", expected, actual)
2864 }
2865 })
2866 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002867 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002868 apex {
2869 name: "myapex_keytest",
2870 key: "myapex.key",
2871 file_contexts: ":myapex-file_contexts",
2872 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002873 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002874 }
2875 apex_key {
2876 name: "myapex.key",
2877 public_key: "testkey.avbpubkey",
2878 private_key: "testkey.pem",
2879 }
2880 android_app_certificate {
2881 name: "myapex.certificate.override",
2882 certificate: "testkey.override",
2883 }`)
2884 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2885 expected := "testkey.override.x509.pem testkey.override.pk8"
2886 if actual := rule.Args["certificates"]; actual != expected {
2887 t.Errorf("certificates should be %q, not %q", expected, actual)
2888 }
2889 })
2890 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002891 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002892 apex {
2893 name: "myapex",
2894 key: "myapex.key",
2895 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002896 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002897 }
2898 apex_key {
2899 name: "myapex.key",
2900 public_key: "testkey.avbpubkey",
2901 private_key: "testkey.pem",
2902 }`)
2903 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2904 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2905 if actual := rule.Args["certificates"]; actual != expected {
2906 t.Errorf("certificates should be %q, not %q", expected, actual)
2907 }
2908 })
2909 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002910 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002911 apex {
2912 name: "myapex_keytest",
2913 key: "myapex.key",
2914 file_contexts: ":myapex-file_contexts",
2915 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002916 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002917 }
2918 apex_key {
2919 name: "myapex.key",
2920 public_key: "testkey.avbpubkey",
2921 private_key: "testkey.pem",
2922 }
2923 android_app_certificate {
2924 name: "myapex.certificate.override",
2925 certificate: "testkey.override",
2926 }`)
2927 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2928 expected := "testkey.override.x509.pem testkey.override.pk8"
2929 if actual := rule.Args["certificates"]; actual != expected {
2930 t.Errorf("certificates should be %q, not %q", expected, actual)
2931 }
2932 })
2933}
2934
Jiyong Park58e364a2019-01-19 19:24:06 +09002935func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002936 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002937 apex {
2938 name: "myapex",
2939 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002940 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002941 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002942 }
2943
2944 apex {
2945 name: "otherapex",
2946 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002947 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002948 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002949 }
2950
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956
2957 cc_library {
2958 name: "mylib",
2959 srcs: ["mylib.cpp"],
2960 system_shared_libs: [],
2961 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002962 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002963 "myapex",
2964 "otherapex",
2965 ],
Jooyung Han24282772020-03-21 23:20:55 +09002966 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002967 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002968 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002969 cc_library {
2970 name: "mylib2",
2971 srcs: ["mylib.cpp"],
2972 system_shared_libs: [],
2973 stl: "none",
2974 apex_available: [
2975 "myapex",
2976 "otherapex",
2977 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002978 static_libs: ["mylib3"],
2979 recovery_available: true,
2980 min_sdk_version: "29",
2981 }
2982 cc_library {
2983 name: "mylib3",
2984 srcs: ["mylib.cpp"],
2985 system_shared_libs: [],
2986 stl: "none",
2987 apex_available: [
2988 "myapex",
2989 "otherapex",
2990 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002991 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002992 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002993 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002994 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002995 `)
2996
Jooyung Hanc87a0592020-03-02 17:44:33 +09002997 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002998 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002999 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003000 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003001
Jooyung Hanccce2f22020-03-07 03:45:53 +09003002 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003003 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003004 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003005 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003006 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003007
Jooyung Hanccce2f22020-03-07 03:45:53 +09003008 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003009 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003010 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003011 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003012 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003013
Colin Crossaede88c2020-08-11 12:17:01 -07003014 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3015 // each variant defines additional macros to distinguish which apex variant it is built for
3016
3017 // non-APEX variant does not have __ANDROID_APEX__ defined
3018 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3019 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3020
3021 // APEX variant has __ANDROID_APEX__ defined
3022 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3023 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3024 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3025 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3026
3027 // APEX variant has __ANDROID_APEX__ defined
3028 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3029 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3030 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3031 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3032
Dan Albertb19953d2020-11-17 15:29:36 -08003033 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003034 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3035 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003036 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003037
3038 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3039 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003040
3041 // non-APEX variant does not have __ANDROID_APEX__ defined
3042 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3043 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3044
3045 // APEX variant has __ANDROID_APEX__ defined
3046 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003047 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003048 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003049 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003050
Jooyung Hanc87a0592020-03-02 17:44:33 +09003051 // APEX variant has __ANDROID_APEX__ defined
3052 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003053 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003054 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003055 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003056
Dan Albertb19953d2020-11-17 15:29:36 -08003057 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003058 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003059 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003060 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003061}
Jiyong Park7e636d02019-01-28 16:16:54 +09003062
3063func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003064 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003065 apex {
3066 name: "myapex",
3067 key: "myapex.key",
3068 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003069 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003070 }
3071
3072 apex_key {
3073 name: "myapex.key",
3074 public_key: "testkey.avbpubkey",
3075 private_key: "testkey.pem",
3076 }
3077
3078 cc_library_headers {
3079 name: "mylib_headers",
3080 export_include_dirs: ["my_include"],
3081 system_shared_libs: [],
3082 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003083 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003084 }
3085
3086 cc_library {
3087 name: "mylib",
3088 srcs: ["mylib.cpp"],
3089 system_shared_libs: [],
3090 stl: "none",
3091 header_libs: ["mylib_headers"],
3092 export_header_lib_headers: ["mylib_headers"],
3093 stubs: {
3094 versions: ["1", "2", "3"],
3095 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003096 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003097 }
3098
3099 cc_library {
3100 name: "otherlib",
3101 srcs: ["mylib.cpp"],
3102 system_shared_libs: [],
3103 stl: "none",
3104 shared_libs: ["mylib"],
3105 }
3106 `)
3107
Colin Cross7113d202019-11-20 16:39:12 -08003108 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003109
3110 // Ensure that the include path of the header lib is exported to 'otherlib'
3111 ensureContains(t, cFlags, "-Imy_include")
3112}
Alex Light9670d332019-01-29 18:07:33 -08003113
Jiyong Park7cd10e32020-01-14 09:22:18 +09003114type fileInApex struct {
3115 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003116 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003117 isLink bool
3118}
3119
Jooyung Hana57af4a2020-01-23 05:36:59 +00003120func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003121 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003122 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003123 copyCmds := apexRule.Args["copy_commands"]
3124 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003125 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003126 for _, cmd := range strings.Split(copyCmds, "&&") {
3127 cmd = strings.TrimSpace(cmd)
3128 if cmd == "" {
3129 continue
3130 }
3131 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003132 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003133 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003134 switch terms[0] {
3135 case "mkdir":
3136 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003137 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003138 t.Fatal("copyCmds contains invalid cp command", cmd)
3139 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003140 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003141 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003142 isLink = false
3143 case "ln":
3144 if len(terms) != 3 && len(terms) != 4 {
3145 // ln LINK TARGET or ln -s LINK TARGET
3146 t.Fatal("copyCmds contains invalid ln command", cmd)
3147 }
3148 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003149 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003150 isLink = true
3151 default:
3152 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3153 }
3154 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003155 index := strings.Index(dst, imageApexDir)
3156 if index == -1 {
3157 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3158 }
3159 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003160 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003161 }
3162 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003163 return ret
3164}
3165
Jooyung Hana57af4a2020-01-23 05:36:59 +00003166func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3167 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003168 var failed bool
3169 var surplus []string
3170 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003171 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003172 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003173 for _, expected := range files {
3174 if matched, _ := path.Match(expected, file.path); matched {
3175 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003176 mactchFound = true
3177 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003178 }
3179 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003180 if !mactchFound {
3181 surplus = append(surplus, file.path)
3182 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003183 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003184
Jooyung Han31c470b2019-10-18 16:26:59 +09003185 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003186 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003187 t.Log("surplus files", surplus)
3188 failed = true
3189 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003190
3191 if len(files) > len(filesMatched) {
3192 var missing []string
3193 for _, expected := range files {
3194 if !filesMatched[expected] {
3195 missing = append(missing, expected)
3196 }
3197 }
3198 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003199 t.Log("missing files", missing)
3200 failed = true
3201 }
3202 if failed {
3203 t.Fail()
3204 }
3205}
3206
Jooyung Han344d5432019-08-23 11:17:39 +09003207func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003208 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003209 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003210 name: "com.android.vndk.current",
3211 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003212 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003213 }
3214
3215 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003216 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003217 public_key: "testkey.avbpubkey",
3218 private_key: "testkey.pem",
3219 }
3220
3221 cc_library {
3222 name: "libvndk",
3223 srcs: ["mylib.cpp"],
3224 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003225 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003226 vndk: {
3227 enabled: true,
3228 },
3229 system_shared_libs: [],
3230 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003231 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003232 }
3233
3234 cc_library {
3235 name: "libvndksp",
3236 srcs: ["mylib.cpp"],
3237 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003238 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003239 vndk: {
3240 enabled: true,
3241 support_system_process: true,
3242 },
3243 system_shared_libs: [],
3244 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003245 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003246 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003247 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003248
Colin Cross2807f002021-03-02 10:15:29 -08003249 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003250 "lib/libvndk.so",
3251 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003252 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003253 "lib64/libvndk.so",
3254 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003255 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003256 "etc/llndk.libraries.VER.txt",
3257 "etc/vndkcore.libraries.VER.txt",
3258 "etc/vndksp.libraries.VER.txt",
3259 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003260 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003261 })
Jooyung Han344d5432019-08-23 11:17:39 +09003262}
3263
3264func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003265 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003266 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003267 name: "com.android.vndk.current",
3268 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003269 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003270 }
3271
3272 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003273 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003274 public_key: "testkey.avbpubkey",
3275 private_key: "testkey.pem",
3276 }
3277
3278 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003279 name: "libvndk",
3280 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003281 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003282 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003283 vndk: {
3284 enabled: true,
3285 },
3286 system_shared_libs: [],
3287 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003288 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003289 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003290
3291 cc_prebuilt_library_shared {
3292 name: "libvndk.arm",
3293 srcs: ["libvndk.arm.so"],
3294 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003295 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003296 vndk: {
3297 enabled: true,
3298 },
3299 enabled: false,
3300 arch: {
3301 arm: {
3302 enabled: true,
3303 },
3304 },
3305 system_shared_libs: [],
3306 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003307 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003308 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003309 `+vndkLibrariesTxtFiles("current"),
3310 withFiles(map[string][]byte{
3311 "libvndk.so": nil,
3312 "libvndk.arm.so": nil,
3313 }))
Colin Cross2807f002021-03-02 10:15:29 -08003314 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003315 "lib/libvndk.so",
3316 "lib/libvndk.arm.so",
3317 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003318 "lib/libc++.so",
3319 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003320 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003321 })
Jooyung Han344d5432019-08-23 11:17:39 +09003322}
3323
Jooyung Han39edb6c2019-11-06 16:53:07 +09003324func vndkLibrariesTxtFiles(vers ...string) (result string) {
3325 for _, v := range vers {
3326 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003327 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003328 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003329 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003330 name: "` + txt + `.libraries.txt",
3331 }
3332 `
3333 }
3334 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003335 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003336 result += `
3337 prebuilt_etc {
3338 name: "` + txt + `.libraries.` + v + `.txt",
3339 src: "dummy.txt",
3340 }
3341 `
3342 }
3343 }
3344 }
3345 return
3346}
3347
Jooyung Han344d5432019-08-23 11:17:39 +09003348func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003349 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003350 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003351 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003352 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003353 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003354 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003355 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003356 }
3357
3358 apex_key {
3359 name: "myapex.key",
3360 public_key: "testkey.avbpubkey",
3361 private_key: "testkey.pem",
3362 }
3363
Jooyung Han31c470b2019-10-18 16:26:59 +09003364 vndk_prebuilt_shared {
3365 name: "libvndk27",
3366 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003367 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003368 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003369 vndk: {
3370 enabled: true,
3371 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003372 target_arch: "arm64",
3373 arch: {
3374 arm: {
3375 srcs: ["libvndk27_arm.so"],
3376 },
3377 arm64: {
3378 srcs: ["libvndk27_arm64.so"],
3379 },
3380 },
Colin Cross2807f002021-03-02 10:15:29 -08003381 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003382 }
3383
3384 vndk_prebuilt_shared {
3385 name: "libvndk27",
3386 version: "27",
3387 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003388 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003389 vndk: {
3390 enabled: true,
3391 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003392 target_arch: "x86_64",
3393 arch: {
3394 x86: {
3395 srcs: ["libvndk27_x86.so"],
3396 },
3397 x86_64: {
3398 srcs: ["libvndk27_x86_64.so"],
3399 },
3400 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003401 }
3402 `+vndkLibrariesTxtFiles("27"),
3403 withFiles(map[string][]byte{
3404 "libvndk27_arm.so": nil,
3405 "libvndk27_arm64.so": nil,
3406 "libvndk27_x86.so": nil,
3407 "libvndk27_x86_64.so": nil,
3408 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003409
Colin Cross2807f002021-03-02 10:15:29 -08003410 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003411 "lib/libvndk27_arm.so",
3412 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003413 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003414 })
Jooyung Han344d5432019-08-23 11:17:39 +09003415}
3416
Jooyung Han90eee022019-10-01 20:02:42 +09003417func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003418 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003419 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003420 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003421 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003422 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003423 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003424 }
3425 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003426 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003427 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003428 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003429 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003430 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003431 }
3432 apex_key {
3433 name: "myapex.key",
3434 public_key: "testkey.avbpubkey",
3435 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003436 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003437
3438 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003439 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003440 actual := proptools.String(bundle.properties.Apex_name)
3441 if !reflect.DeepEqual(actual, expected) {
3442 t.Errorf("Got '%v', expected '%v'", actual, expected)
3443 }
3444 }
3445
Colin Cross2807f002021-03-02 10:15:29 -08003446 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3447 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003448}
3449
Jooyung Han344d5432019-08-23 11:17:39 +09003450func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003451 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003452 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003453 name: "com.android.vndk.current",
3454 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003455 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003456 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003457 }
3458
3459 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003460 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003461 public_key: "testkey.avbpubkey",
3462 private_key: "testkey.pem",
3463 }
3464
3465 cc_library {
3466 name: "libvndk",
3467 srcs: ["mylib.cpp"],
3468 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003469 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003470 native_bridge_supported: true,
3471 host_supported: true,
3472 vndk: {
3473 enabled: true,
3474 },
3475 system_shared_libs: [],
3476 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003477 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003478 }
Colin Cross2807f002021-03-02 10:15:29 -08003479 `+vndkLibrariesTxtFiles("current"),
3480 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003481
Colin Cross2807f002021-03-02 10:15:29 -08003482 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003483 "lib/libvndk.so",
3484 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003485 "lib/libc++.so",
3486 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003487 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003488 })
Jooyung Han344d5432019-08-23 11:17:39 +09003489}
3490
3491func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003492 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003493 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003494 name: "com.android.vndk.current",
3495 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003496 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003497 native_bridge_supported: true,
3498 }
3499
3500 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003501 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003502 public_key: "testkey.avbpubkey",
3503 private_key: "testkey.pem",
3504 }
3505
3506 cc_library {
3507 name: "libvndk",
3508 srcs: ["mylib.cpp"],
3509 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003510 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003511 native_bridge_supported: true,
3512 host_supported: true,
3513 vndk: {
3514 enabled: true,
3515 },
3516 system_shared_libs: [],
3517 stl: "none",
3518 }
3519 `)
3520}
3521
Jooyung Han31c470b2019-10-18 16:26:59 +09003522func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003523 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003524 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003525 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003526 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003527 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003528 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003529 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003530 }
3531
3532 apex_key {
3533 name: "myapex.key",
3534 public_key: "testkey.avbpubkey",
3535 private_key: "testkey.pem",
3536 }
3537
3538 vndk_prebuilt_shared {
3539 name: "libvndk27",
3540 version: "27",
3541 target_arch: "arm",
3542 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003543 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003544 vndk: {
3545 enabled: true,
3546 },
3547 arch: {
3548 arm: {
3549 srcs: ["libvndk27.so"],
3550 }
3551 },
3552 }
3553
3554 vndk_prebuilt_shared {
3555 name: "libvndk27",
3556 version: "27",
3557 target_arch: "arm",
3558 binder32bit: true,
3559 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003560 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003561 vndk: {
3562 enabled: true,
3563 },
3564 arch: {
3565 arm: {
3566 srcs: ["libvndk27binder32.so"],
3567 }
3568 },
Colin Cross2807f002021-03-02 10:15:29 -08003569 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003570 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003571 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003572 withFiles(map[string][]byte{
3573 "libvndk27.so": nil,
3574 "libvndk27binder32.so": nil,
3575 }),
3576 withBinder32bit,
3577 withTargets(map[android.OsType][]android.Target{
3578 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003579 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3580 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003581 },
3582 }),
3583 )
3584
Colin Cross2807f002021-03-02 10:15:29 -08003585 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003586 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003587 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003588 })
3589}
3590
Jooyung Han45a96772020-06-15 14:59:42 +09003591func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003592 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003593 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003594 name: "com.android.vndk.current",
3595 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003596 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003597 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003598 }
3599
3600 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003601 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003602 public_key: "testkey.avbpubkey",
3603 private_key: "testkey.pem",
3604 }
3605
3606 cc_library {
3607 name: "libz",
3608 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003609 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003610 vndk: {
3611 enabled: true,
3612 },
3613 stubs: {
3614 symbol_file: "libz.map.txt",
3615 versions: ["30"],
3616 }
3617 }
3618 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3619 "libz.map.txt": nil,
3620 }))
3621
Colin Cross2807f002021-03-02 10:15:29 -08003622 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003623 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3624 ensureListEmpty(t, provideNativeLibs)
3625}
3626
Jooyung Hane1633032019-08-01 17:41:43 +09003627func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003628 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003629 apex {
3630 name: "myapex_nodep",
3631 key: "myapex.key",
3632 native_shared_libs: ["lib_nodep"],
3633 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003634 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003635 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003636 }
3637
3638 apex {
3639 name: "myapex_dep",
3640 key: "myapex.key",
3641 native_shared_libs: ["lib_dep"],
3642 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003643 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003644 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003645 }
3646
3647 apex {
3648 name: "myapex_provider",
3649 key: "myapex.key",
3650 native_shared_libs: ["libfoo"],
3651 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003652 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003653 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003654 }
3655
3656 apex {
3657 name: "myapex_selfcontained",
3658 key: "myapex.key",
3659 native_shared_libs: ["lib_dep", "libfoo"],
3660 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003661 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003662 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003663 }
3664
3665 apex_key {
3666 name: "myapex.key",
3667 public_key: "testkey.avbpubkey",
3668 private_key: "testkey.pem",
3669 }
3670
3671 cc_library {
3672 name: "lib_nodep",
3673 srcs: ["mylib.cpp"],
3674 system_shared_libs: [],
3675 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003676 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003677 }
3678
3679 cc_library {
3680 name: "lib_dep",
3681 srcs: ["mylib.cpp"],
3682 shared_libs: ["libfoo"],
3683 system_shared_libs: [],
3684 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003685 apex_available: [
3686 "myapex_dep",
3687 "myapex_provider",
3688 "myapex_selfcontained",
3689 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003690 }
3691
3692 cc_library {
3693 name: "libfoo",
3694 srcs: ["mytest.cpp"],
3695 stubs: {
3696 versions: ["1"],
3697 },
3698 system_shared_libs: [],
3699 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003700 apex_available: [
3701 "myapex_provider",
3702 "myapex_selfcontained",
3703 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003704 }
3705 `)
3706
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003707 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003708 var provideNativeLibs, requireNativeLibs []string
3709
Sundong Ahnabb64432019-10-22 13:58:29 +09003710 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003711 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3712 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003713 ensureListEmpty(t, provideNativeLibs)
3714 ensureListEmpty(t, requireNativeLibs)
3715
Sundong Ahnabb64432019-10-22 13:58:29 +09003716 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003717 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3718 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003719 ensureListEmpty(t, provideNativeLibs)
3720 ensureListContains(t, requireNativeLibs, "libfoo.so")
3721
Sundong Ahnabb64432019-10-22 13:58:29 +09003722 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003723 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3724 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003725 ensureListContains(t, provideNativeLibs, "libfoo.so")
3726 ensureListEmpty(t, requireNativeLibs)
3727
Sundong Ahnabb64432019-10-22 13:58:29 +09003728 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003729 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3730 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003731 ensureListContains(t, provideNativeLibs, "libfoo.so")
3732 ensureListEmpty(t, requireNativeLibs)
3733}
3734
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003735func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003736 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003737 apex {
3738 name: "myapex",
3739 key: "myapex.key",
3740 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003741 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003742 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003743 }
3744
3745 apex_key {
3746 name: "myapex.key",
3747 public_key: "testkey.avbpubkey",
3748 private_key: "testkey.pem",
3749 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003750
3751 cc_library {
3752 name: "mylib",
3753 srcs: ["mylib.cpp"],
3754 system_shared_libs: [],
3755 stl: "none",
3756 apex_available: [
3757 "//apex_available:platform",
3758 "myapex",
3759 ],
3760 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003761 `)
3762
Sundong Ahnabb64432019-10-22 13:58:29 +09003763 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003764 apexManifestRule := module.Rule("apexManifestRule")
3765 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3766 apexRule := module.Rule("apexRule")
3767 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003768
3769 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003770 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003771 name := apexBundle.BaseModuleName()
3772 prefix := "TARGET_"
3773 var builder strings.Builder
3774 data.Custom(&builder, name, prefix, "", data)
3775 androidMk := builder.String()
3776 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3777 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003778}
3779
Alex Light0851b882019-02-07 13:20:53 -08003780func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003781 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003782 apex {
3783 name: "myapex",
3784 key: "myapex.key",
3785 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003786 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003787 }
3788
3789 apex_key {
3790 name: "myapex.key",
3791 public_key: "testkey.avbpubkey",
3792 private_key: "testkey.pem",
3793 }
3794
3795 cc_library {
3796 name: "mylib_common",
3797 srcs: ["mylib.cpp"],
3798 system_shared_libs: [],
3799 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003800 apex_available: [
3801 "//apex_available:platform",
3802 "myapex",
3803 ],
Alex Light0851b882019-02-07 13:20:53 -08003804 }
3805 `)
3806
Sundong Ahnabb64432019-10-22 13:58:29 +09003807 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003808 apexRule := module.Rule("apexRule")
3809 copyCmds := apexRule.Args["copy_commands"]
3810
3811 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3812 t.Log("Apex was a test apex!")
3813 t.Fail()
3814 }
3815 // Ensure that main rule creates an output
3816 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3817
3818 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003819 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003820
3821 // Ensure that both direct and indirect deps are copied into apex
3822 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3823
Colin Cross7113d202019-11-20 16:39:12 -08003824 // Ensure that the platform variant ends with _shared
3825 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003826
Colin Cross56a83212020-09-15 18:30:11 -07003827 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003828 t.Log("Found mylib_common not in any apex!")
3829 t.Fail()
3830 }
3831}
3832
3833func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003834 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003835 apex_test {
3836 name: "myapex",
3837 key: "myapex.key",
3838 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003839 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003840 }
3841
3842 apex_key {
3843 name: "myapex.key",
3844 public_key: "testkey.avbpubkey",
3845 private_key: "testkey.pem",
3846 }
3847
3848 cc_library {
3849 name: "mylib_common_test",
3850 srcs: ["mylib.cpp"],
3851 system_shared_libs: [],
3852 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003853 // TODO: remove //apex_available:platform
3854 apex_available: [
3855 "//apex_available:platform",
3856 "myapex",
3857 ],
Alex Light0851b882019-02-07 13:20:53 -08003858 }
3859 `)
3860
Sundong Ahnabb64432019-10-22 13:58:29 +09003861 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003862 apexRule := module.Rule("apexRule")
3863 copyCmds := apexRule.Args["copy_commands"]
3864
3865 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3866 t.Log("Apex was not a test apex!")
3867 t.Fail()
3868 }
3869 // Ensure that main rule creates an output
3870 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3871
3872 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003873 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003874
3875 // Ensure that both direct and indirect deps are copied into apex
3876 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3877
Colin Cross7113d202019-11-20 16:39:12 -08003878 // Ensure that the platform variant ends with _shared
3879 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003880}
3881
Alex Light9670d332019-01-29 18:07:33 -08003882func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003883 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003884 apex {
3885 name: "myapex",
3886 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003887 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003888 multilib: {
3889 first: {
3890 native_shared_libs: ["mylib_common"],
3891 }
3892 },
3893 target: {
3894 android: {
3895 multilib: {
3896 first: {
3897 native_shared_libs: ["mylib"],
3898 }
3899 }
3900 },
3901 host: {
3902 multilib: {
3903 first: {
3904 native_shared_libs: ["mylib2"],
3905 }
3906 }
3907 }
3908 }
3909 }
3910
3911 apex_key {
3912 name: "myapex.key",
3913 public_key: "testkey.avbpubkey",
3914 private_key: "testkey.pem",
3915 }
3916
3917 cc_library {
3918 name: "mylib",
3919 srcs: ["mylib.cpp"],
3920 system_shared_libs: [],
3921 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003922 // TODO: remove //apex_available:platform
3923 apex_available: [
3924 "//apex_available:platform",
3925 "myapex",
3926 ],
Alex Light9670d332019-01-29 18:07:33 -08003927 }
3928
3929 cc_library {
3930 name: "mylib_common",
3931 srcs: ["mylib.cpp"],
3932 system_shared_libs: [],
3933 stl: "none",
3934 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003935 // TODO: remove //apex_available:platform
3936 apex_available: [
3937 "//apex_available:platform",
3938 "myapex",
3939 ],
Alex Light9670d332019-01-29 18:07:33 -08003940 }
3941
3942 cc_library {
3943 name: "mylib2",
3944 srcs: ["mylib.cpp"],
3945 system_shared_libs: [],
3946 stl: "none",
3947 compile_multilib: "first",
3948 }
3949 `)
3950
Sundong Ahnabb64432019-10-22 13:58:29 +09003951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003952 copyCmds := apexRule.Args["copy_commands"]
3953
3954 // Ensure that main rule creates an output
3955 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3956
3957 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003958 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3959 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3960 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003961
3962 // Ensure that both direct and indirect deps are copied into apex
3963 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3964 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3965 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3966
Colin Cross7113d202019-11-20 16:39:12 -08003967 // Ensure that the platform variant ends with _shared
3968 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3969 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3970 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003971}
Jiyong Park04480cf2019-02-06 00:16:29 +09003972
Jiyong Park59140302020-12-14 18:44:04 +09003973func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003974 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003975 apex {
3976 name: "myapex",
3977 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003978 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003979 arch: {
3980 arm64: {
3981 native_shared_libs: ["mylib.arm64"],
3982 },
3983 x86_64: {
3984 native_shared_libs: ["mylib.x64"],
3985 },
3986 }
3987 }
3988
3989 apex_key {
3990 name: "myapex.key",
3991 public_key: "testkey.avbpubkey",
3992 private_key: "testkey.pem",
3993 }
3994
3995 cc_library {
3996 name: "mylib.arm64",
3997 srcs: ["mylib.cpp"],
3998 system_shared_libs: [],
3999 stl: "none",
4000 // TODO: remove //apex_available:platform
4001 apex_available: [
4002 "//apex_available:platform",
4003 "myapex",
4004 ],
4005 }
4006
4007 cc_library {
4008 name: "mylib.x64",
4009 srcs: ["mylib.cpp"],
4010 system_shared_libs: [],
4011 stl: "none",
4012 // TODO: remove //apex_available:platform
4013 apex_available: [
4014 "//apex_available:platform",
4015 "myapex",
4016 ],
4017 }
4018 `)
4019
4020 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4021 copyCmds := apexRule.Args["copy_commands"]
4022
4023 // Ensure that apex variant is created for the direct dep
4024 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4025 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4026
4027 // Ensure that both direct and indirect deps are copied into apex
4028 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4029 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4030}
4031
Jiyong Park04480cf2019-02-06 00:16:29 +09004032func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004033 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004034 apex {
4035 name: "myapex",
4036 key: "myapex.key",
4037 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004038 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004039 }
4040
4041 apex_key {
4042 name: "myapex.key",
4043 public_key: "testkey.avbpubkey",
4044 private_key: "testkey.pem",
4045 }
4046
4047 sh_binary {
4048 name: "myscript",
4049 src: "mylib.cpp",
4050 filename: "myscript.sh",
4051 sub_dir: "script",
4052 }
4053 `)
4054
Sundong Ahnabb64432019-10-22 13:58:29 +09004055 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004056 copyCmds := apexRule.Args["copy_commands"]
4057
4058 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4059}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004060
Jooyung Han91df2082019-11-20 01:49:42 +09004061func TestApexInVariousPartition(t *testing.T) {
4062 testcases := []struct {
4063 propName, parition, flattenedPartition string
4064 }{
4065 {"", "system", "system_ext"},
4066 {"product_specific: true", "product", "product"},
4067 {"soc_specific: true", "vendor", "vendor"},
4068 {"proprietary: true", "vendor", "vendor"},
4069 {"vendor: true", "vendor", "vendor"},
4070 {"system_ext_specific: true", "system_ext", "system_ext"},
4071 }
4072 for _, tc := range testcases {
4073 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004074 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004075 apex {
4076 name: "myapex",
4077 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004078 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004079 `+tc.propName+`
4080 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004081
Jooyung Han91df2082019-11-20 01:49:42 +09004082 apex_key {
4083 name: "myapex.key",
4084 public_key: "testkey.avbpubkey",
4085 private_key: "testkey.pem",
4086 }
4087 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004088
Jooyung Han91df2082019-11-20 01:49:42 +09004089 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004090 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4091 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004092 if actual != expected {
4093 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4094 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004095
Jooyung Han91df2082019-11-20 01:49:42 +09004096 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004097 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4098 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004099 if actual != expected {
4100 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4101 }
4102 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004103 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004104}
Jiyong Park67882562019-03-21 01:11:21 +09004105
Jooyung Han580eb4f2020-06-24 19:33:06 +09004106func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004107 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004108 apex {
4109 name: "myapex",
4110 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004111 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004112 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004113
Jooyung Han580eb4f2020-06-24 19:33:06 +09004114 apex_key {
4115 name: "myapex.key",
4116 public_key: "testkey.avbpubkey",
4117 private_key: "testkey.pem",
4118 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004119 `)
4120 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004121 rule := module.Output("file_contexts")
4122 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4123}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004124
Jooyung Han580eb4f2020-06-24 19:33:06 +09004125func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004126 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004127 apex {
4128 name: "myapex",
4129 key: "myapex.key",
4130 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004131 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004132 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004133
Jooyung Han580eb4f2020-06-24 19:33:06 +09004134 apex_key {
4135 name: "myapex.key",
4136 public_key: "testkey.avbpubkey",
4137 private_key: "testkey.pem",
4138 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004139 `, withFiles(map[string][]byte{
4140 "my_own_file_contexts": nil,
4141 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004142}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004143
Jooyung Han580eb4f2020-06-24 19:33:06 +09004144func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004145 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004146 apex {
4147 name: "myapex",
4148 key: "myapex.key",
4149 product_specific: true,
4150 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004151 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004152 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004153
Jooyung Han580eb4f2020-06-24 19:33:06 +09004154 apex_key {
4155 name: "myapex.key",
4156 public_key: "testkey.avbpubkey",
4157 private_key: "testkey.pem",
4158 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004159 `)
4160
Colin Cross1c460562021-02-16 17:55:47 -08004161 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004162 apex {
4163 name: "myapex",
4164 key: "myapex.key",
4165 product_specific: true,
4166 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004167 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004168 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004169
Jooyung Han580eb4f2020-06-24 19:33:06 +09004170 apex_key {
4171 name: "myapex.key",
4172 public_key: "testkey.avbpubkey",
4173 private_key: "testkey.pem",
4174 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004175 `, withFiles(map[string][]byte{
4176 "product_specific_file_contexts": nil,
4177 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004178 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4179 rule := module.Output("file_contexts")
4180 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4181}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004182
Jooyung Han580eb4f2020-06-24 19:33:06 +09004183func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004184 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004185 apex {
4186 name: "myapex",
4187 key: "myapex.key",
4188 product_specific: true,
4189 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004190 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004191 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004192
Jooyung Han580eb4f2020-06-24 19:33:06 +09004193 apex_key {
4194 name: "myapex.key",
4195 public_key: "testkey.avbpubkey",
4196 private_key: "testkey.pem",
4197 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004198
Jooyung Han580eb4f2020-06-24 19:33:06 +09004199 filegroup {
4200 name: "my-file-contexts",
4201 srcs: ["product_specific_file_contexts"],
4202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004203 `, withFiles(map[string][]byte{
4204 "product_specific_file_contexts": nil,
4205 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004206 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4207 rule := module.Output("file_contexts")
4208 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004209}
4210
Jiyong Park67882562019-03-21 01:11:21 +09004211func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004212 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004213 apex_key {
4214 name: "myapex.key",
4215 public_key: ":my.avbpubkey",
4216 private_key: ":my.pem",
4217 product_specific: true,
4218 }
4219
4220 filegroup {
4221 name: "my.avbpubkey",
4222 srcs: ["testkey2.avbpubkey"],
4223 }
4224
4225 filegroup {
4226 name: "my.pem",
4227 srcs: ["testkey2.pem"],
4228 }
4229 `)
4230
4231 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4232 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004233 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004234 if actual_pubkey != expected_pubkey {
4235 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4236 }
4237 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004238 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004239 if actual_privkey != expected_privkey {
4240 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4241 }
4242}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004243
4244func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004245 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004246 prebuilt_apex {
4247 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004248 arch: {
4249 arm64: {
4250 src: "myapex-arm64.apex",
4251 },
4252 arm: {
4253 src: "myapex-arm.apex",
4254 },
4255 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004256 }
4257 `)
4258
4259 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4260
Jiyong Parkc95714e2019-03-29 14:23:10 +09004261 expectedInput := "myapex-arm64.apex"
4262 if prebuilt.inputApex.String() != expectedInput {
4263 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4264 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004265}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004266
4267func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004268 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004269 prebuilt_apex {
4270 name: "myapex",
4271 src: "myapex-arm.apex",
4272 filename: "notmyapex.apex",
4273 }
4274 `)
4275
4276 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4277
4278 expected := "notmyapex.apex"
4279 if p.installFilename != expected {
4280 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4281 }
4282}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004283
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004284func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004285 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004286 prebuilt_apex {
4287 name: "myapex.prebuilt",
4288 src: "myapex-arm.apex",
4289 overrides: [
4290 "myapex",
4291 ],
4292 }
4293 `)
4294
4295 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4296
4297 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004298 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004299 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004300 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004301 }
4302}
4303
Paul Duffin092153d2021-01-26 11:42:39 +00004304// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4305// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004306func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4307 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004308 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004309 }
4310
Paul Duffin89886cb2021-02-05 16:44:03 +00004311 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004312 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004313 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004314 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004315 stem := android.RemoveOptionalPrebuiltPrefix(name)
4316 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004317 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4318 }
4319 }
4320
Paul Duffin39853512021-02-26 11:09:39 +00004321 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004322 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004323 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004324 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4325 }
4326 }
4327
4328 t.Run("prebuilt only", func(t *testing.T) {
4329 bp := `
4330 prebuilt_apex {
4331 name: "myapex",
4332 arch: {
4333 arm64: {
4334 src: "myapex-arm64.apex",
4335 },
4336 arm: {
4337 src: "myapex-arm.apex",
4338 },
4339 },
Paul Duffin39853512021-02-26 11:09:39 +00004340 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004341 }
4342
4343 java_import {
4344 name: "libfoo",
4345 jars: ["libfoo.jar"],
4346 }
Paul Duffin39853512021-02-26 11:09:39 +00004347
4348 java_sdk_library_import {
4349 name: "libbar",
4350 public: {
4351 jars: ["libbar.jar"],
4352 },
4353 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004354 `
4355
4356 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4357 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4358
Paul Duffinf6932af2021-02-26 18:21:56 +00004359 // Make sure that the deapexer has the correct input APEX.
4360 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4361 rule := deapexer.Rule("deapexer")
4362 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4363 t.Errorf("expected: %q, found: %q", expected, actual)
4364 }
4365
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004366 // Make sure that the prebuilt_apex has the correct input APEX.
4367 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4368 rule = prebuiltApex.Rule("android/soong/android.Cp")
4369 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4370 t.Errorf("expected: %q, found: %q", expected, actual)
4371 }
4372
Paul Duffin89886cb2021-02-05 16:44:03 +00004373 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004374
4375 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004376 })
4377
4378 t.Run("prebuilt with source preferred", func(t *testing.T) {
4379
4380 bp := `
4381 prebuilt_apex {
4382 name: "myapex",
4383 arch: {
4384 arm64: {
4385 src: "myapex-arm64.apex",
4386 },
4387 arm: {
4388 src: "myapex-arm.apex",
4389 },
4390 },
Paul Duffin39853512021-02-26 11:09:39 +00004391 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004392 }
4393
4394 java_import {
4395 name: "libfoo",
4396 jars: ["libfoo.jar"],
4397 }
4398
4399 java_library {
4400 name: "libfoo",
4401 }
Paul Duffin39853512021-02-26 11:09:39 +00004402
4403 java_sdk_library_import {
4404 name: "libbar",
4405 public: {
4406 jars: ["libbar.jar"],
4407 },
4408 }
4409
4410 java_sdk_library {
4411 name: "libbar",
4412 srcs: ["foo/bar/MyClass.java"],
4413 unsafe_ignore_missing_latest_api: true,
4414 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004415 `
4416
4417 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4418 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4419
Paul Duffin89886cb2021-02-05 16:44:03 +00004420 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004421 ensureNoSourceVariant(t, ctx, "libfoo")
4422
4423 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4424 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004425 })
4426
4427 t.Run("prebuilt preferred with source", func(t *testing.T) {
4428 bp := `
4429 prebuilt_apex {
4430 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004431 arch: {
4432 arm64: {
4433 src: "myapex-arm64.apex",
4434 },
4435 arm: {
4436 src: "myapex-arm.apex",
4437 },
4438 },
Paul Duffin39853512021-02-26 11:09:39 +00004439 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004440 }
4441
4442 java_import {
4443 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004444 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004445 jars: ["libfoo.jar"],
4446 }
4447
4448 java_library {
4449 name: "libfoo",
4450 }
Paul Duffin39853512021-02-26 11:09:39 +00004451
4452 java_sdk_library_import {
4453 name: "libbar",
4454 prefer: true,
4455 public: {
4456 jars: ["libbar.jar"],
4457 },
4458 }
4459
4460 java_sdk_library {
4461 name: "libbar",
4462 srcs: ["foo/bar/MyClass.java"],
4463 unsafe_ignore_missing_latest_api: true,
4464 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004465 `
4466
4467 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4468 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4469
Paul Duffin89886cb2021-02-05 16:44:03 +00004470 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004471 ensureNoSourceVariant(t, ctx, "libfoo")
4472
4473 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4474 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004475 })
4476}
4477
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004478func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4479 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004480 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004481 }
4482
Paul Duffin37856732021-02-26 14:24:15 +00004483 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4484 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004485 s := ctx.SingletonForTests("dex_bootjars")
4486 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004487 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004488 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004489 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004490 foundLibfooJar = true
4491 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004492 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004493 }
4494 }
4495 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004496 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().BuildDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004497 }
4498 }
4499
Paul Duffin4fd997b2021-02-03 20:06:33 +00004500 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004501 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004502 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4503 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4504 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4505 }
4506
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004507 t.Run("prebuilt only", func(t *testing.T) {
4508 bp := `
4509 prebuilt_apex {
4510 name: "myapex",
4511 arch: {
4512 arm64: {
4513 src: "myapex-arm64.apex",
4514 },
4515 arm: {
4516 src: "myapex-arm.apex",
4517 },
4518 },
Paul Duffin37856732021-02-26 14:24:15 +00004519 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004520 }
4521
4522 java_import {
4523 name: "libfoo",
4524 jars: ["libfoo.jar"],
4525 apex_available: ["myapex"],
4526 }
Paul Duffin37856732021-02-26 14:24:15 +00004527
4528 java_sdk_library_import {
4529 name: "libbar",
4530 public: {
4531 jars: ["libbar.jar"],
4532 },
4533 apex_available: ["myapex"],
4534 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004535 `
4536
4537 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004538 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4539 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004540
Paul Duffin9d67ca62021-02-03 20:06:33 +00004541 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4542 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004543.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004544.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4545`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004546 })
4547
4548 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4549 bp := `
4550 prebuilt_apex {
4551 name: "myapex",
4552 arch: {
4553 arm64: {
4554 src: "myapex-arm64.apex",
4555 },
4556 arm: {
4557 src: "myapex-arm.apex",
4558 },
4559 },
Paul Duffin37856732021-02-26 14:24:15 +00004560 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004561 }
4562
4563 java_import {
4564 name: "libfoo",
4565 jars: ["libfoo.jar"],
4566 apex_available: ["myapex"],
4567 }
4568
4569 java_library {
4570 name: "libfoo",
4571 srcs: ["foo/bar/MyClass.java"],
4572 apex_available: ["myapex"],
4573 }
Paul Duffin37856732021-02-26 14:24:15 +00004574
4575 java_sdk_library_import {
4576 name: "libbar",
4577 public: {
4578 jars: ["libbar.jar"],
4579 },
4580 apex_available: ["myapex"],
4581 }
4582
4583 java_sdk_library {
4584 name: "libbar",
4585 srcs: ["foo/bar/MyClass.java"],
4586 unsafe_ignore_missing_latest_api: true,
4587 apex_available: ["myapex"],
4588 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004589 `
4590
4591 // In this test the source (java_library) libfoo is active since the
4592 // prebuilt (java_import) defaults to prefer:false. However the
4593 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4594 // find the dex boot jar in it. We either need to disable the source libfoo
4595 // or make the prebuilt libfoo preferred.
4596 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4597 })
4598
4599 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4600 bp := `
4601 prebuilt_apex {
4602 name: "myapex",
4603 arch: {
4604 arm64: {
4605 src: "myapex-arm64.apex",
4606 },
4607 arm: {
4608 src: "myapex-arm.apex",
4609 },
4610 },
Paul Duffin37856732021-02-26 14:24:15 +00004611 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004612 }
4613
4614 java_import {
4615 name: "libfoo",
4616 prefer: true,
4617 jars: ["libfoo.jar"],
4618 apex_available: ["myapex"],
4619 }
4620
4621 java_library {
4622 name: "libfoo",
4623 srcs: ["foo/bar/MyClass.java"],
4624 apex_available: ["myapex"],
4625 }
Paul Duffin37856732021-02-26 14:24:15 +00004626
4627 java_sdk_library_import {
4628 name: "libbar",
4629 prefer: true,
4630 public: {
4631 jars: ["libbar.jar"],
4632 },
4633 apex_available: ["myapex"],
4634 }
4635
4636 java_sdk_library {
4637 name: "libbar",
4638 srcs: ["foo/bar/MyClass.java"],
4639 unsafe_ignore_missing_latest_api: true,
4640 apex_available: ["myapex"],
4641 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004642 `
4643
4644 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004645 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4646 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004647
Paul Duffin9d67ca62021-02-03 20:06:33 +00004648 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4649 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004650.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004651.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4652`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004653 })
4654
4655 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4656 bp := `
4657 apex {
4658 name: "myapex",
4659 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004660 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004661 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004662 }
4663
4664 apex_key {
4665 name: "myapex.key",
4666 public_key: "testkey.avbpubkey",
4667 private_key: "testkey.pem",
4668 }
4669
4670 prebuilt_apex {
4671 name: "myapex",
4672 arch: {
4673 arm64: {
4674 src: "myapex-arm64.apex",
4675 },
4676 arm: {
4677 src: "myapex-arm.apex",
4678 },
4679 },
Paul Duffin37856732021-02-26 14:24:15 +00004680 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004681 }
4682
4683 java_import {
4684 name: "libfoo",
4685 jars: ["libfoo.jar"],
4686 apex_available: ["myapex"],
4687 }
4688
4689 java_library {
4690 name: "libfoo",
4691 srcs: ["foo/bar/MyClass.java"],
4692 apex_available: ["myapex"],
4693 }
Paul Duffin37856732021-02-26 14:24:15 +00004694
4695 java_sdk_library_import {
4696 name: "libbar",
4697 public: {
4698 jars: ["libbar.jar"],
4699 },
4700 apex_available: ["myapex"],
4701 }
4702
4703 java_sdk_library {
4704 name: "libbar",
4705 srcs: ["foo/bar/MyClass.java"],
4706 unsafe_ignore_missing_latest_api: true,
4707 apex_available: ["myapex"],
4708 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004709 `
4710
4711 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004712 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4713 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004714
4715 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4716 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004717.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004718.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4719`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004720 })
4721
4722 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4723 bp := `
4724 apex {
4725 name: "myapex",
4726 enabled: false,
4727 key: "myapex.key",
4728 java_libs: ["libfoo"],
4729 }
4730
4731 apex_key {
4732 name: "myapex.key",
4733 public_key: "testkey.avbpubkey",
4734 private_key: "testkey.pem",
4735 }
4736
4737 prebuilt_apex {
4738 name: "myapex",
4739 arch: {
4740 arm64: {
4741 src: "myapex-arm64.apex",
4742 },
4743 arm: {
4744 src: "myapex-arm.apex",
4745 },
4746 },
Paul Duffin37856732021-02-26 14:24:15 +00004747 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004748 }
4749
4750 java_import {
4751 name: "libfoo",
4752 prefer: true,
4753 jars: ["libfoo.jar"],
4754 apex_available: ["myapex"],
4755 }
4756
4757 java_library {
4758 name: "libfoo",
4759 srcs: ["foo/bar/MyClass.java"],
4760 apex_available: ["myapex"],
4761 }
Paul Duffin37856732021-02-26 14:24:15 +00004762
4763 java_sdk_library_import {
4764 name: "libbar",
4765 prefer: true,
4766 public: {
4767 jars: ["libbar.jar"],
4768 },
4769 apex_available: ["myapex"],
4770 }
4771
4772 java_sdk_library {
4773 name: "libbar",
4774 srcs: ["foo/bar/MyClass.java"],
4775 unsafe_ignore_missing_latest_api: true,
4776 apex_available: ["myapex"],
4777 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004778 `
4779
4780 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin55607122021-03-30 23:32:51 +01004781 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4782 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004783
Paul Duffin9d67ca62021-02-03 20:06:33 +00004784 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4785 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004786.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004787.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4788`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004789 })
4790}
4791
Roland Levillain630846d2019-06-26 12:48:34 +01004792func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004793 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004794 apex_test {
4795 name: "myapex",
4796 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004797 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004798 tests: [
4799 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004800 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004801 ],
4802 }
4803
4804 apex_key {
4805 name: "myapex.key",
4806 public_key: "testkey.avbpubkey",
4807 private_key: "testkey.pem",
4808 }
4809
Liz Kammer1c14a212020-05-12 15:26:55 -07004810 filegroup {
4811 name: "fg",
4812 srcs: [
4813 "baz",
4814 "bar/baz"
4815 ],
4816 }
4817
Roland Levillain630846d2019-06-26 12:48:34 +01004818 cc_test {
4819 name: "mytest",
4820 gtest: false,
4821 srcs: ["mytest.cpp"],
4822 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004823 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004824 system_shared_libs: [],
4825 static_executable: true,
4826 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004827 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004828 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004829
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004830 cc_library {
4831 name: "mylib",
4832 srcs: ["mylib.cpp"],
4833 system_shared_libs: [],
4834 stl: "none",
4835 }
4836
Liz Kammer5bd365f2020-05-27 15:15:11 -07004837 filegroup {
4838 name: "fg2",
4839 srcs: [
4840 "testdata/baz"
4841 ],
4842 }
4843
Roland Levillain9b5fde92019-06-28 15:41:19 +01004844 cc_test {
4845 name: "mytests",
4846 gtest: false,
4847 srcs: [
4848 "mytest1.cpp",
4849 "mytest2.cpp",
4850 "mytest3.cpp",
4851 ],
4852 test_per_src: true,
4853 relative_install_path: "test",
4854 system_shared_libs: [],
4855 static_executable: true,
4856 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004857 data: [
4858 ":fg",
4859 ":fg2",
4860 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004861 }
Roland Levillain630846d2019-06-26 12:48:34 +01004862 `)
4863
Sundong Ahnabb64432019-10-22 13:58:29 +09004864 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004865 copyCmds := apexRule.Args["copy_commands"]
4866
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004867 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004868 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004869 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004870
Liz Kammer1c14a212020-05-12 15:26:55 -07004871 //Ensure that test data are copied into apex.
4872 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4873 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4874
Roland Levillain9b5fde92019-06-28 15:41:19 +01004875 // Ensure that test deps built with `test_per_src` are copied into apex.
4876 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4877 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4878 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004879
4880 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004881 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004882 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004883 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004884 prefix := "TARGET_"
4885 var builder strings.Builder
4886 data.Custom(&builder, name, prefix, "", data)
4887 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004888 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4889 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4890 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4891 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004892 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004893 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004894 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004895
4896 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004897 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004898 data.Custom(&builder, name, prefix, "", data)
4899 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004900 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4901 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004902}
4903
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004904func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004905 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004906 apex {
4907 name: "myapex",
4908 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004909 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004910 }
4911 apex_key {
4912 name: "myapex.key",
4913 public_key: "testkey.avbpubkey",
4914 private_key: "testkey.pem",
4915 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004916 `,
4917 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4918 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4919 }),
4920 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004921 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004922 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004923 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004924 var builder strings.Builder
4925 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4926 androidMk := builder.String()
4927 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4928}
4929
Jooyung Hand48f3c32019-08-23 11:18:57 +09004930func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4931 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4932 apex {
4933 name: "myapex",
4934 key: "myapex.key",
4935 native_shared_libs: ["libfoo"],
4936 }
4937
4938 apex_key {
4939 name: "myapex.key",
4940 public_key: "testkey.avbpubkey",
4941 private_key: "testkey.pem",
4942 }
4943
4944 cc_library {
4945 name: "libfoo",
4946 stl: "none",
4947 system_shared_libs: [],
4948 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004949 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004950 }
4951 `)
4952 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4953 apex {
4954 name: "myapex",
4955 key: "myapex.key",
4956 java_libs: ["myjar"],
4957 }
4958
4959 apex_key {
4960 name: "myapex.key",
4961 public_key: "testkey.avbpubkey",
4962 private_key: "testkey.pem",
4963 }
4964
4965 java_library {
4966 name: "myjar",
4967 srcs: ["foo/bar/MyClass.java"],
4968 sdk_version: "none",
4969 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004970 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004971 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004972 }
4973 `)
4974}
4975
Bill Peckhama41a6962021-01-11 10:58:54 -08004976func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004977 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004978 apex {
4979 name: "myapex",
4980 key: "myapex.key",
4981 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004982 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004983 }
4984
4985 apex_key {
4986 name: "myapex.key",
4987 public_key: "testkey.avbpubkey",
4988 private_key: "testkey.pem",
4989 }
4990
4991 java_import {
4992 name: "myjavaimport",
4993 apex_available: ["myapex"],
4994 jars: ["my.jar"],
4995 compile_dex: true,
4996 }
4997 `)
4998
4999 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5000 apexRule := module.Rule("apexRule")
5001 copyCmds := apexRule.Args["copy_commands"]
5002 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5003}
5004
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005005func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005006 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005007 apex {
5008 name: "myapex",
5009 key: "myapex.key",
5010 apps: [
5011 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005012 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005013 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005014 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005015 }
5016
5017 apex_key {
5018 name: "myapex.key",
5019 public_key: "testkey.avbpubkey",
5020 private_key: "testkey.pem",
5021 }
5022
5023 android_app {
5024 name: "AppFoo",
5025 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005026 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005027 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005028 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005029 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005030 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005031 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005032
5033 android_app {
5034 name: "AppFooPriv",
5035 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005036 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005037 system_modules: "none",
5038 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005039 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005040 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005041 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005042
5043 cc_library_shared {
5044 name: "libjni",
5045 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005046 shared_libs: ["libfoo"],
5047 stl: "none",
5048 system_shared_libs: [],
5049 apex_available: [ "myapex" ],
5050 sdk_version: "current",
5051 }
5052
5053 cc_library_shared {
5054 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005055 stl: "none",
5056 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005057 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005058 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005059 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005060 `)
5061
Sundong Ahnabb64432019-10-22 13:58:29 +09005062 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005063 apexRule := module.Rule("apexRule")
5064 copyCmds := apexRule.Args["copy_commands"]
5065
5066 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005067 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005068
Colin Crossaede88c2020-08-11 12:17:01 -07005069 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005070 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005071 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005072 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005073 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005074 // JNI libraries including transitive deps are
5075 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005076 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005077 // ... embedded inside APK (jnilibs.zip)
5078 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5079 // ... and not directly inside the APEX
5080 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5081 }
Dario Frenicde2a032019-10-27 00:29:22 +01005082}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005083
Dario Frenicde2a032019-10-27 00:29:22 +01005084func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005085 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005086 apex {
5087 name: "myapex",
5088 key: "myapex.key",
5089 apps: [
5090 "AppFooPrebuilt",
5091 "AppFooPrivPrebuilt",
5092 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005093 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005094 }
5095
5096 apex_key {
5097 name: "myapex.key",
5098 public_key: "testkey.avbpubkey",
5099 private_key: "testkey.pem",
5100 }
5101
5102 android_app_import {
5103 name: "AppFooPrebuilt",
5104 apk: "PrebuiltAppFoo.apk",
5105 presigned: true,
5106 dex_preopt: {
5107 enabled: false,
5108 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005109 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005110 }
5111
5112 android_app_import {
5113 name: "AppFooPrivPrebuilt",
5114 apk: "PrebuiltAppFooPriv.apk",
5115 privileged: true,
5116 presigned: true,
5117 dex_preopt: {
5118 enabled: false,
5119 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005120 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005121 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005122 }
5123 `)
5124
Sundong Ahnabb64432019-10-22 13:58:29 +09005125 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005126 apexRule := module.Rule("apexRule")
5127 copyCmds := apexRule.Args["copy_commands"]
5128
5129 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005130 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5131}
5132
5133func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005134 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005135 apex {
5136 name: "myapex",
5137 key: "myapex.key",
5138 apps: [
5139 "AppFoo",
5140 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005141 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005142 }
5143
5144 apex_key {
5145 name: "myapex.key",
5146 public_key: "testkey.avbpubkey",
5147 private_key: "testkey.pem",
5148 }
5149
5150 android_app {
5151 name: "AppFoo",
5152 srcs: ["foo/bar/MyClass.java"],
5153 sdk_version: "none",
5154 system_modules: "none",
5155 apex_available: [ "myapex" ],
5156 }
5157
5158 android_app_import {
5159 name: "AppFoo",
5160 apk: "AppFooPrebuilt.apk",
5161 filename: "AppFooPrebuilt.apk",
5162 presigned: true,
5163 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005164 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005165 }
5166 `, withFiles(map[string][]byte{
5167 "AppFooPrebuilt.apk": nil,
5168 }))
5169
5170 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005171 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005172 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005173}
5174
Dario Freni6f3937c2019-12-20 22:58:03 +00005175func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005176 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005177 apex {
5178 name: "myapex",
5179 key: "myapex.key",
5180 apps: [
5181 "TesterHelpAppFoo",
5182 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005183 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005184 }
5185
5186 apex_key {
5187 name: "myapex.key",
5188 public_key: "testkey.avbpubkey",
5189 private_key: "testkey.pem",
5190 }
5191
5192 android_test_helper_app {
5193 name: "TesterHelpAppFoo",
5194 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005195 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005196 }
5197
5198 `)
5199
5200 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5201 apexRule := module.Rule("apexRule")
5202 copyCmds := apexRule.Args["copy_commands"]
5203
5204 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5205}
5206
Jooyung Han18020ea2019-11-13 10:50:48 +09005207func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5208 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005209 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005210 apex {
5211 name: "myapex",
5212 key: "myapex.key",
5213 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005214 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005215 }
5216
5217 apex_key {
5218 name: "myapex.key",
5219 public_key: "testkey.avbpubkey",
5220 private_key: "testkey.pem",
5221 }
5222
5223 apex {
5224 name: "otherapex",
5225 key: "myapex.key",
5226 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005227 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005228 }
5229
5230 cc_defaults {
5231 name: "libfoo-defaults",
5232 apex_available: ["otherapex"],
5233 }
5234
5235 cc_library {
5236 name: "libfoo",
5237 defaults: ["libfoo-defaults"],
5238 stl: "none",
5239 system_shared_libs: [],
5240 }`)
5241}
5242
Paul Duffine52e66f2020-03-30 17:54:29 +01005243func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005244 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005245 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005246 apex {
5247 name: "myapex",
5248 key: "myapex.key",
5249 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005250 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005251 }
5252
5253 apex_key {
5254 name: "myapex.key",
5255 public_key: "testkey.avbpubkey",
5256 private_key: "testkey.pem",
5257 }
5258
5259 apex {
5260 name: "otherapex",
5261 key: "otherapex.key",
5262 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005263 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005264 }
5265
5266 apex_key {
5267 name: "otherapex.key",
5268 public_key: "testkey.avbpubkey",
5269 private_key: "testkey.pem",
5270 }
5271
5272 cc_library {
5273 name: "libfoo",
5274 stl: "none",
5275 system_shared_libs: [],
5276 apex_available: ["otherapex"],
5277 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005278}
Jiyong Park127b40b2019-09-30 16:04:35 +09005279
Paul Duffine52e66f2020-03-30 17:54:29 +01005280func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005281 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005282 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005283.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005284.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005285.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005286.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005287.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005288.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005289 apex {
5290 name: "myapex",
5291 key: "myapex.key",
5292 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005293 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005294 }
5295
5296 apex_key {
5297 name: "myapex.key",
5298 public_key: "testkey.avbpubkey",
5299 private_key: "testkey.pem",
5300 }
5301
Jiyong Park127b40b2019-09-30 16:04:35 +09005302 cc_library {
5303 name: "libfoo",
5304 stl: "none",
5305 shared_libs: ["libbar"],
5306 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005307 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005308 }
5309
5310 cc_library {
5311 name: "libbar",
5312 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005313 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005314 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005315 apex_available: ["myapex"],
5316 }
5317
5318 cc_library {
5319 name: "libbaz",
5320 stl: "none",
5321 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005322 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005323}
Jiyong Park127b40b2019-09-30 16:04:35 +09005324
Paul Duffine52e66f2020-03-30 17:54:29 +01005325func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005326 testApexError(t, "\"otherapex\" is not a valid module name", `
5327 apex {
5328 name: "myapex",
5329 key: "myapex.key",
5330 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005331 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005332 }
5333
5334 apex_key {
5335 name: "myapex.key",
5336 public_key: "testkey.avbpubkey",
5337 private_key: "testkey.pem",
5338 }
5339
5340 cc_library {
5341 name: "libfoo",
5342 stl: "none",
5343 system_shared_libs: [],
5344 apex_available: ["otherapex"],
5345 }`)
5346
Paul Duffine52e66f2020-03-30 17:54:29 +01005347 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005348 apex {
5349 name: "myapex",
5350 key: "myapex.key",
5351 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005352 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005353 }
5354
5355 apex_key {
5356 name: "myapex.key",
5357 public_key: "testkey.avbpubkey",
5358 private_key: "testkey.pem",
5359 }
5360
5361 cc_library {
5362 name: "libfoo",
5363 stl: "none",
5364 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005365 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005366 apex_available: ["myapex"],
5367 }
5368
5369 cc_library {
5370 name: "libbar",
5371 stl: "none",
5372 system_shared_libs: [],
5373 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005374 }
5375
5376 cc_library {
5377 name: "libbaz",
5378 stl: "none",
5379 system_shared_libs: [],
5380 stubs: {
5381 versions: ["10", "20", "30"],
5382 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005383 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005384}
Jiyong Park127b40b2019-09-30 16:04:35 +09005385
Jiyong Park89e850a2020-04-07 16:37:39 +09005386func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005387 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005388 apex {
5389 name: "myapex",
5390 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005391 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005392 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005393 }
5394
5395 apex_key {
5396 name: "myapex.key",
5397 public_key: "testkey.avbpubkey",
5398 private_key: "testkey.pem",
5399 }
5400
5401 cc_library {
5402 name: "libfoo",
5403 stl: "none",
5404 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005405 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005406 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005407 }
5408
5409 cc_library {
5410 name: "libfoo2",
5411 stl: "none",
5412 system_shared_libs: [],
5413 shared_libs: ["libbaz"],
5414 apex_available: ["//apex_available:platform"],
5415 }
5416
5417 cc_library {
5418 name: "libbar",
5419 stl: "none",
5420 system_shared_libs: [],
5421 apex_available: ["myapex"],
5422 }
5423
5424 cc_library {
5425 name: "libbaz",
5426 stl: "none",
5427 system_shared_libs: [],
5428 apex_available: ["myapex"],
5429 stubs: {
5430 versions: ["1"],
5431 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005432 }`)
5433
Jiyong Park89e850a2020-04-07 16:37:39 +09005434 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5435 // because it depends on libbar which isn't available to platform
5436 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5437 if libfoo.NotAvailableForPlatform() != true {
5438 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5439 }
5440
5441 // libfoo2 however can be available to platform because it depends on libbaz which provides
5442 // stubs
5443 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5444 if libfoo2.NotAvailableForPlatform() == true {
5445 t.Errorf("%q should be available to platform", libfoo2.String())
5446 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005447}
Jiyong Parka90ca002019-10-07 15:47:24 +09005448
Paul Duffine52e66f2020-03-30 17:54:29 +01005449func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005450 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005451 apex {
5452 name: "myapex",
5453 key: "myapex.key",
5454 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005455 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005456 }
5457
5458 apex_key {
5459 name: "myapex.key",
5460 public_key: "testkey.avbpubkey",
5461 private_key: "testkey.pem",
5462 }
5463
5464 cc_library {
5465 name: "libfoo",
5466 stl: "none",
5467 system_shared_libs: [],
5468 apex_available: ["myapex"],
5469 static: {
5470 apex_available: ["//apex_available:platform"],
5471 },
5472 }`)
5473
Jiyong Park89e850a2020-04-07 16:37:39 +09005474 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5475 if libfooShared.NotAvailableForPlatform() != true {
5476 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5477 }
5478 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5479 if libfooStatic.NotAvailableForPlatform() != false {
5480 t.Errorf("%q should be available to platform", libfooStatic.String())
5481 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005482}
5483
Jiyong Park5d790c32019-11-15 18:40:32 +09005484func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005485 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005486 apex {
5487 name: "myapex",
5488 key: "myapex.key",
5489 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005490 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005491 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005492 }
5493
5494 override_apex {
5495 name: "override_myapex",
5496 base: "myapex",
5497 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005498 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005499 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005500 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005501 }
5502
5503 apex_key {
5504 name: "myapex.key",
5505 public_key: "testkey.avbpubkey",
5506 private_key: "testkey.pem",
5507 }
5508
5509 android_app {
5510 name: "app",
5511 srcs: ["foo/bar/MyClass.java"],
5512 package_name: "foo",
5513 sdk_version: "none",
5514 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005515 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005516 }
5517
5518 override_android_app {
5519 name: "override_app",
5520 base: "app",
5521 package_name: "bar",
5522 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005523 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005524
Jiyong Park317645e2019-12-05 13:20:58 +09005525 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5526 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5527 if originalVariant.GetOverriddenBy() != "" {
5528 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5529 }
5530 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5531 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5532 }
5533
Jiyong Park5d790c32019-11-15 18:40:32 +09005534 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5535 apexRule := module.Rule("apexRule")
5536 copyCmds := apexRule.Args["copy_commands"]
5537
5538 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005539 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005540
5541 apexBundle := module.Module().(*apexBundle)
5542 name := apexBundle.Name()
5543 if name != "override_myapex" {
5544 t.Errorf("name should be \"override_myapex\", but was %q", name)
5545 }
5546
Baligh Uddin004d7172020-02-19 21:29:28 -08005547 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5548 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5549 }
5550
Jiyong Park20bacab2020-03-03 11:45:41 +09005551 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005552 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005553
Colin Crossaa255532020-07-03 13:18:24 -07005554 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005555 var builder strings.Builder
5556 data.Custom(&builder, name, "TARGET_", "", data)
5557 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005558 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005559 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5560 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005561 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005562 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005563 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005564 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5565 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005566}
5567
Jooyung Han214bf372019-11-12 13:03:50 +09005568func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005569 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005570 apex {
5571 name: "myapex",
5572 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005573 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005574 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005575 }
5576
5577 apex_key {
5578 name: "myapex.key",
5579 public_key: "testkey.avbpubkey",
5580 private_key: "testkey.pem",
5581 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005582
5583 cc_library {
5584 name: "mylib",
5585 srcs: ["mylib.cpp"],
5586 stl: "libc++",
5587 system_shared_libs: [],
5588 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005589 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005590 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005591 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005592
5593 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5594 args := module.Rule("apexRule").Args
5595 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005596 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005597
5598 // The copies of the libraries in the apex should have one more dependency than
5599 // the ones outside the apex, namely the unwinder. Ideally we should check
5600 // the dependency names directly here but for some reason the names are blank in
5601 // this test.
5602 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005603 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005604 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5605 if len(apexImplicits) != len(nonApexImplicits)+1 {
5606 t.Errorf("%q missing unwinder dep", lib)
5607 }
5608 }
Jooyung Han214bf372019-11-12 13:03:50 +09005609}
5610
Paul Duffine05480a2021-03-08 15:07:14 +00005611var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005612 "api/current.txt": nil,
5613 "api/removed.txt": nil,
5614 "api/system-current.txt": nil,
5615 "api/system-removed.txt": nil,
5616 "api/test-current.txt": nil,
5617 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005618
Anton Hanssondff2c782020-12-21 17:10:01 +00005619 "100/public/api/foo.txt": nil,
5620 "100/public/api/foo-removed.txt": nil,
5621 "100/system/api/foo.txt": nil,
5622 "100/system/api/foo-removed.txt": nil,
5623
Paul Duffineedc5d52020-06-12 17:46:39 +01005624 // For java_sdk_library_import
5625 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005626}
5627
Jooyung Han58f26ab2019-12-18 15:34:32 +09005628func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005629 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005630 apex {
5631 name: "myapex",
5632 key: "myapex.key",
5633 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005634 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005635 }
5636
5637 apex_key {
5638 name: "myapex.key",
5639 public_key: "testkey.avbpubkey",
5640 private_key: "testkey.pem",
5641 }
5642
5643 java_sdk_library {
5644 name: "foo",
5645 srcs: ["a.java"],
5646 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005647 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005648 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005649
5650 prebuilt_apis {
5651 name: "sdk",
5652 api_dirs: ["100"],
5653 }
Paul Duffin9b879592020-05-26 13:21:35 +01005654 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005655
5656 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005657 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005658 "javalib/foo.jar",
5659 "etc/permissions/foo.xml",
5660 })
5661 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005662 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5663 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005664}
5665
Paul Duffin9b879592020-05-26 13:21:35 +01005666func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005667 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005668 apex {
5669 name: "myapex",
5670 key: "myapex.key",
5671 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005672 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005673 }
5674
5675 apex_key {
5676 name: "myapex.key",
5677 public_key: "testkey.avbpubkey",
5678 private_key: "testkey.pem",
5679 }
5680
5681 java_sdk_library {
5682 name: "foo",
5683 srcs: ["a.java"],
5684 api_packages: ["foo"],
5685 apex_available: ["myapex"],
5686 sdk_version: "none",
5687 system_modules: "none",
5688 }
5689
5690 java_library {
5691 name: "bar",
5692 srcs: ["a.java"],
5693 libs: ["foo"],
5694 apex_available: ["myapex"],
5695 sdk_version: "none",
5696 system_modules: "none",
5697 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005698
5699 prebuilt_apis {
5700 name: "sdk",
5701 api_dirs: ["100"],
5702 }
Paul Duffin9b879592020-05-26 13:21:35 +01005703 `, withFiles(filesForSdkLibrary))
5704
5705 // java_sdk_library installs both impl jar and permission XML
5706 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5707 "javalib/bar.jar",
5708 "javalib/foo.jar",
5709 "etc/permissions/foo.xml",
5710 })
5711
5712 // The bar library should depend on the implementation jar.
5713 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005714 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005715 t.Errorf("expected %q, found %#q", expected, actual)
5716 }
5717}
5718
5719func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005720 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005721 apex {
5722 name: "myapex",
5723 key: "myapex.key",
5724 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005725 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005726 }
5727
5728 apex_key {
5729 name: "myapex.key",
5730 public_key: "testkey.avbpubkey",
5731 private_key: "testkey.pem",
5732 }
5733
5734 java_sdk_library {
5735 name: "foo",
5736 srcs: ["a.java"],
5737 api_packages: ["foo"],
5738 apex_available: ["myapex"],
5739 sdk_version: "none",
5740 system_modules: "none",
5741 }
5742
5743 java_library {
5744 name: "bar",
5745 srcs: ["a.java"],
5746 libs: ["foo"],
5747 sdk_version: "none",
5748 system_modules: "none",
5749 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005750
5751 prebuilt_apis {
5752 name: "sdk",
5753 api_dirs: ["100"],
5754 }
Paul Duffin9b879592020-05-26 13:21:35 +01005755 `, withFiles(filesForSdkLibrary))
5756
5757 // java_sdk_library installs both impl jar and permission XML
5758 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5759 "javalib/foo.jar",
5760 "etc/permissions/foo.xml",
5761 })
5762
5763 // The bar library should depend on the stubs jar.
5764 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005765 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005766 t.Errorf("expected %q, found %#q", expected, actual)
5767 }
5768}
5769
Paul Duffineedc5d52020-06-12 17:46:39 +01005770func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005771 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005772 prebuilt_apis {
5773 name: "sdk",
5774 api_dirs: ["100"],
5775 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005776 withFiles(map[string][]byte{
5777 "apex/a.java": nil,
5778 "apex/apex_manifest.json": nil,
5779 "apex/Android.bp": []byte(`
5780 package {
5781 default_visibility: ["//visibility:private"],
5782 }
5783
5784 apex {
5785 name: "myapex",
5786 key: "myapex.key",
5787 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005788 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005789 }
5790
5791 apex_key {
5792 name: "myapex.key",
5793 public_key: "testkey.avbpubkey",
5794 private_key: "testkey.pem",
5795 }
5796
5797 java_library {
5798 name: "bar",
5799 srcs: ["a.java"],
5800 libs: ["foo"],
5801 apex_available: ["myapex"],
5802 sdk_version: "none",
5803 system_modules: "none",
5804 }
5805`),
5806 "source/a.java": nil,
5807 "source/api/current.txt": nil,
5808 "source/api/removed.txt": nil,
5809 "source/Android.bp": []byte(`
5810 package {
5811 default_visibility: ["//visibility:private"],
5812 }
5813
5814 java_sdk_library {
5815 name: "foo",
5816 visibility: ["//apex"],
5817 srcs: ["a.java"],
5818 api_packages: ["foo"],
5819 apex_available: ["myapex"],
5820 sdk_version: "none",
5821 system_modules: "none",
5822 public: {
5823 enabled: true,
5824 },
5825 }
5826`),
5827 "prebuilt/a.jar": nil,
5828 "prebuilt/Android.bp": []byte(`
5829 package {
5830 default_visibility: ["//visibility:private"],
5831 }
5832
5833 java_sdk_library_import {
5834 name: "foo",
5835 visibility: ["//apex", "//source"],
5836 apex_available: ["myapex"],
5837 prefer: true,
5838 public: {
5839 jars: ["a.jar"],
5840 },
5841 }
5842`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005843 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005844 )
5845
5846 // java_sdk_library installs both impl jar and permission XML
5847 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5848 "javalib/bar.jar",
5849 "javalib/foo.jar",
5850 "etc/permissions/foo.xml",
5851 })
5852
5853 // The bar library should depend on the implementation jar.
5854 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005855 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005856 t.Errorf("expected %q, found %#q", expected, actual)
5857 }
5858}
5859
5860func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5861 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5862 apex {
5863 name: "myapex",
5864 key: "myapex.key",
5865 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005866 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005867 }
5868
5869 apex_key {
5870 name: "myapex.key",
5871 public_key: "testkey.avbpubkey",
5872 private_key: "testkey.pem",
5873 }
5874
5875 java_sdk_library_import {
5876 name: "foo",
5877 apex_available: ["myapex"],
5878 prefer: true,
5879 public: {
5880 jars: ["a.jar"],
5881 },
5882 }
5883
5884 `, withFiles(filesForSdkLibrary))
5885}
5886
atrost6e126252020-01-27 17:01:16 +00005887func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005888 result := apexFixtureFactory.
5889 Extend(java.PrepareForTestWithPlatformCompatConfig).
5890 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005891 apex {
5892 name: "myapex",
5893 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005894 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005895 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005896 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005897 }
5898
5899 apex_key {
5900 name: "myapex.key",
5901 public_key: "testkey.avbpubkey",
5902 private_key: "testkey.pem",
5903 }
5904
5905 platform_compat_config {
5906 name: "myjar-platform-compat-config",
5907 src: ":myjar",
5908 }
5909
5910 java_library {
5911 name: "myjar",
5912 srcs: ["foo/bar/MyClass.java"],
5913 sdk_version: "none",
5914 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005915 apex_available: [ "myapex" ],
5916 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005917
5918 // Make sure that a preferred prebuilt does not affect the apex contents.
5919 prebuilt_platform_compat_config {
5920 name: "myjar-platform-compat-config",
5921 metadata: "compat-config/metadata.xml",
5922 prefer: true,
5923 }
atrost6e126252020-01-27 17:01:16 +00005924 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005925 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005926 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5927 "etc/compatconfig/myjar-platform-compat-config.xml",
5928 "javalib/myjar.jar",
5929 })
5930}
5931
Jiyong Park479321d2019-12-16 11:47:12 +09005932func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5933 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5934 apex {
5935 name: "myapex",
5936 key: "myapex.key",
5937 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005938 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005939 }
5940
5941 apex_key {
5942 name: "myapex.key",
5943 public_key: "testkey.avbpubkey",
5944 private_key: "testkey.pem",
5945 }
5946
5947 java_library {
5948 name: "myjar",
5949 srcs: ["foo/bar/MyClass.java"],
5950 sdk_version: "none",
5951 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005952 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005953 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005954 }
5955 `)
5956}
5957
Jiyong Park7afd1072019-12-30 16:56:33 +09005958func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005959 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005960 apex {
5961 name: "myapex",
5962 key: "myapex.key",
5963 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005964 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005965 }
5966
5967 apex_key {
5968 name: "myapex.key",
5969 public_key: "testkey.avbpubkey",
5970 private_key: "testkey.pem",
5971 }
5972
5973 cc_library {
5974 name: "mylib",
5975 srcs: ["mylib.cpp"],
5976 system_shared_libs: [],
5977 stl: "none",
5978 required: ["a", "b"],
5979 host_required: ["c", "d"],
5980 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005981 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005982 }
5983 `)
5984
5985 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005986 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005987 name := apexBundle.BaseModuleName()
5988 prefix := "TARGET_"
5989 var builder strings.Builder
5990 data.Custom(&builder, name, prefix, "", data)
5991 androidMk := builder.String()
5992 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5993 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5994 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5995}
5996
Jiyong Park7cd10e32020-01-14 09:22:18 +09005997func TestSymlinksFromApexToSystem(t *testing.T) {
5998 bp := `
5999 apex {
6000 name: "myapex",
6001 key: "myapex.key",
6002 native_shared_libs: ["mylib"],
6003 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006004 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006005 }
6006
Jiyong Park9d677202020-02-19 16:29:35 +09006007 apex {
6008 name: "myapex.updatable",
6009 key: "myapex.key",
6010 native_shared_libs: ["mylib"],
6011 java_libs: ["myjar"],
6012 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006013 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006014 }
6015
Jiyong Park7cd10e32020-01-14 09:22:18 +09006016 apex_key {
6017 name: "myapex.key",
6018 public_key: "testkey.avbpubkey",
6019 private_key: "testkey.pem",
6020 }
6021
6022 cc_library {
6023 name: "mylib",
6024 srcs: ["mylib.cpp"],
6025 shared_libs: ["myotherlib"],
6026 system_shared_libs: [],
6027 stl: "none",
6028 apex_available: [
6029 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006030 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006031 "//apex_available:platform",
6032 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006033 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006034 }
6035
6036 cc_library {
6037 name: "myotherlib",
6038 srcs: ["mylib.cpp"],
6039 system_shared_libs: [],
6040 stl: "none",
6041 apex_available: [
6042 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006043 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006044 "//apex_available:platform",
6045 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006046 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006047 }
6048
6049 java_library {
6050 name: "myjar",
6051 srcs: ["foo/bar/MyClass.java"],
6052 sdk_version: "none",
6053 system_modules: "none",
6054 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006055 apex_available: [
6056 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006057 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006058 "//apex_available:platform",
6059 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006060 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006061 }
6062
6063 java_library {
6064 name: "myotherjar",
6065 srcs: ["foo/bar/MyClass.java"],
6066 sdk_version: "none",
6067 system_modules: "none",
6068 apex_available: [
6069 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006070 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006071 "//apex_available:platform",
6072 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006073 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006074 }
6075 `
6076
6077 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6078 for _, f := range files {
6079 if f.path == file {
6080 if f.isLink {
6081 t.Errorf("%q is not a real file", file)
6082 }
6083 return
6084 }
6085 }
6086 t.Errorf("%q is not found", file)
6087 }
6088
6089 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6090 for _, f := range files {
6091 if f.path == file {
6092 if !f.isLink {
6093 t.Errorf("%q is not a symlink", file)
6094 }
6095 return
6096 }
6097 }
6098 t.Errorf("%q is not found", file)
6099 }
6100
Jiyong Park9d677202020-02-19 16:29:35 +09006101 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6102 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006103 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006104 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006105 ensureRealfileExists(t, files, "javalib/myjar.jar")
6106 ensureRealfileExists(t, files, "lib64/mylib.so")
6107 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6108
Jiyong Park9d677202020-02-19 16:29:35 +09006109 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6110 ensureRealfileExists(t, files, "javalib/myjar.jar")
6111 ensureRealfileExists(t, files, "lib64/mylib.so")
6112 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6113
6114 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006115 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006116 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006117 ensureRealfileExists(t, files, "javalib/myjar.jar")
6118 ensureRealfileExists(t, files, "lib64/mylib.so")
6119 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006120
6121 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6122 ensureRealfileExists(t, files, "javalib/myjar.jar")
6123 ensureRealfileExists(t, files, "lib64/mylib.so")
6124 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006125}
6126
Yo Chiange8128052020-07-23 20:09:18 +08006127func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006128 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006129 apex {
6130 name: "myapex",
6131 key: "myapex.key",
6132 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006133 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006134 }
6135
6136 apex_key {
6137 name: "myapex.key",
6138 public_key: "testkey.avbpubkey",
6139 private_key: "testkey.pem",
6140 }
6141
6142 cc_library_shared {
6143 name: "mylib",
6144 srcs: ["mylib.cpp"],
6145 shared_libs: ["myotherlib"],
6146 system_shared_libs: [],
6147 stl: "none",
6148 apex_available: [
6149 "myapex",
6150 "//apex_available:platform",
6151 ],
6152 }
6153
6154 cc_prebuilt_library_shared {
6155 name: "myotherlib",
6156 srcs: ["prebuilt.so"],
6157 system_shared_libs: [],
6158 stl: "none",
6159 apex_available: [
6160 "myapex",
6161 "//apex_available:platform",
6162 ],
6163 }
6164 `)
6165
6166 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006167 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006168 var builder strings.Builder
6169 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6170 androidMk := builder.String()
6171 // `myotherlib` is added to `myapex` as symlink
6172 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6173 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6174 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6175 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006176 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 +08006177}
6178
Jooyung Han643adc42020-02-27 13:50:06 +09006179func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006180 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006181 apex {
6182 name: "myapex",
6183 key: "myapex.key",
6184 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006185 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006186 }
6187
6188 apex_key {
6189 name: "myapex.key",
6190 public_key: "testkey.avbpubkey",
6191 private_key: "testkey.pem",
6192 }
6193
6194 cc_library {
6195 name: "mylib",
6196 srcs: ["mylib.cpp"],
6197 shared_libs: ["mylib2"],
6198 system_shared_libs: [],
6199 stl: "none",
6200 apex_available: [ "myapex" ],
6201 }
6202
6203 cc_library {
6204 name: "mylib2",
6205 srcs: ["mylib.cpp"],
6206 system_shared_libs: [],
6207 stl: "none",
6208 apex_available: [ "myapex" ],
6209 }
6210 `)
6211
6212 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6213 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6214 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6215 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6216 "lib64/mylib.so",
6217 "lib64/mylib2.so",
6218 })
6219}
6220
Jooyung Han49f67012020-04-17 13:43:10 +09006221func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006222 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006223 apex {
6224 name: "myapex",
6225 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006226 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006227 }
6228 apex_key {
6229 name: "myapex.key",
6230 public_key: "testkey.avbpubkey",
6231 private_key: "testkey.pem",
6232 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006233 `,
6234 android.FixtureModifyConfig(func(config android.Config) {
6235 delete(config.Targets, android.Android)
6236 config.AndroidCommonTarget = android.Target{}
6237 }),
6238 )
Jooyung Han49f67012020-04-17 13:43:10 +09006239
6240 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6241 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6242 }
6243}
6244
Jiyong Parkbd159612020-02-28 15:22:21 +09006245func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006246 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006247 apex {
6248 name: "myapex",
6249 key: "myapex.key",
6250 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006251 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006252 }
6253
6254 apex_key {
6255 name: "myapex.key",
6256 public_key: "testkey.avbpubkey",
6257 private_key: "testkey.pem",
6258 }
6259
6260 android_app {
6261 name: "AppFoo",
6262 srcs: ["foo/bar/MyClass.java"],
6263 sdk_version: "none",
6264 system_modules: "none",
6265 apex_available: [ "myapex" ],
6266 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006267 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006268
Colin Crosscf371cc2020-11-13 11:48:42 -08006269 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006270 content := bundleConfigRule.Args["content"]
6271
6272 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006273 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 +09006274}
6275
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006276func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006277 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006278 apex {
6279 name: "myapex",
6280 key: "myapex.key",
6281 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006282 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006283 }
6284
6285 apex_key {
6286 name: "myapex.key",
6287 public_key: "testkey.avbpubkey",
6288 private_key: "testkey.pem",
6289 }
6290
6291 android_app_set {
6292 name: "AppSet",
6293 set: "AppSet.apks",
6294 }`)
6295 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006296 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006297 content := bundleConfigRule.Args["content"]
6298 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6299 s := mod.Rule("apexRule").Args["copy_commands"]
6300 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6301 if len(copyCmds) != 3 {
6302 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6303 }
6304 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6305 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6306 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6307}
6308
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006309func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006310 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006311 bp := `
6312 apex_set {
6313 name: "myapex",
6314 filename: "foo_v2.apex",
6315 sanitized: {
6316 none: { set: "myapex.apks", },
6317 hwaddress: { set: "myapex.hwasan.apks", },
6318 },
6319 }`
6320 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006321 }),
6322 prepareForTestWithSantitizeHwaddress,
6323 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006324
6325 m := ctx.ModuleForTests("myapex", "android_common")
Paul Duffin37ba3442021-03-29 00:21:08 +01006326 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006327
6328 actual := extractedApex.Inputs
6329 if len(actual) != 1 {
6330 t.Errorf("expected a single input")
6331 }
6332
6333 expected := "myapex.hwasan.apks"
6334 if actual[0].String() != expected {
6335 t.Errorf("expected %s, got %s", expected, actual[0].String())
6336 }
6337}
6338
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006339func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006340 t.Helper()
6341
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006342 bp := `
6343 java_library {
6344 name: "some-updatable-apex-lib",
6345 srcs: ["a.java"],
6346 sdk_version: "current",
6347 apex_available: [
6348 "some-updatable-apex",
6349 ],
6350 }
6351
6352 java_library {
6353 name: "some-non-updatable-apex-lib",
6354 srcs: ["a.java"],
6355 apex_available: [
6356 "some-non-updatable-apex",
6357 ],
6358 }
6359
6360 java_library {
6361 name: "some-platform-lib",
6362 srcs: ["a.java"],
6363 sdk_version: "current",
6364 installable: true,
6365 }
6366
6367 java_library {
6368 name: "some-art-lib",
6369 srcs: ["a.java"],
6370 sdk_version: "current",
6371 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006372 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006373 ],
6374 hostdex: true,
6375 }
6376
6377 apex {
6378 name: "some-updatable-apex",
6379 key: "some-updatable-apex.key",
6380 java_libs: ["some-updatable-apex-lib"],
6381 updatable: true,
6382 min_sdk_version: "current",
6383 }
6384
6385 apex {
6386 name: "some-non-updatable-apex",
6387 key: "some-non-updatable-apex.key",
6388 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006389 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006390 }
6391
6392 apex_key {
6393 name: "some-updatable-apex.key",
6394 }
6395
6396 apex_key {
6397 name: "some-non-updatable-apex.key",
6398 }
6399
6400 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006401 name: "com.android.art.debug",
6402 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006403 java_libs: ["some-art-lib"],
6404 updatable: true,
6405 min_sdk_version: "current",
6406 }
6407
6408 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006409 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006410 }
6411
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006412 filegroup {
6413 name: "some-updatable-apex-file_contexts",
6414 srcs: [
6415 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6416 ],
6417 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006418
6419 filegroup {
6420 name: "some-non-updatable-apex-file_contexts",
6421 srcs: [
6422 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6423 ],
6424 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006425 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006426
6427 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6428}
6429
Paul Duffin064b70c2020-11-02 17:32:38 +00006430func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006431 t.Helper()
6432
Paul Duffin55607122021-03-30 23:32:51 +01006433 fs := android.MockFS{
6434 "a.java": nil,
6435 "a.jar": nil,
6436 "apex_manifest.json": nil,
6437 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006438 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006439 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6440 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6441 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006442 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006443 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006444
Paul Duffin55607122021-03-30 23:32:51 +01006445 errorHandler := android.FixtureExpectsNoErrors
6446 if errmsg != "" {
6447 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006448 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006449
Paul Duffin55607122021-03-30 23:32:51 +01006450 result := android.GroupFixturePreparers(
6451 cc.PrepareForTestWithCcDefaultModules,
6452 java.PrepareForTestWithHiddenApiBuildComponents,
6453 java.PrepareForTestWithJavaDefaultModules,
6454 java.PrepareForTestWithJavaSdkLibraryFiles,
6455 PrepareForTestWithApexBuildComponents,
6456 android.FixtureModifyConfig(func(config android.Config) {
6457 pathCtx := android.PathContextForTesting(config)
6458 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6459 transformDexpreoptConfig(dexpreoptConfig)
6460 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6461
6462 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
6463 // product variables.
6464 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6465 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6466 }),
6467 fs.AddToFixture(),
6468 ).
6469 ExtendWithErrorHandler(errorHandler).
6470 RunTestWithBp(t, bp)
6471
6472 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006473}
6474
Jooyung Han548640b2020-04-27 12:10:30 +09006475func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6476 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6477 apex {
6478 name: "myapex",
6479 key: "myapex.key",
6480 updatable: true,
6481 }
6482
6483 apex_key {
6484 name: "myapex.key",
6485 public_key: "testkey.avbpubkey",
6486 private_key: "testkey.pem",
6487 }
6488 `)
6489}
6490
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006491func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6492 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6493 apex {
6494 name: "myapex",
6495 key: "myapex.key",
6496 }
6497
6498 apex_key {
6499 name: "myapex.key",
6500 public_key: "testkey.avbpubkey",
6501 private_key: "testkey.pem",
6502 }
6503 `)
6504}
6505
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006506func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006507 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006508 var transform func(*dexpreopt.GlobalConfig)
6509
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006510 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6511 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006512 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006513 }
6514 testNoUpdatableJarsInBootImage(t, "", transform)
6515 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006516
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006517 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006518 err = `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006519 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006520 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006521 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006522 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006523 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006524
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006525 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006526 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006527 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006528 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006529 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006530 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006531 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006532
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006533 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006534 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006535 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006536 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006537 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006538 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006539 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006540
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006541 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006542 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006543 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006544 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006545 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006546 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006547 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006548
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006549 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6550 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006551 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006552 }
6553 testNoUpdatableJarsInBootImage(t, "", transform)
6554 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006555
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006556 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006557 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006558 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006559 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006560 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006561 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006562 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006563
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006564 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006565 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006566 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006567 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006568 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006569 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006570 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006571
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006572 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006573 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006574 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006575 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006576 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006577 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006578 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006579
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006580 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6581 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006582 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006583 }
6584 testNoUpdatableJarsInBootImage(t, "", transform)
6585 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006586
6587}
6588
6589func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6590 transform := func(config *dexpreopt.GlobalConfig) {
6591 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6592 }
6593 t.Run("prebuilt no source", func(t *testing.T) {
6594 testDexpreoptWithApexes(t, `
6595 prebuilt_apex {
6596 name: "myapex" ,
6597 arch: {
6598 arm64: {
6599 src: "myapex-arm64.apex",
6600 },
6601 arm: {
6602 src: "myapex-arm.apex",
6603 },
6604 },
6605 exported_java_libs: ["libfoo"],
6606 }
6607
6608 java_import {
6609 name: "libfoo",
6610 jars: ["libfoo.jar"],
6611 }
6612`, "", transform)
6613 })
6614
6615 t.Run("prebuilt no source", func(t *testing.T) {
6616 testDexpreoptWithApexes(t, `
6617 prebuilt_apex {
6618 name: "myapex" ,
6619 arch: {
6620 arm64: {
6621 src: "myapex-arm64.apex",
6622 },
6623 arm: {
6624 src: "myapex-arm.apex",
6625 },
6626 },
6627 exported_java_libs: ["libfoo"],
6628 }
6629
6630 java_import {
6631 name: "libfoo",
6632 jars: ["libfoo.jar"],
6633 }
6634`, "", transform)
6635 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006636}
6637
Andrei Onea115e7e72020-06-05 21:14:03 +01006638func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6639 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006640 bp += `
6641 apex_key {
6642 name: "myapex.key",
6643 public_key: "testkey.avbpubkey",
6644 private_key: "testkey.pem",
6645 }`
Paul Duffin45338f02021-03-30 23:07:52 +01006646 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01006647 "lib1/src/A.java": nil,
6648 "lib2/src/B.java": nil,
6649 "system/sepolicy/apex/myapex-file_contexts": nil,
6650 }
6651
Paul Duffin45338f02021-03-30 23:07:52 +01006652 errorHandler := android.FixtureExpectsNoErrors
6653 if errmsg != "" {
6654 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07006655 }
Colin Crossae8600b2020-10-29 17:09:13 -07006656
Paul Duffin45338f02021-03-30 23:07:52 +01006657 android.GroupFixturePreparers(
6658 android.PrepareForTestWithAndroidBuildComponents,
6659 java.PrepareForTestWithJavaBuildComponents,
6660 PrepareForTestWithApexBuildComponents,
6661 android.PrepareForTestWithNeverallowRules(rules),
6662 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6663 updatableBootJars := make([]string, 0, len(apexBootJars))
6664 for _, apexBootJar := range apexBootJars {
6665 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6666 }
6667 variables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6668 }),
6669 fs.AddToFixture(),
6670 ).
6671 ExtendWithErrorHandler(errorHandler).
6672 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01006673}
6674
6675func TestApexPermittedPackagesRules(t *testing.T) {
6676 testcases := []struct {
6677 name string
6678 expectedError string
6679 bp string
6680 bootJars []string
6681 modulesPackages map[string][]string
6682 }{
6683
6684 {
6685 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6686 expectedError: "",
6687 bp: `
6688 java_library {
6689 name: "bcp_lib1",
6690 srcs: ["lib1/src/*.java"],
6691 permitted_packages: ["foo.bar"],
6692 apex_available: ["myapex"],
6693 sdk_version: "none",
6694 system_modules: "none",
6695 }
6696 java_library {
6697 name: "nonbcp_lib2",
6698 srcs: ["lib2/src/*.java"],
6699 apex_available: ["myapex"],
6700 permitted_packages: ["a.b"],
6701 sdk_version: "none",
6702 system_modules: "none",
6703 }
6704 apex {
6705 name: "myapex",
6706 key: "myapex.key",
6707 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006708 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006709 }`,
6710 bootJars: []string{"bcp_lib1"},
6711 modulesPackages: map[string][]string{
6712 "myapex": []string{
6713 "foo.bar",
6714 },
6715 },
6716 },
6717 {
6718 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6719 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
6720 bp: `
6721 java_library {
6722 name: "bcp_lib1",
6723 srcs: ["lib1/src/*.java"],
6724 apex_available: ["myapex"],
6725 permitted_packages: ["foo.bar"],
6726 sdk_version: "none",
6727 system_modules: "none",
6728 }
6729 java_library {
6730 name: "bcp_lib2",
6731 srcs: ["lib2/src/*.java"],
6732 apex_available: ["myapex"],
6733 permitted_packages: ["foo.bar", "bar.baz"],
6734 sdk_version: "none",
6735 system_modules: "none",
6736 }
6737 apex {
6738 name: "myapex",
6739 key: "myapex.key",
6740 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006741 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006742 }
6743 `,
6744 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6745 modulesPackages: map[string][]string{
6746 "myapex": []string{
6747 "foo.bar",
6748 },
6749 },
6750 },
6751 }
6752 for _, tc := range testcases {
6753 t.Run(tc.name, func(t *testing.T) {
6754 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6755 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6756 })
6757 }
6758}
6759
Jiyong Park62304bb2020-04-13 16:19:48 +09006760func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006761 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006762 apex {
6763 name: "myapex",
6764 key: "myapex.key",
6765 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006766 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006767 }
6768
6769 apex_key {
6770 name: "myapex.key",
6771 public_key: "testkey.avbpubkey",
6772 private_key: "testkey.pem",
6773 }
6774
6775 cc_library {
6776 name: "mylib",
6777 srcs: ["mylib.cpp"],
6778 system_shared_libs: [],
6779 stl: "none",
6780 stubs: {
6781 versions: ["1"],
6782 },
6783 apex_available: ["myapex"],
6784 }
6785
6786 cc_library {
6787 name: "myprivlib",
6788 srcs: ["mylib.cpp"],
6789 system_shared_libs: [],
6790 stl: "none",
6791 apex_available: ["myapex"],
6792 }
6793
6794
6795 cc_test {
6796 name: "mytest",
6797 gtest: false,
6798 srcs: ["mylib.cpp"],
6799 system_shared_libs: [],
6800 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006801 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006802 test_for: ["myapex"]
6803 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006804
6805 cc_library {
6806 name: "mytestlib",
6807 srcs: ["mylib.cpp"],
6808 system_shared_libs: [],
6809 shared_libs: ["mylib", "myprivlib"],
6810 stl: "none",
6811 test_for: ["myapex"],
6812 }
6813
6814 cc_benchmark {
6815 name: "mybench",
6816 srcs: ["mylib.cpp"],
6817 system_shared_libs: [],
6818 shared_libs: ["mylib", "myprivlib"],
6819 stl: "none",
6820 test_for: ["myapex"],
6821 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006822 `)
6823
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006824 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006825 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006826 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6827 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6828 }
6829
6830 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006831 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006832 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6833 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6834 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6835}
Jiyong Park46a512f2020-12-04 18:02:13 +09006836
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006837func TestIndirectTestFor(t *testing.T) {
6838 ctx := testApex(t, `
6839 apex {
6840 name: "myapex",
6841 key: "myapex.key",
6842 native_shared_libs: ["mylib", "myprivlib"],
6843 updatable: false,
6844 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006845
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006846 apex_key {
6847 name: "myapex.key",
6848 public_key: "testkey.avbpubkey",
6849 private_key: "testkey.pem",
6850 }
6851
6852 cc_library {
6853 name: "mylib",
6854 srcs: ["mylib.cpp"],
6855 system_shared_libs: [],
6856 stl: "none",
6857 stubs: {
6858 versions: ["1"],
6859 },
6860 apex_available: ["myapex"],
6861 }
6862
6863 cc_library {
6864 name: "myprivlib",
6865 srcs: ["mylib.cpp"],
6866 system_shared_libs: [],
6867 stl: "none",
6868 shared_libs: ["mylib"],
6869 apex_available: ["myapex"],
6870 }
6871
6872 cc_library {
6873 name: "mytestlib",
6874 srcs: ["mylib.cpp"],
6875 system_shared_libs: [],
6876 shared_libs: ["myprivlib"],
6877 stl: "none",
6878 test_for: ["myapex"],
6879 }
6880 `)
6881
6882 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01006883 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006884 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6885 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6886 }
6887
6888 // The platform variant of mytestlib links to the platform variant of the
6889 // internal myprivlib.
6890 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6891
6892 // The platform variant of myprivlib links to the platform variant of mylib
6893 // and bypasses its stubs.
6894 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 +09006895}
6896
Martin Stjernholm4d058c82021-03-27 15:18:31 +00006897func TestTestForForLibInOtherApex(t *testing.T) {
6898 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6899 _ = testApex(t, `
6900 apex {
6901 name: "com.android.art",
6902 key: "myapex.key",
6903 native_shared_libs: ["mylib"],
6904 updatable: false,
6905 }
6906
6907 apex {
6908 name: "com.android.art.debug",
6909 key: "myapex.key",
6910 native_shared_libs: ["mylib", "mytestlib"],
6911 updatable: false,
6912 }
6913
6914 apex_key {
6915 name: "myapex.key",
6916 public_key: "testkey.avbpubkey",
6917 private_key: "testkey.pem",
6918 }
6919
6920 cc_library {
6921 name: "mylib",
6922 srcs: ["mylib.cpp"],
6923 system_shared_libs: [],
6924 stl: "none",
6925 stubs: {
6926 versions: ["1"],
6927 },
6928 apex_available: ["com.android.art", "com.android.art.debug"],
6929 }
6930
6931 cc_library {
6932 name: "mytestlib",
6933 srcs: ["mylib.cpp"],
6934 system_shared_libs: [],
6935 shared_libs: ["mylib"],
6936 stl: "none",
6937 apex_available: ["com.android.art.debug"],
6938 test_for: ["com.android.art"],
6939 }
6940 `,
6941 android.MockFS{
6942 "system/sepolicy/apex/com.android.art-file_contexts": nil,
6943 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
6944 }.AddToFixture())
6945}
6946
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006947// TODO(jungjw): Move this to proptools
6948func intPtr(i int) *int {
6949 return &i
6950}
6951
6952func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006953 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006954 apex_set {
6955 name: "myapex",
6956 set: "myapex.apks",
6957 filename: "foo_v2.apex",
6958 overrides: ["foo"],
6959 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006960 `,
6961 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6962 variables.Platform_sdk_version = intPtr(30)
6963 }),
6964 android.FixtureModifyConfig(func(config android.Config) {
6965 config.Targets[android.Android] = []android.Target{
6966 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6967 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6968 }
6969 }),
6970 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006971
6972 m := ctx.ModuleForTests("myapex", "android_common")
6973
6974 // Check extract_apks tool parameters.
Paul Duffin37ba3442021-03-29 00:21:08 +01006975 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006976 actual := extractedApex.Args["abis"]
6977 expected := "ARMEABI_V7A,ARM64_V8A"
6978 if actual != expected {
6979 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6980 }
6981 actual = extractedApex.Args["sdk-version"]
6982 expected = "30"
6983 if actual != expected {
6984 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6985 }
6986
6987 a := m.Module().(*ApexSet)
6988 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006989 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006990 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6991 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6992 }
6993}
6994
Jiyong Park7d95a512020-05-10 15:16:24 +09006995func TestNoStaticLinkingToStubsLib(t *testing.T) {
6996 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6997 apex {
6998 name: "myapex",
6999 key: "myapex.key",
7000 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007001 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007002 }
7003
7004 apex_key {
7005 name: "myapex.key",
7006 public_key: "testkey.avbpubkey",
7007 private_key: "testkey.pem",
7008 }
7009
7010 cc_library {
7011 name: "mylib",
7012 srcs: ["mylib.cpp"],
7013 static_libs: ["otherlib"],
7014 system_shared_libs: [],
7015 stl: "none",
7016 apex_available: [ "myapex" ],
7017 }
7018
7019 cc_library {
7020 name: "otherlib",
7021 srcs: ["mylib.cpp"],
7022 system_shared_libs: [],
7023 stl: "none",
7024 stubs: {
7025 versions: ["1", "2", "3"],
7026 },
7027 apex_available: [ "myapex" ],
7028 }
7029 `)
7030}
7031
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007032func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007033 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007034 apex {
7035 name: "myapex",
7036 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007037 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007038 }
7039
7040 apex_key {
7041 name: "myapex.key",
7042 public_key: "testkey.avbpubkey",
7043 private_key: "testkey.pem",
7044 }
7045
7046 prebuilt_apex {
7047 name: "myapex",
7048 prefer: true,
7049 arch: {
7050 arm64: {
7051 src: "myapex-arm64.apex",
7052 },
7053 arm: {
7054 src: "myapex-arm.apex",
7055 },
7056 },
7057 }
7058
7059 apex_set {
7060 name: "myapex_set",
7061 set: "myapex.apks",
7062 filename: "myapex_set.apex",
7063 overrides: ["myapex"],
7064 }
7065 `)
7066
7067 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7068 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7069 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 +09007070 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 +09007071}
7072
Jooyung Han938b5932020-06-20 12:47:47 +09007073func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007074 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007075 apex {
7076 name: "myapex",
7077 key: "myapex.key",
7078 apps: ["app"],
7079 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007080 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007081 }
7082
7083 apex_key {
7084 name: "myapex.key",
7085 public_key: "testkey.avbpubkey",
7086 private_key: "testkey.pem",
7087 }
7088
7089 android_app {
7090 name: "app",
7091 srcs: ["foo/bar/MyClass.java"],
7092 package_name: "foo",
7093 sdk_version: "none",
7094 system_modules: "none",
7095 apex_available: [ "myapex" ],
7096 }
7097 `, withFiles(map[string][]byte{
7098 "sub/Android.bp": []byte(`
7099 override_apex {
7100 name: "override_myapex",
7101 base: "myapex",
7102 apps: ["override_app"],
7103 allowed_files: ":allowed",
7104 }
7105 // Overridable "path" property should be referenced indirectly
7106 filegroup {
7107 name: "allowed",
7108 srcs: ["allowed.txt"],
7109 }
7110 override_android_app {
7111 name: "override_app",
7112 base: "app",
7113 package_name: "bar",
7114 }
7115 `),
7116 }))
7117
7118 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7119 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7120 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7121 }
7122
7123 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7124 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7125 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7126 }
7127}
7128
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007129func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007130 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007131 apex {
7132 name: "myapex",
7133 key: "myapex.key",
7134 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007135 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007136 }
7137
7138 apex_key {
7139 name: "myapex.key",
7140 public_key: "testkey.avbpubkey",
7141 private_key: "testkey.pem",
7142 }
7143
7144 cc_library {
7145 name: "mylib",
7146 srcs: ["mylib.cpp"],
7147 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007148 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007149 },
7150 apex_available: ["myapex"],
7151 }
7152
7153 cc_prebuilt_library_shared {
7154 name: "mylib",
7155 prefer: false,
7156 srcs: ["prebuilt.so"],
7157 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007158 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007159 },
7160 apex_available: ["myapex"],
7161 }
7162 `)
7163}
7164
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007165func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007166 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007167 apex {
7168 name: "myapex",
7169 key: "myapex.key",
7170 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007171 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007172 }
7173 apex_key {
7174 name: "myapex.key",
7175 public_key: "testkey.avbpubkey",
7176 private_key: "testkey.pem",
7177 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007178 `,
7179 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7180 variables.CompressedApex = proptools.BoolPtr(true)
7181 }),
7182 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007183
7184 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7185 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7186
7187 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7188 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7189
7190 // Make sure output of bundle is .capex
7191 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7192 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7193
7194 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007195 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007196 var builder strings.Builder
7197 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7198 androidMk := builder.String()
7199 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7200}
7201
Martin Stjernholm2856c662020-12-02 15:03:42 +00007202func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007203 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007204 apex {
7205 name: "myapex",
7206 key: "myapex.key",
7207 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007208 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007209 }
7210
7211 apex_key {
7212 name: "myapex.key",
7213 public_key: "testkey.avbpubkey",
7214 private_key: "testkey.pem",
7215 }
7216
7217 cc_library {
7218 name: "mylib",
7219 srcs: ["mylib.cpp"],
7220 apex_available: ["myapex"],
7221 shared_libs: ["otherlib"],
7222 system_shared_libs: [],
7223 }
7224
7225 cc_library {
7226 name: "otherlib",
7227 srcs: ["mylib.cpp"],
7228 stubs: {
7229 versions: ["current"],
7230 },
7231 }
7232
7233 cc_prebuilt_library_shared {
7234 name: "otherlib",
7235 prefer: true,
7236 srcs: ["prebuilt.so"],
7237 stubs: {
7238 versions: ["current"],
7239 },
7240 }
7241 `)
7242
7243 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007244 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007245 var builder strings.Builder
7246 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7247 androidMk := builder.String()
7248
7249 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7250 // a thing there.
7251 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7252}
7253
Jiyong Parke3867542020-12-03 17:28:25 +09007254func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007255 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007256 apex {
7257 name: "myapex",
7258 key: "myapex.key",
7259 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007260 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007261 }
7262
7263 apex_key {
7264 name: "myapex.key",
7265 public_key: "testkey.avbpubkey",
7266 private_key: "testkey.pem",
7267 }
7268
7269 cc_library {
7270 name: "mylib",
7271 srcs: ["mylib.cpp"],
7272 system_shared_libs: [],
7273 stl: "none",
7274 apex_available: ["myapex"],
7275 shared_libs: ["mylib2"],
7276 target: {
7277 apex: {
7278 exclude_shared_libs: ["mylib2"],
7279 },
7280 },
7281 }
7282
7283 cc_library {
7284 name: "mylib2",
7285 srcs: ["mylib.cpp"],
7286 system_shared_libs: [],
7287 stl: "none",
7288 }
7289 `)
7290
7291 // Check if mylib is linked to mylib2 for the non-apex target
7292 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7293 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7294
7295 // Make sure that the link doesn't occur for the apex target
7296 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7297 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7298
7299 // It shouldn't appear in the copy cmd as well.
7300 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7301 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7302}
7303
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007304func TestPrebuiltStubLibDep(t *testing.T) {
7305 bpBase := `
7306 apex {
7307 name: "myapex",
7308 key: "myapex.key",
7309 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007310 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007311 }
7312 apex_key {
7313 name: "myapex.key",
7314 public_key: "testkey.avbpubkey",
7315 private_key: "testkey.pem",
7316 }
7317 cc_library {
7318 name: "mylib",
7319 srcs: ["mylib.cpp"],
7320 apex_available: ["myapex"],
7321 shared_libs: ["stublib"],
7322 system_shared_libs: [],
7323 }
7324 apex {
7325 name: "otherapex",
7326 enabled: %s,
7327 key: "myapex.key",
7328 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007329 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007330 }
7331 `
7332
7333 stublibSourceBp := `
7334 cc_library {
7335 name: "stublib",
7336 srcs: ["mylib.cpp"],
7337 apex_available: ["otherapex"],
7338 system_shared_libs: [],
7339 stl: "none",
7340 stubs: {
7341 versions: ["1"],
7342 },
7343 }
7344 `
7345
7346 stublibPrebuiltBp := `
7347 cc_prebuilt_library_shared {
7348 name: "stublib",
7349 srcs: ["prebuilt.so"],
7350 apex_available: ["otherapex"],
7351 stubs: {
7352 versions: ["1"],
7353 },
7354 %s
7355 }
7356 `
7357
7358 tests := []struct {
7359 name string
7360 stublibBp string
7361 usePrebuilt bool
7362 modNames []string // Modules to collect AndroidMkEntries for
7363 otherApexEnabled []string
7364 }{
7365 {
7366 name: "only_source",
7367 stublibBp: stublibSourceBp,
7368 usePrebuilt: false,
7369 modNames: []string{"stublib"},
7370 otherApexEnabled: []string{"true", "false"},
7371 },
7372 {
7373 name: "source_preferred",
7374 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7375 usePrebuilt: false,
7376 modNames: []string{"stublib", "prebuilt_stublib"},
7377 otherApexEnabled: []string{"true", "false"},
7378 },
7379 {
7380 name: "prebuilt_preferred",
7381 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7382 usePrebuilt: true,
7383 modNames: []string{"stublib", "prebuilt_stublib"},
7384 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7385 },
7386 {
7387 name: "only_prebuilt",
7388 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7389 usePrebuilt: true,
7390 modNames: []string{"stublib"},
7391 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7392 },
7393 }
7394
7395 for _, test := range tests {
7396 t.Run(test.name, func(t *testing.T) {
7397 for _, otherApexEnabled := range test.otherApexEnabled {
7398 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007399 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007400
7401 type modAndMkEntries struct {
7402 mod *cc.Module
7403 mkEntries android.AndroidMkEntries
7404 }
7405 entries := []*modAndMkEntries{}
7406
7407 // Gather shared lib modules that are installable
7408 for _, modName := range test.modNames {
7409 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7410 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7411 continue
7412 }
7413 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007414 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007415 continue
7416 }
Colin Crossaa255532020-07-03 13:18:24 -07007417 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007418 if ent.Disabled {
7419 continue
7420 }
7421 entries = append(entries, &modAndMkEntries{
7422 mod: mod,
7423 mkEntries: ent,
7424 })
7425 }
7426 }
7427 }
7428
7429 var entry *modAndMkEntries = nil
7430 for _, ent := range entries {
7431 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7432 if entry != nil {
7433 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7434 } else {
7435 entry = ent
7436 }
7437 }
7438 }
7439
7440 if entry == nil {
7441 t.Errorf("AndroidMk entry for \"stublib\" missing")
7442 } else {
7443 isPrebuilt := entry.mod.Prebuilt() != nil
7444 if isPrebuilt != test.usePrebuilt {
7445 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7446 }
7447 if !entry.mod.IsStubs() {
7448 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7449 }
7450 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7451 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7452 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007453 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09007454 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09007455 if !android.InList(expected, cflags) {
7456 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7457 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007458 }
7459 })
7460 }
7461 })
7462 }
7463}
7464
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007465func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007466 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007467}