blob: 7ef1eaa675e01c8f71892dd7c49f1cac91571f09 [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 "io/ioutil"
20 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090021 "path"
Paul Duffin37856732021-02-26 14:24:15 +000022 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070023 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010024 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090025 "sort"
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
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070041var buildDir string
42
Jooyung Hand3639552019-08-09 12:57:43 +090043// names returns name list from white space separated string
44func names(s string) (ns []string) {
45 for _, n := range strings.Split(s, " ") {
46 if len(n) > 0 {
47 ns = append(ns, n)
48 }
49 }
50 return
51}
52
Paul Duffin40b62572021-03-20 11:39:01 +000053func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090054 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000055 apexFixtureFactory.Extend(preparers...).
Paul Duffine05480a2021-03-08 15:07:14 +000056 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000057 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090058}
59
Paul Duffin40b62572021-03-20 11:39:01 +000060func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090061 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000062 factory := apexFixtureFactory.Extend(preparers...)
63 if bp != "" {
64 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
65 }
66 result := factory.RunTest(t)
Paul Duffine05480a2021-03-08 15:07:14 +000067 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090068}
69
Paul Duffin810f33d2021-03-09 14:12:32 +000070func withFiles(files android.MockFS) android.FixturePreparer {
71 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090072}
73
Paul Duffin810f33d2021-03-09 14:12:32 +000074func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
75 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090076 for k, v := range targets {
77 config.Targets[k] = v
78 }
Paul Duffin810f33d2021-03-09 14:12:32 +000079 })
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Jooyung Han35155c42020-02-06 17:33:20 +090082// withNativeBridgeTargets sets configuration with targets including:
83// - X86_64 (primary)
84// - X86 (secondary)
85// - Arm64 on X86_64 (native bridge)
86// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000087var withNativeBridgeEnabled = android.FixtureModifyConfig(
88 func(config android.Config) {
89 config.Targets[android.Android] = []android.Target{
90 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
91 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
92 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
93 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
94 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
95 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
96 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
97 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
98 }
99 },
100)
101
102func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
103 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
104 variables.ManifestPackageNameOverrides = specs
105 })
Jooyung Han35155c42020-02-06 17:33:20 +0900106}
107
Paul Duffin810f33d2021-03-09 14:12:32 +0000108var withBinder32bit = android.FixtureModifyProductVariables(
109 func(variables android.FixtureProductVariables) {
110 variables.Binder32bit = proptools.BoolPtr(true)
111 },
112)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900113
Paul Duffin810f33d2021-03-09 14:12:32 +0000114var withUnbundledBuild = android.FixtureModifyProductVariables(
115 func(variables android.FixtureProductVariables) {
116 variables.Unbundled_build = proptools.BoolPtr(true)
117 },
118)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900119
Paul Duffina02cae32021-03-09 01:44:06 +0000120var emptyFixtureFactory = android.NewFixtureFactory(&buildDir)
121
Paul Duffin37aad602021-03-08 09:47:16 +0000122var apexFixtureFactory = android.NewFixtureFactory(
123 &buildDir,
124 // General preparers in alphabetical order as test infrastructure will enforce correct
125 // registration order.
126 android.PrepareForTestWithAndroidBuildComponents,
127 bpf.PrepareForTestWithBpf,
128 cc.PrepareForTestWithCcBuildComponents,
129 java.PrepareForTestWithJavaDefaultModules,
130 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
131 rust.PrepareForTestWithRustDefaultModules,
132 sh.PrepareForTestWithShBuildComponents,
133
134 PrepareForTestWithApexBuildComponents,
135
136 // Additional apex test specific preparers.
137 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
138 filegroup {
139 name: "myapex-file_contexts",
140 srcs: [
141 "apex/myapex-file_contexts",
142 ],
143 }
144 `),
145 android.FixtureMergeMockFs(android.MockFS{
146 "a.java": nil,
147 "PrebuiltAppFoo.apk": nil,
148 "PrebuiltAppFooPriv.apk": nil,
149 "build/make/target/product/security": nil,
150 "apex_manifest.json": nil,
151 "AndroidManifest.xml": nil,
152 "system/sepolicy/apex/myapex-file_contexts": nil,
153 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
154 "system/sepolicy/apex/myapex2-file_contexts": nil,
155 "system/sepolicy/apex/otherapex-file_contexts": nil,
156 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
157 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
158 "mylib.cpp": nil,
159 "mytest.cpp": nil,
160 "mytest1.cpp": nil,
161 "mytest2.cpp": nil,
162 "mytest3.cpp": nil,
163 "myprebuilt": nil,
164 "my_include": nil,
165 "foo/bar/MyClass.java": nil,
166 "prebuilt.jar": nil,
167 "prebuilt.so": nil,
168 "vendor/foo/devkeys/test.x509.pem": nil,
169 "vendor/foo/devkeys/test.pk8": nil,
170 "testkey.x509.pem": nil,
171 "testkey.pk8": nil,
172 "testkey.override.x509.pem": nil,
173 "testkey.override.pk8": nil,
174 "vendor/foo/devkeys/testkey.avbpubkey": nil,
175 "vendor/foo/devkeys/testkey.pem": nil,
176 "NOTICE": nil,
177 "custom_notice": nil,
178 "custom_notice_for_static_lib": nil,
179 "testkey2.avbpubkey": nil,
180 "testkey2.pem": nil,
181 "myapex-arm64.apex": nil,
182 "myapex-arm.apex": nil,
183 "myapex.apks": nil,
184 "frameworks/base/api/current.txt": nil,
185 "framework/aidl/a.aidl": nil,
186 "build/make/core/proguard.flags": nil,
187 "build/make/core/proguard_basic_keeps.flags": nil,
188 "dummy.txt": nil,
189 "baz": nil,
190 "bar/baz": nil,
191 "testdata/baz": nil,
192 "AppSet.apks": nil,
193 "foo.rs": nil,
194 "libfoo.jar": nil,
195 "libbar.jar": nil,
196 },
197 ),
198
199 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
200 variables.DeviceVndkVersion = proptools.StringPtr("current")
201 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
202 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
203 variables.Platform_sdk_codename = proptools.StringPtr("Q")
204 variables.Platform_sdk_final = proptools.BoolPtr(false)
205 variables.Platform_version_active_codenames = []string{"Q"}
206 variables.Platform_vndk_version = proptools.StringPtr("VER")
207 }),
208)
209
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700210func setUp() {
211 var err error
212 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700214 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900215 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900216}
217
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700218func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700219 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220}
221
Jooyung Han643adc42020-02-27 13:50:06 +0900222// ensure that 'result' equals 'expected'
223func ensureEquals(t *testing.T, result string, expected string) {
224 t.Helper()
225 if result != expected {
226 t.Errorf("%q != %q", expected, result)
227 }
228}
229
Jiyong Park25fc6a92018-11-18 18:02:45 +0900230// ensure that 'result' contains 'expected'
231func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900232 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900233 if !strings.Contains(result, expected) {
234 t.Errorf("%q is not found in %q", expected, result)
235 }
236}
237
Liz Kammer5bd365f2020-05-27 15:15:11 -0700238// ensure that 'result' contains 'expected' exactly one time
239func ensureContainsOnce(t *testing.T, result string, expected string) {
240 t.Helper()
241 count := strings.Count(result, expected)
242 if count != 1 {
243 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
244 }
245}
246
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247// ensures that 'result' does not contain 'notExpected'
248func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900249 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250 if strings.Contains(result, notExpected) {
251 t.Errorf("%q is found in %q", notExpected, result)
252 }
253}
254
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700255func ensureMatches(t *testing.T, result string, expectedRex string) {
256 ok, err := regexp.MatchString(expectedRex, result)
257 if err != nil {
258 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
259 return
260 }
261 if !ok {
262 t.Errorf("%s does not match regular expession %s", result, expectedRex)
263 }
264}
265
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900267 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900268 if !android.InList(expected, result) {
269 t.Errorf("%q is not found in %v", expected, result)
270 }
271}
272
273func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900274 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900275 if android.InList(notExpected, result) {
276 t.Errorf("%q is found in %v", notExpected, result)
277 }
278}
279
Jooyung Hane1633032019-08-01 17:41:43 +0900280func ensureListEmpty(t *testing.T, result []string) {
281 t.Helper()
282 if len(result) > 0 {
283 t.Errorf("%q is expected to be empty", result)
284 }
285}
286
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000287func ensureListNotEmpty(t *testing.T, result []string) {
288 t.Helper()
289 if len(result) == 0 {
290 t.Errorf("%q is expected to be not empty", result)
291 }
292}
293
Jiyong Park25fc6a92018-11-18 18:02:45 +0900294// Minimal test
295func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800296 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900297 apex_defaults {
298 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900299 manifest: ":myapex.manifest",
300 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900302 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900303 native_shared_libs: [
304 "mylib",
305 "libfoo.ffi",
306 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900307 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800308 multilib: {
309 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900310 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800311 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900312 },
Jiyong Park77acec62020-06-01 21:39:15 +0900313 java_libs: [
314 "myjar",
315 "myjar_dex",
316 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000317 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318 }
319
Jiyong Park30ca9372019-02-07 16:27:23 +0900320 apex {
321 name: "myapex",
322 defaults: ["myapex-defaults"],
323 }
324
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325 apex_key {
326 name: "myapex.key",
327 public_key: "testkey.avbpubkey",
328 private_key: "testkey.pem",
329 }
330
Jiyong Park809bb722019-02-13 21:33:49 +0900331 filegroup {
332 name: "myapex.manifest",
333 srcs: ["apex_manifest.json"],
334 }
335
336 filegroup {
337 name: "myapex.androidmanifest",
338 srcs: ["AndroidManifest.xml"],
339 }
340
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 cc_library {
342 name: "mylib",
343 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900344 shared_libs: [
345 "mylib2",
346 "libbar.ffi",
347 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 system_shared_libs: [],
349 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000350 // TODO: remove //apex_available:platform
351 apex_available: [
352 "//apex_available:platform",
353 "myapex",
354 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 }
356
Alex Light3d673592019-01-18 14:37:31 -0800357 cc_binary {
358 name: "foo",
359 srcs: ["mylib.cpp"],
360 compile_multilib: "both",
361 multilib: {
362 lib32: {
363 suffix: "32",
364 },
365 lib64: {
366 suffix: "64",
367 },
368 },
369 symlinks: ["foo_link_"],
370 symlink_preferred_arch: true,
371 system_shared_libs: [],
372 static_executable: true,
373 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700374 apex_available: [ "myapex", "com.android.gki.*" ],
375 }
376
Jiyong Park99644e92020-11-17 22:21:02 +0900377 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000378 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900379 srcs: ["foo.rs"],
380 rlibs: ["libfoo.rlib.rust"],
381 dylibs: ["libfoo.dylib.rust"],
382 apex_available: ["myapex"],
383 }
384
385 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000386 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900387 srcs: ["foo.rs"],
388 crate_name: "foo",
389 apex_available: ["myapex"],
390 }
391
392 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000393 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900394 srcs: ["foo.rs"],
395 crate_name: "foo",
396 apex_available: ["myapex"],
397 }
398
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900399 rust_ffi_shared {
400 name: "libfoo.ffi",
401 srcs: ["foo.rs"],
402 crate_name: "foo",
403 apex_available: ["myapex"],
404 }
405
406 rust_ffi_shared {
407 name: "libbar.ffi",
408 srcs: ["foo.rs"],
409 crate_name: "bar",
410 apex_available: ["myapex"],
411 }
412
Yifan Hongd22a84a2020-07-28 17:37:46 -0700413 apex {
414 name: "com.android.gki.fake",
415 binaries: ["foo"],
416 key: "myapex.key",
417 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000418 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800419 }
420
Paul Duffindddd5462020-04-07 15:25:44 +0100421 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900422 name: "mylib2",
423 srcs: ["mylib.cpp"],
424 system_shared_libs: [],
425 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900426 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900427 static_libs: ["libstatic"],
428 // TODO: remove //apex_available:platform
429 apex_available: [
430 "//apex_available:platform",
431 "myapex",
432 ],
433 }
434
Paul Duffindddd5462020-04-07 15:25:44 +0100435 cc_prebuilt_library_shared {
436 name: "mylib2",
437 srcs: ["prebuilt.so"],
438 // TODO: remove //apex_available:platform
439 apex_available: [
440 "//apex_available:platform",
441 "myapex",
442 ],
443 }
444
Jiyong Park9918e1a2020-03-17 19:16:40 +0900445 cc_library_static {
446 name: "libstatic",
447 srcs: ["mylib.cpp"],
448 system_shared_libs: [],
449 stl: "none",
450 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000451 // TODO: remove //apex_available:platform
452 apex_available: [
453 "//apex_available:platform",
454 "myapex",
455 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900456 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900457
458 java_library {
459 name: "myjar",
460 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900461 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900462 sdk_version: "none",
463 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900464 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900465 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000466 // TODO: remove //apex_available:platform
467 apex_available: [
468 "//apex_available:platform",
469 "myapex",
470 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900471 }
472
Jiyong Park77acec62020-06-01 21:39:15 +0900473 dex_import {
474 name: "myjar_dex",
475 jars: ["prebuilt.jar"],
476 apex_available: [
477 "//apex_available:platform",
478 "myapex",
479 ],
480 }
481
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 java_library {
483 name: "myotherjar",
484 srcs: ["foo/bar/MyClass.java"],
485 sdk_version: "none",
486 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900487 // TODO: remove //apex_available:platform
488 apex_available: [
489 "//apex_available:platform",
490 "myapex",
491 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900493
494 java_library {
495 name: "mysharedjar",
496 srcs: ["foo/bar/MyClass.java"],
497 sdk_version: "none",
498 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900499 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900500 `)
501
Sundong Ahnabb64432019-10-22 13:58:29 +0900502 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900503
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900504 // Make sure that Android.mk is created
505 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700506 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900507 var builder strings.Builder
508 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
509
510 androidMk := builder.String()
511 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
512 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
513
Jiyong Park42cca6c2019-04-01 11:15:50 +0900514 optFlags := apexRule.Args["opt_flags"]
515 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700516 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900517 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900518
Jiyong Park25fc6a92018-11-18 18:02:45 +0900519 copyCmds := apexRule.Args["copy_commands"]
520
521 // Ensure that main rule creates an output
522 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
523
524 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700525 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
526 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
527 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900528 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900529 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900530
531 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700532 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
533 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900534 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
535 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900536 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537
538 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800539 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
540 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900541 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900542 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900543 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900544 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
545 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900546 // .. but not for java libs
547 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900548 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800549
Colin Cross7113d202019-11-20 16:39:12 -0800550 // Ensure that the platform variant ends with _shared or _common
551 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900553 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
554 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
556
557 // Ensure that dynamic dependency to java libs are not included
558 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800559
560 // Ensure that all symlinks are present.
561 found_foo_link_64 := false
562 found_foo := false
563 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900564 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800565 if strings.HasSuffix(cmd, "bin/foo") {
566 found_foo = true
567 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
568 found_foo_link_64 = true
569 }
570 }
571 }
572 good := found_foo && found_foo_link_64
573 if !good {
574 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
575 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900576
Sundong Ahnabb64432019-10-22 13:58:29 +0900577 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700578 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900579 if len(noticeInputs) != 3 {
580 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900581 }
582 ensureListContains(t, noticeInputs, "NOTICE")
583 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900584 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900585
Artur Satayeva8bd1132020-04-27 18:07:06 +0100586 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100587 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100588 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
589 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
590 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100591
592 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100593 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100594 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
595 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
596 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800597}
598
Jooyung Hanf21c7972019-12-16 22:32:06 +0900599func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800600 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900601 apex_defaults {
602 name: "myapex-defaults",
603 key: "myapex.key",
604 prebuilts: ["myetc"],
605 native_shared_libs: ["mylib"],
606 java_libs: ["myjar"],
607 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900608 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800609 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000610 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900611 }
612
613 prebuilt_etc {
614 name: "myetc",
615 src: "myprebuilt",
616 }
617
618 apex {
619 name: "myapex",
620 defaults: ["myapex-defaults"],
621 }
622
623 apex_key {
624 name: "myapex.key",
625 public_key: "testkey.avbpubkey",
626 private_key: "testkey.pem",
627 }
628
629 cc_library {
630 name: "mylib",
631 system_shared_libs: [],
632 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000633 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900634 }
635
636 java_library {
637 name: "myjar",
638 srcs: ["foo/bar/MyClass.java"],
639 sdk_version: "none",
640 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000641 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900642 }
643
644 android_app {
645 name: "AppFoo",
646 srcs: ["foo/bar/MyClass.java"],
647 sdk_version: "none",
648 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900651
652 runtime_resource_overlay {
653 name: "rro",
654 theme: "blue",
655 }
656
markchien2f59ec92020-09-02 16:23:38 +0800657 bpf {
658 name: "bpf",
659 srcs: ["bpf.c", "bpf2.c"],
660 }
661
Jooyung Hanf21c7972019-12-16 22:32:06 +0900662 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000663 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900664 "etc/myetc",
665 "javalib/myjar.jar",
666 "lib64/mylib.so",
667 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900668 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800669 "etc/bpf/bpf.o",
670 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900671 })
672}
673
Jooyung Han01a3ee22019-11-02 02:52:25 +0900674func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800675 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900676 apex {
677 name: "myapex",
678 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000679 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900680 }
681
682 apex_key {
683 name: "myapex.key",
684 public_key: "testkey.avbpubkey",
685 private_key: "testkey.pem",
686 }
687 `)
688
689 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900690 args := module.Rule("apexRule").Args
691 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
692 t.Error("manifest should be apex_manifest.pb, but " + manifest)
693 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694}
695
Alex Light5098a612018-11-29 17:12:15 -0800696func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800697 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800698 apex {
699 name: "myapex",
700 key: "myapex.key",
701 payload_type: "zip",
702 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000703 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800704 }
705
706 apex_key {
707 name: "myapex.key",
708 public_key: "testkey.avbpubkey",
709 private_key: "testkey.pem",
710 }
711
712 cc_library {
713 name: "mylib",
714 srcs: ["mylib.cpp"],
715 shared_libs: ["mylib2"],
716 system_shared_libs: [],
717 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000718 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800719 }
720
721 cc_library {
722 name: "mylib2",
723 srcs: ["mylib.cpp"],
724 system_shared_libs: [],
725 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000726 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800727 }
728 `)
729
Sundong Ahnabb64432019-10-22 13:58:29 +0900730 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800731 copyCmds := zipApexRule.Args["copy_commands"]
732
733 // Ensure that main rule creates an output
734 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
735
736 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700737 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800738
739 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700740 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800741
742 // Ensure that both direct and indirect deps are copied into apex
743 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
744 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745}
746
747func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800748 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900749 apex {
750 name: "myapex",
751 key: "myapex.key",
752 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000753 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754 }
755
756 apex_key {
757 name: "myapex.key",
758 public_key: "testkey.avbpubkey",
759 private_key: "testkey.pem",
760 }
761
762 cc_library {
763 name: "mylib",
764 srcs: ["mylib.cpp"],
765 shared_libs: ["mylib2", "mylib3"],
766 system_shared_libs: [],
767 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000768 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900769 }
770
771 cc_library {
772 name: "mylib2",
773 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900774 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900775 system_shared_libs: [],
776 stl: "none",
777 stubs: {
778 versions: ["1", "2", "3"],
779 },
780 }
781
782 cc_library {
783 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900784 srcs: ["mylib.cpp"],
785 shared_libs: ["mylib4"],
786 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900787 stl: "none",
788 stubs: {
789 versions: ["10", "11", "12"],
790 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000791 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900792 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900793
794 cc_library {
795 name: "mylib4",
796 srcs: ["mylib.cpp"],
797 system_shared_libs: [],
798 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000799 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900800 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900801 `)
802
Sundong Ahnabb64432019-10-22 13:58:29 +0900803 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900804 copyCmds := apexRule.Args["copy_commands"]
805
806 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800807 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900808
809 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800810 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900811
812 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800813 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900814
Colin Crossaede88c2020-08-11 12:17:01 -0700815 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900816
817 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900818 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900819 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900820 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900821
822 // 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 -0700823 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900824 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700825 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900826
827 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900828 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900829 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900830
831 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700832 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900833
Jooyung Hana57af4a2020-01-23 05:36:59 +0000834 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900835 "lib64/mylib.so",
836 "lib64/mylib3.so",
837 "lib64/mylib4.so",
838 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900839}
840
Colin Cross7812fd32020-09-25 12:35:10 -0700841func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
842 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800843 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700844 apex {
845 name: "myapex",
846 key: "myapex.key",
847 native_shared_libs: ["mylib", "mylib3"],
848 min_sdk_version: "29",
849 }
850
851 apex_key {
852 name: "myapex.key",
853 public_key: "testkey.avbpubkey",
854 private_key: "testkey.pem",
855 }
856
857 cc_library {
858 name: "mylib",
859 srcs: ["mylib.cpp"],
860 shared_libs: ["mylib2", "mylib3"],
861 system_shared_libs: [],
862 stl: "none",
863 apex_available: [ "myapex" ],
864 min_sdk_version: "28",
865 }
866
867 cc_library {
868 name: "mylib2",
869 srcs: ["mylib.cpp"],
870 cflags: ["-include mylib.h"],
871 system_shared_libs: [],
872 stl: "none",
873 stubs: {
874 versions: ["28", "29", "30", "current"],
875 },
876 min_sdk_version: "28",
877 }
878
879 cc_library {
880 name: "mylib3",
881 srcs: ["mylib.cpp"],
882 shared_libs: ["mylib4"],
883 system_shared_libs: [],
884 stl: "none",
885 stubs: {
886 versions: ["28", "29", "30", "current"],
887 },
888 apex_available: [ "myapex" ],
889 min_sdk_version: "28",
890 }
891
892 cc_library {
893 name: "mylib4",
894 srcs: ["mylib.cpp"],
895 system_shared_libs: [],
896 stl: "none",
897 apex_available: [ "myapex" ],
898 min_sdk_version: "28",
899 }
900 `)
901
902 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
903 copyCmds := apexRule.Args["copy_commands"]
904
905 // Ensure that direct non-stubs dep is always included
906 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
907
908 // Ensure that indirect stubs dep is not included
909 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
910
911 // Ensure that direct stubs dep is included
912 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
913
914 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
915
Jiyong Park55549df2021-02-26 23:57:23 +0900916 // Ensure that mylib is linking with the latest version of stub for mylib2
917 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700918 // ... and not linking to the non-stub (impl) variant of mylib2
919 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
920
921 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
922 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
923 // .. and not linking to the stubs variant of mylib3
924 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
925
926 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700927 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700928 ensureNotContains(t, mylib2Cflags, "-include ")
929
930 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700931 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700932
933 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
934 "lib64/mylib.so",
935 "lib64/mylib3.so",
936 "lib64/mylib4.so",
937 })
938}
939
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900940func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
941 t.Parallel()
942 // myapex (Z)
943 // mylib -----------------.
944 // |
945 // otherapex (29) |
946 // libstub's versions: 29 Z current
947 // |
948 // <platform> |
949 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800950 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900951 apex {
952 name: "myapex",
953 key: "myapex.key",
954 native_shared_libs: ["mylib"],
955 min_sdk_version: "Z", // non-final
956 }
957
958 cc_library {
959 name: "mylib",
960 srcs: ["mylib.cpp"],
961 shared_libs: ["libstub"],
962 apex_available: ["myapex"],
963 min_sdk_version: "Z",
964 }
965
966 apex_key {
967 name: "myapex.key",
968 public_key: "testkey.avbpubkey",
969 private_key: "testkey.pem",
970 }
971
972 apex {
973 name: "otherapex",
974 key: "myapex.key",
975 native_shared_libs: ["libstub"],
976 min_sdk_version: "29",
977 }
978
979 cc_library {
980 name: "libstub",
981 srcs: ["mylib.cpp"],
982 stubs: {
983 versions: ["29", "Z", "current"],
984 },
985 apex_available: ["otherapex"],
986 min_sdk_version: "29",
987 }
988
989 // platform module depending on libstub from otherapex should use the latest stub("current")
990 cc_library {
991 name: "libplatform",
992 srcs: ["mylib.cpp"],
993 shared_libs: ["libstub"],
994 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +0000995 `,
996 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
997 variables.Platform_sdk_codename = proptools.StringPtr("Z")
998 variables.Platform_sdk_final = proptools.BoolPtr(false)
999 variables.Platform_version_active_codenames = []string{"Z"}
1000 }),
1001 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001002
Jiyong Park55549df2021-02-26 23:57:23 +09001003 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001004 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001005 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001006 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001007 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001008
1009 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1010 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1011 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1012 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1013 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1014}
1015
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001016func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001017 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001018 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001019 name: "myapex2",
1020 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001021 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001022 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001023 }
1024
1025 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001026 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001027 public_key: "testkey.avbpubkey",
1028 private_key: "testkey.pem",
1029 }
1030
1031 cc_library {
1032 name: "mylib",
1033 srcs: ["mylib.cpp"],
1034 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001035 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001036 system_shared_libs: [],
1037 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001038 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001039 }
1040
1041 cc_library {
1042 name: "libfoo",
1043 srcs: ["mylib.cpp"],
1044 shared_libs: ["libbar"],
1045 system_shared_libs: [],
1046 stl: "none",
1047 stubs: {
1048 versions: ["10", "20", "30"],
1049 },
1050 }
1051
1052 cc_library {
1053 name: "libbar",
1054 srcs: ["mylib.cpp"],
1055 system_shared_libs: [],
1056 stl: "none",
1057 }
1058
Jiyong Park678c8812020-02-07 17:25:49 +09001059 cc_library_static {
1060 name: "libbaz",
1061 srcs: ["mylib.cpp"],
1062 system_shared_libs: [],
1063 stl: "none",
1064 apex_available: [ "myapex2" ],
1065 }
1066
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001067 `)
1068
Jiyong Park83dc74b2020-01-14 18:38:44 +09001069 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001070 copyCmds := apexRule.Args["copy_commands"]
1071
1072 // Ensure that direct non-stubs dep is always included
1073 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1074
1075 // Ensure that indirect stubs dep is not included
1076 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1077
1078 // Ensure that dependency of stubs is not included
1079 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1080
Colin Crossaede88c2020-08-11 12:17:01 -07001081 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001082
1083 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001084 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001085 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001086 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001087
Jiyong Park3ff16992019-12-27 14:11:47 +09001088 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001089
1090 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1091 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001092
Artur Satayeva8bd1132020-04-27 18:07:06 +01001093 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001094 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001095
Artur Satayeva8bd1132020-04-27 18:07:06 +01001096 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001097 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001098}
1099
Jooyung Hand3639552019-08-09 12:57:43 +09001100func TestApexWithRuntimeLibsDependency(t *testing.T) {
1101 /*
1102 myapex
1103 |
1104 v (runtime_libs)
1105 mylib ------+------> libfoo [provides stub]
1106 |
1107 `------> libbar
1108 */
Colin Cross1c460562021-02-16 17:55:47 -08001109 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001110 apex {
1111 name: "myapex",
1112 key: "myapex.key",
1113 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001114 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001115 }
1116
1117 apex_key {
1118 name: "myapex.key",
1119 public_key: "testkey.avbpubkey",
1120 private_key: "testkey.pem",
1121 }
1122
1123 cc_library {
1124 name: "mylib",
1125 srcs: ["mylib.cpp"],
1126 runtime_libs: ["libfoo", "libbar"],
1127 system_shared_libs: [],
1128 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001129 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001130 }
1131
1132 cc_library {
1133 name: "libfoo",
1134 srcs: ["mylib.cpp"],
1135 system_shared_libs: [],
1136 stl: "none",
1137 stubs: {
1138 versions: ["10", "20", "30"],
1139 },
1140 }
1141
1142 cc_library {
1143 name: "libbar",
1144 srcs: ["mylib.cpp"],
1145 system_shared_libs: [],
1146 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001147 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001148 }
1149
1150 `)
1151
Sundong Ahnabb64432019-10-22 13:58:29 +09001152 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001153 copyCmds := apexRule.Args["copy_commands"]
1154
1155 // Ensure that direct non-stubs dep is always included
1156 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1157
1158 // Ensure that indirect stubs dep is not included
1159 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1160
1161 // Ensure that runtime_libs dep in included
1162 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1163
Sundong Ahnabb64432019-10-22 13:58:29 +09001164 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001165 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1166 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001167
1168}
1169
Paul Duffina02cae32021-03-09 01:44:06 +00001170var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1171 cc.PrepareForTestWithCcBuildComponents,
1172 PrepareForTestWithApexBuildComponents,
1173 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001174 apex {
1175 name: "com.android.runtime",
1176 key: "com.android.runtime.key",
1177 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001178 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001179 }
1180
1181 apex_key {
1182 name: "com.android.runtime.key",
1183 public_key: "testkey.avbpubkey",
1184 private_key: "testkey.pem",
1185 }
Paul Duffina02cae32021-03-09 01:44:06 +00001186 `),
1187 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1188)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001189
Paul Duffina02cae32021-03-09 01:44:06 +00001190func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1191 result := emptyFixtureFactory.Extend(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001192 cc_library {
1193 name: "libc",
1194 no_libcrt: true,
1195 nocrt: true,
1196 stl: "none",
1197 system_shared_libs: [],
1198 stubs: { versions: ["1"] },
1199 apex_available: ["com.android.runtime"],
1200
1201 sanitize: {
1202 hwaddress: true,
1203 }
1204 }
1205
1206 cc_prebuilt_library_shared {
1207 name: "libclang_rt.hwasan-aarch64-android",
1208 no_libcrt: true,
1209 nocrt: true,
1210 stl: "none",
1211 system_shared_libs: [],
1212 srcs: [""],
1213 stubs: { versions: ["1"] },
1214
1215 sanitize: {
1216 never: true,
1217 },
Paul Duffina02cae32021-03-09 01:44:06 +00001218 } `)
1219 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001220
1221 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1222 "lib64/bionic/libc.so",
1223 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1224 })
1225
1226 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1227
1228 installed := hwasan.Description("install libclang_rt.hwasan")
1229 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1230
1231 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1232 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1233 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1234}
1235
1236func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffina02cae32021-03-09 01:44:06 +00001237 result := emptyFixtureFactory.Extend(
1238 prepareForTestOfRuntimeApexWithHwasan,
1239 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1240 variables.SanitizeDevice = []string{"hwaddress"}
1241 }),
1242 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001243 cc_library {
1244 name: "libc",
1245 no_libcrt: true,
1246 nocrt: true,
1247 stl: "none",
1248 system_shared_libs: [],
1249 stubs: { versions: ["1"] },
1250 apex_available: ["com.android.runtime"],
1251 }
1252
1253 cc_prebuilt_library_shared {
1254 name: "libclang_rt.hwasan-aarch64-android",
1255 no_libcrt: true,
1256 nocrt: true,
1257 stl: "none",
1258 system_shared_libs: [],
1259 srcs: [""],
1260 stubs: { versions: ["1"] },
1261
1262 sanitize: {
1263 never: true,
1264 },
1265 }
Paul Duffina02cae32021-03-09 01:44:06 +00001266 `)
1267 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001268
1269 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1270 "lib64/bionic/libc.so",
1271 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1272 })
1273
1274 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1275
1276 installed := hwasan.Description("install libclang_rt.hwasan")
1277 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1278
1279 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1280 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1281 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1282}
1283
Jooyung Han61b66e92020-03-21 14:21:46 +00001284func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1285 testcases := []struct {
1286 name string
1287 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001288 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001289 shouldLink string
1290 shouldNotLink []string
1291 }{
1292 {
Jiyong Park55549df2021-02-26 23:57:23 +09001293 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001294 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001295 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001296 shouldLink: "30",
1297 shouldNotLink: []string{"29"},
1298 },
1299 {
Jiyong Park55549df2021-02-26 23:57:23 +09001300 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001301 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001302 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001303 shouldLink: "30",
1304 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001305 },
1306 }
1307 for _, tc := range testcases {
1308 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001309 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001310 apex {
1311 name: "myapex",
1312 key: "myapex.key",
1313 use_vendor: true,
1314 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001315 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001316 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001317 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001318
Jooyung Han61b66e92020-03-21 14:21:46 +00001319 apex_key {
1320 name: "myapex.key",
1321 public_key: "testkey.avbpubkey",
1322 private_key: "testkey.pem",
1323 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001324
Jooyung Han61b66e92020-03-21 14:21:46 +00001325 cc_library {
1326 name: "mylib",
1327 srcs: ["mylib.cpp"],
1328 vendor_available: true,
1329 shared_libs: ["libbar"],
1330 system_shared_libs: [],
1331 stl: "none",
1332 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001333 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001334 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001335
Jooyung Han61b66e92020-03-21 14:21:46 +00001336 cc_library {
1337 name: "libbar",
1338 srcs: ["mylib.cpp"],
1339 system_shared_libs: [],
1340 stl: "none",
1341 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001342 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001343 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001344
Jooyung Han61b66e92020-03-21 14:21:46 +00001345 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001346 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001347 symbol_file: "",
1348 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001349 `,
1350 setUseVendorAllowListForTest([]string{"myapex"}),
1351 withUnbundledBuild,
1352 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001353
Jooyung Han61b66e92020-03-21 14:21:46 +00001354 // Ensure that LLNDK dep is not included
1355 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1356 "lib64/mylib.so",
1357 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001358
Jooyung Han61b66e92020-03-21 14:21:46 +00001359 // Ensure that LLNDK dep is required
1360 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1361 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1362 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001363
Colin Crossaede88c2020-08-11 12:17:01 -07001364 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001365 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001366 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001367 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001368 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001369
Colin Crossaede88c2020-08-11 12:17:01 -07001370 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001371 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1372 })
1373 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001374}
1375
Jiyong Park25fc6a92018-11-18 18:02:45 +09001376func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001377 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001378 apex {
1379 name: "myapex",
1380 key: "myapex.key",
1381 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001382 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001383 }
1384
1385 apex_key {
1386 name: "myapex.key",
1387 public_key: "testkey.avbpubkey",
1388 private_key: "testkey.pem",
1389 }
1390
1391 cc_library {
1392 name: "mylib",
1393 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001394 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001395 shared_libs: ["libdl#27"],
1396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001397 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001398 }
1399
1400 cc_library_shared {
1401 name: "mylib_shared",
1402 srcs: ["mylib.cpp"],
1403 shared_libs: ["libdl#27"],
1404 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001405 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001406 }
1407
1408 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001409 name: "libBootstrap",
1410 srcs: ["mylib.cpp"],
1411 stl: "none",
1412 bootstrap: true,
1413 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001414 `)
1415
Sundong Ahnabb64432019-10-22 13:58:29 +09001416 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001417 copyCmds := apexRule.Args["copy_commands"]
1418
1419 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001420 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001421 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1422 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001423
1424 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001425 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001426
Colin Crossaede88c2020-08-11 12:17:01 -07001427 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1428 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1429 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001430
1431 // For dependency to libc
1432 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001433 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001434 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001435 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001436 // ... Cflags from stub is correctly exported to mylib
1437 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1438 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1439
1440 // For dependency to libm
1441 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001442 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001444 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001445 // ... and is not compiling with the stub
1446 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1447 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1448
1449 // For dependency to libdl
1450 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001451 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001453 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1454 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001455 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001456 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001457 // ... Cflags from stub is correctly exported to mylib
1458 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1459 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001460
1461 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001462 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1463 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1464 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1465 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001466}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001467
Jooyung Han749dc692020-04-15 11:03:39 +09001468func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001469 // there are three links between liba --> libz.
1470 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001471 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001472 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001473 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001474 apex {
1475 name: "myapex",
1476 key: "myapex.key",
1477 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001478 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001479 }
1480
1481 apex {
1482 name: "otherapex",
1483 key: "myapex.key",
1484 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001485 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001486 }
1487
1488 apex_key {
1489 name: "myapex.key",
1490 public_key: "testkey.avbpubkey",
1491 private_key: "testkey.pem",
1492 }
1493
1494 cc_library {
1495 name: "libx",
1496 shared_libs: ["liba"],
1497 system_shared_libs: [],
1498 stl: "none",
1499 apex_available: [ "myapex" ],
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: "liby",
1505 shared_libs: ["liba"],
1506 system_shared_libs: [],
1507 stl: "none",
1508 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001509 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001510 }
1511
1512 cc_library {
1513 name: "liba",
1514 shared_libs: ["libz"],
1515 system_shared_libs: [],
1516 stl: "none",
1517 apex_available: [
1518 "//apex_available:anyapex",
1519 "//apex_available:platform",
1520 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001521 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001522 }
1523
1524 cc_library {
1525 name: "libz",
1526 system_shared_libs: [],
1527 stl: "none",
1528 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001529 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001530 },
1531 }
Jooyung Han749dc692020-04-15 11:03:39 +09001532 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001533
1534 expectLink := func(from, from_variant, to, to_variant string) {
1535 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1536 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1537 }
1538 expectNoLink := func(from, from_variant, to, to_variant string) {
1539 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1540 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1541 }
1542 // platform liba is linked to non-stub version
1543 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001544 // liba in myapex is linked to #30
1545 expectLink("liba", "shared_apex29", "libz", "shared_30")
1546 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001547 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001548 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001549 expectLink("liba", "shared_apex30", "libz", "shared_30")
1550 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1551 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001552}
1553
Jooyung Hanaed150d2020-04-02 01:41:41 +09001554func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001555 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001556 apex {
1557 name: "myapex",
1558 key: "myapex.key",
1559 native_shared_libs: ["libx"],
1560 min_sdk_version: "R",
1561 }
1562
1563 apex_key {
1564 name: "myapex.key",
1565 public_key: "testkey.avbpubkey",
1566 private_key: "testkey.pem",
1567 }
1568
1569 cc_library {
1570 name: "libx",
1571 shared_libs: ["libz"],
1572 system_shared_libs: [],
1573 stl: "none",
1574 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001575 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001576 }
1577
1578 cc_library {
1579 name: "libz",
1580 system_shared_libs: [],
1581 stl: "none",
1582 stubs: {
1583 versions: ["29", "R"],
1584 },
1585 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001586 `,
1587 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1588 variables.Platform_version_active_codenames = []string{"R"}
1589 }),
1590 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001591
1592 expectLink := func(from, from_variant, to, to_variant string) {
1593 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1594 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1595 }
1596 expectNoLink := func(from, from_variant, to, to_variant string) {
1597 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1598 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1599 }
Dan Albertc8060532020-07-22 22:32:17 -07001600 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001601 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1602 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001603}
1604
Jooyung Han749dc692020-04-15 11:03:39 +09001605func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001606 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001607 apex {
1608 name: "myapex",
1609 key: "myapex.key",
1610 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001611 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001612 }
1613
1614 apex_key {
1615 name: "myapex.key",
1616 public_key: "testkey.avbpubkey",
1617 private_key: "testkey.pem",
1618 }
1619
1620 cc_library {
1621 name: "libx",
1622 shared_libs: ["libz"],
1623 system_shared_libs: [],
1624 stl: "none",
1625 apex_available: [ "myapex" ],
1626 }
1627
1628 cc_library {
1629 name: "libz",
1630 system_shared_libs: [],
1631 stl: "none",
1632 stubs: {
1633 versions: ["1", "2"],
1634 },
1635 }
1636 `)
1637
1638 expectLink := func(from, from_variant, to, to_variant string) {
1639 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1640 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1641 }
1642 expectNoLink := func(from, from_variant, to, to_variant string) {
1643 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1644 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1645 }
Colin Crossaede88c2020-08-11 12:17:01 -07001646 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1647 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1648 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001649}
1650
1651func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001652 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001653 apex {
1654 name: "myapex",
1655 key: "myapex.key",
1656 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001657 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001658 }
1659
1660 apex_key {
1661 name: "myapex.key",
1662 public_key: "testkey.avbpubkey",
1663 private_key: "testkey.pem",
1664 }
1665
1666 cc_library {
1667 name: "libx",
1668 system_shared_libs: [],
1669 stl: "none",
1670 apex_available: [ "myapex" ],
1671 stubs: {
1672 versions: ["1", "2"],
1673 },
1674 }
1675
1676 cc_library {
1677 name: "libz",
1678 shared_libs: ["libx"],
1679 system_shared_libs: [],
1680 stl: "none",
1681 }
1682 `)
1683
1684 expectLink := 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 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1688 }
1689 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001690 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001691 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1692 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1693 }
1694 expectLink("libz", "shared", "libx", "shared_2")
1695 expectNoLink("libz", "shared", "libz", "shared_1")
1696 expectNoLink("libz", "shared", "libz", "shared")
1697}
1698
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001699var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1700 func(variables android.FixtureProductVariables) {
1701 variables.SanitizeDevice = []string{"hwaddress"}
1702 },
1703)
1704
Jooyung Han75568392020-03-20 04:29:24 +09001705func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001706 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001707 apex {
1708 name: "myapex",
1709 key: "myapex.key",
1710 native_shared_libs: ["libx"],
1711 min_sdk_version: "29",
1712 }
1713
1714 apex_key {
1715 name: "myapex.key",
1716 public_key: "testkey.avbpubkey",
1717 private_key: "testkey.pem",
1718 }
1719
1720 cc_library {
1721 name: "libx",
1722 shared_libs: ["libbar"],
1723 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001724 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001725 }
1726
1727 cc_library {
1728 name: "libbar",
1729 stubs: {
1730 versions: ["29", "30"],
1731 },
1732 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001733 `,
1734 prepareForTestWithSantitizeHwaddress,
1735 )
Jooyung Han03b51852020-02-26 22:45:42 +09001736 expectLink := func(from, from_variant, to, to_variant string) {
1737 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1738 libFlags := ld.Args["libFlags"]
1739 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1740 }
Colin Crossaede88c2020-08-11 12:17:01 -07001741 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001742}
1743
Jooyung Han75568392020-03-20 04:29:24 +09001744func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001745 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001746 apex {
1747 name: "myapex",
1748 key: "myapex.key",
1749 native_shared_libs: ["libx"],
1750 min_sdk_version: "29",
1751 }
1752
1753 apex_key {
1754 name: "myapex.key",
1755 public_key: "testkey.avbpubkey",
1756 private_key: "testkey.pem",
1757 }
1758
1759 cc_library {
1760 name: "libx",
1761 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001762 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001763 }
Jooyung Han75568392020-03-20 04:29:24 +09001764 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001765
1766 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001767 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001768 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001769 // note that platform variant is not.
1770 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001771 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001772}
1773
Jooyung Han749dc692020-04-15 11:03:39 +09001774func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1775 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001776 apex {
1777 name: "myapex",
1778 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001779 native_shared_libs: ["mylib"],
1780 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001781 }
1782
1783 apex_key {
1784 name: "myapex.key",
1785 public_key: "testkey.avbpubkey",
1786 private_key: "testkey.pem",
1787 }
Jooyung Han749dc692020-04-15 11:03:39 +09001788
1789 cc_library {
1790 name: "mylib",
1791 srcs: ["mylib.cpp"],
1792 system_shared_libs: [],
1793 stl: "none",
1794 apex_available: [
1795 "myapex",
1796 ],
1797 min_sdk_version: "30",
1798 }
1799 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001800
1801 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1802 apex {
1803 name: "myapex",
1804 key: "myapex.key",
1805 native_shared_libs: ["libfoo.ffi"],
1806 min_sdk_version: "29",
1807 }
1808
1809 apex_key {
1810 name: "myapex.key",
1811 public_key: "testkey.avbpubkey",
1812 private_key: "testkey.pem",
1813 }
1814
1815 rust_ffi_shared {
1816 name: "libfoo.ffi",
1817 srcs: ["foo.rs"],
1818 crate_name: "foo",
1819 apex_available: [
1820 "myapex",
1821 ],
1822 min_sdk_version: "30",
1823 }
1824 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001825}
1826
1827func TestApexMinSdkVersion_Okay(t *testing.T) {
1828 testApex(t, `
1829 apex {
1830 name: "myapex",
1831 key: "myapex.key",
1832 native_shared_libs: ["libfoo"],
1833 java_libs: ["libbar"],
1834 min_sdk_version: "29",
1835 }
1836
1837 apex_key {
1838 name: "myapex.key",
1839 public_key: "testkey.avbpubkey",
1840 private_key: "testkey.pem",
1841 }
1842
1843 cc_library {
1844 name: "libfoo",
1845 srcs: ["mylib.cpp"],
1846 shared_libs: ["libfoo_dep"],
1847 apex_available: ["myapex"],
1848 min_sdk_version: "29",
1849 }
1850
1851 cc_library {
1852 name: "libfoo_dep",
1853 srcs: ["mylib.cpp"],
1854 apex_available: ["myapex"],
1855 min_sdk_version: "29",
1856 }
1857
1858 java_library {
1859 name: "libbar",
1860 sdk_version: "current",
1861 srcs: ["a.java"],
1862 static_libs: ["libbar_dep"],
1863 apex_available: ["myapex"],
1864 min_sdk_version: "29",
1865 }
1866
1867 java_library {
1868 name: "libbar_dep",
1869 sdk_version: "current",
1870 srcs: ["a.java"],
1871 apex_available: ["myapex"],
1872 min_sdk_version: "29",
1873 }
Jooyung Han03b51852020-02-26 22:45:42 +09001874 `)
1875}
1876
Artur Satayev8cf899a2020-04-15 17:29:42 +01001877func TestJavaStableSdkVersion(t *testing.T) {
1878 testCases := []struct {
1879 name string
1880 expectedError string
1881 bp string
1882 }{
1883 {
1884 name: "Non-updatable apex with non-stable dep",
1885 bp: `
1886 apex {
1887 name: "myapex",
1888 java_libs: ["myjar"],
1889 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001890 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001891 }
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897 java_library {
1898 name: "myjar",
1899 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001900 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001901 apex_available: ["myapex"],
1902 }
1903 `,
1904 },
1905 {
1906 name: "Updatable apex with stable dep",
1907 bp: `
1908 apex {
1909 name: "myapex",
1910 java_libs: ["myjar"],
1911 key: "myapex.key",
1912 updatable: true,
1913 min_sdk_version: "29",
1914 }
1915 apex_key {
1916 name: "myapex.key",
1917 public_key: "testkey.avbpubkey",
1918 private_key: "testkey.pem",
1919 }
1920 java_library {
1921 name: "myjar",
1922 srcs: ["foo/bar/MyClass.java"],
1923 sdk_version: "current",
1924 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001925 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001926 }
1927 `,
1928 },
1929 {
1930 name: "Updatable apex with non-stable dep",
1931 expectedError: "cannot depend on \"myjar\"",
1932 bp: `
1933 apex {
1934 name: "myapex",
1935 java_libs: ["myjar"],
1936 key: "myapex.key",
1937 updatable: true,
1938 }
1939 apex_key {
1940 name: "myapex.key",
1941 public_key: "testkey.avbpubkey",
1942 private_key: "testkey.pem",
1943 }
1944 java_library {
1945 name: "myjar",
1946 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001947 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001948 apex_available: ["myapex"],
1949 }
1950 `,
1951 },
1952 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001953 name: "Updatable apex with non-stable transitive dep",
1954 // This is not actually detecting that the transitive dependency is unstable, rather it is
1955 // detecting that the transitive dependency is building against a wider API surface than the
1956 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001957 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001958 bp: `
1959 apex {
1960 name: "myapex",
1961 java_libs: ["myjar"],
1962 key: "myapex.key",
1963 updatable: true,
1964 }
1965 apex_key {
1966 name: "myapex.key",
1967 public_key: "testkey.avbpubkey",
1968 private_key: "testkey.pem",
1969 }
1970 java_library {
1971 name: "myjar",
1972 srcs: ["foo/bar/MyClass.java"],
1973 sdk_version: "current",
1974 apex_available: ["myapex"],
1975 static_libs: ["transitive-jar"],
1976 }
1977 java_library {
1978 name: "transitive-jar",
1979 srcs: ["foo/bar/MyClass.java"],
1980 sdk_version: "core_platform",
1981 apex_available: ["myapex"],
1982 }
1983 `,
1984 },
1985 }
1986
1987 for _, test := range testCases {
1988 t.Run(test.name, func(t *testing.T) {
1989 if test.expectedError == "" {
1990 testApex(t, test.bp)
1991 } else {
1992 testApexError(t, test.expectedError, test.bp)
1993 }
1994 })
1995 }
1996}
1997
Jooyung Han749dc692020-04-15 11:03:39 +09001998func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1999 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2000 apex {
2001 name: "myapex",
2002 key: "myapex.key",
2003 native_shared_libs: ["mylib"],
2004 min_sdk_version: "29",
2005 }
2006
2007 apex_key {
2008 name: "myapex.key",
2009 public_key: "testkey.avbpubkey",
2010 private_key: "testkey.pem",
2011 }
2012
2013 cc_library {
2014 name: "mylib",
2015 srcs: ["mylib.cpp"],
2016 shared_libs: ["mylib2"],
2017 system_shared_libs: [],
2018 stl: "none",
2019 apex_available: [
2020 "myapex",
2021 ],
2022 min_sdk_version: "29",
2023 }
2024
2025 // indirect part of the apex
2026 cc_library {
2027 name: "mylib2",
2028 srcs: ["mylib.cpp"],
2029 system_shared_libs: [],
2030 stl: "none",
2031 apex_available: [
2032 "myapex",
2033 ],
2034 min_sdk_version: "30",
2035 }
2036 `)
2037}
2038
2039func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2040 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2041 apex {
2042 name: "myapex",
2043 key: "myapex.key",
2044 apps: ["AppFoo"],
2045 min_sdk_version: "29",
2046 }
2047
2048 apex_key {
2049 name: "myapex.key",
2050 public_key: "testkey.avbpubkey",
2051 private_key: "testkey.pem",
2052 }
2053
2054 android_app {
2055 name: "AppFoo",
2056 srcs: ["foo/bar/MyClass.java"],
2057 sdk_version: "current",
2058 min_sdk_version: "29",
2059 system_modules: "none",
2060 stl: "none",
2061 static_libs: ["bar"],
2062 apex_available: [ "myapex" ],
2063 }
2064
2065 java_library {
2066 name: "bar",
2067 sdk_version: "current",
2068 srcs: ["a.java"],
2069 apex_available: [ "myapex" ],
2070 }
2071 `)
2072}
2073
2074func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002075 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002076 apex {
2077 name: "myapex",
2078 key: "myapex.key",
2079 native_shared_libs: ["mylib"],
2080 min_sdk_version: "29",
2081 }
2082
2083 apex_key {
2084 name: "myapex.key",
2085 public_key: "testkey.avbpubkey",
2086 private_key: "testkey.pem",
2087 }
2088
Jiyong Park55549df2021-02-26 23:57:23 +09002089 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002090 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2091 cc_library {
2092 name: "mylib",
2093 srcs: ["mylib.cpp"],
2094 shared_libs: ["mylib2"],
2095 system_shared_libs: [],
2096 stl: "none",
2097 apex_available: ["myapex", "otherapex"],
2098 min_sdk_version: "29",
2099 }
2100
2101 cc_library {
2102 name: "mylib2",
2103 srcs: ["mylib.cpp"],
2104 system_shared_libs: [],
2105 stl: "none",
2106 apex_available: ["otherapex"],
2107 stubs: { versions: ["29", "30"] },
2108 min_sdk_version: "30",
2109 }
2110
2111 apex {
2112 name: "otherapex",
2113 key: "myapex.key",
2114 native_shared_libs: ["mylib", "mylib2"],
2115 min_sdk_version: "30",
2116 }
2117 `)
2118 expectLink := func(from, from_variant, to, to_variant string) {
2119 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2120 libFlags := ld.Args["libFlags"]
2121 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2122 }
Jiyong Park55549df2021-02-26 23:57:23 +09002123 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002124 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002125}
2126
Jooyung Haned124c32021-01-26 11:43:46 +09002127func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002128 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2129 func(variables android.FixtureProductVariables) {
2130 variables.Platform_sdk_codename = proptools.StringPtr("S")
2131 variables.Platform_version_active_codenames = []string{"S"}
2132 },
2133 )
Jooyung Haned124c32021-01-26 11:43:46 +09002134 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo"],
2139 min_sdk_version: "S",
2140 }
2141 apex_key {
2142 name: "myapex.key",
2143 public_key: "testkey.avbpubkey",
2144 private_key: "testkey.pem",
2145 }
2146 cc_library {
2147 name: "libfoo",
2148 shared_libs: ["libbar"],
2149 apex_available: ["myapex"],
2150 min_sdk_version: "29",
2151 }
2152 cc_library {
2153 name: "libbar",
2154 apex_available: ["myapex"],
2155 }
2156 `, withSAsActiveCodeNames)
2157}
2158
2159func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002160 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2161 variables.Platform_sdk_codename = proptools.StringPtr("S")
2162 variables.Platform_version_active_codenames = []string{"S", "T"}
2163 })
Colin Cross1c460562021-02-16 17:55:47 -08002164 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002165 apex {
2166 name: "myapex",
2167 key: "myapex.key",
2168 native_shared_libs: ["libfoo"],
2169 min_sdk_version: "S",
2170 }
2171 apex_key {
2172 name: "myapex.key",
2173 public_key: "testkey.avbpubkey",
2174 private_key: "testkey.pem",
2175 }
2176 cc_library {
2177 name: "libfoo",
2178 shared_libs: ["libbar"],
2179 apex_available: ["myapex"],
2180 min_sdk_version: "S",
2181 }
2182 cc_library {
2183 name: "libbar",
2184 stubs: {
2185 symbol_file: "libbar.map.txt",
2186 versions: ["30", "S", "T"],
2187 },
2188 }
2189 `, withSAsActiveCodeNames)
2190
2191 // ensure libfoo is linked with "S" version of libbar stub
2192 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2193 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002194 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002195}
2196
Jiyong Park7c2ee712018-12-07 00:42:25 +09002197func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002198 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002199 apex {
2200 name: "myapex",
2201 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002202 native_shared_libs: ["mylib"],
2203 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002204 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002205 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002206 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002207 }
2208
2209 apex_key {
2210 name: "myapex.key",
2211 public_key: "testkey.avbpubkey",
2212 private_key: "testkey.pem",
2213 }
2214
2215 prebuilt_etc {
2216 name: "myetc",
2217 src: "myprebuilt",
2218 sub_dir: "foo/bar",
2219 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002220
2221 cc_library {
2222 name: "mylib",
2223 srcs: ["mylib.cpp"],
2224 relative_install_path: "foo/bar",
2225 system_shared_libs: [],
2226 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002227 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002228 }
2229
2230 cc_binary {
2231 name: "mybin",
2232 srcs: ["mylib.cpp"],
2233 relative_install_path: "foo/bar",
2234 system_shared_libs: [],
2235 static_executable: true,
2236 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002237 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002238 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002239 `)
2240
Sundong Ahnabb64432019-10-22 13:58:29 +09002241 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002242 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2243
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002244 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002245 ensureListContains(t, dirs, "etc")
2246 ensureListContains(t, dirs, "etc/foo")
2247 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002248 ensureListContains(t, dirs, "lib64")
2249 ensureListContains(t, dirs, "lib64/foo")
2250 ensureListContains(t, dirs, "lib64/foo/bar")
2251 ensureListContains(t, dirs, "lib")
2252 ensureListContains(t, dirs, "lib/foo")
2253 ensureListContains(t, dirs, "lib/foo/bar")
2254
Jiyong Parkbd13e442019-03-15 18:10:35 +09002255 ensureListContains(t, dirs, "bin")
2256 ensureListContains(t, dirs, "bin/foo")
2257 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002258}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002259
Jooyung Han35155c42020-02-06 17:33:20 +09002260func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002261 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002262 apex {
2263 name: "myapex",
2264 key: "myapex.key",
2265 multilib: {
2266 both: {
2267 native_shared_libs: ["mylib"],
2268 binaries: ["mybin"],
2269 },
2270 },
2271 compile_multilib: "both",
2272 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002273 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002274 }
2275
2276 apex_key {
2277 name: "myapex.key",
2278 public_key: "testkey.avbpubkey",
2279 private_key: "testkey.pem",
2280 }
2281
2282 cc_library {
2283 name: "mylib",
2284 relative_install_path: "foo/bar",
2285 system_shared_libs: [],
2286 stl: "none",
2287 apex_available: [ "myapex" ],
2288 native_bridge_supported: true,
2289 }
2290
2291 cc_binary {
2292 name: "mybin",
2293 relative_install_path: "foo/bar",
2294 system_shared_libs: [],
2295 static_executable: true,
2296 stl: "none",
2297 apex_available: [ "myapex" ],
2298 native_bridge_supported: true,
2299 compile_multilib: "both", // default is "first" for binary
2300 multilib: {
2301 lib64: {
2302 suffix: "64",
2303 },
2304 },
2305 }
2306 `, withNativeBridgeEnabled)
2307 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2308 "bin/foo/bar/mybin",
2309 "bin/foo/bar/mybin64",
2310 "bin/arm/foo/bar/mybin",
2311 "bin/arm64/foo/bar/mybin64",
2312 "lib/foo/bar/mylib.so",
2313 "lib/arm/foo/bar/mylib.so",
2314 "lib64/foo/bar/mylib.so",
2315 "lib64/arm64/foo/bar/mylib.so",
2316 })
2317}
2318
Jiyong Parkda6eb592018-12-19 17:12:36 +09002319func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002320 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002321 apex {
2322 name: "myapex",
2323 key: "myapex.key",
2324 native_shared_libs: ["mylib"],
2325 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002326 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002327 }
2328
2329 apex_key {
2330 name: "myapex.key",
2331 public_key: "testkey.avbpubkey",
2332 private_key: "testkey.pem",
2333 }
2334
2335 cc_library {
2336 name: "mylib",
2337 srcs: ["mylib.cpp"],
2338 shared_libs: ["mylib2"],
2339 system_shared_libs: [],
2340 vendor_available: true,
2341 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002342 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002343 }
2344
2345 cc_library {
2346 name: "mylib2",
2347 srcs: ["mylib.cpp"],
2348 system_shared_libs: [],
2349 vendor_available: true,
2350 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002351 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002352 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002353 `,
2354 setUseVendorAllowListForTest([]string{"myapex"}),
2355 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002356
2357 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002358 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002359 for _, implicit := range i.Implicits {
2360 inputsList = append(inputsList, implicit.String())
2361 }
2362 }
2363 inputsString := strings.Join(inputsList, " ")
2364
2365 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002366 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2367 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002368
2369 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002370 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2371 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002372}
Jiyong Park16e91a02018-12-20 18:18:08 +09002373
Jooyung Han85d61762020-06-24 23:50:26 +09002374func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002375 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2376 apex {
2377 name: "myapex",
2378 key: "myapex.key",
2379 use_vendor: true,
2380 }
2381 apex_key {
2382 name: "myapex.key",
2383 public_key: "testkey.avbpubkey",
2384 private_key: "testkey.pem",
2385 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002386 `,
2387 setUseVendorAllowListForTest([]string{""}),
2388 )
Colin Cross440e0d02020-06-11 11:32:11 -07002389 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002390 testApex(t, `
2391 apex {
2392 name: "myapex",
2393 key: "myapex.key",
2394 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002395 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002396 }
2397 apex_key {
2398 name: "myapex.key",
2399 public_key: "testkey.avbpubkey",
2400 private_key: "testkey.pem",
2401 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002402 `,
2403 setUseVendorAllowListForTest([]string{"myapex"}),
2404 )
Jooyung Handc782442019-11-01 03:14:38 +09002405}
2406
Jooyung Han5c998b92019-06-27 11:30:33 +09002407func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2408 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2409 apex {
2410 name: "myapex",
2411 key: "myapex.key",
2412 native_shared_libs: ["mylib"],
2413 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002414 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002415 }
2416
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422
2423 cc_library {
2424 name: "mylib",
2425 srcs: ["mylib.cpp"],
2426 system_shared_libs: [],
2427 stl: "none",
2428 }
2429 `)
2430}
2431
Jooyung Han85d61762020-06-24 23:50:26 +09002432func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002433 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002434 apex {
2435 name: "myapex",
2436 key: "myapex.key",
2437 binaries: ["mybin"],
2438 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002439 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002440 }
2441 apex_key {
2442 name: "myapex.key",
2443 public_key: "testkey.avbpubkey",
2444 private_key: "testkey.pem",
2445 }
2446 cc_binary {
2447 name: "mybin",
2448 vendor: true,
2449 shared_libs: ["libfoo"],
2450 }
2451 cc_library {
2452 name: "libfoo",
2453 proprietary: true,
2454 }
2455 `)
2456
2457 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2458 "bin/mybin",
2459 "lib64/libfoo.so",
2460 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2461 "lib64/libc++.so",
2462 })
2463
2464 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002465 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002466 name := apexBundle.BaseModuleName()
2467 prefix := "TARGET_"
2468 var builder strings.Builder
2469 data.Custom(&builder, name, prefix, "", data)
2470 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002471 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2472 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002473
2474 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2475 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2476 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002477}
2478
Jooyung Handf78e212020-07-22 15:54:47 +09002479func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002480 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002481 apex {
2482 name: "myapex",
2483 key: "myapex.key",
2484 binaries: ["mybin"],
2485 vendor: true,
2486 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002487 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002488 }
2489 apex_key {
2490 name: "myapex.key",
2491 public_key: "testkey.avbpubkey",
2492 private_key: "testkey.pem",
2493 }
2494 cc_binary {
2495 name: "mybin",
2496 vendor: true,
2497 shared_libs: ["libvndk", "libvendor"],
2498 }
2499 cc_library {
2500 name: "libvndk",
2501 vndk: {
2502 enabled: true,
2503 },
2504 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002505 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002506 }
2507 cc_library {
2508 name: "libvendor",
2509 vendor: true,
2510 }
2511 `)
2512
2513 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2514
Colin Crossaede88c2020-08-11 12:17:01 -07002515 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002516 libs := names(ldRule.Args["libFlags"])
2517 // VNDK libs(libvndk/libc++) as they are
2518 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002519 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002520 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002521 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002522
2523 // VNDK libs are not included when use_vndk_as_stable: true
2524 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2525 "bin/mybin",
2526 "lib64/libvendor.so",
2527 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002528
2529 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2530 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2531 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002532}
2533
Justin Yun13decfb2021-03-08 19:25:55 +09002534func TestProductVariant(t *testing.T) {
2535 ctx := testApex(t, `
2536 apex {
2537 name: "myapex",
2538 key: "myapex.key",
2539 updatable: false,
2540 product_specific: true,
2541 binaries: ["foo"],
2542 }
2543
2544 apex_key {
2545 name: "myapex.key",
2546 public_key: "testkey.avbpubkey",
2547 private_key: "testkey.pem",
2548 }
2549
2550 cc_binary {
2551 name: "foo",
2552 product_available: true,
2553 apex_available: ["myapex"],
2554 srcs: ["foo.cpp"],
2555 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002556 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2557 variables.ProductVndkVersion = proptools.StringPtr("current")
2558 }),
2559 )
Justin Yun13decfb2021-03-08 19:25:55 +09002560
2561 cflags := strings.Fields(
2562 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2563 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2564 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2565 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2566 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2567}
2568
Jooyung Han8e5685d2020-09-21 11:02:57 +09002569func TestApex_withPrebuiltFirmware(t *testing.T) {
2570 testCases := []struct {
2571 name string
2572 additionalProp string
2573 }{
2574 {"system apex with prebuilt_firmware", ""},
2575 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2576 }
2577 for _, tc := range testCases {
2578 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002579 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002580 apex {
2581 name: "myapex",
2582 key: "myapex.key",
2583 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002584 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002585 `+tc.additionalProp+`
2586 }
2587 apex_key {
2588 name: "myapex.key",
2589 public_key: "testkey.avbpubkey",
2590 private_key: "testkey.pem",
2591 }
2592 prebuilt_firmware {
2593 name: "myfirmware",
2594 src: "myfirmware.bin",
2595 filename_from_src: true,
2596 `+tc.additionalProp+`
2597 }
2598 `)
2599 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2600 "etc/firmware/myfirmware.bin",
2601 })
2602 })
2603 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002604}
2605
Jooyung Hanefb184e2020-06-25 17:14:25 +09002606func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002607 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002608 apex {
2609 name: "myapex",
2610 key: "myapex.key",
2611 use_vendor: true,
2612 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002613 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002614 }
2615
2616 apex_key {
2617 name: "myapex.key",
2618 public_key: "testkey.avbpubkey",
2619 private_key: "testkey.pem",
2620 }
2621
2622 cc_library {
2623 name: "mylib",
2624 vendor_available: true,
2625 apex_available: ["myapex"],
2626 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002627 `,
2628 setUseVendorAllowListForTest([]string{"myapex"}),
2629 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002630
2631 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002632 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002633 name := apexBundle.BaseModuleName()
2634 prefix := "TARGET_"
2635 var builder strings.Builder
2636 data.Custom(&builder, name, prefix, "", data)
2637 androidMk := builder.String()
2638 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2639}
2640
2641func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002642 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002643 apex {
2644 name: "myapex",
2645 key: "myapex.key",
2646 vendor: true,
2647 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002648 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002649 }
2650
2651 apex_key {
2652 name: "myapex.key",
2653 public_key: "testkey.avbpubkey",
2654 private_key: "testkey.pem",
2655 }
2656
2657 cc_library {
2658 name: "mylib",
2659 vendor_available: true,
2660 }
2661 `)
2662
2663 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002664 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002665 name := apexBundle.BaseModuleName()
2666 prefix := "TARGET_"
2667 var builder strings.Builder
2668 data.Custom(&builder, name, prefix, "", data)
2669 androidMk := builder.String()
2670 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2671}
2672
Jooyung Han2ed99d02020-06-24 23:26:26 +09002673func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002674 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002675 apex {
2676 name: "myapex",
2677 key: "myapex.key",
2678 vintf_fragments: ["fragment.xml"],
2679 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002680 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002681 }
2682 apex_key {
2683 name: "myapex.key",
2684 public_key: "testkey.avbpubkey",
2685 private_key: "testkey.pem",
2686 }
2687 cc_binary {
2688 name: "mybin",
2689 }
2690 `)
2691
2692 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002693 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002694 name := apexBundle.BaseModuleName()
2695 prefix := "TARGET_"
2696 var builder strings.Builder
2697 data.Custom(&builder, name, prefix, "", data)
2698 androidMk := builder.String()
2699 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2700 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2701}
2702
Jiyong Park16e91a02018-12-20 18:18:08 +09002703func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002704 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002709 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002710 }
2711
2712 apex_key {
2713 name: "myapex.key",
2714 public_key: "testkey.avbpubkey",
2715 private_key: "testkey.pem",
2716 }
2717
2718 cc_library {
2719 name: "mylib",
2720 srcs: ["mylib.cpp"],
2721 system_shared_libs: [],
2722 stl: "none",
2723 stubs: {
2724 versions: ["1", "2", "3"],
2725 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002726 apex_available: [
2727 "//apex_available:platform",
2728 "myapex",
2729 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002730 }
2731
2732 cc_binary {
2733 name: "not_in_apex",
2734 srcs: ["mylib.cpp"],
2735 static_libs: ["mylib"],
2736 static_executable: true,
2737 system_shared_libs: [],
2738 stl: "none",
2739 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002740 `)
2741
Colin Cross7113d202019-11-20 16:39:12 -08002742 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002743
2744 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002745 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002746}
Jiyong Park9335a262018-12-24 11:31:58 +09002747
2748func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002749 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002750 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002751 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002752 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002753 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002754 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002755 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002756 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002757 }
2758
2759 cc_library {
2760 name: "mylib",
2761 srcs: ["mylib.cpp"],
2762 system_shared_libs: [],
2763 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002764 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002765 }
2766
2767 apex_key {
2768 name: "myapex.key",
2769 public_key: "testkey.avbpubkey",
2770 private_key: "testkey.pem",
2771 }
2772
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002773 android_app_certificate {
2774 name: "myapex.certificate",
2775 certificate: "testkey",
2776 }
2777
2778 android_app_certificate {
2779 name: "myapex.certificate.override",
2780 certificate: "testkey.override",
2781 }
2782
Jiyong Park9335a262018-12-24 11:31:58 +09002783 `)
2784
2785 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002786 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002787
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002788 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2789 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002790 "vendor/foo/devkeys/testkey.avbpubkey")
2791 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002792 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2793 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002794 "vendor/foo/devkeys/testkey.pem")
2795 }
2796
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002797 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002798 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002799 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002800 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002801 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002802 }
2803}
Jiyong Park58e364a2019-01-19 19:24:06 +09002804
Jooyung Hanf121a652019-12-17 14:30:11 +09002805func TestCertificate(t *testing.T) {
2806 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002807 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002808 apex {
2809 name: "myapex",
2810 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002811 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002812 }
2813 apex_key {
2814 name: "myapex.key",
2815 public_key: "testkey.avbpubkey",
2816 private_key: "testkey.pem",
2817 }`)
2818 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2819 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2820 if actual := rule.Args["certificates"]; actual != expected {
2821 t.Errorf("certificates should be %q, not %q", expected, actual)
2822 }
2823 })
2824 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002825 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002826 apex {
2827 name: "myapex_keytest",
2828 key: "myapex.key",
2829 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002830 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002831 }
2832 apex_key {
2833 name: "myapex.key",
2834 public_key: "testkey.avbpubkey",
2835 private_key: "testkey.pem",
2836 }
2837 android_app_certificate {
2838 name: "myapex.certificate.override",
2839 certificate: "testkey.override",
2840 }`)
2841 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2842 expected := "testkey.override.x509.pem testkey.override.pk8"
2843 if actual := rule.Args["certificates"]; actual != expected {
2844 t.Errorf("certificates should be %q, not %q", expected, actual)
2845 }
2846 })
2847 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002848 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002849 apex {
2850 name: "myapex",
2851 key: "myapex.key",
2852 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002853 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002854 }
2855 apex_key {
2856 name: "myapex.key",
2857 public_key: "testkey.avbpubkey",
2858 private_key: "testkey.pem",
2859 }
2860 android_app_certificate {
2861 name: "myapex.certificate",
2862 certificate: "testkey",
2863 }`)
2864 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2865 expected := "testkey.x509.pem testkey.pk8"
2866 if actual := rule.Args["certificates"]; actual != expected {
2867 t.Errorf("certificates should be %q, not %q", expected, actual)
2868 }
2869 })
2870 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002871 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002872 apex {
2873 name: "myapex_keytest",
2874 key: "myapex.key",
2875 file_contexts: ":myapex-file_contexts",
2876 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002877 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002878 }
2879 apex_key {
2880 name: "myapex.key",
2881 public_key: "testkey.avbpubkey",
2882 private_key: "testkey.pem",
2883 }
2884 android_app_certificate {
2885 name: "myapex.certificate.override",
2886 certificate: "testkey.override",
2887 }`)
2888 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2889 expected := "testkey.override.x509.pem testkey.override.pk8"
2890 if actual := rule.Args["certificates"]; actual != expected {
2891 t.Errorf("certificates should be %q, not %q", expected, actual)
2892 }
2893 })
2894 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002895 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002896 apex {
2897 name: "myapex",
2898 key: "myapex.key",
2899 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002900 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002901 }
2902 apex_key {
2903 name: "myapex.key",
2904 public_key: "testkey.avbpubkey",
2905 private_key: "testkey.pem",
2906 }`)
2907 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2908 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2909 if actual := rule.Args["certificates"]; actual != expected {
2910 t.Errorf("certificates should be %q, not %q", expected, actual)
2911 }
2912 })
2913 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002914 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002915 apex {
2916 name: "myapex_keytest",
2917 key: "myapex.key",
2918 file_contexts: ":myapex-file_contexts",
2919 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002920 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002921 }
2922 apex_key {
2923 name: "myapex.key",
2924 public_key: "testkey.avbpubkey",
2925 private_key: "testkey.pem",
2926 }
2927 android_app_certificate {
2928 name: "myapex.certificate.override",
2929 certificate: "testkey.override",
2930 }`)
2931 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2932 expected := "testkey.override.x509.pem testkey.override.pk8"
2933 if actual := rule.Args["certificates"]; actual != expected {
2934 t.Errorf("certificates should be %q, not %q", expected, actual)
2935 }
2936 })
2937}
2938
Jiyong Park58e364a2019-01-19 19:24:06 +09002939func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002940 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002941 apex {
2942 name: "myapex",
2943 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002944 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002945 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002946 }
2947
2948 apex {
2949 name: "otherapex",
2950 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002951 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002952 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002953 }
2954
2955 apex_key {
2956 name: "myapex.key",
2957 public_key: "testkey.avbpubkey",
2958 private_key: "testkey.pem",
2959 }
2960
2961 cc_library {
2962 name: "mylib",
2963 srcs: ["mylib.cpp"],
2964 system_shared_libs: [],
2965 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002966 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002967 "myapex",
2968 "otherapex",
2969 ],
Jooyung Han24282772020-03-21 23:20:55 +09002970 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002971 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002972 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002973 cc_library {
2974 name: "mylib2",
2975 srcs: ["mylib.cpp"],
2976 system_shared_libs: [],
2977 stl: "none",
2978 apex_available: [
2979 "myapex",
2980 "otherapex",
2981 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002982 static_libs: ["mylib3"],
2983 recovery_available: true,
2984 min_sdk_version: "29",
2985 }
2986 cc_library {
2987 name: "mylib3",
2988 srcs: ["mylib.cpp"],
2989 system_shared_libs: [],
2990 stl: "none",
2991 apex_available: [
2992 "myapex",
2993 "otherapex",
2994 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002995 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002996 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002997 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002998 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002999 `)
3000
Jooyung Hanc87a0592020-03-02 17:44:33 +09003001 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003002 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003003 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003004 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003005
Jooyung Hanccce2f22020-03-07 03:45:53 +09003006 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003007 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003008 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003009 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003010 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003011
Jooyung Hanccce2f22020-03-07 03:45:53 +09003012 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003013 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003014 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003015 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003016 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003017
Colin Crossaede88c2020-08-11 12:17:01 -07003018 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3019 // each variant defines additional macros to distinguish which apex variant it is built for
3020
3021 // non-APEX variant does not have __ANDROID_APEX__ defined
3022 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3023 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3024
3025 // APEX variant has __ANDROID_APEX__ defined
3026 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3027 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3028 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3029 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3030
3031 // APEX variant has __ANDROID_APEX__ defined
3032 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3033 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3034 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3035 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3036
Dan Albertb19953d2020-11-17 15:29:36 -08003037 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003038 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3039 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003040 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003041
3042 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3043 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003044
3045 // non-APEX variant does not have __ANDROID_APEX__ defined
3046 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3047 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3048
3049 // APEX variant has __ANDROID_APEX__ defined
3050 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003051 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003052 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003053 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003054
Jooyung Hanc87a0592020-03-02 17:44:33 +09003055 // APEX variant has __ANDROID_APEX__ defined
3056 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003057 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003058 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003059 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003060
Dan Albertb19953d2020-11-17 15:29:36 -08003061 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003062 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003063 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003064 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003065}
Jiyong Park7e636d02019-01-28 16:16:54 +09003066
3067func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003068 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003069 apex {
3070 name: "myapex",
3071 key: "myapex.key",
3072 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003073 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003074 }
3075
3076 apex_key {
3077 name: "myapex.key",
3078 public_key: "testkey.avbpubkey",
3079 private_key: "testkey.pem",
3080 }
3081
3082 cc_library_headers {
3083 name: "mylib_headers",
3084 export_include_dirs: ["my_include"],
3085 system_shared_libs: [],
3086 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003087 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003088 }
3089
3090 cc_library {
3091 name: "mylib",
3092 srcs: ["mylib.cpp"],
3093 system_shared_libs: [],
3094 stl: "none",
3095 header_libs: ["mylib_headers"],
3096 export_header_lib_headers: ["mylib_headers"],
3097 stubs: {
3098 versions: ["1", "2", "3"],
3099 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003100 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003101 }
3102
3103 cc_library {
3104 name: "otherlib",
3105 srcs: ["mylib.cpp"],
3106 system_shared_libs: [],
3107 stl: "none",
3108 shared_libs: ["mylib"],
3109 }
3110 `)
3111
Colin Cross7113d202019-11-20 16:39:12 -08003112 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003113
3114 // Ensure that the include path of the header lib is exported to 'otherlib'
3115 ensureContains(t, cFlags, "-Imy_include")
3116}
Alex Light9670d332019-01-29 18:07:33 -08003117
Jiyong Park7cd10e32020-01-14 09:22:18 +09003118type fileInApex struct {
3119 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003120 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003121 isLink bool
3122}
3123
Jooyung Hana57af4a2020-01-23 05:36:59 +00003124func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003125 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003126 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003127 copyCmds := apexRule.Args["copy_commands"]
3128 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003129 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003130 for _, cmd := range strings.Split(copyCmds, "&&") {
3131 cmd = strings.TrimSpace(cmd)
3132 if cmd == "" {
3133 continue
3134 }
3135 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003136 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003137 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003138 switch terms[0] {
3139 case "mkdir":
3140 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003141 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003142 t.Fatal("copyCmds contains invalid cp command", cmd)
3143 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003144 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003145 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003146 isLink = false
3147 case "ln":
3148 if len(terms) != 3 && len(terms) != 4 {
3149 // ln LINK TARGET or ln -s LINK TARGET
3150 t.Fatal("copyCmds contains invalid ln command", cmd)
3151 }
3152 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003153 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003154 isLink = true
3155 default:
3156 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3157 }
3158 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003159 index := strings.Index(dst, imageApexDir)
3160 if index == -1 {
3161 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3162 }
3163 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003164 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003165 }
3166 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003167 return ret
3168}
3169
Jooyung Hana57af4a2020-01-23 05:36:59 +00003170func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3171 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003172 var failed bool
3173 var surplus []string
3174 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003175 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003176 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003177 for _, expected := range files {
3178 if matched, _ := path.Match(expected, file.path); matched {
3179 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003180 mactchFound = true
3181 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003182 }
3183 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003184 if !mactchFound {
3185 surplus = append(surplus, file.path)
3186 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003187 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003188
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003190 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003191 t.Log("surplus files", surplus)
3192 failed = true
3193 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003194
3195 if len(files) > len(filesMatched) {
3196 var missing []string
3197 for _, expected := range files {
3198 if !filesMatched[expected] {
3199 missing = append(missing, expected)
3200 }
3201 }
3202 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003203 t.Log("missing files", missing)
3204 failed = true
3205 }
3206 if failed {
3207 t.Fail()
3208 }
3209}
3210
Jooyung Han344d5432019-08-23 11:17:39 +09003211func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003212 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003213 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003214 name: "com.android.vndk.current",
3215 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003216 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003217 }
3218
3219 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003220 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003221 public_key: "testkey.avbpubkey",
3222 private_key: "testkey.pem",
3223 }
3224
3225 cc_library {
3226 name: "libvndk",
3227 srcs: ["mylib.cpp"],
3228 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003229 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003230 vndk: {
3231 enabled: true,
3232 },
3233 system_shared_libs: [],
3234 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003235 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003236 }
3237
3238 cc_library {
3239 name: "libvndksp",
3240 srcs: ["mylib.cpp"],
3241 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003242 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003243 vndk: {
3244 enabled: true,
3245 support_system_process: true,
3246 },
3247 system_shared_libs: [],
3248 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003249 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003250 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003251 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003252
Colin Cross2807f002021-03-02 10:15:29 -08003253 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003254 "lib/libvndk.so",
3255 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003256 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003257 "lib64/libvndk.so",
3258 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003259 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003260 "etc/llndk.libraries.VER.txt",
3261 "etc/vndkcore.libraries.VER.txt",
3262 "etc/vndksp.libraries.VER.txt",
3263 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003264 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003265 })
Jooyung Han344d5432019-08-23 11:17:39 +09003266}
3267
3268func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003269 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003270 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003271 name: "com.android.vndk.current",
3272 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003273 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003274 }
3275
3276 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003277 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003278 public_key: "testkey.avbpubkey",
3279 private_key: "testkey.pem",
3280 }
3281
3282 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003283 name: "libvndk",
3284 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003285 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003286 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003287 vndk: {
3288 enabled: true,
3289 },
3290 system_shared_libs: [],
3291 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003292 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003293 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003294
3295 cc_prebuilt_library_shared {
3296 name: "libvndk.arm",
3297 srcs: ["libvndk.arm.so"],
3298 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003299 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003300 vndk: {
3301 enabled: true,
3302 },
3303 enabled: false,
3304 arch: {
3305 arm: {
3306 enabled: true,
3307 },
3308 },
3309 system_shared_libs: [],
3310 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003311 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003312 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003313 `+vndkLibrariesTxtFiles("current"),
3314 withFiles(map[string][]byte{
3315 "libvndk.so": nil,
3316 "libvndk.arm.so": nil,
3317 }))
Colin Cross2807f002021-03-02 10:15:29 -08003318 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003319 "lib/libvndk.so",
3320 "lib/libvndk.arm.so",
3321 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003322 "lib/libc++.so",
3323 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003324 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003325 })
Jooyung Han344d5432019-08-23 11:17:39 +09003326}
3327
Jooyung Han39edb6c2019-11-06 16:53:07 +09003328func vndkLibrariesTxtFiles(vers ...string) (result string) {
3329 for _, v := range vers {
3330 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003331 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003332 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003333 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003334 name: "` + txt + `.libraries.txt",
3335 }
3336 `
3337 }
3338 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003339 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003340 result += `
3341 prebuilt_etc {
3342 name: "` + txt + `.libraries.` + v + `.txt",
3343 src: "dummy.txt",
3344 }
3345 `
3346 }
3347 }
3348 }
3349 return
3350}
3351
Jooyung Han344d5432019-08-23 11:17:39 +09003352func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003353 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003354 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003355 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003356 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003357 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003358 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003359 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003360 }
3361
3362 apex_key {
3363 name: "myapex.key",
3364 public_key: "testkey.avbpubkey",
3365 private_key: "testkey.pem",
3366 }
3367
Jooyung Han31c470b2019-10-18 16:26:59 +09003368 vndk_prebuilt_shared {
3369 name: "libvndk27",
3370 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003371 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003372 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003373 vndk: {
3374 enabled: true,
3375 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003376 target_arch: "arm64",
3377 arch: {
3378 arm: {
3379 srcs: ["libvndk27_arm.so"],
3380 },
3381 arm64: {
3382 srcs: ["libvndk27_arm64.so"],
3383 },
3384 },
Colin Cross2807f002021-03-02 10:15:29 -08003385 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003386 }
3387
3388 vndk_prebuilt_shared {
3389 name: "libvndk27",
3390 version: "27",
3391 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003392 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003393 vndk: {
3394 enabled: true,
3395 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003396 target_arch: "x86_64",
3397 arch: {
3398 x86: {
3399 srcs: ["libvndk27_x86.so"],
3400 },
3401 x86_64: {
3402 srcs: ["libvndk27_x86_64.so"],
3403 },
3404 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003405 }
3406 `+vndkLibrariesTxtFiles("27"),
3407 withFiles(map[string][]byte{
3408 "libvndk27_arm.so": nil,
3409 "libvndk27_arm64.so": nil,
3410 "libvndk27_x86.so": nil,
3411 "libvndk27_x86_64.so": nil,
3412 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003413
Colin Cross2807f002021-03-02 10:15:29 -08003414 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003415 "lib/libvndk27_arm.so",
3416 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003417 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003418 })
Jooyung Han344d5432019-08-23 11:17:39 +09003419}
3420
Jooyung Han90eee022019-10-01 20:02:42 +09003421func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003422 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003423 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003424 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003425 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003426 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003427 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003428 }
3429 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003430 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003431 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003432 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003433 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003434 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003435 }
3436 apex_key {
3437 name: "myapex.key",
3438 public_key: "testkey.avbpubkey",
3439 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003440 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003441
3442 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003443 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003444 actual := proptools.String(bundle.properties.Apex_name)
3445 if !reflect.DeepEqual(actual, expected) {
3446 t.Errorf("Got '%v', expected '%v'", actual, expected)
3447 }
3448 }
3449
Colin Cross2807f002021-03-02 10:15:29 -08003450 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3451 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003452}
3453
Jooyung Han344d5432019-08-23 11:17:39 +09003454func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003455 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003456 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003457 name: "com.android.vndk.current",
3458 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003459 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003460 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003461 }
3462
3463 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003464 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003465 public_key: "testkey.avbpubkey",
3466 private_key: "testkey.pem",
3467 }
3468
3469 cc_library {
3470 name: "libvndk",
3471 srcs: ["mylib.cpp"],
3472 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003473 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003474 native_bridge_supported: true,
3475 host_supported: true,
3476 vndk: {
3477 enabled: true,
3478 },
3479 system_shared_libs: [],
3480 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003481 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003482 }
Colin Cross2807f002021-03-02 10:15:29 -08003483 `+vndkLibrariesTxtFiles("current"),
3484 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003485
Colin Cross2807f002021-03-02 10:15:29 -08003486 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003487 "lib/libvndk.so",
3488 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003489 "lib/libc++.so",
3490 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003491 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003492 })
Jooyung Han344d5432019-08-23 11:17:39 +09003493}
3494
3495func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003496 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003497 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003498 name: "com.android.vndk.current",
3499 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003500 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003501 native_bridge_supported: true,
3502 }
3503
3504 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003505 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003506 public_key: "testkey.avbpubkey",
3507 private_key: "testkey.pem",
3508 }
3509
3510 cc_library {
3511 name: "libvndk",
3512 srcs: ["mylib.cpp"],
3513 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003514 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003515 native_bridge_supported: true,
3516 host_supported: true,
3517 vndk: {
3518 enabled: true,
3519 },
3520 system_shared_libs: [],
3521 stl: "none",
3522 }
3523 `)
3524}
3525
Jooyung Han31c470b2019-10-18 16:26:59 +09003526func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003527 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003528 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003529 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003530 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003531 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003532 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003533 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003534 }
3535
3536 apex_key {
3537 name: "myapex.key",
3538 public_key: "testkey.avbpubkey",
3539 private_key: "testkey.pem",
3540 }
3541
3542 vndk_prebuilt_shared {
3543 name: "libvndk27",
3544 version: "27",
3545 target_arch: "arm",
3546 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003547 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003548 vndk: {
3549 enabled: true,
3550 },
3551 arch: {
3552 arm: {
3553 srcs: ["libvndk27.so"],
3554 }
3555 },
3556 }
3557
3558 vndk_prebuilt_shared {
3559 name: "libvndk27",
3560 version: "27",
3561 target_arch: "arm",
3562 binder32bit: true,
3563 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003564 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003565 vndk: {
3566 enabled: true,
3567 },
3568 arch: {
3569 arm: {
3570 srcs: ["libvndk27binder32.so"],
3571 }
3572 },
Colin Cross2807f002021-03-02 10:15:29 -08003573 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003574 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003575 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003576 withFiles(map[string][]byte{
3577 "libvndk27.so": nil,
3578 "libvndk27binder32.so": nil,
3579 }),
3580 withBinder32bit,
3581 withTargets(map[android.OsType][]android.Target{
3582 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003583 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3584 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003585 },
3586 }),
3587 )
3588
Colin Cross2807f002021-03-02 10:15:29 -08003589 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003590 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003591 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003592 })
3593}
3594
Jooyung Han45a96772020-06-15 14:59:42 +09003595func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003596 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003597 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003598 name: "com.android.vndk.current",
3599 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003600 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003601 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003602 }
3603
3604 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003605 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003606 public_key: "testkey.avbpubkey",
3607 private_key: "testkey.pem",
3608 }
3609
3610 cc_library {
3611 name: "libz",
3612 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003613 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003614 vndk: {
3615 enabled: true,
3616 },
3617 stubs: {
3618 symbol_file: "libz.map.txt",
3619 versions: ["30"],
3620 }
3621 }
3622 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3623 "libz.map.txt": nil,
3624 }))
3625
Colin Cross2807f002021-03-02 10:15:29 -08003626 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003627 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3628 ensureListEmpty(t, provideNativeLibs)
3629}
3630
Jooyung Hane1633032019-08-01 17:41:43 +09003631func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003632 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003633 apex {
3634 name: "myapex_nodep",
3635 key: "myapex.key",
3636 native_shared_libs: ["lib_nodep"],
3637 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003638 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003639 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003640 }
3641
3642 apex {
3643 name: "myapex_dep",
3644 key: "myapex.key",
3645 native_shared_libs: ["lib_dep"],
3646 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003647 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003648 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003649 }
3650
3651 apex {
3652 name: "myapex_provider",
3653 key: "myapex.key",
3654 native_shared_libs: ["libfoo"],
3655 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003656 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003657 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003658 }
3659
3660 apex {
3661 name: "myapex_selfcontained",
3662 key: "myapex.key",
3663 native_shared_libs: ["lib_dep", "libfoo"],
3664 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003665 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003666 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003667 }
3668
3669 apex_key {
3670 name: "myapex.key",
3671 public_key: "testkey.avbpubkey",
3672 private_key: "testkey.pem",
3673 }
3674
3675 cc_library {
3676 name: "lib_nodep",
3677 srcs: ["mylib.cpp"],
3678 system_shared_libs: [],
3679 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003680 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003681 }
3682
3683 cc_library {
3684 name: "lib_dep",
3685 srcs: ["mylib.cpp"],
3686 shared_libs: ["libfoo"],
3687 system_shared_libs: [],
3688 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003689 apex_available: [
3690 "myapex_dep",
3691 "myapex_provider",
3692 "myapex_selfcontained",
3693 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003694 }
3695
3696 cc_library {
3697 name: "libfoo",
3698 srcs: ["mytest.cpp"],
3699 stubs: {
3700 versions: ["1"],
3701 },
3702 system_shared_libs: [],
3703 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003704 apex_available: [
3705 "myapex_provider",
3706 "myapex_selfcontained",
3707 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003708 }
3709 `)
3710
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003711 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003712 var provideNativeLibs, requireNativeLibs []string
3713
Sundong Ahnabb64432019-10-22 13:58:29 +09003714 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003715 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3716 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003717 ensureListEmpty(t, provideNativeLibs)
3718 ensureListEmpty(t, requireNativeLibs)
3719
Sundong Ahnabb64432019-10-22 13:58:29 +09003720 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003721 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3722 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003723 ensureListEmpty(t, provideNativeLibs)
3724 ensureListContains(t, requireNativeLibs, "libfoo.so")
3725
Sundong Ahnabb64432019-10-22 13:58:29 +09003726 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003727 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3728 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003729 ensureListContains(t, provideNativeLibs, "libfoo.so")
3730 ensureListEmpty(t, requireNativeLibs)
3731
Sundong Ahnabb64432019-10-22 13:58:29 +09003732 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003733 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3734 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003735 ensureListContains(t, provideNativeLibs, "libfoo.so")
3736 ensureListEmpty(t, requireNativeLibs)
3737}
3738
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003739func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003740 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003741 apex {
3742 name: "myapex",
3743 key: "myapex.key",
3744 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003745 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003746 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003747 }
3748
3749 apex_key {
3750 name: "myapex.key",
3751 public_key: "testkey.avbpubkey",
3752 private_key: "testkey.pem",
3753 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003754
3755 cc_library {
3756 name: "mylib",
3757 srcs: ["mylib.cpp"],
3758 system_shared_libs: [],
3759 stl: "none",
3760 apex_available: [
3761 "//apex_available:platform",
3762 "myapex",
3763 ],
3764 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003765 `)
3766
Sundong Ahnabb64432019-10-22 13:58:29 +09003767 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003768 apexManifestRule := module.Rule("apexManifestRule")
3769 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3770 apexRule := module.Rule("apexRule")
3771 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003772
3773 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003774 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003775 name := apexBundle.BaseModuleName()
3776 prefix := "TARGET_"
3777 var builder strings.Builder
3778 data.Custom(&builder, name, prefix, "", data)
3779 androidMk := builder.String()
3780 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3781 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003782}
3783
Alex Light0851b882019-02-07 13:20:53 -08003784func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003785 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003786 apex {
3787 name: "myapex",
3788 key: "myapex.key",
3789 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003790 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003791 }
3792
3793 apex_key {
3794 name: "myapex.key",
3795 public_key: "testkey.avbpubkey",
3796 private_key: "testkey.pem",
3797 }
3798
3799 cc_library {
3800 name: "mylib_common",
3801 srcs: ["mylib.cpp"],
3802 system_shared_libs: [],
3803 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003804 apex_available: [
3805 "//apex_available:platform",
3806 "myapex",
3807 ],
Alex Light0851b882019-02-07 13:20:53 -08003808 }
3809 `)
3810
Sundong Ahnabb64432019-10-22 13:58:29 +09003811 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003812 apexRule := module.Rule("apexRule")
3813 copyCmds := apexRule.Args["copy_commands"]
3814
3815 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3816 t.Log("Apex was a test apex!")
3817 t.Fail()
3818 }
3819 // Ensure that main rule creates an output
3820 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3821
3822 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003823 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003824
3825 // Ensure that both direct and indirect deps are copied into apex
3826 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3827
Colin Cross7113d202019-11-20 16:39:12 -08003828 // Ensure that the platform variant ends with _shared
3829 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003830
Colin Cross56a83212020-09-15 18:30:11 -07003831 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003832 t.Log("Found mylib_common not in any apex!")
3833 t.Fail()
3834 }
3835}
3836
3837func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003838 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003839 apex_test {
3840 name: "myapex",
3841 key: "myapex.key",
3842 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003843 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003844 }
3845
3846 apex_key {
3847 name: "myapex.key",
3848 public_key: "testkey.avbpubkey",
3849 private_key: "testkey.pem",
3850 }
3851
3852 cc_library {
3853 name: "mylib_common_test",
3854 srcs: ["mylib.cpp"],
3855 system_shared_libs: [],
3856 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003857 // TODO: remove //apex_available:platform
3858 apex_available: [
3859 "//apex_available:platform",
3860 "myapex",
3861 ],
Alex Light0851b882019-02-07 13:20:53 -08003862 }
3863 `)
3864
Sundong Ahnabb64432019-10-22 13:58:29 +09003865 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003866 apexRule := module.Rule("apexRule")
3867 copyCmds := apexRule.Args["copy_commands"]
3868
3869 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3870 t.Log("Apex was not a test apex!")
3871 t.Fail()
3872 }
3873 // Ensure that main rule creates an output
3874 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3875
3876 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003877 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003878
3879 // Ensure that both direct and indirect deps are copied into apex
3880 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3881
Colin Cross7113d202019-11-20 16:39:12 -08003882 // Ensure that the platform variant ends with _shared
3883 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003884}
3885
Alex Light9670d332019-01-29 18:07:33 -08003886func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003887 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003888 apex {
3889 name: "myapex",
3890 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003891 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003892 multilib: {
3893 first: {
3894 native_shared_libs: ["mylib_common"],
3895 }
3896 },
3897 target: {
3898 android: {
3899 multilib: {
3900 first: {
3901 native_shared_libs: ["mylib"],
3902 }
3903 }
3904 },
3905 host: {
3906 multilib: {
3907 first: {
3908 native_shared_libs: ["mylib2"],
3909 }
3910 }
3911 }
3912 }
3913 }
3914
3915 apex_key {
3916 name: "myapex.key",
3917 public_key: "testkey.avbpubkey",
3918 private_key: "testkey.pem",
3919 }
3920
3921 cc_library {
3922 name: "mylib",
3923 srcs: ["mylib.cpp"],
3924 system_shared_libs: [],
3925 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003926 // TODO: remove //apex_available:platform
3927 apex_available: [
3928 "//apex_available:platform",
3929 "myapex",
3930 ],
Alex Light9670d332019-01-29 18:07:33 -08003931 }
3932
3933 cc_library {
3934 name: "mylib_common",
3935 srcs: ["mylib.cpp"],
3936 system_shared_libs: [],
3937 stl: "none",
3938 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003939 // TODO: remove //apex_available:platform
3940 apex_available: [
3941 "//apex_available:platform",
3942 "myapex",
3943 ],
Alex Light9670d332019-01-29 18:07:33 -08003944 }
3945
3946 cc_library {
3947 name: "mylib2",
3948 srcs: ["mylib.cpp"],
3949 system_shared_libs: [],
3950 stl: "none",
3951 compile_multilib: "first",
3952 }
3953 `)
3954
Sundong Ahnabb64432019-10-22 13:58:29 +09003955 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003956 copyCmds := apexRule.Args["copy_commands"]
3957
3958 // Ensure that main rule creates an output
3959 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3960
3961 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003962 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3963 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3964 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003965
3966 // Ensure that both direct and indirect deps are copied into apex
3967 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3968 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3969 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3970
Colin Cross7113d202019-11-20 16:39:12 -08003971 // Ensure that the platform variant ends with _shared
3972 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3973 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3974 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003975}
Jiyong Park04480cf2019-02-06 00:16:29 +09003976
Jiyong Park59140302020-12-14 18:44:04 +09003977func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003978 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003979 apex {
3980 name: "myapex",
3981 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003982 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003983 arch: {
3984 arm64: {
3985 native_shared_libs: ["mylib.arm64"],
3986 },
3987 x86_64: {
3988 native_shared_libs: ["mylib.x64"],
3989 },
3990 }
3991 }
3992
3993 apex_key {
3994 name: "myapex.key",
3995 public_key: "testkey.avbpubkey",
3996 private_key: "testkey.pem",
3997 }
3998
3999 cc_library {
4000 name: "mylib.arm64",
4001 srcs: ["mylib.cpp"],
4002 system_shared_libs: [],
4003 stl: "none",
4004 // TODO: remove //apex_available:platform
4005 apex_available: [
4006 "//apex_available:platform",
4007 "myapex",
4008 ],
4009 }
4010
4011 cc_library {
4012 name: "mylib.x64",
4013 srcs: ["mylib.cpp"],
4014 system_shared_libs: [],
4015 stl: "none",
4016 // TODO: remove //apex_available:platform
4017 apex_available: [
4018 "//apex_available:platform",
4019 "myapex",
4020 ],
4021 }
4022 `)
4023
4024 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4025 copyCmds := apexRule.Args["copy_commands"]
4026
4027 // Ensure that apex variant is created for the direct dep
4028 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4029 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4030
4031 // Ensure that both direct and indirect deps are copied into apex
4032 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4033 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4034}
4035
Jiyong Park04480cf2019-02-06 00:16:29 +09004036func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004037 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004038 apex {
4039 name: "myapex",
4040 key: "myapex.key",
4041 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004042 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004043 }
4044
4045 apex_key {
4046 name: "myapex.key",
4047 public_key: "testkey.avbpubkey",
4048 private_key: "testkey.pem",
4049 }
4050
4051 sh_binary {
4052 name: "myscript",
4053 src: "mylib.cpp",
4054 filename: "myscript.sh",
4055 sub_dir: "script",
4056 }
4057 `)
4058
Sundong Ahnabb64432019-10-22 13:58:29 +09004059 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004060 copyCmds := apexRule.Args["copy_commands"]
4061
4062 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4063}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004064
Jooyung Han91df2082019-11-20 01:49:42 +09004065func TestApexInVariousPartition(t *testing.T) {
4066 testcases := []struct {
4067 propName, parition, flattenedPartition string
4068 }{
4069 {"", "system", "system_ext"},
4070 {"product_specific: true", "product", "product"},
4071 {"soc_specific: true", "vendor", "vendor"},
4072 {"proprietary: true", "vendor", "vendor"},
4073 {"vendor: true", "vendor", "vendor"},
4074 {"system_ext_specific: true", "system_ext", "system_ext"},
4075 }
4076 for _, tc := range testcases {
4077 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004078 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004079 apex {
4080 name: "myapex",
4081 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004082 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004083 `+tc.propName+`
4084 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004085
Jooyung Han91df2082019-11-20 01:49:42 +09004086 apex_key {
4087 name: "myapex.key",
4088 public_key: "testkey.avbpubkey",
4089 private_key: "testkey.pem",
4090 }
4091 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004092
Jooyung Han91df2082019-11-20 01:49:42 +09004093 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4094 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4095 actual := apex.installDir.String()
4096 if actual != expected {
4097 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4098 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004099
Jooyung Han91df2082019-11-20 01:49:42 +09004100 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4101 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4102 actual = flattened.installDir.String()
4103 if actual != expected {
4104 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4105 }
4106 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004107 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004108}
Jiyong Park67882562019-03-21 01:11:21 +09004109
Jooyung Han580eb4f2020-06-24 19:33:06 +09004110func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004111 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004112 apex {
4113 name: "myapex",
4114 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004115 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004116 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004117
Jooyung Han580eb4f2020-06-24 19:33:06 +09004118 apex_key {
4119 name: "myapex.key",
4120 public_key: "testkey.avbpubkey",
4121 private_key: "testkey.pem",
4122 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004123 `)
4124 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004125 rule := module.Output("file_contexts")
4126 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4127}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004128
Jooyung Han580eb4f2020-06-24 19:33:06 +09004129func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004130 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004131 apex {
4132 name: "myapex",
4133 key: "myapex.key",
4134 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004135 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004136 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004137
Jooyung Han580eb4f2020-06-24 19:33:06 +09004138 apex_key {
4139 name: "myapex.key",
4140 public_key: "testkey.avbpubkey",
4141 private_key: "testkey.pem",
4142 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004143 `, withFiles(map[string][]byte{
4144 "my_own_file_contexts": nil,
4145 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004146}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004147
Jooyung Han580eb4f2020-06-24 19:33:06 +09004148func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004149 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004150 apex {
4151 name: "myapex",
4152 key: "myapex.key",
4153 product_specific: true,
4154 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004155 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004156 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004157
Jooyung Han580eb4f2020-06-24 19:33:06 +09004158 apex_key {
4159 name: "myapex.key",
4160 public_key: "testkey.avbpubkey",
4161 private_key: "testkey.pem",
4162 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004163 `)
4164
Colin Cross1c460562021-02-16 17:55:47 -08004165 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004166 apex {
4167 name: "myapex",
4168 key: "myapex.key",
4169 product_specific: true,
4170 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004171 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004172 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004173
Jooyung Han580eb4f2020-06-24 19:33:06 +09004174 apex_key {
4175 name: "myapex.key",
4176 public_key: "testkey.avbpubkey",
4177 private_key: "testkey.pem",
4178 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004179 `, withFiles(map[string][]byte{
4180 "product_specific_file_contexts": nil,
4181 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004182 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4183 rule := module.Output("file_contexts")
4184 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4185}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004186
Jooyung Han580eb4f2020-06-24 19:33:06 +09004187func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004188 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004189 apex {
4190 name: "myapex",
4191 key: "myapex.key",
4192 product_specific: true,
4193 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004194 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004195 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004196
Jooyung Han580eb4f2020-06-24 19:33:06 +09004197 apex_key {
4198 name: "myapex.key",
4199 public_key: "testkey.avbpubkey",
4200 private_key: "testkey.pem",
4201 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004202
Jooyung Han580eb4f2020-06-24 19:33:06 +09004203 filegroup {
4204 name: "my-file-contexts",
4205 srcs: ["product_specific_file_contexts"],
4206 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004207 `, withFiles(map[string][]byte{
4208 "product_specific_file_contexts": nil,
4209 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004210 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4211 rule := module.Output("file_contexts")
4212 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004213}
4214
Jiyong Park67882562019-03-21 01:11:21 +09004215func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004216 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004217 apex_key {
4218 name: "myapex.key",
4219 public_key: ":my.avbpubkey",
4220 private_key: ":my.pem",
4221 product_specific: true,
4222 }
4223
4224 filegroup {
4225 name: "my.avbpubkey",
4226 srcs: ["testkey2.avbpubkey"],
4227 }
4228
4229 filegroup {
4230 name: "my.pem",
4231 srcs: ["testkey2.pem"],
4232 }
4233 `)
4234
4235 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4236 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004237 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004238 if actual_pubkey != expected_pubkey {
4239 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4240 }
4241 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004242 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004243 if actual_privkey != expected_privkey {
4244 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4245 }
4246}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004247
4248func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004249 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004250 prebuilt_apex {
4251 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004252 arch: {
4253 arm64: {
4254 src: "myapex-arm64.apex",
4255 },
4256 arm: {
4257 src: "myapex-arm.apex",
4258 },
4259 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004260 }
4261 `)
4262
4263 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4264
Jiyong Parkc95714e2019-03-29 14:23:10 +09004265 expectedInput := "myapex-arm64.apex"
4266 if prebuilt.inputApex.String() != expectedInput {
4267 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4268 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004269}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004270
4271func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004272 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004273 prebuilt_apex {
4274 name: "myapex",
4275 src: "myapex-arm.apex",
4276 filename: "notmyapex.apex",
4277 }
4278 `)
4279
4280 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4281
4282 expected := "notmyapex.apex"
4283 if p.installFilename != expected {
4284 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4285 }
4286}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004287
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004288func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004289 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004290 prebuilt_apex {
4291 name: "myapex.prebuilt",
4292 src: "myapex-arm.apex",
4293 overrides: [
4294 "myapex",
4295 ],
4296 }
4297 `)
4298
4299 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4300
4301 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004302 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004303 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004304 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004305 }
4306}
4307
Paul Duffin092153d2021-01-26 11:42:39 +00004308// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4309// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004310func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4311 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004312 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004313 }
4314
Paul Duffin89886cb2021-02-05 16:44:03 +00004315 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004316 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004317 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004318 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004319 stem := android.RemoveOptionalPrebuiltPrefix(name)
4320 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004321 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4322 }
4323 }
4324
Paul Duffin39853512021-02-26 11:09:39 +00004325 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004326 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004327 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004328 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4329 }
4330 }
4331
4332 t.Run("prebuilt only", func(t *testing.T) {
4333 bp := `
4334 prebuilt_apex {
4335 name: "myapex",
4336 arch: {
4337 arm64: {
4338 src: "myapex-arm64.apex",
4339 },
4340 arm: {
4341 src: "myapex-arm.apex",
4342 },
4343 },
Paul Duffin39853512021-02-26 11:09:39 +00004344 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004345 }
4346
4347 java_import {
4348 name: "libfoo",
4349 jars: ["libfoo.jar"],
4350 }
Paul Duffin39853512021-02-26 11:09:39 +00004351
4352 java_sdk_library_import {
4353 name: "libbar",
4354 public: {
4355 jars: ["libbar.jar"],
4356 },
4357 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004358 `
4359
4360 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4361 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4362
Paul Duffinf6932af2021-02-26 18:21:56 +00004363 // Make sure that the deapexer has the correct input APEX.
4364 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4365 rule := deapexer.Rule("deapexer")
4366 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4367 t.Errorf("expected: %q, found: %q", expected, actual)
4368 }
4369
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004370 // Make sure that the prebuilt_apex has the correct input APEX.
4371 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4372 rule = prebuiltApex.Rule("android/soong/android.Cp")
4373 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4374 t.Errorf("expected: %q, found: %q", expected, actual)
4375 }
4376
Paul Duffin89886cb2021-02-05 16:44:03 +00004377 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004378
4379 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004380 })
4381
4382 t.Run("prebuilt with source preferred", func(t *testing.T) {
4383
4384 bp := `
4385 prebuilt_apex {
4386 name: "myapex",
4387 arch: {
4388 arm64: {
4389 src: "myapex-arm64.apex",
4390 },
4391 arm: {
4392 src: "myapex-arm.apex",
4393 },
4394 },
Paul Duffin39853512021-02-26 11:09:39 +00004395 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004396 }
4397
4398 java_import {
4399 name: "libfoo",
4400 jars: ["libfoo.jar"],
4401 }
4402
4403 java_library {
4404 name: "libfoo",
4405 }
Paul Duffin39853512021-02-26 11:09:39 +00004406
4407 java_sdk_library_import {
4408 name: "libbar",
4409 public: {
4410 jars: ["libbar.jar"],
4411 },
4412 }
4413
4414 java_sdk_library {
4415 name: "libbar",
4416 srcs: ["foo/bar/MyClass.java"],
4417 unsafe_ignore_missing_latest_api: true,
4418 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004419 `
4420
4421 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4422 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4423
Paul Duffin89886cb2021-02-05 16:44:03 +00004424 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004425 ensureNoSourceVariant(t, ctx, "libfoo")
4426
4427 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4428 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004429 })
4430
4431 t.Run("prebuilt preferred with source", func(t *testing.T) {
4432 bp := `
4433 prebuilt_apex {
4434 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004435 arch: {
4436 arm64: {
4437 src: "myapex-arm64.apex",
4438 },
4439 arm: {
4440 src: "myapex-arm.apex",
4441 },
4442 },
Paul Duffin39853512021-02-26 11:09:39 +00004443 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004444 }
4445
4446 java_import {
4447 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004448 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004449 jars: ["libfoo.jar"],
4450 }
4451
4452 java_library {
4453 name: "libfoo",
4454 }
Paul Duffin39853512021-02-26 11:09:39 +00004455
4456 java_sdk_library_import {
4457 name: "libbar",
4458 prefer: true,
4459 public: {
4460 jars: ["libbar.jar"],
4461 },
4462 }
4463
4464 java_sdk_library {
4465 name: "libbar",
4466 srcs: ["foo/bar/MyClass.java"],
4467 unsafe_ignore_missing_latest_api: true,
4468 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004469 `
4470
4471 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4472 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4473
Paul Duffin89886cb2021-02-05 16:44:03 +00004474 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004475 ensureNoSourceVariant(t, ctx, "libfoo")
4476
4477 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4478 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004479 })
4480}
4481
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004482func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4483 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004484 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004485 }
4486
Paul Duffin37856732021-02-26 14:24:15 +00004487 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4488 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004489 s := ctx.SingletonForTests("dex_bootjars")
4490 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004491 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004492 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004493 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004494 foundLibfooJar = true
4495 buildRule := s.Output(output)
4496 actual := android.NormalizePathForTesting(buildRule.Input)
4497 if actual != bootDexJarPath {
4498 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4499 }
4500 }
4501 }
4502 if !foundLibfooJar {
4503 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4504 }
4505 }
4506
Paul Duffin4fd997b2021-02-03 20:06:33 +00004507 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004508 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004509 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4510 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4511 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4512 }
4513
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004514 t.Run("prebuilt only", func(t *testing.T) {
4515 bp := `
4516 prebuilt_apex {
4517 name: "myapex",
4518 arch: {
4519 arm64: {
4520 src: "myapex-arm64.apex",
4521 },
4522 arm: {
4523 src: "myapex-arm.apex",
4524 },
4525 },
Paul Duffin37856732021-02-26 14:24:15 +00004526 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004527 }
4528
4529 java_import {
4530 name: "libfoo",
4531 jars: ["libfoo.jar"],
4532 apex_available: ["myapex"],
4533 }
Paul Duffin37856732021-02-26 14:24:15 +00004534
4535 java_sdk_library_import {
4536 name: "libbar",
4537 public: {
4538 jars: ["libbar.jar"],
4539 },
4540 apex_available: ["myapex"],
4541 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004542 `
4543
4544 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004545 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4546 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004547
Paul Duffin9d67ca62021-02-03 20:06:33 +00004548 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4549 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004550.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004551.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4552`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004553 })
4554
4555 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4556 bp := `
4557 prebuilt_apex {
4558 name: "myapex",
4559 arch: {
4560 arm64: {
4561 src: "myapex-arm64.apex",
4562 },
4563 arm: {
4564 src: "myapex-arm.apex",
4565 },
4566 },
Paul Duffin37856732021-02-26 14:24:15 +00004567 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004568 }
4569
4570 java_import {
4571 name: "libfoo",
4572 jars: ["libfoo.jar"],
4573 apex_available: ["myapex"],
4574 }
4575
4576 java_library {
4577 name: "libfoo",
4578 srcs: ["foo/bar/MyClass.java"],
4579 apex_available: ["myapex"],
4580 }
Paul Duffin37856732021-02-26 14:24:15 +00004581
4582 java_sdk_library_import {
4583 name: "libbar",
4584 public: {
4585 jars: ["libbar.jar"],
4586 },
4587 apex_available: ["myapex"],
4588 }
4589
4590 java_sdk_library {
4591 name: "libbar",
4592 srcs: ["foo/bar/MyClass.java"],
4593 unsafe_ignore_missing_latest_api: true,
4594 apex_available: ["myapex"],
4595 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004596 `
4597
4598 // In this test the source (java_library) libfoo is active since the
4599 // prebuilt (java_import) defaults to prefer:false. However the
4600 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4601 // find the dex boot jar in it. We either need to disable the source libfoo
4602 // or make the prebuilt libfoo preferred.
4603 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4604 })
4605
4606 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4607 bp := `
4608 prebuilt_apex {
4609 name: "myapex",
4610 arch: {
4611 arm64: {
4612 src: "myapex-arm64.apex",
4613 },
4614 arm: {
4615 src: "myapex-arm.apex",
4616 },
4617 },
Paul Duffin37856732021-02-26 14:24:15 +00004618 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004619 }
4620
4621 java_import {
4622 name: "libfoo",
4623 prefer: true,
4624 jars: ["libfoo.jar"],
4625 apex_available: ["myapex"],
4626 }
4627
4628 java_library {
4629 name: "libfoo",
4630 srcs: ["foo/bar/MyClass.java"],
4631 apex_available: ["myapex"],
4632 }
Paul Duffin37856732021-02-26 14:24:15 +00004633
4634 java_sdk_library_import {
4635 name: "libbar",
4636 prefer: true,
4637 public: {
4638 jars: ["libbar.jar"],
4639 },
4640 apex_available: ["myapex"],
4641 }
4642
4643 java_sdk_library {
4644 name: "libbar",
4645 srcs: ["foo/bar/MyClass.java"],
4646 unsafe_ignore_missing_latest_api: true,
4647 apex_available: ["myapex"],
4648 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004649 `
4650
4651 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004652 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4653 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004654
Paul Duffin9d67ca62021-02-03 20:06:33 +00004655 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4656 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004657.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004658.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4659`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004660 })
4661
4662 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4663 bp := `
4664 apex {
4665 name: "myapex",
4666 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004667 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004668 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004669 }
4670
4671 apex_key {
4672 name: "myapex.key",
4673 public_key: "testkey.avbpubkey",
4674 private_key: "testkey.pem",
4675 }
4676
4677 prebuilt_apex {
4678 name: "myapex",
4679 arch: {
4680 arm64: {
4681 src: "myapex-arm64.apex",
4682 },
4683 arm: {
4684 src: "myapex-arm.apex",
4685 },
4686 },
Paul Duffin37856732021-02-26 14:24:15 +00004687 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004688 }
4689
4690 java_import {
4691 name: "libfoo",
4692 jars: ["libfoo.jar"],
4693 apex_available: ["myapex"],
4694 }
4695
4696 java_library {
4697 name: "libfoo",
4698 srcs: ["foo/bar/MyClass.java"],
4699 apex_available: ["myapex"],
4700 }
Paul Duffin37856732021-02-26 14:24:15 +00004701
4702 java_sdk_library_import {
4703 name: "libbar",
4704 public: {
4705 jars: ["libbar.jar"],
4706 },
4707 apex_available: ["myapex"],
4708 }
4709
4710 java_sdk_library {
4711 name: "libbar",
4712 srcs: ["foo/bar/MyClass.java"],
4713 unsafe_ignore_missing_latest_api: true,
4714 apex_available: ["myapex"],
4715 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004716 `
4717
4718 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004719 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4720 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004721
4722 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4723 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004724.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004725.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4726`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004727 })
4728
4729 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4730 bp := `
4731 apex {
4732 name: "myapex",
4733 enabled: false,
4734 key: "myapex.key",
4735 java_libs: ["libfoo"],
4736 }
4737
4738 apex_key {
4739 name: "myapex.key",
4740 public_key: "testkey.avbpubkey",
4741 private_key: "testkey.pem",
4742 }
4743
4744 prebuilt_apex {
4745 name: "myapex",
4746 arch: {
4747 arm64: {
4748 src: "myapex-arm64.apex",
4749 },
4750 arm: {
4751 src: "myapex-arm.apex",
4752 },
4753 },
Paul Duffin37856732021-02-26 14:24:15 +00004754 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004755 }
4756
4757 java_import {
4758 name: "libfoo",
4759 prefer: true,
4760 jars: ["libfoo.jar"],
4761 apex_available: ["myapex"],
4762 }
4763
4764 java_library {
4765 name: "libfoo",
4766 srcs: ["foo/bar/MyClass.java"],
4767 apex_available: ["myapex"],
4768 }
Paul Duffin37856732021-02-26 14:24:15 +00004769
4770 java_sdk_library_import {
4771 name: "libbar",
4772 prefer: true,
4773 public: {
4774 jars: ["libbar.jar"],
4775 },
4776 apex_available: ["myapex"],
4777 }
4778
4779 java_sdk_library {
4780 name: "libbar",
4781 srcs: ["foo/bar/MyClass.java"],
4782 unsafe_ignore_missing_latest_api: true,
4783 apex_available: ["myapex"],
4784 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004785 `
4786
4787 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004788 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4789 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004790
Paul Duffin9d67ca62021-02-03 20:06:33 +00004791 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4792 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004793.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004794.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4795`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004796 })
4797}
4798
Roland Levillain630846d2019-06-26 12:48:34 +01004799func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004800 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004801 apex_test {
4802 name: "myapex",
4803 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004804 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004805 tests: [
4806 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004807 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004808 ],
4809 }
4810
4811 apex_key {
4812 name: "myapex.key",
4813 public_key: "testkey.avbpubkey",
4814 private_key: "testkey.pem",
4815 }
4816
Liz Kammer1c14a212020-05-12 15:26:55 -07004817 filegroup {
4818 name: "fg",
4819 srcs: [
4820 "baz",
4821 "bar/baz"
4822 ],
4823 }
4824
Roland Levillain630846d2019-06-26 12:48:34 +01004825 cc_test {
4826 name: "mytest",
4827 gtest: false,
4828 srcs: ["mytest.cpp"],
4829 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004830 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004831 system_shared_libs: [],
4832 static_executable: true,
4833 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004834 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004835 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004836
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004837 cc_library {
4838 name: "mylib",
4839 srcs: ["mylib.cpp"],
4840 system_shared_libs: [],
4841 stl: "none",
4842 }
4843
Liz Kammer5bd365f2020-05-27 15:15:11 -07004844 filegroup {
4845 name: "fg2",
4846 srcs: [
4847 "testdata/baz"
4848 ],
4849 }
4850
Roland Levillain9b5fde92019-06-28 15:41:19 +01004851 cc_test {
4852 name: "mytests",
4853 gtest: false,
4854 srcs: [
4855 "mytest1.cpp",
4856 "mytest2.cpp",
4857 "mytest3.cpp",
4858 ],
4859 test_per_src: true,
4860 relative_install_path: "test",
4861 system_shared_libs: [],
4862 static_executable: true,
4863 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004864 data: [
4865 ":fg",
4866 ":fg2",
4867 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004868 }
Roland Levillain630846d2019-06-26 12:48:34 +01004869 `)
4870
Sundong Ahnabb64432019-10-22 13:58:29 +09004871 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004872 copyCmds := apexRule.Args["copy_commands"]
4873
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004874 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004875 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004876 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004877
Liz Kammer1c14a212020-05-12 15:26:55 -07004878 //Ensure that test data are copied into apex.
4879 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4880 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4881
Roland Levillain9b5fde92019-06-28 15:41:19 +01004882 // Ensure that test deps built with `test_per_src` are copied into apex.
4883 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4884 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4885 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004886
4887 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004888 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004889 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004890 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004891 prefix := "TARGET_"
4892 var builder strings.Builder
4893 data.Custom(&builder, name, prefix, "", data)
4894 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004895 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4896 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4897 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4898 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004899 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004900 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004901 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004902
4903 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004904 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004905 data.Custom(&builder, name, prefix, "", data)
4906 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004907 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4908 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004909}
4910
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004911func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004912 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004913 apex {
4914 name: "myapex",
4915 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004916 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004917 }
4918 apex_key {
4919 name: "myapex.key",
4920 public_key: "testkey.avbpubkey",
4921 private_key: "testkey.pem",
4922 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004923 `,
4924 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4925 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4926 }),
4927 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004928 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004929 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004930 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004931 var builder strings.Builder
4932 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4933 androidMk := builder.String()
4934 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4935}
4936
Jooyung Hand48f3c32019-08-23 11:18:57 +09004937func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4938 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4939 apex {
4940 name: "myapex",
4941 key: "myapex.key",
4942 native_shared_libs: ["libfoo"],
4943 }
4944
4945 apex_key {
4946 name: "myapex.key",
4947 public_key: "testkey.avbpubkey",
4948 private_key: "testkey.pem",
4949 }
4950
4951 cc_library {
4952 name: "libfoo",
4953 stl: "none",
4954 system_shared_libs: [],
4955 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004956 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004957 }
4958 `)
4959 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4960 apex {
4961 name: "myapex",
4962 key: "myapex.key",
4963 java_libs: ["myjar"],
4964 }
4965
4966 apex_key {
4967 name: "myapex.key",
4968 public_key: "testkey.avbpubkey",
4969 private_key: "testkey.pem",
4970 }
4971
4972 java_library {
4973 name: "myjar",
4974 srcs: ["foo/bar/MyClass.java"],
4975 sdk_version: "none",
4976 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004977 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004978 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004979 }
4980 `)
4981}
4982
Bill Peckhama41a6962021-01-11 10:58:54 -08004983func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004984 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004985 apex {
4986 name: "myapex",
4987 key: "myapex.key",
4988 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004989 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004990 }
4991
4992 apex_key {
4993 name: "myapex.key",
4994 public_key: "testkey.avbpubkey",
4995 private_key: "testkey.pem",
4996 }
4997
4998 java_import {
4999 name: "myjavaimport",
5000 apex_available: ["myapex"],
5001 jars: ["my.jar"],
5002 compile_dex: true,
5003 }
5004 `)
5005
5006 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5007 apexRule := module.Rule("apexRule")
5008 copyCmds := apexRule.Args["copy_commands"]
5009 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5010}
5011
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005012func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005013 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005014 apex {
5015 name: "myapex",
5016 key: "myapex.key",
5017 apps: [
5018 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005019 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005020 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005021 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005022 }
5023
5024 apex_key {
5025 name: "myapex.key",
5026 public_key: "testkey.avbpubkey",
5027 private_key: "testkey.pem",
5028 }
5029
5030 android_app {
5031 name: "AppFoo",
5032 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005033 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005034 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005035 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005036 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005037 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005038 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005039
5040 android_app {
5041 name: "AppFooPriv",
5042 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005043 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005044 system_modules: "none",
5045 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005046 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005047 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005048 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005049
5050 cc_library_shared {
5051 name: "libjni",
5052 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005053 shared_libs: ["libfoo"],
5054 stl: "none",
5055 system_shared_libs: [],
5056 apex_available: [ "myapex" ],
5057 sdk_version: "current",
5058 }
5059
5060 cc_library_shared {
5061 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005062 stl: "none",
5063 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005064 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005065 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005066 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005067 `)
5068
Sundong Ahnabb64432019-10-22 13:58:29 +09005069 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005070 apexRule := module.Rule("apexRule")
5071 copyCmds := apexRule.Args["copy_commands"]
5072
5073 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005074 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005075
Colin Crossaede88c2020-08-11 12:17:01 -07005076 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005077 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005078 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005079 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005080 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005081 // JNI libraries including transitive deps are
5082 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005083 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005084 // ... embedded inside APK (jnilibs.zip)
5085 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5086 // ... and not directly inside the APEX
5087 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5088 }
Dario Frenicde2a032019-10-27 00:29:22 +01005089}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005090
Dario Frenicde2a032019-10-27 00:29:22 +01005091func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005092 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005093 apex {
5094 name: "myapex",
5095 key: "myapex.key",
5096 apps: [
5097 "AppFooPrebuilt",
5098 "AppFooPrivPrebuilt",
5099 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005100 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005101 }
5102
5103 apex_key {
5104 name: "myapex.key",
5105 public_key: "testkey.avbpubkey",
5106 private_key: "testkey.pem",
5107 }
5108
5109 android_app_import {
5110 name: "AppFooPrebuilt",
5111 apk: "PrebuiltAppFoo.apk",
5112 presigned: true,
5113 dex_preopt: {
5114 enabled: false,
5115 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005116 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005117 }
5118
5119 android_app_import {
5120 name: "AppFooPrivPrebuilt",
5121 apk: "PrebuiltAppFooPriv.apk",
5122 privileged: true,
5123 presigned: true,
5124 dex_preopt: {
5125 enabled: false,
5126 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005127 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005128 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005129 }
5130 `)
5131
Sundong Ahnabb64432019-10-22 13:58:29 +09005132 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005133 apexRule := module.Rule("apexRule")
5134 copyCmds := apexRule.Args["copy_commands"]
5135
5136 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005137 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5138}
5139
5140func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005141 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005142 apex {
5143 name: "myapex",
5144 key: "myapex.key",
5145 apps: [
5146 "AppFoo",
5147 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005148 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005149 }
5150
5151 apex_key {
5152 name: "myapex.key",
5153 public_key: "testkey.avbpubkey",
5154 private_key: "testkey.pem",
5155 }
5156
5157 android_app {
5158 name: "AppFoo",
5159 srcs: ["foo/bar/MyClass.java"],
5160 sdk_version: "none",
5161 system_modules: "none",
5162 apex_available: [ "myapex" ],
5163 }
5164
5165 android_app_import {
5166 name: "AppFoo",
5167 apk: "AppFooPrebuilt.apk",
5168 filename: "AppFooPrebuilt.apk",
5169 presigned: true,
5170 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005171 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005172 }
5173 `, withFiles(map[string][]byte{
5174 "AppFooPrebuilt.apk": nil,
5175 }))
5176
5177 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005178 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005179 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005180}
5181
Dario Freni6f3937c2019-12-20 22:58:03 +00005182func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005183 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005184 apex {
5185 name: "myapex",
5186 key: "myapex.key",
5187 apps: [
5188 "TesterHelpAppFoo",
5189 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005190 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005191 }
5192
5193 apex_key {
5194 name: "myapex.key",
5195 public_key: "testkey.avbpubkey",
5196 private_key: "testkey.pem",
5197 }
5198
5199 android_test_helper_app {
5200 name: "TesterHelpAppFoo",
5201 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005202 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005203 }
5204
5205 `)
5206
5207 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5208 apexRule := module.Rule("apexRule")
5209 copyCmds := apexRule.Args["copy_commands"]
5210
5211 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5212}
5213
Jooyung Han18020ea2019-11-13 10:50:48 +09005214func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5215 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005216 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005217 apex {
5218 name: "myapex",
5219 key: "myapex.key",
5220 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005221 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005222 }
5223
5224 apex_key {
5225 name: "myapex.key",
5226 public_key: "testkey.avbpubkey",
5227 private_key: "testkey.pem",
5228 }
5229
5230 apex {
5231 name: "otherapex",
5232 key: "myapex.key",
5233 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005234 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005235 }
5236
5237 cc_defaults {
5238 name: "libfoo-defaults",
5239 apex_available: ["otherapex"],
5240 }
5241
5242 cc_library {
5243 name: "libfoo",
5244 defaults: ["libfoo-defaults"],
5245 stl: "none",
5246 system_shared_libs: [],
5247 }`)
5248}
5249
Paul Duffine52e66f2020-03-30 17:54:29 +01005250func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005251 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005252 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005253 apex {
5254 name: "myapex",
5255 key: "myapex.key",
5256 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005257 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005258 }
5259
5260 apex_key {
5261 name: "myapex.key",
5262 public_key: "testkey.avbpubkey",
5263 private_key: "testkey.pem",
5264 }
5265
5266 apex {
5267 name: "otherapex",
5268 key: "otherapex.key",
5269 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005270 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005271 }
5272
5273 apex_key {
5274 name: "otherapex.key",
5275 public_key: "testkey.avbpubkey",
5276 private_key: "testkey.pem",
5277 }
5278
5279 cc_library {
5280 name: "libfoo",
5281 stl: "none",
5282 system_shared_libs: [],
5283 apex_available: ["otherapex"],
5284 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005285}
Jiyong Park127b40b2019-09-30 16:04:35 +09005286
Paul Duffine52e66f2020-03-30 17:54:29 +01005287func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005288 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005289 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005290.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005291.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005292.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005293.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005294.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005295.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005296 apex {
5297 name: "myapex",
5298 key: "myapex.key",
5299 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005300 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005301 }
5302
5303 apex_key {
5304 name: "myapex.key",
5305 public_key: "testkey.avbpubkey",
5306 private_key: "testkey.pem",
5307 }
5308
Jiyong Park127b40b2019-09-30 16:04:35 +09005309 cc_library {
5310 name: "libfoo",
5311 stl: "none",
5312 shared_libs: ["libbar"],
5313 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005314 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005315 }
5316
5317 cc_library {
5318 name: "libbar",
5319 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005320 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005321 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005322 apex_available: ["myapex"],
5323 }
5324
5325 cc_library {
5326 name: "libbaz",
5327 stl: "none",
5328 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005329 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005330}
Jiyong Park127b40b2019-09-30 16:04:35 +09005331
Paul Duffine52e66f2020-03-30 17:54:29 +01005332func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005333 testApexError(t, "\"otherapex\" is not a valid module name", `
5334 apex {
5335 name: "myapex",
5336 key: "myapex.key",
5337 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005338 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005339 }
5340
5341 apex_key {
5342 name: "myapex.key",
5343 public_key: "testkey.avbpubkey",
5344 private_key: "testkey.pem",
5345 }
5346
5347 cc_library {
5348 name: "libfoo",
5349 stl: "none",
5350 system_shared_libs: [],
5351 apex_available: ["otherapex"],
5352 }`)
5353
Paul Duffine52e66f2020-03-30 17:54:29 +01005354 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005355 apex {
5356 name: "myapex",
5357 key: "myapex.key",
5358 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005359 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005360 }
5361
5362 apex_key {
5363 name: "myapex.key",
5364 public_key: "testkey.avbpubkey",
5365 private_key: "testkey.pem",
5366 }
5367
5368 cc_library {
5369 name: "libfoo",
5370 stl: "none",
5371 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005372 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005373 apex_available: ["myapex"],
5374 }
5375
5376 cc_library {
5377 name: "libbar",
5378 stl: "none",
5379 system_shared_libs: [],
5380 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005381 }
5382
5383 cc_library {
5384 name: "libbaz",
5385 stl: "none",
5386 system_shared_libs: [],
5387 stubs: {
5388 versions: ["10", "20", "30"],
5389 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005390 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005391}
Jiyong Park127b40b2019-09-30 16:04:35 +09005392
Jiyong Park89e850a2020-04-07 16:37:39 +09005393func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005394 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005395 apex {
5396 name: "myapex",
5397 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005398 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005399 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005400 }
5401
5402 apex_key {
5403 name: "myapex.key",
5404 public_key: "testkey.avbpubkey",
5405 private_key: "testkey.pem",
5406 }
5407
5408 cc_library {
5409 name: "libfoo",
5410 stl: "none",
5411 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005412 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005413 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005414 }
5415
5416 cc_library {
5417 name: "libfoo2",
5418 stl: "none",
5419 system_shared_libs: [],
5420 shared_libs: ["libbaz"],
5421 apex_available: ["//apex_available:platform"],
5422 }
5423
5424 cc_library {
5425 name: "libbar",
5426 stl: "none",
5427 system_shared_libs: [],
5428 apex_available: ["myapex"],
5429 }
5430
5431 cc_library {
5432 name: "libbaz",
5433 stl: "none",
5434 system_shared_libs: [],
5435 apex_available: ["myapex"],
5436 stubs: {
5437 versions: ["1"],
5438 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005439 }`)
5440
Jiyong Park89e850a2020-04-07 16:37:39 +09005441 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5442 // because it depends on libbar which isn't available to platform
5443 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5444 if libfoo.NotAvailableForPlatform() != true {
5445 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5446 }
5447
5448 // libfoo2 however can be available to platform because it depends on libbaz which provides
5449 // stubs
5450 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5451 if libfoo2.NotAvailableForPlatform() == true {
5452 t.Errorf("%q should be available to platform", libfoo2.String())
5453 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005454}
Jiyong Parka90ca002019-10-07 15:47:24 +09005455
Paul Duffine52e66f2020-03-30 17:54:29 +01005456func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005457 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005458 apex {
5459 name: "myapex",
5460 key: "myapex.key",
5461 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005462 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005463 }
5464
5465 apex_key {
5466 name: "myapex.key",
5467 public_key: "testkey.avbpubkey",
5468 private_key: "testkey.pem",
5469 }
5470
5471 cc_library {
5472 name: "libfoo",
5473 stl: "none",
5474 system_shared_libs: [],
5475 apex_available: ["myapex"],
5476 static: {
5477 apex_available: ["//apex_available:platform"],
5478 },
5479 }`)
5480
Jiyong Park89e850a2020-04-07 16:37:39 +09005481 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5482 if libfooShared.NotAvailableForPlatform() != true {
5483 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5484 }
5485 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5486 if libfooStatic.NotAvailableForPlatform() != false {
5487 t.Errorf("%q should be available to platform", libfooStatic.String())
5488 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005489}
5490
Jiyong Park5d790c32019-11-15 18:40:32 +09005491func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005492 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005493 apex {
5494 name: "myapex",
5495 key: "myapex.key",
5496 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005497 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005498 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005499 }
5500
5501 override_apex {
5502 name: "override_myapex",
5503 base: "myapex",
5504 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005505 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005506 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005507 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005508 }
5509
5510 apex_key {
5511 name: "myapex.key",
5512 public_key: "testkey.avbpubkey",
5513 private_key: "testkey.pem",
5514 }
5515
5516 android_app {
5517 name: "app",
5518 srcs: ["foo/bar/MyClass.java"],
5519 package_name: "foo",
5520 sdk_version: "none",
5521 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005522 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005523 }
5524
5525 override_android_app {
5526 name: "override_app",
5527 base: "app",
5528 package_name: "bar",
5529 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005530 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005531
Jiyong Park317645e2019-12-05 13:20:58 +09005532 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5533 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5534 if originalVariant.GetOverriddenBy() != "" {
5535 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5536 }
5537 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5538 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5539 }
5540
Jiyong Park5d790c32019-11-15 18:40:32 +09005541 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5542 apexRule := module.Rule("apexRule")
5543 copyCmds := apexRule.Args["copy_commands"]
5544
5545 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005546 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005547
5548 apexBundle := module.Module().(*apexBundle)
5549 name := apexBundle.Name()
5550 if name != "override_myapex" {
5551 t.Errorf("name should be \"override_myapex\", but was %q", name)
5552 }
5553
Baligh Uddin004d7172020-02-19 21:29:28 -08005554 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5555 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5556 }
5557
Jiyong Park20bacab2020-03-03 11:45:41 +09005558 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005559 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005560
Colin Crossaa255532020-07-03 13:18:24 -07005561 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005562 var builder strings.Builder
5563 data.Custom(&builder, name, "TARGET_", "", data)
5564 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005565 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005566 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5567 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005568 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005569 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005570 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005571 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5572 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005573}
5574
Jooyung Han214bf372019-11-12 13:03:50 +09005575func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005576 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005577 apex {
5578 name: "myapex",
5579 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005580 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005581 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005582 }
5583
5584 apex_key {
5585 name: "myapex.key",
5586 public_key: "testkey.avbpubkey",
5587 private_key: "testkey.pem",
5588 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005589
5590 cc_library {
5591 name: "mylib",
5592 srcs: ["mylib.cpp"],
5593 stl: "libc++",
5594 system_shared_libs: [],
5595 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005596 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005597 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005598 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005599
5600 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5601 args := module.Rule("apexRule").Args
5602 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005603 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005604
5605 // The copies of the libraries in the apex should have one more dependency than
5606 // the ones outside the apex, namely the unwinder. Ideally we should check
5607 // the dependency names directly here but for some reason the names are blank in
5608 // this test.
5609 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005610 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005611 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5612 if len(apexImplicits) != len(nonApexImplicits)+1 {
5613 t.Errorf("%q missing unwinder dep", lib)
5614 }
5615 }
Jooyung Han214bf372019-11-12 13:03:50 +09005616}
5617
Paul Duffine05480a2021-03-08 15:07:14 +00005618var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005619 "api/current.txt": nil,
5620 "api/removed.txt": nil,
5621 "api/system-current.txt": nil,
5622 "api/system-removed.txt": nil,
5623 "api/test-current.txt": nil,
5624 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005625
Anton Hanssondff2c782020-12-21 17:10:01 +00005626 "100/public/api/foo.txt": nil,
5627 "100/public/api/foo-removed.txt": nil,
5628 "100/system/api/foo.txt": nil,
5629 "100/system/api/foo-removed.txt": nil,
5630
Paul Duffineedc5d52020-06-12 17:46:39 +01005631 // For java_sdk_library_import
5632 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005633}
5634
Jooyung Han58f26ab2019-12-18 15:34:32 +09005635func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005636 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005637 apex {
5638 name: "myapex",
5639 key: "myapex.key",
5640 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005641 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005642 }
5643
5644 apex_key {
5645 name: "myapex.key",
5646 public_key: "testkey.avbpubkey",
5647 private_key: "testkey.pem",
5648 }
5649
5650 java_sdk_library {
5651 name: "foo",
5652 srcs: ["a.java"],
5653 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005654 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005655 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005656
5657 prebuilt_apis {
5658 name: "sdk",
5659 api_dirs: ["100"],
5660 }
Paul Duffin9b879592020-05-26 13:21:35 +01005661 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005662
5663 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005664 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005665 "javalib/foo.jar",
5666 "etc/permissions/foo.xml",
5667 })
5668 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005669 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5670 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005671}
5672
Paul Duffin9b879592020-05-26 13:21:35 +01005673func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005674 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005675 apex {
5676 name: "myapex",
5677 key: "myapex.key",
5678 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005679 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005680 }
5681
5682 apex_key {
5683 name: "myapex.key",
5684 public_key: "testkey.avbpubkey",
5685 private_key: "testkey.pem",
5686 }
5687
5688 java_sdk_library {
5689 name: "foo",
5690 srcs: ["a.java"],
5691 api_packages: ["foo"],
5692 apex_available: ["myapex"],
5693 sdk_version: "none",
5694 system_modules: "none",
5695 }
5696
5697 java_library {
5698 name: "bar",
5699 srcs: ["a.java"],
5700 libs: ["foo"],
5701 apex_available: ["myapex"],
5702 sdk_version: "none",
5703 system_modules: "none",
5704 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005705
5706 prebuilt_apis {
5707 name: "sdk",
5708 api_dirs: ["100"],
5709 }
Paul Duffin9b879592020-05-26 13:21:35 +01005710 `, withFiles(filesForSdkLibrary))
5711
5712 // java_sdk_library installs both impl jar and permission XML
5713 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5714 "javalib/bar.jar",
5715 "javalib/foo.jar",
5716 "etc/permissions/foo.xml",
5717 })
5718
5719 // The bar library should depend on the implementation jar.
5720 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5721 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5722 t.Errorf("expected %q, found %#q", expected, actual)
5723 }
5724}
5725
5726func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005727 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005728 apex {
5729 name: "myapex",
5730 key: "myapex.key",
5731 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005732 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005733 }
5734
5735 apex_key {
5736 name: "myapex.key",
5737 public_key: "testkey.avbpubkey",
5738 private_key: "testkey.pem",
5739 }
5740
5741 java_sdk_library {
5742 name: "foo",
5743 srcs: ["a.java"],
5744 api_packages: ["foo"],
5745 apex_available: ["myapex"],
5746 sdk_version: "none",
5747 system_modules: "none",
5748 }
5749
5750 java_library {
5751 name: "bar",
5752 srcs: ["a.java"],
5753 libs: ["foo"],
5754 sdk_version: "none",
5755 system_modules: "none",
5756 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005757
5758 prebuilt_apis {
5759 name: "sdk",
5760 api_dirs: ["100"],
5761 }
Paul Duffin9b879592020-05-26 13:21:35 +01005762 `, withFiles(filesForSdkLibrary))
5763
5764 // java_sdk_library installs both impl jar and permission XML
5765 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5766 "javalib/foo.jar",
5767 "etc/permissions/foo.xml",
5768 })
5769
5770 // The bar library should depend on the stubs jar.
5771 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5772 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5773 t.Errorf("expected %q, found %#q", expected, actual)
5774 }
5775}
5776
Paul Duffineedc5d52020-06-12 17:46:39 +01005777func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005778 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005779 prebuilt_apis {
5780 name: "sdk",
5781 api_dirs: ["100"],
5782 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005783 withFiles(map[string][]byte{
5784 "apex/a.java": nil,
5785 "apex/apex_manifest.json": nil,
5786 "apex/Android.bp": []byte(`
5787 package {
5788 default_visibility: ["//visibility:private"],
5789 }
5790
5791 apex {
5792 name: "myapex",
5793 key: "myapex.key",
5794 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005795 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005796 }
5797
5798 apex_key {
5799 name: "myapex.key",
5800 public_key: "testkey.avbpubkey",
5801 private_key: "testkey.pem",
5802 }
5803
5804 java_library {
5805 name: "bar",
5806 srcs: ["a.java"],
5807 libs: ["foo"],
5808 apex_available: ["myapex"],
5809 sdk_version: "none",
5810 system_modules: "none",
5811 }
5812`),
5813 "source/a.java": nil,
5814 "source/api/current.txt": nil,
5815 "source/api/removed.txt": nil,
5816 "source/Android.bp": []byte(`
5817 package {
5818 default_visibility: ["//visibility:private"],
5819 }
5820
5821 java_sdk_library {
5822 name: "foo",
5823 visibility: ["//apex"],
5824 srcs: ["a.java"],
5825 api_packages: ["foo"],
5826 apex_available: ["myapex"],
5827 sdk_version: "none",
5828 system_modules: "none",
5829 public: {
5830 enabled: true,
5831 },
5832 }
5833`),
5834 "prebuilt/a.jar": nil,
5835 "prebuilt/Android.bp": []byte(`
5836 package {
5837 default_visibility: ["//visibility:private"],
5838 }
5839
5840 java_sdk_library_import {
5841 name: "foo",
5842 visibility: ["//apex", "//source"],
5843 apex_available: ["myapex"],
5844 prefer: true,
5845 public: {
5846 jars: ["a.jar"],
5847 },
5848 }
5849`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005850 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005851 )
5852
5853 // java_sdk_library installs both impl jar and permission XML
5854 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5855 "javalib/bar.jar",
5856 "javalib/foo.jar",
5857 "etc/permissions/foo.xml",
5858 })
5859
5860 // The bar library should depend on the implementation jar.
5861 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5862 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5863 t.Errorf("expected %q, found %#q", expected, actual)
5864 }
5865}
5866
5867func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5868 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5869 apex {
5870 name: "myapex",
5871 key: "myapex.key",
5872 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005873 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005874 }
5875
5876 apex_key {
5877 name: "myapex.key",
5878 public_key: "testkey.avbpubkey",
5879 private_key: "testkey.pem",
5880 }
5881
5882 java_sdk_library_import {
5883 name: "foo",
5884 apex_available: ["myapex"],
5885 prefer: true,
5886 public: {
5887 jars: ["a.jar"],
5888 },
5889 }
5890
5891 `, withFiles(filesForSdkLibrary))
5892}
5893
atrost6e126252020-01-27 17:01:16 +00005894func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005895 result := apexFixtureFactory.
5896 Extend(java.PrepareForTestWithPlatformCompatConfig).
5897 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005898 apex {
5899 name: "myapex",
5900 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005901 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005902 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005903 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005904 }
5905
5906 apex_key {
5907 name: "myapex.key",
5908 public_key: "testkey.avbpubkey",
5909 private_key: "testkey.pem",
5910 }
5911
5912 platform_compat_config {
5913 name: "myjar-platform-compat-config",
5914 src: ":myjar",
5915 }
5916
5917 java_library {
5918 name: "myjar",
5919 srcs: ["foo/bar/MyClass.java"],
5920 sdk_version: "none",
5921 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005922 apex_available: [ "myapex" ],
5923 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005924
5925 // Make sure that a preferred prebuilt does not affect the apex contents.
5926 prebuilt_platform_compat_config {
5927 name: "myjar-platform-compat-config",
5928 metadata: "compat-config/metadata.xml",
5929 prefer: true,
5930 }
atrost6e126252020-01-27 17:01:16 +00005931 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005932 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005933 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5934 "etc/compatconfig/myjar-platform-compat-config.xml",
5935 "javalib/myjar.jar",
5936 })
5937}
5938
Jiyong Park479321d2019-12-16 11:47:12 +09005939func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5940 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5941 apex {
5942 name: "myapex",
5943 key: "myapex.key",
5944 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005945 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005946 }
5947
5948 apex_key {
5949 name: "myapex.key",
5950 public_key: "testkey.avbpubkey",
5951 private_key: "testkey.pem",
5952 }
5953
5954 java_library {
5955 name: "myjar",
5956 srcs: ["foo/bar/MyClass.java"],
5957 sdk_version: "none",
5958 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005959 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005960 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005961 }
5962 `)
5963}
5964
Jiyong Park7afd1072019-12-30 16:56:33 +09005965func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005966 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005967 apex {
5968 name: "myapex",
5969 key: "myapex.key",
5970 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005971 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005972 }
5973
5974 apex_key {
5975 name: "myapex.key",
5976 public_key: "testkey.avbpubkey",
5977 private_key: "testkey.pem",
5978 }
5979
5980 cc_library {
5981 name: "mylib",
5982 srcs: ["mylib.cpp"],
5983 system_shared_libs: [],
5984 stl: "none",
5985 required: ["a", "b"],
5986 host_required: ["c", "d"],
5987 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005988 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005989 }
5990 `)
5991
5992 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005993 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005994 name := apexBundle.BaseModuleName()
5995 prefix := "TARGET_"
5996 var builder strings.Builder
5997 data.Custom(&builder, name, prefix, "", data)
5998 androidMk := builder.String()
5999 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6000 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6001 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6002}
6003
Jiyong Park7cd10e32020-01-14 09:22:18 +09006004func TestSymlinksFromApexToSystem(t *testing.T) {
6005 bp := `
6006 apex {
6007 name: "myapex",
6008 key: "myapex.key",
6009 native_shared_libs: ["mylib"],
6010 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006011 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006012 }
6013
Jiyong Park9d677202020-02-19 16:29:35 +09006014 apex {
6015 name: "myapex.updatable",
6016 key: "myapex.key",
6017 native_shared_libs: ["mylib"],
6018 java_libs: ["myjar"],
6019 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006020 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006021 }
6022
Jiyong Park7cd10e32020-01-14 09:22:18 +09006023 apex_key {
6024 name: "myapex.key",
6025 public_key: "testkey.avbpubkey",
6026 private_key: "testkey.pem",
6027 }
6028
6029 cc_library {
6030 name: "mylib",
6031 srcs: ["mylib.cpp"],
6032 shared_libs: ["myotherlib"],
6033 system_shared_libs: [],
6034 stl: "none",
6035 apex_available: [
6036 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006037 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006038 "//apex_available:platform",
6039 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006040 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006041 }
6042
6043 cc_library {
6044 name: "myotherlib",
6045 srcs: ["mylib.cpp"],
6046 system_shared_libs: [],
6047 stl: "none",
6048 apex_available: [
6049 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006050 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006051 "//apex_available:platform",
6052 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006053 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006054 }
6055
6056 java_library {
6057 name: "myjar",
6058 srcs: ["foo/bar/MyClass.java"],
6059 sdk_version: "none",
6060 system_modules: "none",
6061 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006062 apex_available: [
6063 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006064 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006065 "//apex_available:platform",
6066 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006067 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006068 }
6069
6070 java_library {
6071 name: "myotherjar",
6072 srcs: ["foo/bar/MyClass.java"],
6073 sdk_version: "none",
6074 system_modules: "none",
6075 apex_available: [
6076 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006077 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006078 "//apex_available:platform",
6079 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006080 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006081 }
6082 `
6083
6084 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6085 for _, f := range files {
6086 if f.path == file {
6087 if f.isLink {
6088 t.Errorf("%q is not a real file", file)
6089 }
6090 return
6091 }
6092 }
6093 t.Errorf("%q is not found", file)
6094 }
6095
6096 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6097 for _, f := range files {
6098 if f.path == file {
6099 if !f.isLink {
6100 t.Errorf("%q is not a symlink", file)
6101 }
6102 return
6103 }
6104 }
6105 t.Errorf("%q is not found", file)
6106 }
6107
Jiyong Park9d677202020-02-19 16:29:35 +09006108 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6109 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006110 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006111 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006112 ensureRealfileExists(t, files, "javalib/myjar.jar")
6113 ensureRealfileExists(t, files, "lib64/mylib.so")
6114 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6115
Jiyong Park9d677202020-02-19 16:29:35 +09006116 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6117 ensureRealfileExists(t, files, "javalib/myjar.jar")
6118 ensureRealfileExists(t, files, "lib64/mylib.so")
6119 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6120
6121 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006122 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006123 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006124 ensureRealfileExists(t, files, "javalib/myjar.jar")
6125 ensureRealfileExists(t, files, "lib64/mylib.so")
6126 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006127
6128 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6129 ensureRealfileExists(t, files, "javalib/myjar.jar")
6130 ensureRealfileExists(t, files, "lib64/mylib.so")
6131 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006132}
6133
Yo Chiange8128052020-07-23 20:09:18 +08006134func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006135 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006136 apex {
6137 name: "myapex",
6138 key: "myapex.key",
6139 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006140 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006141 }
6142
6143 apex_key {
6144 name: "myapex.key",
6145 public_key: "testkey.avbpubkey",
6146 private_key: "testkey.pem",
6147 }
6148
6149 cc_library_shared {
6150 name: "mylib",
6151 srcs: ["mylib.cpp"],
6152 shared_libs: ["myotherlib"],
6153 system_shared_libs: [],
6154 stl: "none",
6155 apex_available: [
6156 "myapex",
6157 "//apex_available:platform",
6158 ],
6159 }
6160
6161 cc_prebuilt_library_shared {
6162 name: "myotherlib",
6163 srcs: ["prebuilt.so"],
6164 system_shared_libs: [],
6165 stl: "none",
6166 apex_available: [
6167 "myapex",
6168 "//apex_available:platform",
6169 ],
6170 }
6171 `)
6172
6173 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006174 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006175 var builder strings.Builder
6176 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6177 androidMk := builder.String()
6178 // `myotherlib` is added to `myapex` as symlink
6179 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6180 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6181 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6182 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006183 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 +08006184}
6185
Jooyung Han643adc42020-02-27 13:50:06 +09006186func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006187 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006188 apex {
6189 name: "myapex",
6190 key: "myapex.key",
6191 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006192 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006193 }
6194
6195 apex_key {
6196 name: "myapex.key",
6197 public_key: "testkey.avbpubkey",
6198 private_key: "testkey.pem",
6199 }
6200
6201 cc_library {
6202 name: "mylib",
6203 srcs: ["mylib.cpp"],
6204 shared_libs: ["mylib2"],
6205 system_shared_libs: [],
6206 stl: "none",
6207 apex_available: [ "myapex" ],
6208 }
6209
6210 cc_library {
6211 name: "mylib2",
6212 srcs: ["mylib.cpp"],
6213 system_shared_libs: [],
6214 stl: "none",
6215 apex_available: [ "myapex" ],
6216 }
6217 `)
6218
6219 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6220 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6221 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6222 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6223 "lib64/mylib.so",
6224 "lib64/mylib2.so",
6225 })
6226}
6227
Jooyung Han49f67012020-04-17 13:43:10 +09006228func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006229 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006230 apex {
6231 name: "myapex",
6232 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006233 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006234 }
6235 apex_key {
6236 name: "myapex.key",
6237 public_key: "testkey.avbpubkey",
6238 private_key: "testkey.pem",
6239 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006240 `,
6241 android.FixtureModifyConfig(func(config android.Config) {
6242 delete(config.Targets, android.Android)
6243 config.AndroidCommonTarget = android.Target{}
6244 }),
6245 )
Jooyung Han49f67012020-04-17 13:43:10 +09006246
6247 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6248 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6249 }
6250}
6251
Jiyong Parkbd159612020-02-28 15:22:21 +09006252func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006253 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006254 apex {
6255 name: "myapex",
6256 key: "myapex.key",
6257 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006258 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006259 }
6260
6261 apex_key {
6262 name: "myapex.key",
6263 public_key: "testkey.avbpubkey",
6264 private_key: "testkey.pem",
6265 }
6266
6267 android_app {
6268 name: "AppFoo",
6269 srcs: ["foo/bar/MyClass.java"],
6270 sdk_version: "none",
6271 system_modules: "none",
6272 apex_available: [ "myapex" ],
6273 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006274 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006275
Colin Crosscf371cc2020-11-13 11:48:42 -08006276 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006277 content := bundleConfigRule.Args["content"]
6278
6279 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006280 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 +09006281}
6282
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006283func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006284 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006285 apex {
6286 name: "myapex",
6287 key: "myapex.key",
6288 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006289 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006290 }
6291
6292 apex_key {
6293 name: "myapex.key",
6294 public_key: "testkey.avbpubkey",
6295 private_key: "testkey.pem",
6296 }
6297
6298 android_app_set {
6299 name: "AppSet",
6300 set: "AppSet.apks",
6301 }`)
6302 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006303 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006304 content := bundleConfigRule.Args["content"]
6305 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6306 s := mod.Rule("apexRule").Args["copy_commands"]
6307 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6308 if len(copyCmds) != 3 {
6309 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6310 }
6311 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6312 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6313 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6314}
6315
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006316func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006317 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006318 bp := `
6319 apex_set {
6320 name: "myapex",
6321 filename: "foo_v2.apex",
6322 sanitized: {
6323 none: { set: "myapex.apks", },
6324 hwaddress: { set: "myapex.hwasan.apks", },
6325 },
6326 }`
6327 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006328 }),
6329 prepareForTestWithSantitizeHwaddress,
6330 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006331
6332 m := ctx.ModuleForTests("myapex", "android_common")
6333 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6334
6335 actual := extractedApex.Inputs
6336 if len(actual) != 1 {
6337 t.Errorf("expected a single input")
6338 }
6339
6340 expected := "myapex.hwasan.apks"
6341 if actual[0].String() != expected {
6342 t.Errorf("expected %s, got %s", expected, actual[0].String())
6343 }
6344}
6345
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006346func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006347 t.Helper()
6348
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006349 bp := `
6350 java_library {
6351 name: "some-updatable-apex-lib",
6352 srcs: ["a.java"],
6353 sdk_version: "current",
6354 apex_available: [
6355 "some-updatable-apex",
6356 ],
6357 }
6358
6359 java_library {
6360 name: "some-non-updatable-apex-lib",
6361 srcs: ["a.java"],
6362 apex_available: [
6363 "some-non-updatable-apex",
6364 ],
6365 }
6366
6367 java_library {
6368 name: "some-platform-lib",
6369 srcs: ["a.java"],
6370 sdk_version: "current",
6371 installable: true,
6372 }
6373
6374 java_library {
6375 name: "some-art-lib",
6376 srcs: ["a.java"],
6377 sdk_version: "current",
6378 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006379 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006380 ],
6381 hostdex: true,
6382 }
6383
6384 apex {
6385 name: "some-updatable-apex",
6386 key: "some-updatable-apex.key",
6387 java_libs: ["some-updatable-apex-lib"],
6388 updatable: true,
6389 min_sdk_version: "current",
6390 }
6391
6392 apex {
6393 name: "some-non-updatable-apex",
6394 key: "some-non-updatable-apex.key",
6395 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006396 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006397 }
6398
6399 apex_key {
6400 name: "some-updatable-apex.key",
6401 }
6402
6403 apex_key {
6404 name: "some-non-updatable-apex.key",
6405 }
6406
6407 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006408 name: "com.android.art.debug",
6409 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006410 java_libs: ["some-art-lib"],
6411 updatable: true,
6412 min_sdk_version: "current",
6413 }
6414
6415 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006416 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006417 }
6418
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006419 filegroup {
6420 name: "some-updatable-apex-file_contexts",
6421 srcs: [
6422 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6423 ],
6424 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006425
6426 filegroup {
6427 name: "some-non-updatable-apex-file_contexts",
6428 srcs: [
6429 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6430 ],
6431 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006432 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006433
6434 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6435}
6436
Paul Duffin064b70c2020-11-02 17:32:38 +00006437func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006438 t.Helper()
6439
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006440 bp += cc.GatherRequiredDepsForTest(android.Android)
6441 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006442
6443 fs := map[string][]byte{
6444 "a.java": nil,
6445 "a.jar": nil,
6446 "build/make/target/product/security": nil,
6447 "apex_manifest.json": nil,
6448 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006449 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006450 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6451 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6452 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006453 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006454 }
6455 cc.GatherRequiredFilesForTest(fs)
6456
Paul Duffin39853512021-02-26 11:09:39 +00006457 for k, v := range filesForSdkLibrary {
6458 fs[k] = v
6459 }
Colin Crossae8600b2020-10-29 17:09:13 -07006460 config := android.TestArchConfig(buildDir, nil, bp, fs)
6461
6462 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006463 ctx.RegisterModuleType("apex", BundleFactory)
6464 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006465 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006466 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006467 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006468 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006469 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006470 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006471 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006472 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006473 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6474 ctx.PreDepsMutators(RegisterPreDepsMutators)
6475 ctx.PostDepsMutators(RegisterPostDepsMutators)
6476
Colin Crossae8600b2020-10-29 17:09:13 -07006477 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006478
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006479 pathCtx := android.PathContextForTesting(config)
6480 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6481 transformDexpreoptConfig(dexpreoptConfig)
6482 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6483
Paul Duffinf38931c2021-02-05 16:58:28 +00006484 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006485 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006486 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6487 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6488
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006489 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6490 android.FailIfErrored(t, errs)
6491
6492 _, errs = ctx.PrepareBuildActions(config)
6493 if errmsg == "" {
6494 android.FailIfErrored(t, errs)
6495 } else if len(errs) > 0 {
6496 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006497 } else {
6498 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6499 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006500
6501 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006502}
6503
Jooyung Han548640b2020-04-27 12:10:30 +09006504func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6505 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6506 apex {
6507 name: "myapex",
6508 key: "myapex.key",
6509 updatable: true,
6510 }
6511
6512 apex_key {
6513 name: "myapex.key",
6514 public_key: "testkey.avbpubkey",
6515 private_key: "testkey.pem",
6516 }
6517 `)
6518}
6519
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006520func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6521 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6522 apex {
6523 name: "myapex",
6524 key: "myapex.key",
6525 }
6526
6527 apex_key {
6528 name: "myapex.key",
6529 public_key: "testkey.avbpubkey",
6530 private_key: "testkey.pem",
6531 }
6532 `)
6533}
6534
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006535func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006536 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006537 var transform func(*dexpreopt.GlobalConfig)
6538
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006539 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6540 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006541 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006542 }
6543 testNoUpdatableJarsInBootImage(t, "", transform)
6544 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006545
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006546 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006547 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 +01006548 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006549 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006550 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006551 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006552 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006553
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006554 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 -07006555 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 +01006556 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006557 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006558 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006559 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006560 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006561
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006562 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 -07006563 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006564 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006565 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006566 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006567 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006568 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006569
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006570 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 -07006571 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 +01006572 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006573 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006574 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006575 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006576 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006577
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006578 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6579 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006580 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006581 }
6582 testNoUpdatableJarsInBootImage(t, "", transform)
6583 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006584
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006585 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006586 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006588 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006590 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006591 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006592
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006593 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006594 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006595 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006596 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006597 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006598 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006599 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006600
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006601 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006602 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006603 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006604 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006605 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006606 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006607 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006608
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006609 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6610 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006611 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006612 }
6613 testNoUpdatableJarsInBootImage(t, "", transform)
6614 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006615
6616}
6617
6618func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6619 transform := func(config *dexpreopt.GlobalConfig) {
6620 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6621 }
6622 t.Run("prebuilt no source", func(t *testing.T) {
6623 testDexpreoptWithApexes(t, `
6624 prebuilt_apex {
6625 name: "myapex" ,
6626 arch: {
6627 arm64: {
6628 src: "myapex-arm64.apex",
6629 },
6630 arm: {
6631 src: "myapex-arm.apex",
6632 },
6633 },
6634 exported_java_libs: ["libfoo"],
6635 }
6636
6637 java_import {
6638 name: "libfoo",
6639 jars: ["libfoo.jar"],
6640 }
6641`, "", transform)
6642 })
6643
6644 t.Run("prebuilt no source", func(t *testing.T) {
6645 testDexpreoptWithApexes(t, `
6646 prebuilt_apex {
6647 name: "myapex" ,
6648 arch: {
6649 arm64: {
6650 src: "myapex-arm64.apex",
6651 },
6652 arm: {
6653 src: "myapex-arm.apex",
6654 },
6655 },
6656 exported_java_libs: ["libfoo"],
6657 }
6658
6659 java_import {
6660 name: "libfoo",
6661 jars: ["libfoo.jar"],
6662 }
6663`, "", transform)
6664 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006665}
6666
Andrei Onea115e7e72020-06-05 21:14:03 +01006667func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6668 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006669 bp += `
6670 apex_key {
6671 name: "myapex.key",
6672 public_key: "testkey.avbpubkey",
6673 private_key: "testkey.pem",
6674 }`
6675 fs := map[string][]byte{
6676 "lib1/src/A.java": nil,
6677 "lib2/src/B.java": nil,
6678 "system/sepolicy/apex/myapex-file_contexts": nil,
6679 }
6680
Colin Crossae8600b2020-10-29 17:09:13 -07006681 config := android.TestArchConfig(buildDir, nil, bp, fs)
6682 android.SetTestNeverallowRules(config, rules)
6683 updatableBootJars := make([]string, 0, len(apexBootJars))
6684 for _, apexBootJar := range apexBootJars {
6685 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6686 }
6687 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6688
6689 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006690 ctx.RegisterModuleType("apex", BundleFactory)
6691 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6692 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6693 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006694 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006695 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6696 ctx.PreDepsMutators(RegisterPreDepsMutators)
6697 ctx.PostDepsMutators(RegisterPostDepsMutators)
6698 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6699
Colin Crossae8600b2020-10-29 17:09:13 -07006700 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006701
6702 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6703 android.FailIfErrored(t, errs)
6704
6705 _, errs = ctx.PrepareBuildActions(config)
6706 if errmsg == "" {
6707 android.FailIfErrored(t, errs)
6708 } else if len(errs) > 0 {
6709 android.FailIfNoMatchingErrors(t, errmsg, errs)
6710 return
6711 } else {
6712 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6713 }
6714}
6715
6716func TestApexPermittedPackagesRules(t *testing.T) {
6717 testcases := []struct {
6718 name string
6719 expectedError string
6720 bp string
6721 bootJars []string
6722 modulesPackages map[string][]string
6723 }{
6724
6725 {
6726 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6727 expectedError: "",
6728 bp: `
6729 java_library {
6730 name: "bcp_lib1",
6731 srcs: ["lib1/src/*.java"],
6732 permitted_packages: ["foo.bar"],
6733 apex_available: ["myapex"],
6734 sdk_version: "none",
6735 system_modules: "none",
6736 }
6737 java_library {
6738 name: "nonbcp_lib2",
6739 srcs: ["lib2/src/*.java"],
6740 apex_available: ["myapex"],
6741 permitted_packages: ["a.b"],
6742 sdk_version: "none",
6743 system_modules: "none",
6744 }
6745 apex {
6746 name: "myapex",
6747 key: "myapex.key",
6748 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006749 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006750 }`,
6751 bootJars: []string{"bcp_lib1"},
6752 modulesPackages: map[string][]string{
6753 "myapex": []string{
6754 "foo.bar",
6755 },
6756 },
6757 },
6758 {
6759 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6760 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.`,
6761 bp: `
6762 java_library {
6763 name: "bcp_lib1",
6764 srcs: ["lib1/src/*.java"],
6765 apex_available: ["myapex"],
6766 permitted_packages: ["foo.bar"],
6767 sdk_version: "none",
6768 system_modules: "none",
6769 }
6770 java_library {
6771 name: "bcp_lib2",
6772 srcs: ["lib2/src/*.java"],
6773 apex_available: ["myapex"],
6774 permitted_packages: ["foo.bar", "bar.baz"],
6775 sdk_version: "none",
6776 system_modules: "none",
6777 }
6778 apex {
6779 name: "myapex",
6780 key: "myapex.key",
6781 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006782 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006783 }
6784 `,
6785 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6786 modulesPackages: map[string][]string{
6787 "myapex": []string{
6788 "foo.bar",
6789 },
6790 },
6791 },
6792 }
6793 for _, tc := range testcases {
6794 t.Run(tc.name, func(t *testing.T) {
6795 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6796 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6797 })
6798 }
6799}
6800
Jiyong Park62304bb2020-04-13 16:19:48 +09006801func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006802 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006803 apex {
6804 name: "myapex",
6805 key: "myapex.key",
6806 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006807 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006808 }
6809
6810 apex_key {
6811 name: "myapex.key",
6812 public_key: "testkey.avbpubkey",
6813 private_key: "testkey.pem",
6814 }
6815
6816 cc_library {
6817 name: "mylib",
6818 srcs: ["mylib.cpp"],
6819 system_shared_libs: [],
6820 stl: "none",
6821 stubs: {
6822 versions: ["1"],
6823 },
6824 apex_available: ["myapex"],
6825 }
6826
6827 cc_library {
6828 name: "myprivlib",
6829 srcs: ["mylib.cpp"],
6830 system_shared_libs: [],
6831 stl: "none",
6832 apex_available: ["myapex"],
6833 }
6834
6835
6836 cc_test {
6837 name: "mytest",
6838 gtest: false,
6839 srcs: ["mylib.cpp"],
6840 system_shared_libs: [],
6841 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006842 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006843 test_for: ["myapex"]
6844 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006845
6846 cc_library {
6847 name: "mytestlib",
6848 srcs: ["mylib.cpp"],
6849 system_shared_libs: [],
6850 shared_libs: ["mylib", "myprivlib"],
6851 stl: "none",
6852 test_for: ["myapex"],
6853 }
6854
6855 cc_benchmark {
6856 name: "mybench",
6857 srcs: ["mylib.cpp"],
6858 system_shared_libs: [],
6859 shared_libs: ["mylib", "myprivlib"],
6860 stl: "none",
6861 test_for: ["myapex"],
6862 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006863 `)
6864
6865 // the test 'mytest' is a test for the apex, therefore is linked to the
6866 // actual implementation of mylib instead of its stub.
6867 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6868 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6869 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006870
6871 // The same should be true for cc_library
6872 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6873 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6874 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6875
6876 // ... and for cc_benchmark
6877 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6878 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6879 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006880}
6881
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006882// TODO(jungjw): Move this to proptools
6883func intPtr(i int) *int {
6884 return &i
6885}
6886
6887func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006888 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006889 apex_set {
6890 name: "myapex",
6891 set: "myapex.apks",
6892 filename: "foo_v2.apex",
6893 overrides: ["foo"],
6894 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006895 `,
6896 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6897 variables.Platform_sdk_version = intPtr(30)
6898 }),
6899 android.FixtureModifyConfig(func(config android.Config) {
6900 config.Targets[android.Android] = []android.Target{
6901 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6902 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6903 }
6904 }),
6905 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006906
6907 m := ctx.ModuleForTests("myapex", "android_common")
6908
6909 // Check extract_apks tool parameters.
6910 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6911 actual := extractedApex.Args["abis"]
6912 expected := "ARMEABI_V7A,ARM64_V8A"
6913 if actual != expected {
6914 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6915 }
6916 actual = extractedApex.Args["sdk-version"]
6917 expected = "30"
6918 if actual != expected {
6919 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6920 }
6921
6922 a := m.Module().(*ApexSet)
6923 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006924 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006925 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6926 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6927 }
6928}
6929
Jiyong Park7d95a512020-05-10 15:16:24 +09006930func TestNoStaticLinkingToStubsLib(t *testing.T) {
6931 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6932 apex {
6933 name: "myapex",
6934 key: "myapex.key",
6935 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006936 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006937 }
6938
6939 apex_key {
6940 name: "myapex.key",
6941 public_key: "testkey.avbpubkey",
6942 private_key: "testkey.pem",
6943 }
6944
6945 cc_library {
6946 name: "mylib",
6947 srcs: ["mylib.cpp"],
6948 static_libs: ["otherlib"],
6949 system_shared_libs: [],
6950 stl: "none",
6951 apex_available: [ "myapex" ],
6952 }
6953
6954 cc_library {
6955 name: "otherlib",
6956 srcs: ["mylib.cpp"],
6957 system_shared_libs: [],
6958 stl: "none",
6959 stubs: {
6960 versions: ["1", "2", "3"],
6961 },
6962 apex_available: [ "myapex" ],
6963 }
6964 `)
6965}
6966
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006967func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006968 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006969 apex {
6970 name: "myapex",
6971 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006972 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006973 }
6974
6975 apex_key {
6976 name: "myapex.key",
6977 public_key: "testkey.avbpubkey",
6978 private_key: "testkey.pem",
6979 }
6980
6981 prebuilt_apex {
6982 name: "myapex",
6983 prefer: true,
6984 arch: {
6985 arm64: {
6986 src: "myapex-arm64.apex",
6987 },
6988 arm: {
6989 src: "myapex-arm.apex",
6990 },
6991 },
6992 }
6993
6994 apex_set {
6995 name: "myapex_set",
6996 set: "myapex.apks",
6997 filename: "myapex_set.apex",
6998 overrides: ["myapex"],
6999 }
7000 `)
7001
7002 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7003 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7004 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 +09007005 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 +09007006}
7007
Jooyung Han938b5932020-06-20 12:47:47 +09007008func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007009 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007010 apex {
7011 name: "myapex",
7012 key: "myapex.key",
7013 apps: ["app"],
7014 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007015 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007016 }
7017
7018 apex_key {
7019 name: "myapex.key",
7020 public_key: "testkey.avbpubkey",
7021 private_key: "testkey.pem",
7022 }
7023
7024 android_app {
7025 name: "app",
7026 srcs: ["foo/bar/MyClass.java"],
7027 package_name: "foo",
7028 sdk_version: "none",
7029 system_modules: "none",
7030 apex_available: [ "myapex" ],
7031 }
7032 `, withFiles(map[string][]byte{
7033 "sub/Android.bp": []byte(`
7034 override_apex {
7035 name: "override_myapex",
7036 base: "myapex",
7037 apps: ["override_app"],
7038 allowed_files: ":allowed",
7039 }
7040 // Overridable "path" property should be referenced indirectly
7041 filegroup {
7042 name: "allowed",
7043 srcs: ["allowed.txt"],
7044 }
7045 override_android_app {
7046 name: "override_app",
7047 base: "app",
7048 package_name: "bar",
7049 }
7050 `),
7051 }))
7052
7053 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7054 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7055 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7056 }
7057
7058 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7059 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7060 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7061 }
7062}
7063
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007064func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007065 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007066 apex {
7067 name: "myapex",
7068 key: "myapex.key",
7069 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007070 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007071 }
7072
7073 apex_key {
7074 name: "myapex.key",
7075 public_key: "testkey.avbpubkey",
7076 private_key: "testkey.pem",
7077 }
7078
7079 cc_library {
7080 name: "mylib",
7081 srcs: ["mylib.cpp"],
7082 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007083 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007084 },
7085 apex_available: ["myapex"],
7086 }
7087
7088 cc_prebuilt_library_shared {
7089 name: "mylib",
7090 prefer: false,
7091 srcs: ["prebuilt.so"],
7092 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007093 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007094 },
7095 apex_available: ["myapex"],
7096 }
7097 `)
7098}
7099
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007100func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007101 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007102 apex {
7103 name: "myapex",
7104 key: "myapex.key",
7105 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007106 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007107 }
7108 apex_key {
7109 name: "myapex.key",
7110 public_key: "testkey.avbpubkey",
7111 private_key: "testkey.pem",
7112 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007113 `,
7114 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7115 variables.CompressedApex = proptools.BoolPtr(true)
7116 }),
7117 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007118
7119 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7120 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7121
7122 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7123 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7124
7125 // Make sure output of bundle is .capex
7126 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7127 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7128
7129 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007130 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007131 var builder strings.Builder
7132 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7133 androidMk := builder.String()
7134 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7135}
7136
Martin Stjernholm2856c662020-12-02 15:03:42 +00007137func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007138 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007139 apex {
7140 name: "myapex",
7141 key: "myapex.key",
7142 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007143 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007144 }
7145
7146 apex_key {
7147 name: "myapex.key",
7148 public_key: "testkey.avbpubkey",
7149 private_key: "testkey.pem",
7150 }
7151
7152 cc_library {
7153 name: "mylib",
7154 srcs: ["mylib.cpp"],
7155 apex_available: ["myapex"],
7156 shared_libs: ["otherlib"],
7157 system_shared_libs: [],
7158 }
7159
7160 cc_library {
7161 name: "otherlib",
7162 srcs: ["mylib.cpp"],
7163 stubs: {
7164 versions: ["current"],
7165 },
7166 }
7167
7168 cc_prebuilt_library_shared {
7169 name: "otherlib",
7170 prefer: true,
7171 srcs: ["prebuilt.so"],
7172 stubs: {
7173 versions: ["current"],
7174 },
7175 }
7176 `)
7177
7178 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007179 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007180 var builder strings.Builder
7181 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7182 androidMk := builder.String()
7183
7184 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7185 // a thing there.
7186 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7187}
7188
Jiyong Parke3867542020-12-03 17:28:25 +09007189func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007190 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007191 apex {
7192 name: "myapex",
7193 key: "myapex.key",
7194 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007195 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007196 }
7197
7198 apex_key {
7199 name: "myapex.key",
7200 public_key: "testkey.avbpubkey",
7201 private_key: "testkey.pem",
7202 }
7203
7204 cc_library {
7205 name: "mylib",
7206 srcs: ["mylib.cpp"],
7207 system_shared_libs: [],
7208 stl: "none",
7209 apex_available: ["myapex"],
7210 shared_libs: ["mylib2"],
7211 target: {
7212 apex: {
7213 exclude_shared_libs: ["mylib2"],
7214 },
7215 },
7216 }
7217
7218 cc_library {
7219 name: "mylib2",
7220 srcs: ["mylib.cpp"],
7221 system_shared_libs: [],
7222 stl: "none",
7223 }
7224 `)
7225
7226 // Check if mylib is linked to mylib2 for the non-apex target
7227 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7228 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7229
7230 // Make sure that the link doesn't occur for the apex target
7231 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7232 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7233
7234 // It shouldn't appear in the copy cmd as well.
7235 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7236 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7237}
7238
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007239func TestPrebuiltStubLibDep(t *testing.T) {
7240 bpBase := `
7241 apex {
7242 name: "myapex",
7243 key: "myapex.key",
7244 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007245 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007246 }
7247 apex_key {
7248 name: "myapex.key",
7249 public_key: "testkey.avbpubkey",
7250 private_key: "testkey.pem",
7251 }
7252 cc_library {
7253 name: "mylib",
7254 srcs: ["mylib.cpp"],
7255 apex_available: ["myapex"],
7256 shared_libs: ["stublib"],
7257 system_shared_libs: [],
7258 }
7259 apex {
7260 name: "otherapex",
7261 enabled: %s,
7262 key: "myapex.key",
7263 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007264 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007265 }
7266 `
7267
7268 stublibSourceBp := `
7269 cc_library {
7270 name: "stublib",
7271 srcs: ["mylib.cpp"],
7272 apex_available: ["otherapex"],
7273 system_shared_libs: [],
7274 stl: "none",
7275 stubs: {
7276 versions: ["1"],
7277 },
7278 }
7279 `
7280
7281 stublibPrebuiltBp := `
7282 cc_prebuilt_library_shared {
7283 name: "stublib",
7284 srcs: ["prebuilt.so"],
7285 apex_available: ["otherapex"],
7286 stubs: {
7287 versions: ["1"],
7288 },
7289 %s
7290 }
7291 `
7292
7293 tests := []struct {
7294 name string
7295 stublibBp string
7296 usePrebuilt bool
7297 modNames []string // Modules to collect AndroidMkEntries for
7298 otherApexEnabled []string
7299 }{
7300 {
7301 name: "only_source",
7302 stublibBp: stublibSourceBp,
7303 usePrebuilt: false,
7304 modNames: []string{"stublib"},
7305 otherApexEnabled: []string{"true", "false"},
7306 },
7307 {
7308 name: "source_preferred",
7309 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7310 usePrebuilt: false,
7311 modNames: []string{"stublib", "prebuilt_stublib"},
7312 otherApexEnabled: []string{"true", "false"},
7313 },
7314 {
7315 name: "prebuilt_preferred",
7316 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7317 usePrebuilt: true,
7318 modNames: []string{"stublib", "prebuilt_stublib"},
7319 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7320 },
7321 {
7322 name: "only_prebuilt",
7323 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7324 usePrebuilt: true,
7325 modNames: []string{"stublib"},
7326 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7327 },
7328 }
7329
7330 for _, test := range tests {
7331 t.Run(test.name, func(t *testing.T) {
7332 for _, otherApexEnabled := range test.otherApexEnabled {
7333 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007334 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007335
7336 type modAndMkEntries struct {
7337 mod *cc.Module
7338 mkEntries android.AndroidMkEntries
7339 }
7340 entries := []*modAndMkEntries{}
7341
7342 // Gather shared lib modules that are installable
7343 for _, modName := range test.modNames {
7344 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7345 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7346 continue
7347 }
7348 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007349 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007350 continue
7351 }
Colin Crossaa255532020-07-03 13:18:24 -07007352 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007353 if ent.Disabled {
7354 continue
7355 }
7356 entries = append(entries, &modAndMkEntries{
7357 mod: mod,
7358 mkEntries: ent,
7359 })
7360 }
7361 }
7362 }
7363
7364 var entry *modAndMkEntries = nil
7365 for _, ent := range entries {
7366 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7367 if entry != nil {
7368 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7369 } else {
7370 entry = ent
7371 }
7372 }
7373 }
7374
7375 if entry == nil {
7376 t.Errorf("AndroidMk entry for \"stublib\" missing")
7377 } else {
7378 isPrebuilt := entry.mod.Prebuilt() != nil
7379 if isPrebuilt != test.usePrebuilt {
7380 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7381 }
7382 if !entry.mod.IsStubs() {
7383 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7384 }
7385 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7386 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7387 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007388 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7389 expected := "-D__STUBLIB_API__=1"
7390 if !android.InList(expected, cflags) {
7391 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7392 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007393 }
7394 })
7395 }
7396 })
7397 }
7398}
7399
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007400func TestMain(m *testing.M) {
7401 run := func() int {
7402 setUp()
7403 defer tearDown()
7404
7405 return m.Run()
7406 }
7407
7408 os.Exit(run())
7409}