blob: 1b32f72a69149b49dd6581f52a117a8e548decae [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 Duffin37aad602021-03-08 09:47:16 +0000120var apexFixtureFactory = android.NewFixtureFactory(
121 &buildDir,
122 // General preparers in alphabetical order as test infrastructure will enforce correct
123 // registration order.
124 android.PrepareForTestWithAndroidBuildComponents,
125 bpf.PrepareForTestWithBpf,
126 cc.PrepareForTestWithCcBuildComponents,
127 java.PrepareForTestWithJavaDefaultModules,
128 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
129 rust.PrepareForTestWithRustDefaultModules,
130 sh.PrepareForTestWithShBuildComponents,
131
132 PrepareForTestWithApexBuildComponents,
133
134 // Additional apex test specific preparers.
135 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
136 filegroup {
137 name: "myapex-file_contexts",
138 srcs: [
139 "apex/myapex-file_contexts",
140 ],
141 }
142 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000143 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000144 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000145 "a.java": nil,
146 "PrebuiltAppFoo.apk": nil,
147 "PrebuiltAppFooPriv.apk": nil,
148 "apex_manifest.json": nil,
149 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000150 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
151 "system/sepolicy/apex/myapex2-file_contexts": nil,
152 "system/sepolicy/apex/otherapex-file_contexts": nil,
153 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
154 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
155 "mylib.cpp": nil,
156 "mytest.cpp": nil,
157 "mytest1.cpp": nil,
158 "mytest2.cpp": nil,
159 "mytest3.cpp": nil,
160 "myprebuilt": nil,
161 "my_include": nil,
162 "foo/bar/MyClass.java": nil,
163 "prebuilt.jar": nil,
164 "prebuilt.so": nil,
165 "vendor/foo/devkeys/test.x509.pem": nil,
166 "vendor/foo/devkeys/test.pk8": nil,
167 "testkey.x509.pem": nil,
168 "testkey.pk8": nil,
169 "testkey.override.x509.pem": nil,
170 "testkey.override.pk8": nil,
171 "vendor/foo/devkeys/testkey.avbpubkey": nil,
172 "vendor/foo/devkeys/testkey.pem": nil,
173 "NOTICE": nil,
174 "custom_notice": nil,
175 "custom_notice_for_static_lib": nil,
176 "testkey2.avbpubkey": nil,
177 "testkey2.pem": nil,
178 "myapex-arm64.apex": nil,
179 "myapex-arm.apex": nil,
180 "myapex.apks": nil,
181 "frameworks/base/api/current.txt": nil,
182 "framework/aidl/a.aidl": nil,
183 "build/make/core/proguard.flags": nil,
184 "build/make/core/proguard_basic_keeps.flags": nil,
185 "dummy.txt": nil,
186 "baz": nil,
187 "bar/baz": nil,
188 "testdata/baz": nil,
189 "AppSet.apks": nil,
190 "foo.rs": nil,
191 "libfoo.jar": nil,
192 "libbar.jar": nil,
193 },
194 ),
195
196 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
197 variables.DeviceVndkVersion = proptools.StringPtr("current")
198 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
199 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
200 variables.Platform_sdk_codename = proptools.StringPtr("Q")
201 variables.Platform_sdk_final = proptools.BoolPtr(false)
202 variables.Platform_version_active_codenames = []string{"Q"}
203 variables.Platform_vndk_version = proptools.StringPtr("VER")
204 }),
205)
206
Paul Duffin52bfaa42021-03-23 23:40:12 +0000207var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
208 "system/sepolicy/apex/myapex-file_contexts": nil,
209})
210
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700211func setUp() {
212 var err error
213 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700215 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900216 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217}
218
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700219func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700220 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221}
222
Jooyung Han643adc42020-02-27 13:50:06 +0900223// ensure that 'result' equals 'expected'
224func ensureEquals(t *testing.T, result string, expected string) {
225 t.Helper()
226 if result != expected {
227 t.Errorf("%q != %q", expected, result)
228 }
229}
230
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231// ensure that 'result' contains 'expected'
232func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900233 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900234 if !strings.Contains(result, expected) {
235 t.Errorf("%q is not found in %q", expected, result)
236 }
237}
238
Liz Kammer5bd365f2020-05-27 15:15:11 -0700239// ensure that 'result' contains 'expected' exactly one time
240func ensureContainsOnce(t *testing.T, result string, expected string) {
241 t.Helper()
242 count := strings.Count(result, expected)
243 if count != 1 {
244 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
245 }
246}
247
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248// ensures that 'result' does not contain 'notExpected'
249func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900250 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900251 if strings.Contains(result, notExpected) {
252 t.Errorf("%q is found in %q", notExpected, result)
253 }
254}
255
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700256func ensureMatches(t *testing.T, result string, expectedRex string) {
257 ok, err := regexp.MatchString(expectedRex, result)
258 if err != nil {
259 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
260 return
261 }
262 if !ok {
263 t.Errorf("%s does not match regular expession %s", result, expectedRex)
264 }
265}
266
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900268 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900269 if !android.InList(expected, result) {
270 t.Errorf("%q is not found in %v", expected, result)
271 }
272}
273
274func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900275 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276 if android.InList(notExpected, result) {
277 t.Errorf("%q is found in %v", notExpected, result)
278 }
279}
280
Jooyung Hane1633032019-08-01 17:41:43 +0900281func ensureListEmpty(t *testing.T, result []string) {
282 t.Helper()
283 if len(result) > 0 {
284 t.Errorf("%q is expected to be empty", result)
285 }
286}
287
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000288func ensureListNotEmpty(t *testing.T, result []string) {
289 t.Helper()
290 if len(result) == 0 {
291 t.Errorf("%q is expected to be not empty", result)
292 }
293}
294
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295// Minimal test
296func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800297 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900298 apex_defaults {
299 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900300 manifest: ":myapex.manifest",
301 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900303 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900304 native_shared_libs: [
305 "mylib",
306 "libfoo.ffi",
307 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900308 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800309 multilib: {
310 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900311 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800312 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900313 },
Jiyong Park77acec62020-06-01 21:39:15 +0900314 java_libs: [
315 "myjar",
316 "myjar_dex",
317 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000318 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900319 }
320
Jiyong Park30ca9372019-02-07 16:27:23 +0900321 apex {
322 name: "myapex",
323 defaults: ["myapex-defaults"],
324 }
325
Jiyong Park25fc6a92018-11-18 18:02:45 +0900326 apex_key {
327 name: "myapex.key",
328 public_key: "testkey.avbpubkey",
329 private_key: "testkey.pem",
330 }
331
Jiyong Park809bb722019-02-13 21:33:49 +0900332 filegroup {
333 name: "myapex.manifest",
334 srcs: ["apex_manifest.json"],
335 }
336
337 filegroup {
338 name: "myapex.androidmanifest",
339 srcs: ["AndroidManifest.xml"],
340 }
341
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 cc_library {
343 name: "mylib",
344 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900345 shared_libs: [
346 "mylib2",
347 "libbar.ffi",
348 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 system_shared_libs: [],
350 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000351 // TODO: remove //apex_available:platform
352 apex_available: [
353 "//apex_available:platform",
354 "myapex",
355 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 }
357
Alex Light3d673592019-01-18 14:37:31 -0800358 cc_binary {
359 name: "foo",
360 srcs: ["mylib.cpp"],
361 compile_multilib: "both",
362 multilib: {
363 lib32: {
364 suffix: "32",
365 },
366 lib64: {
367 suffix: "64",
368 },
369 },
370 symlinks: ["foo_link_"],
371 symlink_preferred_arch: true,
372 system_shared_libs: [],
373 static_executable: true,
374 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700375 apex_available: [ "myapex", "com.android.gki.*" ],
376 }
377
Jiyong Park99644e92020-11-17 22:21:02 +0900378 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000379 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900380 srcs: ["foo.rs"],
381 rlibs: ["libfoo.rlib.rust"],
382 dylibs: ["libfoo.dylib.rust"],
383 apex_available: ["myapex"],
384 }
385
386 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000387 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900388 srcs: ["foo.rs"],
389 crate_name: "foo",
390 apex_available: ["myapex"],
391 }
392
393 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000394 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900395 srcs: ["foo.rs"],
396 crate_name: "foo",
397 apex_available: ["myapex"],
398 }
399
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900400 rust_ffi_shared {
401 name: "libfoo.ffi",
402 srcs: ["foo.rs"],
403 crate_name: "foo",
404 apex_available: ["myapex"],
405 }
406
407 rust_ffi_shared {
408 name: "libbar.ffi",
409 srcs: ["foo.rs"],
410 crate_name: "bar",
411 apex_available: ["myapex"],
412 }
413
Yifan Hongd22a84a2020-07-28 17:37:46 -0700414 apex {
415 name: "com.android.gki.fake",
416 binaries: ["foo"],
417 key: "myapex.key",
418 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000419 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800420 }
421
Paul Duffindddd5462020-04-07 15:25:44 +0100422 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900423 name: "mylib2",
424 srcs: ["mylib.cpp"],
425 system_shared_libs: [],
426 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900427 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900428 static_libs: ["libstatic"],
429 // TODO: remove //apex_available:platform
430 apex_available: [
431 "//apex_available:platform",
432 "myapex",
433 ],
434 }
435
Paul Duffindddd5462020-04-07 15:25:44 +0100436 cc_prebuilt_library_shared {
437 name: "mylib2",
438 srcs: ["prebuilt.so"],
439 // TODO: remove //apex_available:platform
440 apex_available: [
441 "//apex_available:platform",
442 "myapex",
443 ],
444 }
445
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 cc_library_static {
447 name: "libstatic",
448 srcs: ["mylib.cpp"],
449 system_shared_libs: [],
450 stl: "none",
451 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900458
459 java_library {
460 name: "myjar",
461 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900462 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900463 sdk_version: "none",
464 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900465 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900466 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000467 // TODO: remove //apex_available:platform
468 apex_available: [
469 "//apex_available:platform",
470 "myapex",
471 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900472 }
473
Jiyong Park77acec62020-06-01 21:39:15 +0900474 dex_import {
475 name: "myjar_dex",
476 jars: ["prebuilt.jar"],
477 apex_available: [
478 "//apex_available:platform",
479 "myapex",
480 ],
481 }
482
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 java_library {
484 name: "myotherjar",
485 srcs: ["foo/bar/MyClass.java"],
486 sdk_version: "none",
487 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900488 // TODO: remove //apex_available:platform
489 apex_available: [
490 "//apex_available:platform",
491 "myapex",
492 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900493 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900494
495 java_library {
496 name: "mysharedjar",
497 srcs: ["foo/bar/MyClass.java"],
498 sdk_version: "none",
499 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900500 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900501 `)
502
Sundong Ahnabb64432019-10-22 13:58:29 +0900503 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900504
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900505 // Make sure that Android.mk is created
506 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700507 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900508 var builder strings.Builder
509 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
510
511 androidMk := builder.String()
512 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
513 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
514
Jiyong Park42cca6c2019-04-01 11:15:50 +0900515 optFlags := apexRule.Args["opt_flags"]
516 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700517 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900518 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900519
Jiyong Park25fc6a92018-11-18 18:02:45 +0900520 copyCmds := apexRule.Args["copy_commands"]
521
522 // Ensure that main rule creates an output
523 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
524
525 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700526 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
527 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
528 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900529 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900530 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900531
532 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700533 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
534 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900535 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
536 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900537 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900538
539 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800540 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
541 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900542 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900543 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900544 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900545 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
546 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900547 // .. but not for java libs
548 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900549 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800550
Colin Cross7113d202019-11-20 16:39:12 -0800551 // Ensure that the platform variant ends with _shared or _common
552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
553 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
555 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
557
558 // Ensure that dynamic dependency to java libs are not included
559 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800560
561 // Ensure that all symlinks are present.
562 found_foo_link_64 := false
563 found_foo := false
564 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900565 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800566 if strings.HasSuffix(cmd, "bin/foo") {
567 found_foo = true
568 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
569 found_foo_link_64 = true
570 }
571 }
572 }
573 good := found_foo && found_foo_link_64
574 if !good {
575 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
576 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900577
Sundong Ahnabb64432019-10-22 13:58:29 +0900578 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700579 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900580 if len(noticeInputs) != 3 {
581 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900582 }
583 ensureListContains(t, noticeInputs, "NOTICE")
584 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900585 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900586
Artur Satayeva8bd1132020-04-27 18:07:06 +0100587 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100588 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100589 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
590 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
591 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100592
593 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100594 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100595 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
596 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
597 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800598}
599
Jooyung Hanf21c7972019-12-16 22:32:06 +0900600func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800601 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900602 apex_defaults {
603 name: "myapex-defaults",
604 key: "myapex.key",
605 prebuilts: ["myetc"],
606 native_shared_libs: ["mylib"],
607 java_libs: ["myjar"],
608 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900609 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800610 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000611 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 }
613
614 prebuilt_etc {
615 name: "myetc",
616 src: "myprebuilt",
617 }
618
619 apex {
620 name: "myapex",
621 defaults: ["myapex-defaults"],
622 }
623
624 apex_key {
625 name: "myapex.key",
626 public_key: "testkey.avbpubkey",
627 private_key: "testkey.pem",
628 }
629
630 cc_library {
631 name: "mylib",
632 system_shared_libs: [],
633 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000634 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900635 }
636
637 java_library {
638 name: "myjar",
639 srcs: ["foo/bar/MyClass.java"],
640 sdk_version: "none",
641 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000642 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900643 }
644
645 android_app {
646 name: "AppFoo",
647 srcs: ["foo/bar/MyClass.java"],
648 sdk_version: "none",
649 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900651 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900652
653 runtime_resource_overlay {
654 name: "rro",
655 theme: "blue",
656 }
657
markchien2f59ec92020-09-02 16:23:38 +0800658 bpf {
659 name: "bpf",
660 srcs: ["bpf.c", "bpf2.c"],
661 }
662
Jooyung Hanf21c7972019-12-16 22:32:06 +0900663 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000664 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 "etc/myetc",
666 "javalib/myjar.jar",
667 "lib64/mylib.so",
668 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900669 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800670 "etc/bpf/bpf.o",
671 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900672 })
673}
674
Jooyung Han01a3ee22019-11-02 02:52:25 +0900675func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800676 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900677 apex {
678 name: "myapex",
679 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000680 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900681 }
682
683 apex_key {
684 name: "myapex.key",
685 public_key: "testkey.avbpubkey",
686 private_key: "testkey.pem",
687 }
688 `)
689
690 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900691 args := module.Rule("apexRule").Args
692 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
693 t.Error("manifest should be apex_manifest.pb, but " + manifest)
694 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900695}
696
Alex Light5098a612018-11-29 17:12:15 -0800697func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800698 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800699 apex {
700 name: "myapex",
701 key: "myapex.key",
702 payload_type: "zip",
703 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000704 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800705 }
706
707 apex_key {
708 name: "myapex.key",
709 public_key: "testkey.avbpubkey",
710 private_key: "testkey.pem",
711 }
712
713 cc_library {
714 name: "mylib",
715 srcs: ["mylib.cpp"],
716 shared_libs: ["mylib2"],
717 system_shared_libs: [],
718 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000719 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800720 }
721
722 cc_library {
723 name: "mylib2",
724 srcs: ["mylib.cpp"],
725 system_shared_libs: [],
726 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000727 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800728 }
729 `)
730
Sundong Ahnabb64432019-10-22 13:58:29 +0900731 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800732 copyCmds := zipApexRule.Args["copy_commands"]
733
734 // Ensure that main rule creates an output
735 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
736
737 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700738 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800739
740 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700741 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800742
743 // Ensure that both direct and indirect deps are copied into apex
744 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
745 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900746}
747
748func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800749 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750 apex {
751 name: "myapex",
752 key: "myapex.key",
753 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000754 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755 }
756
757 apex_key {
758 name: "myapex.key",
759 public_key: "testkey.avbpubkey",
760 private_key: "testkey.pem",
761 }
762
763 cc_library {
764 name: "mylib",
765 srcs: ["mylib.cpp"],
766 shared_libs: ["mylib2", "mylib3"],
767 system_shared_libs: [],
768 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000769 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900770 }
771
772 cc_library {
773 name: "mylib2",
774 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900775 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900776 system_shared_libs: [],
777 stl: "none",
778 stubs: {
779 versions: ["1", "2", "3"],
780 },
781 }
782
783 cc_library {
784 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900785 srcs: ["mylib.cpp"],
786 shared_libs: ["mylib4"],
787 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900788 stl: "none",
789 stubs: {
790 versions: ["10", "11", "12"],
791 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000792 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900793 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900794
795 cc_library {
796 name: "mylib4",
797 srcs: ["mylib.cpp"],
798 system_shared_libs: [],
799 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000800 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900801 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900802 `)
803
Sundong Ahnabb64432019-10-22 13:58:29 +0900804 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900805 copyCmds := apexRule.Args["copy_commands"]
806
807 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800808 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900809
810 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800811 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900812
813 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800814 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900815
Colin Crossaede88c2020-08-11 12:17:01 -0700816 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900817
818 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900819 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900820 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900821 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900822
823 // 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 -0700824 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900825 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700826 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900827
828 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900829 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900830 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900831
832 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700833 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900834
Jooyung Hana57af4a2020-01-23 05:36:59 +0000835 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900836 "lib64/mylib.so",
837 "lib64/mylib3.so",
838 "lib64/mylib4.so",
839 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840}
841
Colin Cross7812fd32020-09-25 12:35:10 -0700842func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
843 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800844 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700845 apex {
846 name: "myapex",
847 key: "myapex.key",
848 native_shared_libs: ["mylib", "mylib3"],
849 min_sdk_version: "29",
850 }
851
852 apex_key {
853 name: "myapex.key",
854 public_key: "testkey.avbpubkey",
855 private_key: "testkey.pem",
856 }
857
858 cc_library {
859 name: "mylib",
860 srcs: ["mylib.cpp"],
861 shared_libs: ["mylib2", "mylib3"],
862 system_shared_libs: [],
863 stl: "none",
864 apex_available: [ "myapex" ],
865 min_sdk_version: "28",
866 }
867
868 cc_library {
869 name: "mylib2",
870 srcs: ["mylib.cpp"],
871 cflags: ["-include mylib.h"],
872 system_shared_libs: [],
873 stl: "none",
874 stubs: {
875 versions: ["28", "29", "30", "current"],
876 },
877 min_sdk_version: "28",
878 }
879
880 cc_library {
881 name: "mylib3",
882 srcs: ["mylib.cpp"],
883 shared_libs: ["mylib4"],
884 system_shared_libs: [],
885 stl: "none",
886 stubs: {
887 versions: ["28", "29", "30", "current"],
888 },
889 apex_available: [ "myapex" ],
890 min_sdk_version: "28",
891 }
892
893 cc_library {
894 name: "mylib4",
895 srcs: ["mylib.cpp"],
896 system_shared_libs: [],
897 stl: "none",
898 apex_available: [ "myapex" ],
899 min_sdk_version: "28",
900 }
901 `)
902
903 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
904 copyCmds := apexRule.Args["copy_commands"]
905
906 // Ensure that direct non-stubs dep is always included
907 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
908
909 // Ensure that indirect stubs dep is not included
910 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
911
912 // Ensure that direct stubs dep is included
913 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
914
915 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
916
Jiyong Park55549df2021-02-26 23:57:23 +0900917 // Ensure that mylib is linking with the latest version of stub for mylib2
918 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700919 // ... and not linking to the non-stub (impl) variant of mylib2
920 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
921
922 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
923 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
924 // .. and not linking to the stubs variant of mylib3
925 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
926
927 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700928 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700929 ensureNotContains(t, mylib2Cflags, "-include ")
930
931 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700932 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700933
934 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
935 "lib64/mylib.so",
936 "lib64/mylib3.so",
937 "lib64/mylib4.so",
938 })
939}
940
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900941func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
942 t.Parallel()
943 // myapex (Z)
944 // mylib -----------------.
945 // |
946 // otherapex (29) |
947 // libstub's versions: 29 Z current
948 // |
949 // <platform> |
950 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800951 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900952 apex {
953 name: "myapex",
954 key: "myapex.key",
955 native_shared_libs: ["mylib"],
956 min_sdk_version: "Z", // non-final
957 }
958
959 cc_library {
960 name: "mylib",
961 srcs: ["mylib.cpp"],
962 shared_libs: ["libstub"],
963 apex_available: ["myapex"],
964 min_sdk_version: "Z",
965 }
966
967 apex_key {
968 name: "myapex.key",
969 public_key: "testkey.avbpubkey",
970 private_key: "testkey.pem",
971 }
972
973 apex {
974 name: "otherapex",
975 key: "myapex.key",
976 native_shared_libs: ["libstub"],
977 min_sdk_version: "29",
978 }
979
980 cc_library {
981 name: "libstub",
982 srcs: ["mylib.cpp"],
983 stubs: {
984 versions: ["29", "Z", "current"],
985 },
986 apex_available: ["otherapex"],
987 min_sdk_version: "29",
988 }
989
990 // platform module depending on libstub from otherapex should use the latest stub("current")
991 cc_library {
992 name: "libplatform",
993 srcs: ["mylib.cpp"],
994 shared_libs: ["libstub"],
995 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +0000996 `,
997 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
998 variables.Platform_sdk_codename = proptools.StringPtr("Z")
999 variables.Platform_sdk_final = proptools.BoolPtr(false)
1000 variables.Platform_version_active_codenames = []string{"Z"}
1001 }),
1002 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001003
Jiyong Park55549df2021-02-26 23:57:23 +09001004 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001005 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001006 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001007 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001008 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001009
1010 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1011 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1012 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1013 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1014 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1015}
1016
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001017func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001018 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001019 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001020 name: "myapex2",
1021 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001022 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001023 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001024 }
1025
1026 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001027 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001028 public_key: "testkey.avbpubkey",
1029 private_key: "testkey.pem",
1030 }
1031
1032 cc_library {
1033 name: "mylib",
1034 srcs: ["mylib.cpp"],
1035 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001036 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001037 system_shared_libs: [],
1038 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001039 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001040 }
1041
1042 cc_library {
1043 name: "libfoo",
1044 srcs: ["mylib.cpp"],
1045 shared_libs: ["libbar"],
1046 system_shared_libs: [],
1047 stl: "none",
1048 stubs: {
1049 versions: ["10", "20", "30"],
1050 },
1051 }
1052
1053 cc_library {
1054 name: "libbar",
1055 srcs: ["mylib.cpp"],
1056 system_shared_libs: [],
1057 stl: "none",
1058 }
1059
Jiyong Park678c8812020-02-07 17:25:49 +09001060 cc_library_static {
1061 name: "libbaz",
1062 srcs: ["mylib.cpp"],
1063 system_shared_libs: [],
1064 stl: "none",
1065 apex_available: [ "myapex2" ],
1066 }
1067
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001068 `)
1069
Jiyong Park83dc74b2020-01-14 18:38:44 +09001070 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001071 copyCmds := apexRule.Args["copy_commands"]
1072
1073 // Ensure that direct non-stubs dep is always included
1074 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1075
1076 // Ensure that indirect stubs dep is not included
1077 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1078
1079 // Ensure that dependency of stubs is not included
1080 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1081
Colin Crossaede88c2020-08-11 12:17:01 -07001082 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001083
1084 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001085 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001086 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001087 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001088
Jiyong Park3ff16992019-12-27 14:11:47 +09001089 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001090
1091 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1092 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001093
Artur Satayeva8bd1132020-04-27 18:07:06 +01001094 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001095 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001096
Artur Satayeva8bd1132020-04-27 18:07:06 +01001097 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001098 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001099}
1100
Jooyung Hand3639552019-08-09 12:57:43 +09001101func TestApexWithRuntimeLibsDependency(t *testing.T) {
1102 /*
1103 myapex
1104 |
1105 v (runtime_libs)
1106 mylib ------+------> libfoo [provides stub]
1107 |
1108 `------> libbar
1109 */
Colin Cross1c460562021-02-16 17:55:47 -08001110 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001111 apex {
1112 name: "myapex",
1113 key: "myapex.key",
1114 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001115 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001116 }
1117
1118 apex_key {
1119 name: "myapex.key",
1120 public_key: "testkey.avbpubkey",
1121 private_key: "testkey.pem",
1122 }
1123
1124 cc_library {
1125 name: "mylib",
1126 srcs: ["mylib.cpp"],
1127 runtime_libs: ["libfoo", "libbar"],
1128 system_shared_libs: [],
1129 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001130 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001131 }
1132
1133 cc_library {
1134 name: "libfoo",
1135 srcs: ["mylib.cpp"],
1136 system_shared_libs: [],
1137 stl: "none",
1138 stubs: {
1139 versions: ["10", "20", "30"],
1140 },
1141 }
1142
1143 cc_library {
1144 name: "libbar",
1145 srcs: ["mylib.cpp"],
1146 system_shared_libs: [],
1147 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001148 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001149 }
1150
1151 `)
1152
Sundong Ahnabb64432019-10-22 13:58:29 +09001153 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001154 copyCmds := apexRule.Args["copy_commands"]
1155
1156 // Ensure that direct non-stubs dep is always included
1157 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1158
1159 // Ensure that indirect stubs dep is not included
1160 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1161
1162 // Ensure that runtime_libs dep in included
1163 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1164
Sundong Ahnabb64432019-10-22 13:58:29 +09001165 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001166 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1167 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001168
1169}
1170
Paul Duffina02cae32021-03-09 01:44:06 +00001171var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1172 cc.PrepareForTestWithCcBuildComponents,
1173 PrepareForTestWithApexBuildComponents,
1174 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001175 apex {
1176 name: "com.android.runtime",
1177 key: "com.android.runtime.key",
1178 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001179 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001180 }
1181
1182 apex_key {
1183 name: "com.android.runtime.key",
1184 public_key: "testkey.avbpubkey",
1185 private_key: "testkey.pem",
1186 }
Paul Duffina02cae32021-03-09 01:44:06 +00001187 `),
1188 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1189)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001190
Paul Duffina02cae32021-03-09 01:44:06 +00001191func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001192 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001193 cc_library {
1194 name: "libc",
1195 no_libcrt: true,
1196 nocrt: true,
1197 stl: "none",
1198 system_shared_libs: [],
1199 stubs: { versions: ["1"] },
1200 apex_available: ["com.android.runtime"],
1201
1202 sanitize: {
1203 hwaddress: true,
1204 }
1205 }
1206
1207 cc_prebuilt_library_shared {
1208 name: "libclang_rt.hwasan-aarch64-android",
1209 no_libcrt: true,
1210 nocrt: true,
1211 stl: "none",
1212 system_shared_libs: [],
1213 srcs: [""],
1214 stubs: { versions: ["1"] },
1215
1216 sanitize: {
1217 never: true,
1218 },
Paul Duffina02cae32021-03-09 01:44:06 +00001219 } `)
1220 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001221
1222 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1223 "lib64/bionic/libc.so",
1224 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1225 })
1226
1227 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1228
1229 installed := hwasan.Description("install libclang_rt.hwasan")
1230 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1231
1232 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1233 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1234 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1235}
1236
1237func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001238 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001239 prepareForTestOfRuntimeApexWithHwasan,
1240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1241 variables.SanitizeDevice = []string{"hwaddress"}
1242 }),
1243 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001244 cc_library {
1245 name: "libc",
1246 no_libcrt: true,
1247 nocrt: true,
1248 stl: "none",
1249 system_shared_libs: [],
1250 stubs: { versions: ["1"] },
1251 apex_available: ["com.android.runtime"],
1252 }
1253
1254 cc_prebuilt_library_shared {
1255 name: "libclang_rt.hwasan-aarch64-android",
1256 no_libcrt: true,
1257 nocrt: true,
1258 stl: "none",
1259 system_shared_libs: [],
1260 srcs: [""],
1261 stubs: { versions: ["1"] },
1262
1263 sanitize: {
1264 never: true,
1265 },
1266 }
Paul Duffina02cae32021-03-09 01:44:06 +00001267 `)
1268 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001269
1270 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1271 "lib64/bionic/libc.so",
1272 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1273 })
1274
1275 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1276
1277 installed := hwasan.Description("install libclang_rt.hwasan")
1278 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1279
1280 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1281 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1282 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1283}
1284
Jooyung Han61b66e92020-03-21 14:21:46 +00001285func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1286 testcases := []struct {
1287 name string
1288 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001289 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001290 shouldLink string
1291 shouldNotLink []string
1292 }{
1293 {
Jiyong Park55549df2021-02-26 23:57:23 +09001294 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001295 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001296 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001297 shouldLink: "30",
1298 shouldNotLink: []string{"29"},
1299 },
1300 {
Jiyong Park55549df2021-02-26 23:57:23 +09001301 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001302 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001303 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001304 shouldLink: "30",
1305 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001306 },
1307 }
1308 for _, tc := range testcases {
1309 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001310 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001311 apex {
1312 name: "myapex",
1313 key: "myapex.key",
1314 use_vendor: true,
1315 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001316 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001317 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001318 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001319
Jooyung Han61b66e92020-03-21 14:21:46 +00001320 apex_key {
1321 name: "myapex.key",
1322 public_key: "testkey.avbpubkey",
1323 private_key: "testkey.pem",
1324 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001325
Jooyung Han61b66e92020-03-21 14:21:46 +00001326 cc_library {
1327 name: "mylib",
1328 srcs: ["mylib.cpp"],
1329 vendor_available: true,
1330 shared_libs: ["libbar"],
1331 system_shared_libs: [],
1332 stl: "none",
1333 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001334 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001335 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001336
Jooyung Han61b66e92020-03-21 14:21:46 +00001337 cc_library {
1338 name: "libbar",
1339 srcs: ["mylib.cpp"],
1340 system_shared_libs: [],
1341 stl: "none",
1342 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001343 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001344 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001345
Jooyung Han61b66e92020-03-21 14:21:46 +00001346 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001347 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001348 symbol_file: "",
1349 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001350 `,
1351 setUseVendorAllowListForTest([]string{"myapex"}),
1352 withUnbundledBuild,
1353 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001354
Jooyung Han61b66e92020-03-21 14:21:46 +00001355 // Ensure that LLNDK dep is not included
1356 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1357 "lib64/mylib.so",
1358 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001359
Jooyung Han61b66e92020-03-21 14:21:46 +00001360 // Ensure that LLNDK dep is required
1361 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1362 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1363 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001364
Colin Crossaede88c2020-08-11 12:17:01 -07001365 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001366 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001367 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001368 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001369 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001370
Colin Crossaede88c2020-08-11 12:17:01 -07001371 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001372 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1373 })
1374 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001375}
1376
Jiyong Park25fc6a92018-11-18 18:02:45 +09001377func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001378 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001379 apex {
1380 name: "myapex",
1381 key: "myapex.key",
1382 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001383 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001384 }
1385
1386 apex_key {
1387 name: "myapex.key",
1388 public_key: "testkey.avbpubkey",
1389 private_key: "testkey.pem",
1390 }
1391
1392 cc_library {
1393 name: "mylib",
1394 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001395 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001396 shared_libs: ["libdl#27"],
1397 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001398 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001399 }
1400
1401 cc_library_shared {
1402 name: "mylib_shared",
1403 srcs: ["mylib.cpp"],
1404 shared_libs: ["libdl#27"],
1405 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001406 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001407 }
1408
1409 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001410 name: "libBootstrap",
1411 srcs: ["mylib.cpp"],
1412 stl: "none",
1413 bootstrap: true,
1414 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001415 `)
1416
Sundong Ahnabb64432019-10-22 13:58:29 +09001417 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001418 copyCmds := apexRule.Args["copy_commands"]
1419
1420 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001421 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001422 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1423 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001424
1425 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001426 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001427
Colin Crossaede88c2020-08-11 12:17:01 -07001428 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1429 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1430 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001431
1432 // For dependency to libc
1433 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001434 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001435 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001436 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001437 // ... Cflags from stub is correctly exported to mylib
1438 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1439 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1440
1441 // For dependency to libm
1442 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001443 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001444 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001445 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001446 // ... and is not compiling with the stub
1447 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1448 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1449
1450 // For dependency to libdl
1451 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001452 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001453 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001454 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1455 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001456 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001457 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001458 // ... Cflags from stub is correctly exported to mylib
1459 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1460 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001461
1462 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001463 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1464 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1465 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1466 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001467}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001468
Jooyung Han749dc692020-04-15 11:03:39 +09001469func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001470 // there are three links between liba --> libz.
1471 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001472 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001473 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001474 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001475 apex {
1476 name: "myapex",
1477 key: "myapex.key",
1478 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001479 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001480 }
1481
1482 apex {
1483 name: "otherapex",
1484 key: "myapex.key",
1485 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001486 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001487 }
1488
1489 apex_key {
1490 name: "myapex.key",
1491 public_key: "testkey.avbpubkey",
1492 private_key: "testkey.pem",
1493 }
1494
1495 cc_library {
1496 name: "libx",
1497 shared_libs: ["liba"],
1498 system_shared_libs: [],
1499 stl: "none",
1500 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001501 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001502 }
1503
1504 cc_library {
1505 name: "liby",
1506 shared_libs: ["liba"],
1507 system_shared_libs: [],
1508 stl: "none",
1509 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001510 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001511 }
1512
1513 cc_library {
1514 name: "liba",
1515 shared_libs: ["libz"],
1516 system_shared_libs: [],
1517 stl: "none",
1518 apex_available: [
1519 "//apex_available:anyapex",
1520 "//apex_available:platform",
1521 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001522 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001523 }
1524
1525 cc_library {
1526 name: "libz",
1527 system_shared_libs: [],
1528 stl: "none",
1529 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001530 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001531 },
1532 }
Jooyung Han749dc692020-04-15 11:03:39 +09001533 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001534
1535 expectLink := func(from, from_variant, to, to_variant string) {
1536 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1537 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1538 }
1539 expectNoLink := func(from, from_variant, to, to_variant string) {
1540 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1541 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1542 }
1543 // platform liba is linked to non-stub version
1544 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001545 // liba in myapex is linked to #30
1546 expectLink("liba", "shared_apex29", "libz", "shared_30")
1547 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001548 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001549 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001550 expectLink("liba", "shared_apex30", "libz", "shared_30")
1551 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1552 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001553}
1554
Jooyung Hanaed150d2020-04-02 01:41:41 +09001555func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001556 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001557 apex {
1558 name: "myapex",
1559 key: "myapex.key",
1560 native_shared_libs: ["libx"],
1561 min_sdk_version: "R",
1562 }
1563
1564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
1569
1570 cc_library {
1571 name: "libx",
1572 shared_libs: ["libz"],
1573 system_shared_libs: [],
1574 stl: "none",
1575 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001576 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001577 }
1578
1579 cc_library {
1580 name: "libz",
1581 system_shared_libs: [],
1582 stl: "none",
1583 stubs: {
1584 versions: ["29", "R"],
1585 },
1586 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001587 `,
1588 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1589 variables.Platform_version_active_codenames = []string{"R"}
1590 }),
1591 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001592
1593 expectLink := func(from, from_variant, to, to_variant string) {
1594 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1595 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1596 }
1597 expectNoLink := func(from, from_variant, to, to_variant string) {
1598 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1599 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1600 }
Dan Albertc8060532020-07-22 22:32:17 -07001601 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001602 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1603 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001604}
1605
Jooyung Han749dc692020-04-15 11:03:39 +09001606func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001607 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001608 apex {
1609 name: "myapex",
1610 key: "myapex.key",
1611 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001612 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001613 }
1614
1615 apex_key {
1616 name: "myapex.key",
1617 public_key: "testkey.avbpubkey",
1618 private_key: "testkey.pem",
1619 }
1620
1621 cc_library {
1622 name: "libx",
1623 shared_libs: ["libz"],
1624 system_shared_libs: [],
1625 stl: "none",
1626 apex_available: [ "myapex" ],
1627 }
1628
1629 cc_library {
1630 name: "libz",
1631 system_shared_libs: [],
1632 stl: "none",
1633 stubs: {
1634 versions: ["1", "2"],
1635 },
1636 }
1637 `)
1638
1639 expectLink := func(from, from_variant, to, to_variant string) {
1640 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1641 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1642 }
1643 expectNoLink := func(from, from_variant, to, to_variant string) {
1644 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1645 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1646 }
Colin Crossaede88c2020-08-11 12:17:01 -07001647 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1648 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1649 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001650}
1651
1652func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001653 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001654 apex {
1655 name: "myapex",
1656 key: "myapex.key",
1657 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001658 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001659 }
1660
1661 apex_key {
1662 name: "myapex.key",
1663 public_key: "testkey.avbpubkey",
1664 private_key: "testkey.pem",
1665 }
1666
1667 cc_library {
1668 name: "libx",
1669 system_shared_libs: [],
1670 stl: "none",
1671 apex_available: [ "myapex" ],
1672 stubs: {
1673 versions: ["1", "2"],
1674 },
1675 }
1676
1677 cc_library {
1678 name: "libz",
1679 shared_libs: ["libx"],
1680 system_shared_libs: [],
1681 stl: "none",
1682 }
1683 `)
1684
1685 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001686 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001687 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1688 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1689 }
1690 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001691 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001692 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1693 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1694 }
1695 expectLink("libz", "shared", "libx", "shared_2")
1696 expectNoLink("libz", "shared", "libz", "shared_1")
1697 expectNoLink("libz", "shared", "libz", "shared")
1698}
1699
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001700var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1701 func(variables android.FixtureProductVariables) {
1702 variables.SanitizeDevice = []string{"hwaddress"}
1703 },
1704)
1705
Jooyung Han75568392020-03-20 04:29:24 +09001706func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001707 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001708 apex {
1709 name: "myapex",
1710 key: "myapex.key",
1711 native_shared_libs: ["libx"],
1712 min_sdk_version: "29",
1713 }
1714
1715 apex_key {
1716 name: "myapex.key",
1717 public_key: "testkey.avbpubkey",
1718 private_key: "testkey.pem",
1719 }
1720
1721 cc_library {
1722 name: "libx",
1723 shared_libs: ["libbar"],
1724 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001725 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001726 }
1727
1728 cc_library {
1729 name: "libbar",
1730 stubs: {
1731 versions: ["29", "30"],
1732 },
1733 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001734 `,
1735 prepareForTestWithSantitizeHwaddress,
1736 )
Jooyung Han03b51852020-02-26 22:45:42 +09001737 expectLink := func(from, from_variant, to, to_variant string) {
1738 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1739 libFlags := ld.Args["libFlags"]
1740 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1741 }
Colin Crossaede88c2020-08-11 12:17:01 -07001742 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001743}
1744
Jooyung Han75568392020-03-20 04:29:24 +09001745func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001746 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001747 apex {
1748 name: "myapex",
1749 key: "myapex.key",
1750 native_shared_libs: ["libx"],
1751 min_sdk_version: "29",
1752 }
1753
1754 apex_key {
1755 name: "myapex.key",
1756 public_key: "testkey.avbpubkey",
1757 private_key: "testkey.pem",
1758 }
1759
1760 cc_library {
1761 name: "libx",
1762 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001763 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001764 }
Jooyung Han75568392020-03-20 04:29:24 +09001765 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001766
1767 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001768 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001769 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001770 // note that platform variant is not.
1771 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001772 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001773}
1774
Jooyung Han749dc692020-04-15 11:03:39 +09001775func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1776 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001777 apex {
1778 name: "myapex",
1779 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001780 native_shared_libs: ["mylib"],
1781 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001782 }
1783
1784 apex_key {
1785 name: "myapex.key",
1786 public_key: "testkey.avbpubkey",
1787 private_key: "testkey.pem",
1788 }
Jooyung Han749dc692020-04-15 11:03:39 +09001789
1790 cc_library {
1791 name: "mylib",
1792 srcs: ["mylib.cpp"],
1793 system_shared_libs: [],
1794 stl: "none",
1795 apex_available: [
1796 "myapex",
1797 ],
1798 min_sdk_version: "30",
1799 }
1800 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001801
1802 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libfoo.ffi"],
1807 min_sdk_version: "29",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 rust_ffi_shared {
1817 name: "libfoo.ffi",
1818 srcs: ["foo.rs"],
1819 crate_name: "foo",
1820 apex_available: [
1821 "myapex",
1822 ],
1823 min_sdk_version: "30",
1824 }
1825 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001826}
1827
1828func TestApexMinSdkVersion_Okay(t *testing.T) {
1829 testApex(t, `
1830 apex {
1831 name: "myapex",
1832 key: "myapex.key",
1833 native_shared_libs: ["libfoo"],
1834 java_libs: ["libbar"],
1835 min_sdk_version: "29",
1836 }
1837
1838 apex_key {
1839 name: "myapex.key",
1840 public_key: "testkey.avbpubkey",
1841 private_key: "testkey.pem",
1842 }
1843
1844 cc_library {
1845 name: "libfoo",
1846 srcs: ["mylib.cpp"],
1847 shared_libs: ["libfoo_dep"],
1848 apex_available: ["myapex"],
1849 min_sdk_version: "29",
1850 }
1851
1852 cc_library {
1853 name: "libfoo_dep",
1854 srcs: ["mylib.cpp"],
1855 apex_available: ["myapex"],
1856 min_sdk_version: "29",
1857 }
1858
1859 java_library {
1860 name: "libbar",
1861 sdk_version: "current",
1862 srcs: ["a.java"],
1863 static_libs: ["libbar_dep"],
1864 apex_available: ["myapex"],
1865 min_sdk_version: "29",
1866 }
1867
1868 java_library {
1869 name: "libbar_dep",
1870 sdk_version: "current",
1871 srcs: ["a.java"],
1872 apex_available: ["myapex"],
1873 min_sdk_version: "29",
1874 }
Jooyung Han03b51852020-02-26 22:45:42 +09001875 `)
1876}
1877
Artur Satayev8cf899a2020-04-15 17:29:42 +01001878func TestJavaStableSdkVersion(t *testing.T) {
1879 testCases := []struct {
1880 name string
1881 expectedError string
1882 bp string
1883 }{
1884 {
1885 name: "Non-updatable apex with non-stable dep",
1886 bp: `
1887 apex {
1888 name: "myapex",
1889 java_libs: ["myjar"],
1890 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001891 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001892 }
1893 apex_key {
1894 name: "myapex.key",
1895 public_key: "testkey.avbpubkey",
1896 private_key: "testkey.pem",
1897 }
1898 java_library {
1899 name: "myjar",
1900 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001901 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001902 apex_available: ["myapex"],
1903 }
1904 `,
1905 },
1906 {
1907 name: "Updatable apex with stable dep",
1908 bp: `
1909 apex {
1910 name: "myapex",
1911 java_libs: ["myjar"],
1912 key: "myapex.key",
1913 updatable: true,
1914 min_sdk_version: "29",
1915 }
1916 apex_key {
1917 name: "myapex.key",
1918 public_key: "testkey.avbpubkey",
1919 private_key: "testkey.pem",
1920 }
1921 java_library {
1922 name: "myjar",
1923 srcs: ["foo/bar/MyClass.java"],
1924 sdk_version: "current",
1925 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001926 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001927 }
1928 `,
1929 },
1930 {
1931 name: "Updatable apex with non-stable dep",
1932 expectedError: "cannot depend on \"myjar\"",
1933 bp: `
1934 apex {
1935 name: "myapex",
1936 java_libs: ["myjar"],
1937 key: "myapex.key",
1938 updatable: true,
1939 }
1940 apex_key {
1941 name: "myapex.key",
1942 public_key: "testkey.avbpubkey",
1943 private_key: "testkey.pem",
1944 }
1945 java_library {
1946 name: "myjar",
1947 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001948 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001949 apex_available: ["myapex"],
1950 }
1951 `,
1952 },
1953 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001954 name: "Updatable apex with non-stable transitive dep",
1955 // This is not actually detecting that the transitive dependency is unstable, rather it is
1956 // detecting that the transitive dependency is building against a wider API surface than the
1957 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001958 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001959 bp: `
1960 apex {
1961 name: "myapex",
1962 java_libs: ["myjar"],
1963 key: "myapex.key",
1964 updatable: true,
1965 }
1966 apex_key {
1967 name: "myapex.key",
1968 public_key: "testkey.avbpubkey",
1969 private_key: "testkey.pem",
1970 }
1971 java_library {
1972 name: "myjar",
1973 srcs: ["foo/bar/MyClass.java"],
1974 sdk_version: "current",
1975 apex_available: ["myapex"],
1976 static_libs: ["transitive-jar"],
1977 }
1978 java_library {
1979 name: "transitive-jar",
1980 srcs: ["foo/bar/MyClass.java"],
1981 sdk_version: "core_platform",
1982 apex_available: ["myapex"],
1983 }
1984 `,
1985 },
1986 }
1987
1988 for _, test := range testCases {
1989 t.Run(test.name, func(t *testing.T) {
1990 if test.expectedError == "" {
1991 testApex(t, test.bp)
1992 } else {
1993 testApexError(t, test.expectedError, test.bp)
1994 }
1995 })
1996 }
1997}
1998
Jooyung Han749dc692020-04-15 11:03:39 +09001999func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2000 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2001 apex {
2002 name: "myapex",
2003 key: "myapex.key",
2004 native_shared_libs: ["mylib"],
2005 min_sdk_version: "29",
2006 }
2007
2008 apex_key {
2009 name: "myapex.key",
2010 public_key: "testkey.avbpubkey",
2011 private_key: "testkey.pem",
2012 }
2013
2014 cc_library {
2015 name: "mylib",
2016 srcs: ["mylib.cpp"],
2017 shared_libs: ["mylib2"],
2018 system_shared_libs: [],
2019 stl: "none",
2020 apex_available: [
2021 "myapex",
2022 ],
2023 min_sdk_version: "29",
2024 }
2025
2026 // indirect part of the apex
2027 cc_library {
2028 name: "mylib2",
2029 srcs: ["mylib.cpp"],
2030 system_shared_libs: [],
2031 stl: "none",
2032 apex_available: [
2033 "myapex",
2034 ],
2035 min_sdk_version: "30",
2036 }
2037 `)
2038}
2039
2040func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2041 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2042 apex {
2043 name: "myapex",
2044 key: "myapex.key",
2045 apps: ["AppFoo"],
2046 min_sdk_version: "29",
2047 }
2048
2049 apex_key {
2050 name: "myapex.key",
2051 public_key: "testkey.avbpubkey",
2052 private_key: "testkey.pem",
2053 }
2054
2055 android_app {
2056 name: "AppFoo",
2057 srcs: ["foo/bar/MyClass.java"],
2058 sdk_version: "current",
2059 min_sdk_version: "29",
2060 system_modules: "none",
2061 stl: "none",
2062 static_libs: ["bar"],
2063 apex_available: [ "myapex" ],
2064 }
2065
2066 java_library {
2067 name: "bar",
2068 sdk_version: "current",
2069 srcs: ["a.java"],
2070 apex_available: [ "myapex" ],
2071 }
2072 `)
2073}
2074
2075func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002076 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002077 apex {
2078 name: "myapex",
2079 key: "myapex.key",
2080 native_shared_libs: ["mylib"],
2081 min_sdk_version: "29",
2082 }
2083
2084 apex_key {
2085 name: "myapex.key",
2086 public_key: "testkey.avbpubkey",
2087 private_key: "testkey.pem",
2088 }
2089
Jiyong Park55549df2021-02-26 23:57:23 +09002090 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002091 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2092 cc_library {
2093 name: "mylib",
2094 srcs: ["mylib.cpp"],
2095 shared_libs: ["mylib2"],
2096 system_shared_libs: [],
2097 stl: "none",
2098 apex_available: ["myapex", "otherapex"],
2099 min_sdk_version: "29",
2100 }
2101
2102 cc_library {
2103 name: "mylib2",
2104 srcs: ["mylib.cpp"],
2105 system_shared_libs: [],
2106 stl: "none",
2107 apex_available: ["otherapex"],
2108 stubs: { versions: ["29", "30"] },
2109 min_sdk_version: "30",
2110 }
2111
2112 apex {
2113 name: "otherapex",
2114 key: "myapex.key",
2115 native_shared_libs: ["mylib", "mylib2"],
2116 min_sdk_version: "30",
2117 }
2118 `)
2119 expectLink := func(from, from_variant, to, to_variant string) {
2120 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2121 libFlags := ld.Args["libFlags"]
2122 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2123 }
Jiyong Park55549df2021-02-26 23:57:23 +09002124 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002125 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002126}
2127
Jooyung Haned124c32021-01-26 11:43:46 +09002128func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002129 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2130 func(variables android.FixtureProductVariables) {
2131 variables.Platform_sdk_codename = proptools.StringPtr("S")
2132 variables.Platform_version_active_codenames = []string{"S"}
2133 },
2134 )
Jooyung Haned124c32021-01-26 11:43:46 +09002135 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2136 apex {
2137 name: "myapex",
2138 key: "myapex.key",
2139 native_shared_libs: ["libfoo"],
2140 min_sdk_version: "S",
2141 }
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147 cc_library {
2148 name: "libfoo",
2149 shared_libs: ["libbar"],
2150 apex_available: ["myapex"],
2151 min_sdk_version: "29",
2152 }
2153 cc_library {
2154 name: "libbar",
2155 apex_available: ["myapex"],
2156 }
2157 `, withSAsActiveCodeNames)
2158}
2159
2160func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002161 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2162 variables.Platform_sdk_codename = proptools.StringPtr("S")
2163 variables.Platform_version_active_codenames = []string{"S", "T"}
2164 })
Colin Cross1c460562021-02-16 17:55:47 -08002165 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002166 apex {
2167 name: "myapex",
2168 key: "myapex.key",
2169 native_shared_libs: ["libfoo"],
2170 min_sdk_version: "S",
2171 }
2172 apex_key {
2173 name: "myapex.key",
2174 public_key: "testkey.avbpubkey",
2175 private_key: "testkey.pem",
2176 }
2177 cc_library {
2178 name: "libfoo",
2179 shared_libs: ["libbar"],
2180 apex_available: ["myapex"],
2181 min_sdk_version: "S",
2182 }
2183 cc_library {
2184 name: "libbar",
2185 stubs: {
2186 symbol_file: "libbar.map.txt",
2187 versions: ["30", "S", "T"],
2188 },
2189 }
2190 `, withSAsActiveCodeNames)
2191
2192 // ensure libfoo is linked with "S" version of libbar stub
2193 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2194 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002195 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002196}
2197
Jiyong Park7c2ee712018-12-07 00:42:25 +09002198func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002199 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002200 apex {
2201 name: "myapex",
2202 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002203 native_shared_libs: ["mylib"],
2204 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002205 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002206 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002207 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002208 }
2209
2210 apex_key {
2211 name: "myapex.key",
2212 public_key: "testkey.avbpubkey",
2213 private_key: "testkey.pem",
2214 }
2215
2216 prebuilt_etc {
2217 name: "myetc",
2218 src: "myprebuilt",
2219 sub_dir: "foo/bar",
2220 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002221
2222 cc_library {
2223 name: "mylib",
2224 srcs: ["mylib.cpp"],
2225 relative_install_path: "foo/bar",
2226 system_shared_libs: [],
2227 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002228 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002229 }
2230
2231 cc_binary {
2232 name: "mybin",
2233 srcs: ["mylib.cpp"],
2234 relative_install_path: "foo/bar",
2235 system_shared_libs: [],
2236 static_executable: true,
2237 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002238 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002239 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002240 `)
2241
Sundong Ahnabb64432019-10-22 13:58:29 +09002242 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002243 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2244
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002245 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002246 ensureListContains(t, dirs, "etc")
2247 ensureListContains(t, dirs, "etc/foo")
2248 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002249 ensureListContains(t, dirs, "lib64")
2250 ensureListContains(t, dirs, "lib64/foo")
2251 ensureListContains(t, dirs, "lib64/foo/bar")
2252 ensureListContains(t, dirs, "lib")
2253 ensureListContains(t, dirs, "lib/foo")
2254 ensureListContains(t, dirs, "lib/foo/bar")
2255
Jiyong Parkbd13e442019-03-15 18:10:35 +09002256 ensureListContains(t, dirs, "bin")
2257 ensureListContains(t, dirs, "bin/foo")
2258 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002259}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002260
Jooyung Han35155c42020-02-06 17:33:20 +09002261func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002262 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002263 apex {
2264 name: "myapex",
2265 key: "myapex.key",
2266 multilib: {
2267 both: {
2268 native_shared_libs: ["mylib"],
2269 binaries: ["mybin"],
2270 },
2271 },
2272 compile_multilib: "both",
2273 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002274 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002275 }
2276
2277 apex_key {
2278 name: "myapex.key",
2279 public_key: "testkey.avbpubkey",
2280 private_key: "testkey.pem",
2281 }
2282
2283 cc_library {
2284 name: "mylib",
2285 relative_install_path: "foo/bar",
2286 system_shared_libs: [],
2287 stl: "none",
2288 apex_available: [ "myapex" ],
2289 native_bridge_supported: true,
2290 }
2291
2292 cc_binary {
2293 name: "mybin",
2294 relative_install_path: "foo/bar",
2295 system_shared_libs: [],
2296 static_executable: true,
2297 stl: "none",
2298 apex_available: [ "myapex" ],
2299 native_bridge_supported: true,
2300 compile_multilib: "both", // default is "first" for binary
2301 multilib: {
2302 lib64: {
2303 suffix: "64",
2304 },
2305 },
2306 }
2307 `, withNativeBridgeEnabled)
2308 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2309 "bin/foo/bar/mybin",
2310 "bin/foo/bar/mybin64",
2311 "bin/arm/foo/bar/mybin",
2312 "bin/arm64/foo/bar/mybin64",
2313 "lib/foo/bar/mylib.so",
2314 "lib/arm/foo/bar/mylib.so",
2315 "lib64/foo/bar/mylib.so",
2316 "lib64/arm64/foo/bar/mylib.so",
2317 })
2318}
2319
Jiyong Parkda6eb592018-12-19 17:12:36 +09002320func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002321 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002322 apex {
2323 name: "myapex",
2324 key: "myapex.key",
2325 native_shared_libs: ["mylib"],
2326 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002327 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002328 }
2329
2330 apex_key {
2331 name: "myapex.key",
2332 public_key: "testkey.avbpubkey",
2333 private_key: "testkey.pem",
2334 }
2335
2336 cc_library {
2337 name: "mylib",
2338 srcs: ["mylib.cpp"],
2339 shared_libs: ["mylib2"],
2340 system_shared_libs: [],
2341 vendor_available: true,
2342 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002343 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002344 }
2345
2346 cc_library {
2347 name: "mylib2",
2348 srcs: ["mylib.cpp"],
2349 system_shared_libs: [],
2350 vendor_available: true,
2351 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002352 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002353 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002354 `,
2355 setUseVendorAllowListForTest([]string{"myapex"}),
2356 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002357
2358 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002359 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002360 for _, implicit := range i.Implicits {
2361 inputsList = append(inputsList, implicit.String())
2362 }
2363 }
2364 inputsString := strings.Join(inputsList, " ")
2365
2366 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002367 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2368 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002369
2370 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002371 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2372 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002373}
Jiyong Park16e91a02018-12-20 18:18:08 +09002374
Jooyung Han85d61762020-06-24 23:50:26 +09002375func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002376 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2377 apex {
2378 name: "myapex",
2379 key: "myapex.key",
2380 use_vendor: true,
2381 }
2382 apex_key {
2383 name: "myapex.key",
2384 public_key: "testkey.avbpubkey",
2385 private_key: "testkey.pem",
2386 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002387 `,
2388 setUseVendorAllowListForTest([]string{""}),
2389 )
Colin Cross440e0d02020-06-11 11:32:11 -07002390 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002391 testApex(t, `
2392 apex {
2393 name: "myapex",
2394 key: "myapex.key",
2395 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002396 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002397 }
2398 apex_key {
2399 name: "myapex.key",
2400 public_key: "testkey.avbpubkey",
2401 private_key: "testkey.pem",
2402 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002403 `,
2404 setUseVendorAllowListForTest([]string{"myapex"}),
2405 )
Jooyung Handc782442019-11-01 03:14:38 +09002406}
2407
Jooyung Han5c998b92019-06-27 11:30:33 +09002408func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2409 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2410 apex {
2411 name: "myapex",
2412 key: "myapex.key",
2413 native_shared_libs: ["mylib"],
2414 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002415 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002416 }
2417
2418 apex_key {
2419 name: "myapex.key",
2420 public_key: "testkey.avbpubkey",
2421 private_key: "testkey.pem",
2422 }
2423
2424 cc_library {
2425 name: "mylib",
2426 srcs: ["mylib.cpp"],
2427 system_shared_libs: [],
2428 stl: "none",
2429 }
2430 `)
2431}
2432
Jooyung Han85d61762020-06-24 23:50:26 +09002433func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002434 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002435 apex {
2436 name: "myapex",
2437 key: "myapex.key",
2438 binaries: ["mybin"],
2439 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002440 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002441 }
2442 apex_key {
2443 name: "myapex.key",
2444 public_key: "testkey.avbpubkey",
2445 private_key: "testkey.pem",
2446 }
2447 cc_binary {
2448 name: "mybin",
2449 vendor: true,
2450 shared_libs: ["libfoo"],
2451 }
2452 cc_library {
2453 name: "libfoo",
2454 proprietary: true,
2455 }
2456 `)
2457
2458 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2459 "bin/mybin",
2460 "lib64/libfoo.so",
2461 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2462 "lib64/libc++.so",
2463 })
2464
2465 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002466 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002467 name := apexBundle.BaseModuleName()
2468 prefix := "TARGET_"
2469 var builder strings.Builder
2470 data.Custom(&builder, name, prefix, "", data)
2471 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002472 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2473 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002474
2475 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2476 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2477 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002478}
2479
Jooyung Handf78e212020-07-22 15:54:47 +09002480func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002481 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002482 apex {
2483 name: "myapex",
2484 key: "myapex.key",
2485 binaries: ["mybin"],
2486 vendor: true,
2487 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002488 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002489 }
2490 apex_key {
2491 name: "myapex.key",
2492 public_key: "testkey.avbpubkey",
2493 private_key: "testkey.pem",
2494 }
2495 cc_binary {
2496 name: "mybin",
2497 vendor: true,
2498 shared_libs: ["libvndk", "libvendor"],
2499 }
2500 cc_library {
2501 name: "libvndk",
2502 vndk: {
2503 enabled: true,
2504 },
2505 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002506 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002507 }
2508 cc_library {
2509 name: "libvendor",
2510 vendor: true,
2511 }
2512 `)
2513
2514 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2515
Colin Crossaede88c2020-08-11 12:17:01 -07002516 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002517 libs := names(ldRule.Args["libFlags"])
2518 // VNDK libs(libvndk/libc++) as they are
2519 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002520 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002521 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002522 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002523
2524 // VNDK libs are not included when use_vndk_as_stable: true
2525 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2526 "bin/mybin",
2527 "lib64/libvendor.so",
2528 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002529
2530 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2531 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2532 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002533}
2534
Justin Yun13decfb2021-03-08 19:25:55 +09002535func TestProductVariant(t *testing.T) {
2536 ctx := testApex(t, `
2537 apex {
2538 name: "myapex",
2539 key: "myapex.key",
2540 updatable: false,
2541 product_specific: true,
2542 binaries: ["foo"],
2543 }
2544
2545 apex_key {
2546 name: "myapex.key",
2547 public_key: "testkey.avbpubkey",
2548 private_key: "testkey.pem",
2549 }
2550
2551 cc_binary {
2552 name: "foo",
2553 product_available: true,
2554 apex_available: ["myapex"],
2555 srcs: ["foo.cpp"],
2556 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002557 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2558 variables.ProductVndkVersion = proptools.StringPtr("current")
2559 }),
2560 )
Justin Yun13decfb2021-03-08 19:25:55 +09002561
2562 cflags := strings.Fields(
2563 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2564 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2565 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2566 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2567 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2568}
2569
Jooyung Han8e5685d2020-09-21 11:02:57 +09002570func TestApex_withPrebuiltFirmware(t *testing.T) {
2571 testCases := []struct {
2572 name string
2573 additionalProp string
2574 }{
2575 {"system apex with prebuilt_firmware", ""},
2576 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2577 }
2578 for _, tc := range testCases {
2579 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002580 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002581 apex {
2582 name: "myapex",
2583 key: "myapex.key",
2584 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002585 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002586 `+tc.additionalProp+`
2587 }
2588 apex_key {
2589 name: "myapex.key",
2590 public_key: "testkey.avbpubkey",
2591 private_key: "testkey.pem",
2592 }
2593 prebuilt_firmware {
2594 name: "myfirmware",
2595 src: "myfirmware.bin",
2596 filename_from_src: true,
2597 `+tc.additionalProp+`
2598 }
2599 `)
2600 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2601 "etc/firmware/myfirmware.bin",
2602 })
2603 })
2604 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002605}
2606
Jooyung Hanefb184e2020-06-25 17:14:25 +09002607func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002608 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002609 apex {
2610 name: "myapex",
2611 key: "myapex.key",
2612 use_vendor: true,
2613 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002614 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002615 }
2616
2617 apex_key {
2618 name: "myapex.key",
2619 public_key: "testkey.avbpubkey",
2620 private_key: "testkey.pem",
2621 }
2622
2623 cc_library {
2624 name: "mylib",
2625 vendor_available: true,
2626 apex_available: ["myapex"],
2627 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002628 `,
2629 setUseVendorAllowListForTest([]string{"myapex"}),
2630 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002631
2632 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002633 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002634 name := apexBundle.BaseModuleName()
2635 prefix := "TARGET_"
2636 var builder strings.Builder
2637 data.Custom(&builder, name, prefix, "", data)
2638 androidMk := builder.String()
2639 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2640}
2641
2642func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002643 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002644 apex {
2645 name: "myapex",
2646 key: "myapex.key",
2647 vendor: true,
2648 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002649 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002650 }
2651
2652 apex_key {
2653 name: "myapex.key",
2654 public_key: "testkey.avbpubkey",
2655 private_key: "testkey.pem",
2656 }
2657
2658 cc_library {
2659 name: "mylib",
2660 vendor_available: true,
2661 }
2662 `)
2663
2664 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002665 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002666 name := apexBundle.BaseModuleName()
2667 prefix := "TARGET_"
2668 var builder strings.Builder
2669 data.Custom(&builder, name, prefix, "", data)
2670 androidMk := builder.String()
2671 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2672}
2673
Jooyung Han2ed99d02020-06-24 23:26:26 +09002674func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002675 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002676 apex {
2677 name: "myapex",
2678 key: "myapex.key",
2679 vintf_fragments: ["fragment.xml"],
2680 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002681 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002682 }
2683 apex_key {
2684 name: "myapex.key",
2685 public_key: "testkey.avbpubkey",
2686 private_key: "testkey.pem",
2687 }
2688 cc_binary {
2689 name: "mybin",
2690 }
2691 `)
2692
2693 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002694 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002695 name := apexBundle.BaseModuleName()
2696 prefix := "TARGET_"
2697 var builder strings.Builder
2698 data.Custom(&builder, name, prefix, "", data)
2699 androidMk := builder.String()
2700 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2701 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2702}
2703
Jiyong Park16e91a02018-12-20 18:18:08 +09002704func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002705 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002706 apex {
2707 name: "myapex",
2708 key: "myapex.key",
2709 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002710 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002711 }
2712
2713 apex_key {
2714 name: "myapex.key",
2715 public_key: "testkey.avbpubkey",
2716 private_key: "testkey.pem",
2717 }
2718
2719 cc_library {
2720 name: "mylib",
2721 srcs: ["mylib.cpp"],
2722 system_shared_libs: [],
2723 stl: "none",
2724 stubs: {
2725 versions: ["1", "2", "3"],
2726 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002727 apex_available: [
2728 "//apex_available:platform",
2729 "myapex",
2730 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002731 }
2732
2733 cc_binary {
2734 name: "not_in_apex",
2735 srcs: ["mylib.cpp"],
2736 static_libs: ["mylib"],
2737 static_executable: true,
2738 system_shared_libs: [],
2739 stl: "none",
2740 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002741 `)
2742
Colin Cross7113d202019-11-20 16:39:12 -08002743 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002744
2745 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002746 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002747}
Jiyong Park9335a262018-12-24 11:31:58 +09002748
2749func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002750 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002751 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002752 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002753 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002754 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002755 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002756 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002757 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002758 }
2759
2760 cc_library {
2761 name: "mylib",
2762 srcs: ["mylib.cpp"],
2763 system_shared_libs: [],
2764 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002765 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002766 }
2767
2768 apex_key {
2769 name: "myapex.key",
2770 public_key: "testkey.avbpubkey",
2771 private_key: "testkey.pem",
2772 }
2773
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002774 android_app_certificate {
2775 name: "myapex.certificate",
2776 certificate: "testkey",
2777 }
2778
2779 android_app_certificate {
2780 name: "myapex.certificate.override",
2781 certificate: "testkey.override",
2782 }
2783
Jiyong Park9335a262018-12-24 11:31:58 +09002784 `)
2785
2786 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002787 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002788
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002789 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2790 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002791 "vendor/foo/devkeys/testkey.avbpubkey")
2792 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002793 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2794 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002795 "vendor/foo/devkeys/testkey.pem")
2796 }
2797
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002798 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002799 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002800 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002801 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002802 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002803 }
2804}
Jiyong Park58e364a2019-01-19 19:24:06 +09002805
Jooyung Hanf121a652019-12-17 14:30:11 +09002806func TestCertificate(t *testing.T) {
2807 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002808 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002809 apex {
2810 name: "myapex",
2811 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002812 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002813 }
2814 apex_key {
2815 name: "myapex.key",
2816 public_key: "testkey.avbpubkey",
2817 private_key: "testkey.pem",
2818 }`)
2819 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2820 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2821 if actual := rule.Args["certificates"]; actual != expected {
2822 t.Errorf("certificates should be %q, not %q", expected, actual)
2823 }
2824 })
2825 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002826 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002827 apex {
2828 name: "myapex_keytest",
2829 key: "myapex.key",
2830 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002831 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002832 }
2833 apex_key {
2834 name: "myapex.key",
2835 public_key: "testkey.avbpubkey",
2836 private_key: "testkey.pem",
2837 }
2838 android_app_certificate {
2839 name: "myapex.certificate.override",
2840 certificate: "testkey.override",
2841 }`)
2842 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2843 expected := "testkey.override.x509.pem testkey.override.pk8"
2844 if actual := rule.Args["certificates"]; actual != expected {
2845 t.Errorf("certificates should be %q, not %q", expected, actual)
2846 }
2847 })
2848 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002849 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002850 apex {
2851 name: "myapex",
2852 key: "myapex.key",
2853 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002854 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002855 }
2856 apex_key {
2857 name: "myapex.key",
2858 public_key: "testkey.avbpubkey",
2859 private_key: "testkey.pem",
2860 }
2861 android_app_certificate {
2862 name: "myapex.certificate",
2863 certificate: "testkey",
2864 }`)
2865 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2866 expected := "testkey.x509.pem testkey.pk8"
2867 if actual := rule.Args["certificates"]; actual != expected {
2868 t.Errorf("certificates should be %q, not %q", expected, actual)
2869 }
2870 })
2871 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002872 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002873 apex {
2874 name: "myapex_keytest",
2875 key: "myapex.key",
2876 file_contexts: ":myapex-file_contexts",
2877 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002878 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002879 }
2880 apex_key {
2881 name: "myapex.key",
2882 public_key: "testkey.avbpubkey",
2883 private_key: "testkey.pem",
2884 }
2885 android_app_certificate {
2886 name: "myapex.certificate.override",
2887 certificate: "testkey.override",
2888 }`)
2889 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2890 expected := "testkey.override.x509.pem testkey.override.pk8"
2891 if actual := rule.Args["certificates"]; actual != expected {
2892 t.Errorf("certificates should be %q, not %q", expected, actual)
2893 }
2894 })
2895 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002896 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002897 apex {
2898 name: "myapex",
2899 key: "myapex.key",
2900 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002901 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002902 }
2903 apex_key {
2904 name: "myapex.key",
2905 public_key: "testkey.avbpubkey",
2906 private_key: "testkey.pem",
2907 }`)
2908 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2909 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2910 if actual := rule.Args["certificates"]; actual != expected {
2911 t.Errorf("certificates should be %q, not %q", expected, actual)
2912 }
2913 })
2914 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002915 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002916 apex {
2917 name: "myapex_keytest",
2918 key: "myapex.key",
2919 file_contexts: ":myapex-file_contexts",
2920 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002921 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002922 }
2923 apex_key {
2924 name: "myapex.key",
2925 public_key: "testkey.avbpubkey",
2926 private_key: "testkey.pem",
2927 }
2928 android_app_certificate {
2929 name: "myapex.certificate.override",
2930 certificate: "testkey.override",
2931 }`)
2932 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2933 expected := "testkey.override.x509.pem testkey.override.pk8"
2934 if actual := rule.Args["certificates"]; actual != expected {
2935 t.Errorf("certificates should be %q, not %q", expected, actual)
2936 }
2937 })
2938}
2939
Jiyong Park58e364a2019-01-19 19:24:06 +09002940func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002941 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002942 apex {
2943 name: "myapex",
2944 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002945 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002946 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002947 }
2948
2949 apex {
2950 name: "otherapex",
2951 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002952 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002953 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002954 }
2955
2956 apex_key {
2957 name: "myapex.key",
2958 public_key: "testkey.avbpubkey",
2959 private_key: "testkey.pem",
2960 }
2961
2962 cc_library {
2963 name: "mylib",
2964 srcs: ["mylib.cpp"],
2965 system_shared_libs: [],
2966 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002967 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002968 "myapex",
2969 "otherapex",
2970 ],
Jooyung Han24282772020-03-21 23:20:55 +09002971 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002972 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002973 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002974 cc_library {
2975 name: "mylib2",
2976 srcs: ["mylib.cpp"],
2977 system_shared_libs: [],
2978 stl: "none",
2979 apex_available: [
2980 "myapex",
2981 "otherapex",
2982 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002983 static_libs: ["mylib3"],
2984 recovery_available: true,
2985 min_sdk_version: "29",
2986 }
2987 cc_library {
2988 name: "mylib3",
2989 srcs: ["mylib.cpp"],
2990 system_shared_libs: [],
2991 stl: "none",
2992 apex_available: [
2993 "myapex",
2994 "otherapex",
2995 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002996 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002997 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002998 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002999 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003000 `)
3001
Jooyung Hanc87a0592020-03-02 17:44:33 +09003002 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003003 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003004 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003005 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003006
Jooyung Hanccce2f22020-03-07 03:45:53 +09003007 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003008 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003009 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003010 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003011 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003012
Jooyung Hanccce2f22020-03-07 03:45:53 +09003013 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003014 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003015 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003016 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003017 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003018
Colin Crossaede88c2020-08-11 12:17:01 -07003019 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3020 // each variant defines additional macros to distinguish which apex variant it is built for
3021
3022 // non-APEX variant does not have __ANDROID_APEX__ defined
3023 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3024 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3025
3026 // APEX variant has __ANDROID_APEX__ defined
3027 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3028 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3029 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3030 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3031
3032 // APEX variant has __ANDROID_APEX__ defined
3033 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3034 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3035 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3036 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3037
Dan Albertb19953d2020-11-17 15:29:36 -08003038 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003039 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3040 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003041 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003042
3043 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3044 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003045
3046 // non-APEX variant does not have __ANDROID_APEX__ defined
3047 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3048 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3049
3050 // APEX variant has __ANDROID_APEX__ defined
3051 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003052 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003053 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003054 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003055
Jooyung Hanc87a0592020-03-02 17:44:33 +09003056 // APEX variant has __ANDROID_APEX__ defined
3057 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003058 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003059 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003060 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003061
Dan Albertb19953d2020-11-17 15:29:36 -08003062 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003063 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003064 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003065 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003066}
Jiyong Park7e636d02019-01-28 16:16:54 +09003067
3068func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003069 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003070 apex {
3071 name: "myapex",
3072 key: "myapex.key",
3073 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003074 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003075 }
3076
3077 apex_key {
3078 name: "myapex.key",
3079 public_key: "testkey.avbpubkey",
3080 private_key: "testkey.pem",
3081 }
3082
3083 cc_library_headers {
3084 name: "mylib_headers",
3085 export_include_dirs: ["my_include"],
3086 system_shared_libs: [],
3087 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003088 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003089 }
3090
3091 cc_library {
3092 name: "mylib",
3093 srcs: ["mylib.cpp"],
3094 system_shared_libs: [],
3095 stl: "none",
3096 header_libs: ["mylib_headers"],
3097 export_header_lib_headers: ["mylib_headers"],
3098 stubs: {
3099 versions: ["1", "2", "3"],
3100 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003101 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003102 }
3103
3104 cc_library {
3105 name: "otherlib",
3106 srcs: ["mylib.cpp"],
3107 system_shared_libs: [],
3108 stl: "none",
3109 shared_libs: ["mylib"],
3110 }
3111 `)
3112
Colin Cross7113d202019-11-20 16:39:12 -08003113 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003114
3115 // Ensure that the include path of the header lib is exported to 'otherlib'
3116 ensureContains(t, cFlags, "-Imy_include")
3117}
Alex Light9670d332019-01-29 18:07:33 -08003118
Jiyong Park7cd10e32020-01-14 09:22:18 +09003119type fileInApex struct {
3120 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003121 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003122 isLink bool
3123}
3124
Jooyung Hana57af4a2020-01-23 05:36:59 +00003125func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003126 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003127 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003128 copyCmds := apexRule.Args["copy_commands"]
3129 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003130 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003131 for _, cmd := range strings.Split(copyCmds, "&&") {
3132 cmd = strings.TrimSpace(cmd)
3133 if cmd == "" {
3134 continue
3135 }
3136 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003137 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003138 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003139 switch terms[0] {
3140 case "mkdir":
3141 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003142 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003143 t.Fatal("copyCmds contains invalid cp command", cmd)
3144 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003145 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003146 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003147 isLink = false
3148 case "ln":
3149 if len(terms) != 3 && len(terms) != 4 {
3150 // ln LINK TARGET or ln -s LINK TARGET
3151 t.Fatal("copyCmds contains invalid ln command", cmd)
3152 }
3153 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003154 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003155 isLink = true
3156 default:
3157 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3158 }
3159 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003160 index := strings.Index(dst, imageApexDir)
3161 if index == -1 {
3162 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3163 }
3164 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003165 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003166 }
3167 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003168 return ret
3169}
3170
Jooyung Hana57af4a2020-01-23 05:36:59 +00003171func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3172 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003173 var failed bool
3174 var surplus []string
3175 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003176 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003177 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003178 for _, expected := range files {
3179 if matched, _ := path.Match(expected, file.path); matched {
3180 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003181 mactchFound = true
3182 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003183 }
3184 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003185 if !mactchFound {
3186 surplus = append(surplus, file.path)
3187 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003188 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003189
Jooyung Han31c470b2019-10-18 16:26:59 +09003190 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003191 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003192 t.Log("surplus files", surplus)
3193 failed = true
3194 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003195
3196 if len(files) > len(filesMatched) {
3197 var missing []string
3198 for _, expected := range files {
3199 if !filesMatched[expected] {
3200 missing = append(missing, expected)
3201 }
3202 }
3203 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003204 t.Log("missing files", missing)
3205 failed = true
3206 }
3207 if failed {
3208 t.Fail()
3209 }
3210}
3211
Jooyung Han344d5432019-08-23 11:17:39 +09003212func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003213 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003214 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003215 name: "com.android.vndk.current",
3216 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003217 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003218 }
3219
3220 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003221 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003222 public_key: "testkey.avbpubkey",
3223 private_key: "testkey.pem",
3224 }
3225
3226 cc_library {
3227 name: "libvndk",
3228 srcs: ["mylib.cpp"],
3229 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003230 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003231 vndk: {
3232 enabled: true,
3233 },
3234 system_shared_libs: [],
3235 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003236 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003237 }
3238
3239 cc_library {
3240 name: "libvndksp",
3241 srcs: ["mylib.cpp"],
3242 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003243 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003244 vndk: {
3245 enabled: true,
3246 support_system_process: true,
3247 },
3248 system_shared_libs: [],
3249 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003250 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003251 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003252 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003253
Colin Cross2807f002021-03-02 10:15:29 -08003254 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003255 "lib/libvndk.so",
3256 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003257 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003258 "lib64/libvndk.so",
3259 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003260 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003261 "etc/llndk.libraries.VER.txt",
3262 "etc/vndkcore.libraries.VER.txt",
3263 "etc/vndksp.libraries.VER.txt",
3264 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003265 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003266 })
Jooyung Han344d5432019-08-23 11:17:39 +09003267}
3268
3269func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003270 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003271 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003272 name: "com.android.vndk.current",
3273 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003274 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003275 }
3276
3277 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003278 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003279 public_key: "testkey.avbpubkey",
3280 private_key: "testkey.pem",
3281 }
3282
3283 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003284 name: "libvndk",
3285 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003286 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003287 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003288 vndk: {
3289 enabled: true,
3290 },
3291 system_shared_libs: [],
3292 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003293 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003294 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003295
3296 cc_prebuilt_library_shared {
3297 name: "libvndk.arm",
3298 srcs: ["libvndk.arm.so"],
3299 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003300 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003301 vndk: {
3302 enabled: true,
3303 },
3304 enabled: false,
3305 arch: {
3306 arm: {
3307 enabled: true,
3308 },
3309 },
3310 system_shared_libs: [],
3311 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003312 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003313 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003314 `+vndkLibrariesTxtFiles("current"),
3315 withFiles(map[string][]byte{
3316 "libvndk.so": nil,
3317 "libvndk.arm.so": nil,
3318 }))
Colin Cross2807f002021-03-02 10:15:29 -08003319 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003320 "lib/libvndk.so",
3321 "lib/libvndk.arm.so",
3322 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003323 "lib/libc++.so",
3324 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003325 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003326 })
Jooyung Han344d5432019-08-23 11:17:39 +09003327}
3328
Jooyung Han39edb6c2019-11-06 16:53:07 +09003329func vndkLibrariesTxtFiles(vers ...string) (result string) {
3330 for _, v := range vers {
3331 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003332 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003333 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003334 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003335 name: "` + txt + `.libraries.txt",
3336 }
3337 `
3338 }
3339 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003340 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003341 result += `
3342 prebuilt_etc {
3343 name: "` + txt + `.libraries.` + v + `.txt",
3344 src: "dummy.txt",
3345 }
3346 `
3347 }
3348 }
3349 }
3350 return
3351}
3352
Jooyung Han344d5432019-08-23 11:17:39 +09003353func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003354 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003355 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003356 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003357 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003358 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003359 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003360 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003361 }
3362
3363 apex_key {
3364 name: "myapex.key",
3365 public_key: "testkey.avbpubkey",
3366 private_key: "testkey.pem",
3367 }
3368
Jooyung Han31c470b2019-10-18 16:26:59 +09003369 vndk_prebuilt_shared {
3370 name: "libvndk27",
3371 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003372 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003373 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003374 vndk: {
3375 enabled: true,
3376 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003377 target_arch: "arm64",
3378 arch: {
3379 arm: {
3380 srcs: ["libvndk27_arm.so"],
3381 },
3382 arm64: {
3383 srcs: ["libvndk27_arm64.so"],
3384 },
3385 },
Colin Cross2807f002021-03-02 10:15:29 -08003386 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003387 }
3388
3389 vndk_prebuilt_shared {
3390 name: "libvndk27",
3391 version: "27",
3392 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003393 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003394 vndk: {
3395 enabled: true,
3396 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 target_arch: "x86_64",
3398 arch: {
3399 x86: {
3400 srcs: ["libvndk27_x86.so"],
3401 },
3402 x86_64: {
3403 srcs: ["libvndk27_x86_64.so"],
3404 },
3405 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003406 }
3407 `+vndkLibrariesTxtFiles("27"),
3408 withFiles(map[string][]byte{
3409 "libvndk27_arm.so": nil,
3410 "libvndk27_arm64.so": nil,
3411 "libvndk27_x86.so": nil,
3412 "libvndk27_x86_64.so": nil,
3413 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003414
Colin Cross2807f002021-03-02 10:15:29 -08003415 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003416 "lib/libvndk27_arm.so",
3417 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003418 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003419 })
Jooyung Han344d5432019-08-23 11:17:39 +09003420}
3421
Jooyung Han90eee022019-10-01 20:02:42 +09003422func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003423 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003424 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003425 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003426 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003427 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003428 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003429 }
3430 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003431 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003432 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003433 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003434 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003435 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003436 }
3437 apex_key {
3438 name: "myapex.key",
3439 public_key: "testkey.avbpubkey",
3440 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003441 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003442
3443 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003444 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003445 actual := proptools.String(bundle.properties.Apex_name)
3446 if !reflect.DeepEqual(actual, expected) {
3447 t.Errorf("Got '%v', expected '%v'", actual, expected)
3448 }
3449 }
3450
Colin Cross2807f002021-03-02 10:15:29 -08003451 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3452 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003453}
3454
Jooyung Han344d5432019-08-23 11:17:39 +09003455func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003456 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003457 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003458 name: "com.android.vndk.current",
3459 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003460 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003461 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003462 }
3463
3464 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003465 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003466 public_key: "testkey.avbpubkey",
3467 private_key: "testkey.pem",
3468 }
3469
3470 cc_library {
3471 name: "libvndk",
3472 srcs: ["mylib.cpp"],
3473 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003474 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003475 native_bridge_supported: true,
3476 host_supported: true,
3477 vndk: {
3478 enabled: true,
3479 },
3480 system_shared_libs: [],
3481 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003482 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003483 }
Colin Cross2807f002021-03-02 10:15:29 -08003484 `+vndkLibrariesTxtFiles("current"),
3485 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003486
Colin Cross2807f002021-03-02 10:15:29 -08003487 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003488 "lib/libvndk.so",
3489 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003490 "lib/libc++.so",
3491 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003492 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003493 })
Jooyung Han344d5432019-08-23 11:17:39 +09003494}
3495
3496func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003497 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003498 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003499 name: "com.android.vndk.current",
3500 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003501 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003502 native_bridge_supported: true,
3503 }
3504
3505 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003506 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003507 public_key: "testkey.avbpubkey",
3508 private_key: "testkey.pem",
3509 }
3510
3511 cc_library {
3512 name: "libvndk",
3513 srcs: ["mylib.cpp"],
3514 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003515 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003516 native_bridge_supported: true,
3517 host_supported: true,
3518 vndk: {
3519 enabled: true,
3520 },
3521 system_shared_libs: [],
3522 stl: "none",
3523 }
3524 `)
3525}
3526
Jooyung Han31c470b2019-10-18 16:26:59 +09003527func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003528 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003529 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003530 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003531 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003532 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003533 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003534 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003535 }
3536
3537 apex_key {
3538 name: "myapex.key",
3539 public_key: "testkey.avbpubkey",
3540 private_key: "testkey.pem",
3541 }
3542
3543 vndk_prebuilt_shared {
3544 name: "libvndk27",
3545 version: "27",
3546 target_arch: "arm",
3547 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003548 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003549 vndk: {
3550 enabled: true,
3551 },
3552 arch: {
3553 arm: {
3554 srcs: ["libvndk27.so"],
3555 }
3556 },
3557 }
3558
3559 vndk_prebuilt_shared {
3560 name: "libvndk27",
3561 version: "27",
3562 target_arch: "arm",
3563 binder32bit: true,
3564 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003565 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003566 vndk: {
3567 enabled: true,
3568 },
3569 arch: {
3570 arm: {
3571 srcs: ["libvndk27binder32.so"],
3572 }
3573 },
Colin Cross2807f002021-03-02 10:15:29 -08003574 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003575 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003576 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003577 withFiles(map[string][]byte{
3578 "libvndk27.so": nil,
3579 "libvndk27binder32.so": nil,
3580 }),
3581 withBinder32bit,
3582 withTargets(map[android.OsType][]android.Target{
3583 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003584 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3585 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003586 },
3587 }),
3588 )
3589
Colin Cross2807f002021-03-02 10:15:29 -08003590 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003591 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003592 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003593 })
3594}
3595
Jooyung Han45a96772020-06-15 14:59:42 +09003596func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003597 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003598 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003599 name: "com.android.vndk.current",
3600 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003601 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003602 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003603 }
3604
3605 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003606 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003607 public_key: "testkey.avbpubkey",
3608 private_key: "testkey.pem",
3609 }
3610
3611 cc_library {
3612 name: "libz",
3613 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003614 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003615 vndk: {
3616 enabled: true,
3617 },
3618 stubs: {
3619 symbol_file: "libz.map.txt",
3620 versions: ["30"],
3621 }
3622 }
3623 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3624 "libz.map.txt": nil,
3625 }))
3626
Colin Cross2807f002021-03-02 10:15:29 -08003627 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003628 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3629 ensureListEmpty(t, provideNativeLibs)
3630}
3631
Jooyung Hane1633032019-08-01 17:41:43 +09003632func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003633 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003634 apex {
3635 name: "myapex_nodep",
3636 key: "myapex.key",
3637 native_shared_libs: ["lib_nodep"],
3638 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003639 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003640 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003641 }
3642
3643 apex {
3644 name: "myapex_dep",
3645 key: "myapex.key",
3646 native_shared_libs: ["lib_dep"],
3647 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003648 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003649 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003650 }
3651
3652 apex {
3653 name: "myapex_provider",
3654 key: "myapex.key",
3655 native_shared_libs: ["libfoo"],
3656 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003657 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003658 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003659 }
3660
3661 apex {
3662 name: "myapex_selfcontained",
3663 key: "myapex.key",
3664 native_shared_libs: ["lib_dep", "libfoo"],
3665 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003666 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003667 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003668 }
3669
3670 apex_key {
3671 name: "myapex.key",
3672 public_key: "testkey.avbpubkey",
3673 private_key: "testkey.pem",
3674 }
3675
3676 cc_library {
3677 name: "lib_nodep",
3678 srcs: ["mylib.cpp"],
3679 system_shared_libs: [],
3680 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003681 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003682 }
3683
3684 cc_library {
3685 name: "lib_dep",
3686 srcs: ["mylib.cpp"],
3687 shared_libs: ["libfoo"],
3688 system_shared_libs: [],
3689 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003690 apex_available: [
3691 "myapex_dep",
3692 "myapex_provider",
3693 "myapex_selfcontained",
3694 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003695 }
3696
3697 cc_library {
3698 name: "libfoo",
3699 srcs: ["mytest.cpp"],
3700 stubs: {
3701 versions: ["1"],
3702 },
3703 system_shared_libs: [],
3704 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003705 apex_available: [
3706 "myapex_provider",
3707 "myapex_selfcontained",
3708 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003709 }
3710 `)
3711
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003712 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003713 var provideNativeLibs, requireNativeLibs []string
3714
Sundong Ahnabb64432019-10-22 13:58:29 +09003715 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003716 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3717 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003718 ensureListEmpty(t, provideNativeLibs)
3719 ensureListEmpty(t, requireNativeLibs)
3720
Sundong Ahnabb64432019-10-22 13:58:29 +09003721 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003722 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3723 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003724 ensureListEmpty(t, provideNativeLibs)
3725 ensureListContains(t, requireNativeLibs, "libfoo.so")
3726
Sundong Ahnabb64432019-10-22 13:58:29 +09003727 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003728 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3729 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003730 ensureListContains(t, provideNativeLibs, "libfoo.so")
3731 ensureListEmpty(t, requireNativeLibs)
3732
Sundong Ahnabb64432019-10-22 13:58:29 +09003733 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003734 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3735 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003736 ensureListContains(t, provideNativeLibs, "libfoo.so")
3737 ensureListEmpty(t, requireNativeLibs)
3738}
3739
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003740func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003741 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003742 apex {
3743 name: "myapex",
3744 key: "myapex.key",
3745 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003746 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003747 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003748 }
3749
3750 apex_key {
3751 name: "myapex.key",
3752 public_key: "testkey.avbpubkey",
3753 private_key: "testkey.pem",
3754 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003755
3756 cc_library {
3757 name: "mylib",
3758 srcs: ["mylib.cpp"],
3759 system_shared_libs: [],
3760 stl: "none",
3761 apex_available: [
3762 "//apex_available:platform",
3763 "myapex",
3764 ],
3765 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003766 `)
3767
Sundong Ahnabb64432019-10-22 13:58:29 +09003768 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003769 apexManifestRule := module.Rule("apexManifestRule")
3770 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3771 apexRule := module.Rule("apexRule")
3772 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003773
3774 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003775 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003776 name := apexBundle.BaseModuleName()
3777 prefix := "TARGET_"
3778 var builder strings.Builder
3779 data.Custom(&builder, name, prefix, "", data)
3780 androidMk := builder.String()
3781 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3782 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003783}
3784
Alex Light0851b882019-02-07 13:20:53 -08003785func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003786 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003787 apex {
3788 name: "myapex",
3789 key: "myapex.key",
3790 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003791 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003792 }
3793
3794 apex_key {
3795 name: "myapex.key",
3796 public_key: "testkey.avbpubkey",
3797 private_key: "testkey.pem",
3798 }
3799
3800 cc_library {
3801 name: "mylib_common",
3802 srcs: ["mylib.cpp"],
3803 system_shared_libs: [],
3804 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003805 apex_available: [
3806 "//apex_available:platform",
3807 "myapex",
3808 ],
Alex Light0851b882019-02-07 13:20:53 -08003809 }
3810 `)
3811
Sundong Ahnabb64432019-10-22 13:58:29 +09003812 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003813 apexRule := module.Rule("apexRule")
3814 copyCmds := apexRule.Args["copy_commands"]
3815
3816 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3817 t.Log("Apex was a test apex!")
3818 t.Fail()
3819 }
3820 // Ensure that main rule creates an output
3821 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3822
3823 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003824 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003825
3826 // Ensure that both direct and indirect deps are copied into apex
3827 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3828
Colin Cross7113d202019-11-20 16:39:12 -08003829 // Ensure that the platform variant ends with _shared
3830 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003831
Colin Cross56a83212020-09-15 18:30:11 -07003832 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003833 t.Log("Found mylib_common not in any apex!")
3834 t.Fail()
3835 }
3836}
3837
3838func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003839 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003840 apex_test {
3841 name: "myapex",
3842 key: "myapex.key",
3843 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003844 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003845 }
3846
3847 apex_key {
3848 name: "myapex.key",
3849 public_key: "testkey.avbpubkey",
3850 private_key: "testkey.pem",
3851 }
3852
3853 cc_library {
3854 name: "mylib_common_test",
3855 srcs: ["mylib.cpp"],
3856 system_shared_libs: [],
3857 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003858 // TODO: remove //apex_available:platform
3859 apex_available: [
3860 "//apex_available:platform",
3861 "myapex",
3862 ],
Alex Light0851b882019-02-07 13:20:53 -08003863 }
3864 `)
3865
Sundong Ahnabb64432019-10-22 13:58:29 +09003866 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003867 apexRule := module.Rule("apexRule")
3868 copyCmds := apexRule.Args["copy_commands"]
3869
3870 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3871 t.Log("Apex was not a test apex!")
3872 t.Fail()
3873 }
3874 // Ensure that main rule creates an output
3875 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3876
3877 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003878 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003879
3880 // Ensure that both direct and indirect deps are copied into apex
3881 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3882
Colin Cross7113d202019-11-20 16:39:12 -08003883 // Ensure that the platform variant ends with _shared
3884 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003885}
3886
Alex Light9670d332019-01-29 18:07:33 -08003887func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003888 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003889 apex {
3890 name: "myapex",
3891 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003892 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003893 multilib: {
3894 first: {
3895 native_shared_libs: ["mylib_common"],
3896 }
3897 },
3898 target: {
3899 android: {
3900 multilib: {
3901 first: {
3902 native_shared_libs: ["mylib"],
3903 }
3904 }
3905 },
3906 host: {
3907 multilib: {
3908 first: {
3909 native_shared_libs: ["mylib2"],
3910 }
3911 }
3912 }
3913 }
3914 }
3915
3916 apex_key {
3917 name: "myapex.key",
3918 public_key: "testkey.avbpubkey",
3919 private_key: "testkey.pem",
3920 }
3921
3922 cc_library {
3923 name: "mylib",
3924 srcs: ["mylib.cpp"],
3925 system_shared_libs: [],
3926 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003927 // TODO: remove //apex_available:platform
3928 apex_available: [
3929 "//apex_available:platform",
3930 "myapex",
3931 ],
Alex Light9670d332019-01-29 18:07:33 -08003932 }
3933
3934 cc_library {
3935 name: "mylib_common",
3936 srcs: ["mylib.cpp"],
3937 system_shared_libs: [],
3938 stl: "none",
3939 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003940 // TODO: remove //apex_available:platform
3941 apex_available: [
3942 "//apex_available:platform",
3943 "myapex",
3944 ],
Alex Light9670d332019-01-29 18:07:33 -08003945 }
3946
3947 cc_library {
3948 name: "mylib2",
3949 srcs: ["mylib.cpp"],
3950 system_shared_libs: [],
3951 stl: "none",
3952 compile_multilib: "first",
3953 }
3954 `)
3955
Sundong Ahnabb64432019-10-22 13:58:29 +09003956 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003957 copyCmds := apexRule.Args["copy_commands"]
3958
3959 // Ensure that main rule creates an output
3960 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3961
3962 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003963 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3964 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3965 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003966
3967 // Ensure that both direct and indirect deps are copied into apex
3968 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3969 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3970 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3971
Colin Cross7113d202019-11-20 16:39:12 -08003972 // Ensure that the platform variant ends with _shared
3973 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3974 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3975 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003976}
Jiyong Park04480cf2019-02-06 00:16:29 +09003977
Jiyong Park59140302020-12-14 18:44:04 +09003978func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003979 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003980 apex {
3981 name: "myapex",
3982 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003983 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003984 arch: {
3985 arm64: {
3986 native_shared_libs: ["mylib.arm64"],
3987 },
3988 x86_64: {
3989 native_shared_libs: ["mylib.x64"],
3990 },
3991 }
3992 }
3993
3994 apex_key {
3995 name: "myapex.key",
3996 public_key: "testkey.avbpubkey",
3997 private_key: "testkey.pem",
3998 }
3999
4000 cc_library {
4001 name: "mylib.arm64",
4002 srcs: ["mylib.cpp"],
4003 system_shared_libs: [],
4004 stl: "none",
4005 // TODO: remove //apex_available:platform
4006 apex_available: [
4007 "//apex_available:platform",
4008 "myapex",
4009 ],
4010 }
4011
4012 cc_library {
4013 name: "mylib.x64",
4014 srcs: ["mylib.cpp"],
4015 system_shared_libs: [],
4016 stl: "none",
4017 // TODO: remove //apex_available:platform
4018 apex_available: [
4019 "//apex_available:platform",
4020 "myapex",
4021 ],
4022 }
4023 `)
4024
4025 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4026 copyCmds := apexRule.Args["copy_commands"]
4027
4028 // Ensure that apex variant is created for the direct dep
4029 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4030 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4031
4032 // Ensure that both direct and indirect deps are copied into apex
4033 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4034 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4035}
4036
Jiyong Park04480cf2019-02-06 00:16:29 +09004037func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004038 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004039 apex {
4040 name: "myapex",
4041 key: "myapex.key",
4042 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004043 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004044 }
4045
4046 apex_key {
4047 name: "myapex.key",
4048 public_key: "testkey.avbpubkey",
4049 private_key: "testkey.pem",
4050 }
4051
4052 sh_binary {
4053 name: "myscript",
4054 src: "mylib.cpp",
4055 filename: "myscript.sh",
4056 sub_dir: "script",
4057 }
4058 `)
4059
Sundong Ahnabb64432019-10-22 13:58:29 +09004060 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004061 copyCmds := apexRule.Args["copy_commands"]
4062
4063 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4064}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004065
Jooyung Han91df2082019-11-20 01:49:42 +09004066func TestApexInVariousPartition(t *testing.T) {
4067 testcases := []struct {
4068 propName, parition, flattenedPartition string
4069 }{
4070 {"", "system", "system_ext"},
4071 {"product_specific: true", "product", "product"},
4072 {"soc_specific: true", "vendor", "vendor"},
4073 {"proprietary: true", "vendor", "vendor"},
4074 {"vendor: true", "vendor", "vendor"},
4075 {"system_ext_specific: true", "system_ext", "system_ext"},
4076 }
4077 for _, tc := range testcases {
4078 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004079 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004080 apex {
4081 name: "myapex",
4082 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004083 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004084 `+tc.propName+`
4085 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004086
Jooyung Han91df2082019-11-20 01:49:42 +09004087 apex_key {
4088 name: "myapex.key",
4089 public_key: "testkey.avbpubkey",
4090 private_key: "testkey.pem",
4091 }
4092 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004093
Jooyung Han91df2082019-11-20 01:49:42 +09004094 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4095 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4096 actual := apex.installDir.String()
4097 if actual != expected {
4098 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4099 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004100
Jooyung Han91df2082019-11-20 01:49:42 +09004101 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4102 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4103 actual = flattened.installDir.String()
4104 if actual != expected {
4105 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4106 }
4107 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004108 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004109}
Jiyong Park67882562019-03-21 01:11:21 +09004110
Jooyung Han580eb4f2020-06-24 19:33:06 +09004111func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004112 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004113 apex {
4114 name: "myapex",
4115 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004116 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004117 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004118
Jooyung Han580eb4f2020-06-24 19:33:06 +09004119 apex_key {
4120 name: "myapex.key",
4121 public_key: "testkey.avbpubkey",
4122 private_key: "testkey.pem",
4123 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004124 `)
4125 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004126 rule := module.Output("file_contexts")
4127 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4128}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004129
Jooyung Han580eb4f2020-06-24 19:33:06 +09004130func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004131 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004132 apex {
4133 name: "myapex",
4134 key: "myapex.key",
4135 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004136 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004137 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004138
Jooyung Han580eb4f2020-06-24 19:33:06 +09004139 apex_key {
4140 name: "myapex.key",
4141 public_key: "testkey.avbpubkey",
4142 private_key: "testkey.pem",
4143 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004144 `, withFiles(map[string][]byte{
4145 "my_own_file_contexts": nil,
4146 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004147}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004148
Jooyung Han580eb4f2020-06-24 19:33:06 +09004149func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004150 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004151 apex {
4152 name: "myapex",
4153 key: "myapex.key",
4154 product_specific: true,
4155 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004156 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004157 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004158
Jooyung Han580eb4f2020-06-24 19:33:06 +09004159 apex_key {
4160 name: "myapex.key",
4161 public_key: "testkey.avbpubkey",
4162 private_key: "testkey.pem",
4163 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004164 `)
4165
Colin Cross1c460562021-02-16 17:55:47 -08004166 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004167 apex {
4168 name: "myapex",
4169 key: "myapex.key",
4170 product_specific: true,
4171 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004172 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004173 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004174
Jooyung Han580eb4f2020-06-24 19:33:06 +09004175 apex_key {
4176 name: "myapex.key",
4177 public_key: "testkey.avbpubkey",
4178 private_key: "testkey.pem",
4179 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004180 `, withFiles(map[string][]byte{
4181 "product_specific_file_contexts": nil,
4182 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004183 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4184 rule := module.Output("file_contexts")
4185 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4186}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004187
Jooyung Han580eb4f2020-06-24 19:33:06 +09004188func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004189 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004190 apex {
4191 name: "myapex",
4192 key: "myapex.key",
4193 product_specific: true,
4194 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004195 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004196 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004197
Jooyung Han580eb4f2020-06-24 19:33:06 +09004198 apex_key {
4199 name: "myapex.key",
4200 public_key: "testkey.avbpubkey",
4201 private_key: "testkey.pem",
4202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004203
Jooyung Han580eb4f2020-06-24 19:33:06 +09004204 filegroup {
4205 name: "my-file-contexts",
4206 srcs: ["product_specific_file_contexts"],
4207 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004208 `, withFiles(map[string][]byte{
4209 "product_specific_file_contexts": nil,
4210 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004211 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4212 rule := module.Output("file_contexts")
4213 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004214}
4215
Jiyong Park67882562019-03-21 01:11:21 +09004216func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004217 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004218 apex_key {
4219 name: "myapex.key",
4220 public_key: ":my.avbpubkey",
4221 private_key: ":my.pem",
4222 product_specific: true,
4223 }
4224
4225 filegroup {
4226 name: "my.avbpubkey",
4227 srcs: ["testkey2.avbpubkey"],
4228 }
4229
4230 filegroup {
4231 name: "my.pem",
4232 srcs: ["testkey2.pem"],
4233 }
4234 `)
4235
4236 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4237 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004238 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004239 if actual_pubkey != expected_pubkey {
4240 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4241 }
4242 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004243 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004244 if actual_privkey != expected_privkey {
4245 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4246 }
4247}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004248
4249func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004250 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004251 prebuilt_apex {
4252 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004253 arch: {
4254 arm64: {
4255 src: "myapex-arm64.apex",
4256 },
4257 arm: {
4258 src: "myapex-arm.apex",
4259 },
4260 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004261 }
4262 `)
4263
4264 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4265
Jiyong Parkc95714e2019-03-29 14:23:10 +09004266 expectedInput := "myapex-arm64.apex"
4267 if prebuilt.inputApex.String() != expectedInput {
4268 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4269 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004270}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004271
4272func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004273 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004274 prebuilt_apex {
4275 name: "myapex",
4276 src: "myapex-arm.apex",
4277 filename: "notmyapex.apex",
4278 }
4279 `)
4280
4281 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4282
4283 expected := "notmyapex.apex"
4284 if p.installFilename != expected {
4285 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4286 }
4287}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004288
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004289func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004290 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004291 prebuilt_apex {
4292 name: "myapex.prebuilt",
4293 src: "myapex-arm.apex",
4294 overrides: [
4295 "myapex",
4296 ],
4297 }
4298 `)
4299
4300 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4301
4302 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004303 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004304 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004305 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004306 }
4307}
4308
Paul Duffin092153d2021-01-26 11:42:39 +00004309// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4310// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004311func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4312 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004313 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004314 }
4315
Paul Duffin89886cb2021-02-05 16:44:03 +00004316 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004317 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004318 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004319 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004320 stem := android.RemoveOptionalPrebuiltPrefix(name)
4321 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004322 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4323 }
4324 }
4325
Paul Duffin39853512021-02-26 11:09:39 +00004326 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004327 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004328 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004329 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4330 }
4331 }
4332
4333 t.Run("prebuilt only", func(t *testing.T) {
4334 bp := `
4335 prebuilt_apex {
4336 name: "myapex",
4337 arch: {
4338 arm64: {
4339 src: "myapex-arm64.apex",
4340 },
4341 arm: {
4342 src: "myapex-arm.apex",
4343 },
4344 },
Paul Duffin39853512021-02-26 11:09:39 +00004345 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004346 }
4347
4348 java_import {
4349 name: "libfoo",
4350 jars: ["libfoo.jar"],
4351 }
Paul Duffin39853512021-02-26 11:09:39 +00004352
4353 java_sdk_library_import {
4354 name: "libbar",
4355 public: {
4356 jars: ["libbar.jar"],
4357 },
4358 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004359 `
4360
4361 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4362 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4363
Paul Duffinf6932af2021-02-26 18:21:56 +00004364 // Make sure that the deapexer has the correct input APEX.
4365 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4366 rule := deapexer.Rule("deapexer")
4367 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4368 t.Errorf("expected: %q, found: %q", expected, actual)
4369 }
4370
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004371 // Make sure that the prebuilt_apex has the correct input APEX.
4372 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4373 rule = prebuiltApex.Rule("android/soong/android.Cp")
4374 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4375 t.Errorf("expected: %q, found: %q", expected, actual)
4376 }
4377
Paul Duffin89886cb2021-02-05 16:44:03 +00004378 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004379
4380 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004381 })
4382
4383 t.Run("prebuilt with source preferred", func(t *testing.T) {
4384
4385 bp := `
4386 prebuilt_apex {
4387 name: "myapex",
4388 arch: {
4389 arm64: {
4390 src: "myapex-arm64.apex",
4391 },
4392 arm: {
4393 src: "myapex-arm.apex",
4394 },
4395 },
Paul Duffin39853512021-02-26 11:09:39 +00004396 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004397 }
4398
4399 java_import {
4400 name: "libfoo",
4401 jars: ["libfoo.jar"],
4402 }
4403
4404 java_library {
4405 name: "libfoo",
4406 }
Paul Duffin39853512021-02-26 11:09:39 +00004407
4408 java_sdk_library_import {
4409 name: "libbar",
4410 public: {
4411 jars: ["libbar.jar"],
4412 },
4413 }
4414
4415 java_sdk_library {
4416 name: "libbar",
4417 srcs: ["foo/bar/MyClass.java"],
4418 unsafe_ignore_missing_latest_api: true,
4419 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004420 `
4421
4422 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4423 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4424
Paul Duffin89886cb2021-02-05 16:44:03 +00004425 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004426 ensureNoSourceVariant(t, ctx, "libfoo")
4427
4428 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4429 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004430 })
4431
4432 t.Run("prebuilt preferred with source", func(t *testing.T) {
4433 bp := `
4434 prebuilt_apex {
4435 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004436 arch: {
4437 arm64: {
4438 src: "myapex-arm64.apex",
4439 },
4440 arm: {
4441 src: "myapex-arm.apex",
4442 },
4443 },
Paul Duffin39853512021-02-26 11:09:39 +00004444 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004445 }
4446
4447 java_import {
4448 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004449 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004450 jars: ["libfoo.jar"],
4451 }
4452
4453 java_library {
4454 name: "libfoo",
4455 }
Paul Duffin39853512021-02-26 11:09:39 +00004456
4457 java_sdk_library_import {
4458 name: "libbar",
4459 prefer: true,
4460 public: {
4461 jars: ["libbar.jar"],
4462 },
4463 }
4464
4465 java_sdk_library {
4466 name: "libbar",
4467 srcs: ["foo/bar/MyClass.java"],
4468 unsafe_ignore_missing_latest_api: true,
4469 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004470 `
4471
4472 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4473 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4474
Paul Duffin89886cb2021-02-05 16:44:03 +00004475 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004476 ensureNoSourceVariant(t, ctx, "libfoo")
4477
4478 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4479 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004480 })
4481}
4482
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004483func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4484 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004485 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004486 }
4487
Paul Duffin37856732021-02-26 14:24:15 +00004488 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4489 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004490 s := ctx.SingletonForTests("dex_bootjars")
4491 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004492 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004493 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004494 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004495 foundLibfooJar = true
4496 buildRule := s.Output(output)
4497 actual := android.NormalizePathForTesting(buildRule.Input)
4498 if actual != bootDexJarPath {
4499 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4500 }
4501 }
4502 }
4503 if !foundLibfooJar {
4504 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4505 }
4506 }
4507
Paul Duffin4fd997b2021-02-03 20:06:33 +00004508 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004509 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004510 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4511 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4512 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4513 }
4514
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004515 t.Run("prebuilt only", func(t *testing.T) {
4516 bp := `
4517 prebuilt_apex {
4518 name: "myapex",
4519 arch: {
4520 arm64: {
4521 src: "myapex-arm64.apex",
4522 },
4523 arm: {
4524 src: "myapex-arm.apex",
4525 },
4526 },
Paul Duffin37856732021-02-26 14:24:15 +00004527 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004528 }
4529
4530 java_import {
4531 name: "libfoo",
4532 jars: ["libfoo.jar"],
4533 apex_available: ["myapex"],
4534 }
Paul Duffin37856732021-02-26 14:24:15 +00004535
4536 java_sdk_library_import {
4537 name: "libbar",
4538 public: {
4539 jars: ["libbar.jar"],
4540 },
4541 apex_available: ["myapex"],
4542 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004543 `
4544
4545 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004546 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4547 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004548
Paul Duffin9d67ca62021-02-03 20:06:33 +00004549 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4550 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004551.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004552.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4553`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004554 })
4555
4556 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4557 bp := `
4558 prebuilt_apex {
4559 name: "myapex",
4560 arch: {
4561 arm64: {
4562 src: "myapex-arm64.apex",
4563 },
4564 arm: {
4565 src: "myapex-arm.apex",
4566 },
4567 },
Paul Duffin37856732021-02-26 14:24:15 +00004568 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004569 }
4570
4571 java_import {
4572 name: "libfoo",
4573 jars: ["libfoo.jar"],
4574 apex_available: ["myapex"],
4575 }
4576
4577 java_library {
4578 name: "libfoo",
4579 srcs: ["foo/bar/MyClass.java"],
4580 apex_available: ["myapex"],
4581 }
Paul Duffin37856732021-02-26 14:24:15 +00004582
4583 java_sdk_library_import {
4584 name: "libbar",
4585 public: {
4586 jars: ["libbar.jar"],
4587 },
4588 apex_available: ["myapex"],
4589 }
4590
4591 java_sdk_library {
4592 name: "libbar",
4593 srcs: ["foo/bar/MyClass.java"],
4594 unsafe_ignore_missing_latest_api: true,
4595 apex_available: ["myapex"],
4596 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004597 `
4598
4599 // In this test the source (java_library) libfoo is active since the
4600 // prebuilt (java_import) defaults to prefer:false. However the
4601 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4602 // find the dex boot jar in it. We either need to disable the source libfoo
4603 // or make the prebuilt libfoo preferred.
4604 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4605 })
4606
4607 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4608 bp := `
4609 prebuilt_apex {
4610 name: "myapex",
4611 arch: {
4612 arm64: {
4613 src: "myapex-arm64.apex",
4614 },
4615 arm: {
4616 src: "myapex-arm.apex",
4617 },
4618 },
Paul Duffin37856732021-02-26 14:24:15 +00004619 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004620 }
4621
4622 java_import {
4623 name: "libfoo",
4624 prefer: true,
4625 jars: ["libfoo.jar"],
4626 apex_available: ["myapex"],
4627 }
4628
4629 java_library {
4630 name: "libfoo",
4631 srcs: ["foo/bar/MyClass.java"],
4632 apex_available: ["myapex"],
4633 }
Paul Duffin37856732021-02-26 14:24:15 +00004634
4635 java_sdk_library_import {
4636 name: "libbar",
4637 prefer: true,
4638 public: {
4639 jars: ["libbar.jar"],
4640 },
4641 apex_available: ["myapex"],
4642 }
4643
4644 java_sdk_library {
4645 name: "libbar",
4646 srcs: ["foo/bar/MyClass.java"],
4647 unsafe_ignore_missing_latest_api: true,
4648 apex_available: ["myapex"],
4649 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004650 `
4651
4652 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004653 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4654 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004655
Paul Duffin9d67ca62021-02-03 20:06:33 +00004656 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4657 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004658.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004659.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4660`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004661 })
4662
4663 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4664 bp := `
4665 apex {
4666 name: "myapex",
4667 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004668 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004669 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004670 }
4671
4672 apex_key {
4673 name: "myapex.key",
4674 public_key: "testkey.avbpubkey",
4675 private_key: "testkey.pem",
4676 }
4677
4678 prebuilt_apex {
4679 name: "myapex",
4680 arch: {
4681 arm64: {
4682 src: "myapex-arm64.apex",
4683 },
4684 arm: {
4685 src: "myapex-arm.apex",
4686 },
4687 },
Paul Duffin37856732021-02-26 14:24:15 +00004688 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004689 }
4690
4691 java_import {
4692 name: "libfoo",
4693 jars: ["libfoo.jar"],
4694 apex_available: ["myapex"],
4695 }
4696
4697 java_library {
4698 name: "libfoo",
4699 srcs: ["foo/bar/MyClass.java"],
4700 apex_available: ["myapex"],
4701 }
Paul Duffin37856732021-02-26 14:24:15 +00004702
4703 java_sdk_library_import {
4704 name: "libbar",
4705 public: {
4706 jars: ["libbar.jar"],
4707 },
4708 apex_available: ["myapex"],
4709 }
4710
4711 java_sdk_library {
4712 name: "libbar",
4713 srcs: ["foo/bar/MyClass.java"],
4714 unsafe_ignore_missing_latest_api: true,
4715 apex_available: ["myapex"],
4716 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004717 `
4718
4719 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004720 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4721 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004722
4723 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4724 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004725.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004726.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4727`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004728 })
4729
4730 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4731 bp := `
4732 apex {
4733 name: "myapex",
4734 enabled: false,
4735 key: "myapex.key",
4736 java_libs: ["libfoo"],
4737 }
4738
4739 apex_key {
4740 name: "myapex.key",
4741 public_key: "testkey.avbpubkey",
4742 private_key: "testkey.pem",
4743 }
4744
4745 prebuilt_apex {
4746 name: "myapex",
4747 arch: {
4748 arm64: {
4749 src: "myapex-arm64.apex",
4750 },
4751 arm: {
4752 src: "myapex-arm.apex",
4753 },
4754 },
Paul Duffin37856732021-02-26 14:24:15 +00004755 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004756 }
4757
4758 java_import {
4759 name: "libfoo",
4760 prefer: true,
4761 jars: ["libfoo.jar"],
4762 apex_available: ["myapex"],
4763 }
4764
4765 java_library {
4766 name: "libfoo",
4767 srcs: ["foo/bar/MyClass.java"],
4768 apex_available: ["myapex"],
4769 }
Paul Duffin37856732021-02-26 14:24:15 +00004770
4771 java_sdk_library_import {
4772 name: "libbar",
4773 prefer: true,
4774 public: {
4775 jars: ["libbar.jar"],
4776 },
4777 apex_available: ["myapex"],
4778 }
4779
4780 java_sdk_library {
4781 name: "libbar",
4782 srcs: ["foo/bar/MyClass.java"],
4783 unsafe_ignore_missing_latest_api: true,
4784 apex_available: ["myapex"],
4785 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004786 `
4787
4788 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004789 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4790 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004791
Paul Duffin9d67ca62021-02-03 20:06:33 +00004792 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4793 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004794.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004795.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4796`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004797 })
4798}
4799
Roland Levillain630846d2019-06-26 12:48:34 +01004800func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004801 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004802 apex_test {
4803 name: "myapex",
4804 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004805 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004806 tests: [
4807 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004808 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004809 ],
4810 }
4811
4812 apex_key {
4813 name: "myapex.key",
4814 public_key: "testkey.avbpubkey",
4815 private_key: "testkey.pem",
4816 }
4817
Liz Kammer1c14a212020-05-12 15:26:55 -07004818 filegroup {
4819 name: "fg",
4820 srcs: [
4821 "baz",
4822 "bar/baz"
4823 ],
4824 }
4825
Roland Levillain630846d2019-06-26 12:48:34 +01004826 cc_test {
4827 name: "mytest",
4828 gtest: false,
4829 srcs: ["mytest.cpp"],
4830 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004831 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004832 system_shared_libs: [],
4833 static_executable: true,
4834 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004835 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004836 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004837
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004838 cc_library {
4839 name: "mylib",
4840 srcs: ["mylib.cpp"],
4841 system_shared_libs: [],
4842 stl: "none",
4843 }
4844
Liz Kammer5bd365f2020-05-27 15:15:11 -07004845 filegroup {
4846 name: "fg2",
4847 srcs: [
4848 "testdata/baz"
4849 ],
4850 }
4851
Roland Levillain9b5fde92019-06-28 15:41:19 +01004852 cc_test {
4853 name: "mytests",
4854 gtest: false,
4855 srcs: [
4856 "mytest1.cpp",
4857 "mytest2.cpp",
4858 "mytest3.cpp",
4859 ],
4860 test_per_src: true,
4861 relative_install_path: "test",
4862 system_shared_libs: [],
4863 static_executable: true,
4864 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004865 data: [
4866 ":fg",
4867 ":fg2",
4868 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004869 }
Roland Levillain630846d2019-06-26 12:48:34 +01004870 `)
4871
Sundong Ahnabb64432019-10-22 13:58:29 +09004872 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004873 copyCmds := apexRule.Args["copy_commands"]
4874
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004875 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004876 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004877 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004878
Liz Kammer1c14a212020-05-12 15:26:55 -07004879 //Ensure that test data are copied into apex.
4880 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4881 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4882
Roland Levillain9b5fde92019-06-28 15:41:19 +01004883 // Ensure that test deps built with `test_per_src` are copied into apex.
4884 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4885 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4886 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004887
4888 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004889 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004890 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004891 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004892 prefix := "TARGET_"
4893 var builder strings.Builder
4894 data.Custom(&builder, name, prefix, "", data)
4895 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004896 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4897 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4898 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4899 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004900 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004901 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004902 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004903
4904 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004905 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004906 data.Custom(&builder, name, prefix, "", data)
4907 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004908 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4909 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004910}
4911
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004912func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004913 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004914 apex {
4915 name: "myapex",
4916 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004917 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004918 }
4919 apex_key {
4920 name: "myapex.key",
4921 public_key: "testkey.avbpubkey",
4922 private_key: "testkey.pem",
4923 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004924 `,
4925 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4926 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4927 }),
4928 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004929 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004930 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004931 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004932 var builder strings.Builder
4933 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4934 androidMk := builder.String()
4935 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4936}
4937
Jooyung Hand48f3c32019-08-23 11:18:57 +09004938func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4939 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4940 apex {
4941 name: "myapex",
4942 key: "myapex.key",
4943 native_shared_libs: ["libfoo"],
4944 }
4945
4946 apex_key {
4947 name: "myapex.key",
4948 public_key: "testkey.avbpubkey",
4949 private_key: "testkey.pem",
4950 }
4951
4952 cc_library {
4953 name: "libfoo",
4954 stl: "none",
4955 system_shared_libs: [],
4956 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004957 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004958 }
4959 `)
4960 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4961 apex {
4962 name: "myapex",
4963 key: "myapex.key",
4964 java_libs: ["myjar"],
4965 }
4966
4967 apex_key {
4968 name: "myapex.key",
4969 public_key: "testkey.avbpubkey",
4970 private_key: "testkey.pem",
4971 }
4972
4973 java_library {
4974 name: "myjar",
4975 srcs: ["foo/bar/MyClass.java"],
4976 sdk_version: "none",
4977 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004978 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004979 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004980 }
4981 `)
4982}
4983
Bill Peckhama41a6962021-01-11 10:58:54 -08004984func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004985 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004986 apex {
4987 name: "myapex",
4988 key: "myapex.key",
4989 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004990 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004991 }
4992
4993 apex_key {
4994 name: "myapex.key",
4995 public_key: "testkey.avbpubkey",
4996 private_key: "testkey.pem",
4997 }
4998
4999 java_import {
5000 name: "myjavaimport",
5001 apex_available: ["myapex"],
5002 jars: ["my.jar"],
5003 compile_dex: true,
5004 }
5005 `)
5006
5007 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5008 apexRule := module.Rule("apexRule")
5009 copyCmds := apexRule.Args["copy_commands"]
5010 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5011}
5012
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005013func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005014 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005015 apex {
5016 name: "myapex",
5017 key: "myapex.key",
5018 apps: [
5019 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005020 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005021 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005022 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005023 }
5024
5025 apex_key {
5026 name: "myapex.key",
5027 public_key: "testkey.avbpubkey",
5028 private_key: "testkey.pem",
5029 }
5030
5031 android_app {
5032 name: "AppFoo",
5033 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005034 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005035 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005036 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005037 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005038 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005039 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005040
5041 android_app {
5042 name: "AppFooPriv",
5043 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005044 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005045 system_modules: "none",
5046 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005047 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005048 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005049 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005050
5051 cc_library_shared {
5052 name: "libjni",
5053 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005054 shared_libs: ["libfoo"],
5055 stl: "none",
5056 system_shared_libs: [],
5057 apex_available: [ "myapex" ],
5058 sdk_version: "current",
5059 }
5060
5061 cc_library_shared {
5062 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005063 stl: "none",
5064 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005065 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005066 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005067 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005068 `)
5069
Sundong Ahnabb64432019-10-22 13:58:29 +09005070 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005071 apexRule := module.Rule("apexRule")
5072 copyCmds := apexRule.Args["copy_commands"]
5073
5074 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005075 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005076
Colin Crossaede88c2020-08-11 12:17:01 -07005077 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005078 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005079 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005080 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005081 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005082 // JNI libraries including transitive deps are
5083 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005084 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005085 // ... embedded inside APK (jnilibs.zip)
5086 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5087 // ... and not directly inside the APEX
5088 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5089 }
Dario Frenicde2a032019-10-27 00:29:22 +01005090}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005091
Dario Frenicde2a032019-10-27 00:29:22 +01005092func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005093 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005094 apex {
5095 name: "myapex",
5096 key: "myapex.key",
5097 apps: [
5098 "AppFooPrebuilt",
5099 "AppFooPrivPrebuilt",
5100 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005101 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005102 }
5103
5104 apex_key {
5105 name: "myapex.key",
5106 public_key: "testkey.avbpubkey",
5107 private_key: "testkey.pem",
5108 }
5109
5110 android_app_import {
5111 name: "AppFooPrebuilt",
5112 apk: "PrebuiltAppFoo.apk",
5113 presigned: true,
5114 dex_preopt: {
5115 enabled: false,
5116 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005117 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005118 }
5119
5120 android_app_import {
5121 name: "AppFooPrivPrebuilt",
5122 apk: "PrebuiltAppFooPriv.apk",
5123 privileged: true,
5124 presigned: true,
5125 dex_preopt: {
5126 enabled: false,
5127 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005128 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005129 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005130 }
5131 `)
5132
Sundong Ahnabb64432019-10-22 13:58:29 +09005133 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005134 apexRule := module.Rule("apexRule")
5135 copyCmds := apexRule.Args["copy_commands"]
5136
5137 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005138 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5139}
5140
5141func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005142 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005143 apex {
5144 name: "myapex",
5145 key: "myapex.key",
5146 apps: [
5147 "AppFoo",
5148 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005149 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005150 }
5151
5152 apex_key {
5153 name: "myapex.key",
5154 public_key: "testkey.avbpubkey",
5155 private_key: "testkey.pem",
5156 }
5157
5158 android_app {
5159 name: "AppFoo",
5160 srcs: ["foo/bar/MyClass.java"],
5161 sdk_version: "none",
5162 system_modules: "none",
5163 apex_available: [ "myapex" ],
5164 }
5165
5166 android_app_import {
5167 name: "AppFoo",
5168 apk: "AppFooPrebuilt.apk",
5169 filename: "AppFooPrebuilt.apk",
5170 presigned: true,
5171 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005172 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005173 }
5174 `, withFiles(map[string][]byte{
5175 "AppFooPrebuilt.apk": nil,
5176 }))
5177
5178 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005179 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005180 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005181}
5182
Dario Freni6f3937c2019-12-20 22:58:03 +00005183func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005184 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005185 apex {
5186 name: "myapex",
5187 key: "myapex.key",
5188 apps: [
5189 "TesterHelpAppFoo",
5190 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005191 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005192 }
5193
5194 apex_key {
5195 name: "myapex.key",
5196 public_key: "testkey.avbpubkey",
5197 private_key: "testkey.pem",
5198 }
5199
5200 android_test_helper_app {
5201 name: "TesterHelpAppFoo",
5202 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005203 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005204 }
5205
5206 `)
5207
5208 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5209 apexRule := module.Rule("apexRule")
5210 copyCmds := apexRule.Args["copy_commands"]
5211
5212 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5213}
5214
Jooyung Han18020ea2019-11-13 10:50:48 +09005215func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5216 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005217 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005218 apex {
5219 name: "myapex",
5220 key: "myapex.key",
5221 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005222 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005223 }
5224
5225 apex_key {
5226 name: "myapex.key",
5227 public_key: "testkey.avbpubkey",
5228 private_key: "testkey.pem",
5229 }
5230
5231 apex {
5232 name: "otherapex",
5233 key: "myapex.key",
5234 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005235 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005236 }
5237
5238 cc_defaults {
5239 name: "libfoo-defaults",
5240 apex_available: ["otherapex"],
5241 }
5242
5243 cc_library {
5244 name: "libfoo",
5245 defaults: ["libfoo-defaults"],
5246 stl: "none",
5247 system_shared_libs: [],
5248 }`)
5249}
5250
Paul Duffine52e66f2020-03-30 17:54:29 +01005251func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005252 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005253 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005254 apex {
5255 name: "myapex",
5256 key: "myapex.key",
5257 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005258 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005259 }
5260
5261 apex_key {
5262 name: "myapex.key",
5263 public_key: "testkey.avbpubkey",
5264 private_key: "testkey.pem",
5265 }
5266
5267 apex {
5268 name: "otherapex",
5269 key: "otherapex.key",
5270 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005271 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005272 }
5273
5274 apex_key {
5275 name: "otherapex.key",
5276 public_key: "testkey.avbpubkey",
5277 private_key: "testkey.pem",
5278 }
5279
5280 cc_library {
5281 name: "libfoo",
5282 stl: "none",
5283 system_shared_libs: [],
5284 apex_available: ["otherapex"],
5285 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005286}
Jiyong Park127b40b2019-09-30 16:04:35 +09005287
Paul Duffine52e66f2020-03-30 17:54:29 +01005288func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005289 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005290 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005291.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005292.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005293.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005294.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005295.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005296.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005297 apex {
5298 name: "myapex",
5299 key: "myapex.key",
5300 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005301 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005302 }
5303
5304 apex_key {
5305 name: "myapex.key",
5306 public_key: "testkey.avbpubkey",
5307 private_key: "testkey.pem",
5308 }
5309
Jiyong Park127b40b2019-09-30 16:04:35 +09005310 cc_library {
5311 name: "libfoo",
5312 stl: "none",
5313 shared_libs: ["libbar"],
5314 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005315 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005316 }
5317
5318 cc_library {
5319 name: "libbar",
5320 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005321 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005322 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005323 apex_available: ["myapex"],
5324 }
5325
5326 cc_library {
5327 name: "libbaz",
5328 stl: "none",
5329 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005330 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005331}
Jiyong Park127b40b2019-09-30 16:04:35 +09005332
Paul Duffine52e66f2020-03-30 17:54:29 +01005333func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005334 testApexError(t, "\"otherapex\" is not a valid module name", `
5335 apex {
5336 name: "myapex",
5337 key: "myapex.key",
5338 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005339 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005340 }
5341
5342 apex_key {
5343 name: "myapex.key",
5344 public_key: "testkey.avbpubkey",
5345 private_key: "testkey.pem",
5346 }
5347
5348 cc_library {
5349 name: "libfoo",
5350 stl: "none",
5351 system_shared_libs: [],
5352 apex_available: ["otherapex"],
5353 }`)
5354
Paul Duffine52e66f2020-03-30 17:54:29 +01005355 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005356 apex {
5357 name: "myapex",
5358 key: "myapex.key",
5359 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005360 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005361 }
5362
5363 apex_key {
5364 name: "myapex.key",
5365 public_key: "testkey.avbpubkey",
5366 private_key: "testkey.pem",
5367 }
5368
5369 cc_library {
5370 name: "libfoo",
5371 stl: "none",
5372 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005373 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005374 apex_available: ["myapex"],
5375 }
5376
5377 cc_library {
5378 name: "libbar",
5379 stl: "none",
5380 system_shared_libs: [],
5381 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005382 }
5383
5384 cc_library {
5385 name: "libbaz",
5386 stl: "none",
5387 system_shared_libs: [],
5388 stubs: {
5389 versions: ["10", "20", "30"],
5390 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005391 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005392}
Jiyong Park127b40b2019-09-30 16:04:35 +09005393
Jiyong Park89e850a2020-04-07 16:37:39 +09005394func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005395 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005396 apex {
5397 name: "myapex",
5398 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005399 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005400 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005401 }
5402
5403 apex_key {
5404 name: "myapex.key",
5405 public_key: "testkey.avbpubkey",
5406 private_key: "testkey.pem",
5407 }
5408
5409 cc_library {
5410 name: "libfoo",
5411 stl: "none",
5412 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005413 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005414 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005415 }
5416
5417 cc_library {
5418 name: "libfoo2",
5419 stl: "none",
5420 system_shared_libs: [],
5421 shared_libs: ["libbaz"],
5422 apex_available: ["//apex_available:platform"],
5423 }
5424
5425 cc_library {
5426 name: "libbar",
5427 stl: "none",
5428 system_shared_libs: [],
5429 apex_available: ["myapex"],
5430 }
5431
5432 cc_library {
5433 name: "libbaz",
5434 stl: "none",
5435 system_shared_libs: [],
5436 apex_available: ["myapex"],
5437 stubs: {
5438 versions: ["1"],
5439 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005440 }`)
5441
Jiyong Park89e850a2020-04-07 16:37:39 +09005442 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5443 // because it depends on libbar which isn't available to platform
5444 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5445 if libfoo.NotAvailableForPlatform() != true {
5446 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5447 }
5448
5449 // libfoo2 however can be available to platform because it depends on libbaz which provides
5450 // stubs
5451 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5452 if libfoo2.NotAvailableForPlatform() == true {
5453 t.Errorf("%q should be available to platform", libfoo2.String())
5454 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005455}
Jiyong Parka90ca002019-10-07 15:47:24 +09005456
Paul Duffine52e66f2020-03-30 17:54:29 +01005457func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005458 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005459 apex {
5460 name: "myapex",
5461 key: "myapex.key",
5462 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005463 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005464 }
5465
5466 apex_key {
5467 name: "myapex.key",
5468 public_key: "testkey.avbpubkey",
5469 private_key: "testkey.pem",
5470 }
5471
5472 cc_library {
5473 name: "libfoo",
5474 stl: "none",
5475 system_shared_libs: [],
5476 apex_available: ["myapex"],
5477 static: {
5478 apex_available: ["//apex_available:platform"],
5479 },
5480 }`)
5481
Jiyong Park89e850a2020-04-07 16:37:39 +09005482 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5483 if libfooShared.NotAvailableForPlatform() != true {
5484 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5485 }
5486 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5487 if libfooStatic.NotAvailableForPlatform() != false {
5488 t.Errorf("%q should be available to platform", libfooStatic.String())
5489 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005490}
5491
Jiyong Park5d790c32019-11-15 18:40:32 +09005492func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005493 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005494 apex {
5495 name: "myapex",
5496 key: "myapex.key",
5497 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005498 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005499 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005500 }
5501
5502 override_apex {
5503 name: "override_myapex",
5504 base: "myapex",
5505 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005506 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005507 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005508 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005509 }
5510
5511 apex_key {
5512 name: "myapex.key",
5513 public_key: "testkey.avbpubkey",
5514 private_key: "testkey.pem",
5515 }
5516
5517 android_app {
5518 name: "app",
5519 srcs: ["foo/bar/MyClass.java"],
5520 package_name: "foo",
5521 sdk_version: "none",
5522 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005523 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005524 }
5525
5526 override_android_app {
5527 name: "override_app",
5528 base: "app",
5529 package_name: "bar",
5530 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005531 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005532
Jiyong Park317645e2019-12-05 13:20:58 +09005533 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5534 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5535 if originalVariant.GetOverriddenBy() != "" {
5536 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5537 }
5538 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5539 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5540 }
5541
Jiyong Park5d790c32019-11-15 18:40:32 +09005542 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5543 apexRule := module.Rule("apexRule")
5544 copyCmds := apexRule.Args["copy_commands"]
5545
5546 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005547 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005548
5549 apexBundle := module.Module().(*apexBundle)
5550 name := apexBundle.Name()
5551 if name != "override_myapex" {
5552 t.Errorf("name should be \"override_myapex\", but was %q", name)
5553 }
5554
Baligh Uddin004d7172020-02-19 21:29:28 -08005555 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5556 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5557 }
5558
Jiyong Park20bacab2020-03-03 11:45:41 +09005559 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005560 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005561
Colin Crossaa255532020-07-03 13:18:24 -07005562 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005563 var builder strings.Builder
5564 data.Custom(&builder, name, "TARGET_", "", data)
5565 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005566 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005567 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5568 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005569 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005570 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005571 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005572 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5573 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005574}
5575
Jooyung Han214bf372019-11-12 13:03:50 +09005576func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005577 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005578 apex {
5579 name: "myapex",
5580 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005581 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005582 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005583 }
5584
5585 apex_key {
5586 name: "myapex.key",
5587 public_key: "testkey.avbpubkey",
5588 private_key: "testkey.pem",
5589 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005590
5591 cc_library {
5592 name: "mylib",
5593 srcs: ["mylib.cpp"],
5594 stl: "libc++",
5595 system_shared_libs: [],
5596 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005597 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005598 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005599 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005600
5601 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5602 args := module.Rule("apexRule").Args
5603 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005604 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005605
5606 // The copies of the libraries in the apex should have one more dependency than
5607 // the ones outside the apex, namely the unwinder. Ideally we should check
5608 // the dependency names directly here but for some reason the names are blank in
5609 // this test.
5610 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005611 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005612 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5613 if len(apexImplicits) != len(nonApexImplicits)+1 {
5614 t.Errorf("%q missing unwinder dep", lib)
5615 }
5616 }
Jooyung Han214bf372019-11-12 13:03:50 +09005617}
5618
Paul Duffine05480a2021-03-08 15:07:14 +00005619var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005620 "api/current.txt": nil,
5621 "api/removed.txt": nil,
5622 "api/system-current.txt": nil,
5623 "api/system-removed.txt": nil,
5624 "api/test-current.txt": nil,
5625 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005626
Anton Hanssondff2c782020-12-21 17:10:01 +00005627 "100/public/api/foo.txt": nil,
5628 "100/public/api/foo-removed.txt": nil,
5629 "100/system/api/foo.txt": nil,
5630 "100/system/api/foo-removed.txt": nil,
5631
Paul Duffineedc5d52020-06-12 17:46:39 +01005632 // For java_sdk_library_import
5633 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005634}
5635
Jooyung Han58f26ab2019-12-18 15:34:32 +09005636func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005637 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005638 apex {
5639 name: "myapex",
5640 key: "myapex.key",
5641 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005642 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005643 }
5644
5645 apex_key {
5646 name: "myapex.key",
5647 public_key: "testkey.avbpubkey",
5648 private_key: "testkey.pem",
5649 }
5650
5651 java_sdk_library {
5652 name: "foo",
5653 srcs: ["a.java"],
5654 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005655 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005656 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005657
5658 prebuilt_apis {
5659 name: "sdk",
5660 api_dirs: ["100"],
5661 }
Paul Duffin9b879592020-05-26 13:21:35 +01005662 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005663
5664 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005665 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005666 "javalib/foo.jar",
5667 "etc/permissions/foo.xml",
5668 })
5669 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005670 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5671 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005672}
5673
Paul Duffin9b879592020-05-26 13:21:35 +01005674func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005675 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005676 apex {
5677 name: "myapex",
5678 key: "myapex.key",
5679 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005680 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005681 }
5682
5683 apex_key {
5684 name: "myapex.key",
5685 public_key: "testkey.avbpubkey",
5686 private_key: "testkey.pem",
5687 }
5688
5689 java_sdk_library {
5690 name: "foo",
5691 srcs: ["a.java"],
5692 api_packages: ["foo"],
5693 apex_available: ["myapex"],
5694 sdk_version: "none",
5695 system_modules: "none",
5696 }
5697
5698 java_library {
5699 name: "bar",
5700 srcs: ["a.java"],
5701 libs: ["foo"],
5702 apex_available: ["myapex"],
5703 sdk_version: "none",
5704 system_modules: "none",
5705 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005706
5707 prebuilt_apis {
5708 name: "sdk",
5709 api_dirs: ["100"],
5710 }
Paul Duffin9b879592020-05-26 13:21:35 +01005711 `, withFiles(filesForSdkLibrary))
5712
5713 // java_sdk_library installs both impl jar and permission XML
5714 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5715 "javalib/bar.jar",
5716 "javalib/foo.jar",
5717 "etc/permissions/foo.xml",
5718 })
5719
5720 // The bar library should depend on the implementation jar.
5721 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5722 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5723 t.Errorf("expected %q, found %#q", expected, actual)
5724 }
5725}
5726
5727func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005728 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005729 apex {
5730 name: "myapex",
5731 key: "myapex.key",
5732 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005733 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005734 }
5735
5736 apex_key {
5737 name: "myapex.key",
5738 public_key: "testkey.avbpubkey",
5739 private_key: "testkey.pem",
5740 }
5741
5742 java_sdk_library {
5743 name: "foo",
5744 srcs: ["a.java"],
5745 api_packages: ["foo"],
5746 apex_available: ["myapex"],
5747 sdk_version: "none",
5748 system_modules: "none",
5749 }
5750
5751 java_library {
5752 name: "bar",
5753 srcs: ["a.java"],
5754 libs: ["foo"],
5755 sdk_version: "none",
5756 system_modules: "none",
5757 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005758
5759 prebuilt_apis {
5760 name: "sdk",
5761 api_dirs: ["100"],
5762 }
Paul Duffin9b879592020-05-26 13:21:35 +01005763 `, withFiles(filesForSdkLibrary))
5764
5765 // java_sdk_library installs both impl jar and permission XML
5766 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5767 "javalib/foo.jar",
5768 "etc/permissions/foo.xml",
5769 })
5770
5771 // The bar library should depend on the stubs jar.
5772 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5773 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5774 t.Errorf("expected %q, found %#q", expected, actual)
5775 }
5776}
5777
Paul Duffineedc5d52020-06-12 17:46:39 +01005778func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005779 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005780 prebuilt_apis {
5781 name: "sdk",
5782 api_dirs: ["100"],
5783 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005784 withFiles(map[string][]byte{
5785 "apex/a.java": nil,
5786 "apex/apex_manifest.json": nil,
5787 "apex/Android.bp": []byte(`
5788 package {
5789 default_visibility: ["//visibility:private"],
5790 }
5791
5792 apex {
5793 name: "myapex",
5794 key: "myapex.key",
5795 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005796 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005797 }
5798
5799 apex_key {
5800 name: "myapex.key",
5801 public_key: "testkey.avbpubkey",
5802 private_key: "testkey.pem",
5803 }
5804
5805 java_library {
5806 name: "bar",
5807 srcs: ["a.java"],
5808 libs: ["foo"],
5809 apex_available: ["myapex"],
5810 sdk_version: "none",
5811 system_modules: "none",
5812 }
5813`),
5814 "source/a.java": nil,
5815 "source/api/current.txt": nil,
5816 "source/api/removed.txt": nil,
5817 "source/Android.bp": []byte(`
5818 package {
5819 default_visibility: ["//visibility:private"],
5820 }
5821
5822 java_sdk_library {
5823 name: "foo",
5824 visibility: ["//apex"],
5825 srcs: ["a.java"],
5826 api_packages: ["foo"],
5827 apex_available: ["myapex"],
5828 sdk_version: "none",
5829 system_modules: "none",
5830 public: {
5831 enabled: true,
5832 },
5833 }
5834`),
5835 "prebuilt/a.jar": nil,
5836 "prebuilt/Android.bp": []byte(`
5837 package {
5838 default_visibility: ["//visibility:private"],
5839 }
5840
5841 java_sdk_library_import {
5842 name: "foo",
5843 visibility: ["//apex", "//source"],
5844 apex_available: ["myapex"],
5845 prefer: true,
5846 public: {
5847 jars: ["a.jar"],
5848 },
5849 }
5850`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005851 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005852 )
5853
5854 // java_sdk_library installs both impl jar and permission XML
5855 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5856 "javalib/bar.jar",
5857 "javalib/foo.jar",
5858 "etc/permissions/foo.xml",
5859 })
5860
5861 // The bar library should depend on the implementation jar.
5862 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5863 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5864 t.Errorf("expected %q, found %#q", expected, actual)
5865 }
5866}
5867
5868func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5869 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5870 apex {
5871 name: "myapex",
5872 key: "myapex.key",
5873 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005874 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005875 }
5876
5877 apex_key {
5878 name: "myapex.key",
5879 public_key: "testkey.avbpubkey",
5880 private_key: "testkey.pem",
5881 }
5882
5883 java_sdk_library_import {
5884 name: "foo",
5885 apex_available: ["myapex"],
5886 prefer: true,
5887 public: {
5888 jars: ["a.jar"],
5889 },
5890 }
5891
5892 `, withFiles(filesForSdkLibrary))
5893}
5894
atrost6e126252020-01-27 17:01:16 +00005895func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005896 result := apexFixtureFactory.
5897 Extend(java.PrepareForTestWithPlatformCompatConfig).
5898 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005899 apex {
5900 name: "myapex",
5901 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005902 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005903 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005904 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005905 }
5906
5907 apex_key {
5908 name: "myapex.key",
5909 public_key: "testkey.avbpubkey",
5910 private_key: "testkey.pem",
5911 }
5912
5913 platform_compat_config {
5914 name: "myjar-platform-compat-config",
5915 src: ":myjar",
5916 }
5917
5918 java_library {
5919 name: "myjar",
5920 srcs: ["foo/bar/MyClass.java"],
5921 sdk_version: "none",
5922 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005923 apex_available: [ "myapex" ],
5924 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005925
5926 // Make sure that a preferred prebuilt does not affect the apex contents.
5927 prebuilt_platform_compat_config {
5928 name: "myjar-platform-compat-config",
5929 metadata: "compat-config/metadata.xml",
5930 prefer: true,
5931 }
atrost6e126252020-01-27 17:01:16 +00005932 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005933 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005934 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5935 "etc/compatconfig/myjar-platform-compat-config.xml",
5936 "javalib/myjar.jar",
5937 })
5938}
5939
Jiyong Park479321d2019-12-16 11:47:12 +09005940func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5941 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5942 apex {
5943 name: "myapex",
5944 key: "myapex.key",
5945 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005946 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005947 }
5948
5949 apex_key {
5950 name: "myapex.key",
5951 public_key: "testkey.avbpubkey",
5952 private_key: "testkey.pem",
5953 }
5954
5955 java_library {
5956 name: "myjar",
5957 srcs: ["foo/bar/MyClass.java"],
5958 sdk_version: "none",
5959 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005960 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005961 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005962 }
5963 `)
5964}
5965
Jiyong Park7afd1072019-12-30 16:56:33 +09005966func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005967 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005968 apex {
5969 name: "myapex",
5970 key: "myapex.key",
5971 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005972 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005973 }
5974
5975 apex_key {
5976 name: "myapex.key",
5977 public_key: "testkey.avbpubkey",
5978 private_key: "testkey.pem",
5979 }
5980
5981 cc_library {
5982 name: "mylib",
5983 srcs: ["mylib.cpp"],
5984 system_shared_libs: [],
5985 stl: "none",
5986 required: ["a", "b"],
5987 host_required: ["c", "d"],
5988 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005989 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005990 }
5991 `)
5992
5993 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005994 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005995 name := apexBundle.BaseModuleName()
5996 prefix := "TARGET_"
5997 var builder strings.Builder
5998 data.Custom(&builder, name, prefix, "", data)
5999 androidMk := builder.String()
6000 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6001 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6002 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6003}
6004
Jiyong Park7cd10e32020-01-14 09:22:18 +09006005func TestSymlinksFromApexToSystem(t *testing.T) {
6006 bp := `
6007 apex {
6008 name: "myapex",
6009 key: "myapex.key",
6010 native_shared_libs: ["mylib"],
6011 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006012 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006013 }
6014
Jiyong Park9d677202020-02-19 16:29:35 +09006015 apex {
6016 name: "myapex.updatable",
6017 key: "myapex.key",
6018 native_shared_libs: ["mylib"],
6019 java_libs: ["myjar"],
6020 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006021 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006022 }
6023
Jiyong Park7cd10e32020-01-14 09:22:18 +09006024 apex_key {
6025 name: "myapex.key",
6026 public_key: "testkey.avbpubkey",
6027 private_key: "testkey.pem",
6028 }
6029
6030 cc_library {
6031 name: "mylib",
6032 srcs: ["mylib.cpp"],
6033 shared_libs: ["myotherlib"],
6034 system_shared_libs: [],
6035 stl: "none",
6036 apex_available: [
6037 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006038 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006039 "//apex_available:platform",
6040 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006041 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006042 }
6043
6044 cc_library {
6045 name: "myotherlib",
6046 srcs: ["mylib.cpp"],
6047 system_shared_libs: [],
6048 stl: "none",
6049 apex_available: [
6050 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006051 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006052 "//apex_available:platform",
6053 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006054 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006055 }
6056
6057 java_library {
6058 name: "myjar",
6059 srcs: ["foo/bar/MyClass.java"],
6060 sdk_version: "none",
6061 system_modules: "none",
6062 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006063 apex_available: [
6064 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006065 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006066 "//apex_available:platform",
6067 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006068 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006069 }
6070
6071 java_library {
6072 name: "myotherjar",
6073 srcs: ["foo/bar/MyClass.java"],
6074 sdk_version: "none",
6075 system_modules: "none",
6076 apex_available: [
6077 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006078 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006079 "//apex_available:platform",
6080 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006081 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006082 }
6083 `
6084
6085 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6086 for _, f := range files {
6087 if f.path == file {
6088 if f.isLink {
6089 t.Errorf("%q is not a real file", file)
6090 }
6091 return
6092 }
6093 }
6094 t.Errorf("%q is not found", file)
6095 }
6096
6097 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6098 for _, f := range files {
6099 if f.path == file {
6100 if !f.isLink {
6101 t.Errorf("%q is not a symlink", file)
6102 }
6103 return
6104 }
6105 }
6106 t.Errorf("%q is not found", file)
6107 }
6108
Jiyong Park9d677202020-02-19 16:29:35 +09006109 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6110 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006111 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006112 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006113 ensureRealfileExists(t, files, "javalib/myjar.jar")
6114 ensureRealfileExists(t, files, "lib64/mylib.so")
6115 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6116
Jiyong Park9d677202020-02-19 16:29:35 +09006117 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6118 ensureRealfileExists(t, files, "javalib/myjar.jar")
6119 ensureRealfileExists(t, files, "lib64/mylib.so")
6120 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6121
6122 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006123 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006124 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006125 ensureRealfileExists(t, files, "javalib/myjar.jar")
6126 ensureRealfileExists(t, files, "lib64/mylib.so")
6127 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006128
6129 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6130 ensureRealfileExists(t, files, "javalib/myjar.jar")
6131 ensureRealfileExists(t, files, "lib64/mylib.so")
6132 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006133}
6134
Yo Chiange8128052020-07-23 20:09:18 +08006135func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006136 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006137 apex {
6138 name: "myapex",
6139 key: "myapex.key",
6140 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006141 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006142 }
6143
6144 apex_key {
6145 name: "myapex.key",
6146 public_key: "testkey.avbpubkey",
6147 private_key: "testkey.pem",
6148 }
6149
6150 cc_library_shared {
6151 name: "mylib",
6152 srcs: ["mylib.cpp"],
6153 shared_libs: ["myotherlib"],
6154 system_shared_libs: [],
6155 stl: "none",
6156 apex_available: [
6157 "myapex",
6158 "//apex_available:platform",
6159 ],
6160 }
6161
6162 cc_prebuilt_library_shared {
6163 name: "myotherlib",
6164 srcs: ["prebuilt.so"],
6165 system_shared_libs: [],
6166 stl: "none",
6167 apex_available: [
6168 "myapex",
6169 "//apex_available:platform",
6170 ],
6171 }
6172 `)
6173
6174 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006175 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006176 var builder strings.Builder
6177 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6178 androidMk := builder.String()
6179 // `myotherlib` is added to `myapex` as symlink
6180 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6181 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6182 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6183 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006184 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 +08006185}
6186
Jooyung Han643adc42020-02-27 13:50:06 +09006187func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006188 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006189 apex {
6190 name: "myapex",
6191 key: "myapex.key",
6192 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006193 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006194 }
6195
6196 apex_key {
6197 name: "myapex.key",
6198 public_key: "testkey.avbpubkey",
6199 private_key: "testkey.pem",
6200 }
6201
6202 cc_library {
6203 name: "mylib",
6204 srcs: ["mylib.cpp"],
6205 shared_libs: ["mylib2"],
6206 system_shared_libs: [],
6207 stl: "none",
6208 apex_available: [ "myapex" ],
6209 }
6210
6211 cc_library {
6212 name: "mylib2",
6213 srcs: ["mylib.cpp"],
6214 system_shared_libs: [],
6215 stl: "none",
6216 apex_available: [ "myapex" ],
6217 }
6218 `)
6219
6220 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6221 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6222 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6223 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6224 "lib64/mylib.so",
6225 "lib64/mylib2.so",
6226 })
6227}
6228
Jooyung Han49f67012020-04-17 13:43:10 +09006229func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006230 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006231 apex {
6232 name: "myapex",
6233 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006234 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006235 }
6236 apex_key {
6237 name: "myapex.key",
6238 public_key: "testkey.avbpubkey",
6239 private_key: "testkey.pem",
6240 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006241 `,
6242 android.FixtureModifyConfig(func(config android.Config) {
6243 delete(config.Targets, android.Android)
6244 config.AndroidCommonTarget = android.Target{}
6245 }),
6246 )
Jooyung Han49f67012020-04-17 13:43:10 +09006247
6248 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6249 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6250 }
6251}
6252
Jiyong Parkbd159612020-02-28 15:22:21 +09006253func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006254 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006255 apex {
6256 name: "myapex",
6257 key: "myapex.key",
6258 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006259 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006260 }
6261
6262 apex_key {
6263 name: "myapex.key",
6264 public_key: "testkey.avbpubkey",
6265 private_key: "testkey.pem",
6266 }
6267
6268 android_app {
6269 name: "AppFoo",
6270 srcs: ["foo/bar/MyClass.java"],
6271 sdk_version: "none",
6272 system_modules: "none",
6273 apex_available: [ "myapex" ],
6274 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006275 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006276
Colin Crosscf371cc2020-11-13 11:48:42 -08006277 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006278 content := bundleConfigRule.Args["content"]
6279
6280 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006281 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 +09006282}
6283
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006284func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006285 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006286 apex {
6287 name: "myapex",
6288 key: "myapex.key",
6289 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006290 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006291 }
6292
6293 apex_key {
6294 name: "myapex.key",
6295 public_key: "testkey.avbpubkey",
6296 private_key: "testkey.pem",
6297 }
6298
6299 android_app_set {
6300 name: "AppSet",
6301 set: "AppSet.apks",
6302 }`)
6303 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006304 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006305 content := bundleConfigRule.Args["content"]
6306 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6307 s := mod.Rule("apexRule").Args["copy_commands"]
6308 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6309 if len(copyCmds) != 3 {
6310 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6311 }
6312 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6313 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6314 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6315}
6316
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006317func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006318 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006319 bp := `
6320 apex_set {
6321 name: "myapex",
6322 filename: "foo_v2.apex",
6323 sanitized: {
6324 none: { set: "myapex.apks", },
6325 hwaddress: { set: "myapex.hwasan.apks", },
6326 },
6327 }`
6328 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006329 }),
6330 prepareForTestWithSantitizeHwaddress,
6331 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006332
6333 m := ctx.ModuleForTests("myapex", "android_common")
6334 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6335
6336 actual := extractedApex.Inputs
6337 if len(actual) != 1 {
6338 t.Errorf("expected a single input")
6339 }
6340
6341 expected := "myapex.hwasan.apks"
6342 if actual[0].String() != expected {
6343 t.Errorf("expected %s, got %s", expected, actual[0].String())
6344 }
6345}
6346
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006347func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006348 t.Helper()
6349
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006350 bp := `
6351 java_library {
6352 name: "some-updatable-apex-lib",
6353 srcs: ["a.java"],
6354 sdk_version: "current",
6355 apex_available: [
6356 "some-updatable-apex",
6357 ],
6358 }
6359
6360 java_library {
6361 name: "some-non-updatable-apex-lib",
6362 srcs: ["a.java"],
6363 apex_available: [
6364 "some-non-updatable-apex",
6365 ],
6366 }
6367
6368 java_library {
6369 name: "some-platform-lib",
6370 srcs: ["a.java"],
6371 sdk_version: "current",
6372 installable: true,
6373 }
6374
6375 java_library {
6376 name: "some-art-lib",
6377 srcs: ["a.java"],
6378 sdk_version: "current",
6379 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006380 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006381 ],
6382 hostdex: true,
6383 }
6384
6385 apex {
6386 name: "some-updatable-apex",
6387 key: "some-updatable-apex.key",
6388 java_libs: ["some-updatable-apex-lib"],
6389 updatable: true,
6390 min_sdk_version: "current",
6391 }
6392
6393 apex {
6394 name: "some-non-updatable-apex",
6395 key: "some-non-updatable-apex.key",
6396 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006397 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006398 }
6399
6400 apex_key {
6401 name: "some-updatable-apex.key",
6402 }
6403
6404 apex_key {
6405 name: "some-non-updatable-apex.key",
6406 }
6407
6408 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006409 name: "com.android.art.debug",
6410 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006411 java_libs: ["some-art-lib"],
6412 updatable: true,
6413 min_sdk_version: "current",
6414 }
6415
6416 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006417 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006418 }
6419
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006420 filegroup {
6421 name: "some-updatable-apex-file_contexts",
6422 srcs: [
6423 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6424 ],
6425 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006426
6427 filegroup {
6428 name: "some-non-updatable-apex-file_contexts",
6429 srcs: [
6430 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6431 ],
6432 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006433 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006434
6435 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6436}
6437
Paul Duffin064b70c2020-11-02 17:32:38 +00006438func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006439 t.Helper()
6440
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006441 bp += cc.GatherRequiredDepsForTest(android.Android)
6442 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006443
6444 fs := map[string][]byte{
6445 "a.java": nil,
6446 "a.jar": nil,
6447 "build/make/target/product/security": nil,
6448 "apex_manifest.json": nil,
6449 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006450 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006451 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6452 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6453 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006454 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006455 }
6456 cc.GatherRequiredFilesForTest(fs)
6457
Paul Duffin39853512021-02-26 11:09:39 +00006458 for k, v := range filesForSdkLibrary {
6459 fs[k] = v
6460 }
Colin Crossae8600b2020-10-29 17:09:13 -07006461 config := android.TestArchConfig(buildDir, nil, bp, fs)
6462
6463 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006464 ctx.RegisterModuleType("apex", BundleFactory)
6465 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006466 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006467 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006468 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006469 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006470 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006471 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006472 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006473 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006474 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6475 ctx.PreDepsMutators(RegisterPreDepsMutators)
6476 ctx.PostDepsMutators(RegisterPostDepsMutators)
6477
Colin Crossae8600b2020-10-29 17:09:13 -07006478 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006479
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006480 pathCtx := android.PathContextForTesting(config)
6481 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6482 transformDexpreoptConfig(dexpreoptConfig)
6483 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6484
Paul Duffinf38931c2021-02-05 16:58:28 +00006485 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006486 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006487 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6488 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6489
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006490 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6491 android.FailIfErrored(t, errs)
6492
6493 _, errs = ctx.PrepareBuildActions(config)
6494 if errmsg == "" {
6495 android.FailIfErrored(t, errs)
6496 } else if len(errs) > 0 {
6497 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006498 } else {
6499 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6500 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006501
6502 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006503}
6504
Jooyung Han548640b2020-04-27 12:10:30 +09006505func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6506 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6507 apex {
6508 name: "myapex",
6509 key: "myapex.key",
6510 updatable: true,
6511 }
6512
6513 apex_key {
6514 name: "myapex.key",
6515 public_key: "testkey.avbpubkey",
6516 private_key: "testkey.pem",
6517 }
6518 `)
6519}
6520
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006521func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6522 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6523 apex {
6524 name: "myapex",
6525 key: "myapex.key",
6526 }
6527
6528 apex_key {
6529 name: "myapex.key",
6530 public_key: "testkey.avbpubkey",
6531 private_key: "testkey.pem",
6532 }
6533 `)
6534}
6535
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006536func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006537 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006538 var transform func(*dexpreopt.GlobalConfig)
6539
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006540 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6541 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006542 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006543 }
6544 testNoUpdatableJarsInBootImage(t, "", transform)
6545 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006546
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006547 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006548 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 +01006549 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006550 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006551 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006552 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006553 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006554
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006555 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 -07006556 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 +01006557 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006558 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006559 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006560 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006561 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006562
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006563 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 -07006564 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006565 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006566 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006567 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006568 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006569 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006570
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 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 -07006572 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 +01006573 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006574 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006576 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006577 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006578
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006579 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6580 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006581 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006582 }
6583 testNoUpdatableJarsInBootImage(t, "", transform)
6584 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006585
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006586 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006587 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006588 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006589 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006590 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006591 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006592 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006593
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006594 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006595 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006597 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006599 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006600 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006601
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006602 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006603 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006604 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006605 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006606 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006607 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006608 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006609
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006610 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6611 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006612 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006613 }
6614 testNoUpdatableJarsInBootImage(t, "", transform)
6615 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006616
6617}
6618
6619func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6620 transform := func(config *dexpreopt.GlobalConfig) {
6621 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6622 }
6623 t.Run("prebuilt no source", func(t *testing.T) {
6624 testDexpreoptWithApexes(t, `
6625 prebuilt_apex {
6626 name: "myapex" ,
6627 arch: {
6628 arm64: {
6629 src: "myapex-arm64.apex",
6630 },
6631 arm: {
6632 src: "myapex-arm.apex",
6633 },
6634 },
6635 exported_java_libs: ["libfoo"],
6636 }
6637
6638 java_import {
6639 name: "libfoo",
6640 jars: ["libfoo.jar"],
6641 }
6642`, "", transform)
6643 })
6644
6645 t.Run("prebuilt no source", func(t *testing.T) {
6646 testDexpreoptWithApexes(t, `
6647 prebuilt_apex {
6648 name: "myapex" ,
6649 arch: {
6650 arm64: {
6651 src: "myapex-arm64.apex",
6652 },
6653 arm: {
6654 src: "myapex-arm.apex",
6655 },
6656 },
6657 exported_java_libs: ["libfoo"],
6658 }
6659
6660 java_import {
6661 name: "libfoo",
6662 jars: ["libfoo.jar"],
6663 }
6664`, "", transform)
6665 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006666}
6667
Andrei Onea115e7e72020-06-05 21:14:03 +01006668func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6669 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006670 bp += `
6671 apex_key {
6672 name: "myapex.key",
6673 public_key: "testkey.avbpubkey",
6674 private_key: "testkey.pem",
6675 }`
6676 fs := map[string][]byte{
6677 "lib1/src/A.java": nil,
6678 "lib2/src/B.java": nil,
6679 "system/sepolicy/apex/myapex-file_contexts": nil,
6680 }
6681
Colin Crossae8600b2020-10-29 17:09:13 -07006682 config := android.TestArchConfig(buildDir, nil, bp, fs)
6683 android.SetTestNeverallowRules(config, rules)
6684 updatableBootJars := make([]string, 0, len(apexBootJars))
6685 for _, apexBootJar := range apexBootJars {
6686 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6687 }
6688 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6689
6690 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006691 ctx.RegisterModuleType("apex", BundleFactory)
6692 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6693 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6694 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006695 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006696 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6697 ctx.PreDepsMutators(RegisterPreDepsMutators)
6698 ctx.PostDepsMutators(RegisterPostDepsMutators)
6699 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6700
Colin Crossae8600b2020-10-29 17:09:13 -07006701 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006702
6703 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6704 android.FailIfErrored(t, errs)
6705
6706 _, errs = ctx.PrepareBuildActions(config)
6707 if errmsg == "" {
6708 android.FailIfErrored(t, errs)
6709 } else if len(errs) > 0 {
6710 android.FailIfNoMatchingErrors(t, errmsg, errs)
6711 return
6712 } else {
6713 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6714 }
6715}
6716
6717func TestApexPermittedPackagesRules(t *testing.T) {
6718 testcases := []struct {
6719 name string
6720 expectedError string
6721 bp string
6722 bootJars []string
6723 modulesPackages map[string][]string
6724 }{
6725
6726 {
6727 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6728 expectedError: "",
6729 bp: `
6730 java_library {
6731 name: "bcp_lib1",
6732 srcs: ["lib1/src/*.java"],
6733 permitted_packages: ["foo.bar"],
6734 apex_available: ["myapex"],
6735 sdk_version: "none",
6736 system_modules: "none",
6737 }
6738 java_library {
6739 name: "nonbcp_lib2",
6740 srcs: ["lib2/src/*.java"],
6741 apex_available: ["myapex"],
6742 permitted_packages: ["a.b"],
6743 sdk_version: "none",
6744 system_modules: "none",
6745 }
6746 apex {
6747 name: "myapex",
6748 key: "myapex.key",
6749 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006750 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006751 }`,
6752 bootJars: []string{"bcp_lib1"},
6753 modulesPackages: map[string][]string{
6754 "myapex": []string{
6755 "foo.bar",
6756 },
6757 },
6758 },
6759 {
6760 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6761 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.`,
6762 bp: `
6763 java_library {
6764 name: "bcp_lib1",
6765 srcs: ["lib1/src/*.java"],
6766 apex_available: ["myapex"],
6767 permitted_packages: ["foo.bar"],
6768 sdk_version: "none",
6769 system_modules: "none",
6770 }
6771 java_library {
6772 name: "bcp_lib2",
6773 srcs: ["lib2/src/*.java"],
6774 apex_available: ["myapex"],
6775 permitted_packages: ["foo.bar", "bar.baz"],
6776 sdk_version: "none",
6777 system_modules: "none",
6778 }
6779 apex {
6780 name: "myapex",
6781 key: "myapex.key",
6782 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006783 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006784 }
6785 `,
6786 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6787 modulesPackages: map[string][]string{
6788 "myapex": []string{
6789 "foo.bar",
6790 },
6791 },
6792 },
6793 }
6794 for _, tc := range testcases {
6795 t.Run(tc.name, func(t *testing.T) {
6796 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6797 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6798 })
6799 }
6800}
6801
Jiyong Park62304bb2020-04-13 16:19:48 +09006802func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006803 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006804 apex {
6805 name: "myapex",
6806 key: "myapex.key",
6807 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006808 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006809 }
6810
6811 apex_key {
6812 name: "myapex.key",
6813 public_key: "testkey.avbpubkey",
6814 private_key: "testkey.pem",
6815 }
6816
6817 cc_library {
6818 name: "mylib",
6819 srcs: ["mylib.cpp"],
6820 system_shared_libs: [],
6821 stl: "none",
6822 stubs: {
6823 versions: ["1"],
6824 },
6825 apex_available: ["myapex"],
6826 }
6827
6828 cc_library {
6829 name: "myprivlib",
6830 srcs: ["mylib.cpp"],
6831 system_shared_libs: [],
6832 stl: "none",
6833 apex_available: ["myapex"],
6834 }
6835
6836
6837 cc_test {
6838 name: "mytest",
6839 gtest: false,
6840 srcs: ["mylib.cpp"],
6841 system_shared_libs: [],
6842 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006843 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006844 test_for: ["myapex"]
6845 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006846
6847 cc_library {
6848 name: "mytestlib",
6849 srcs: ["mylib.cpp"],
6850 system_shared_libs: [],
6851 shared_libs: ["mylib", "myprivlib"],
6852 stl: "none",
6853 test_for: ["myapex"],
6854 }
6855
6856 cc_benchmark {
6857 name: "mybench",
6858 srcs: ["mylib.cpp"],
6859 system_shared_libs: [],
6860 shared_libs: ["mylib", "myprivlib"],
6861 stl: "none",
6862 test_for: ["myapex"],
6863 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006864 `)
6865
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006866 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
6867 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").RelativeToTop().Args["libFlags"], " ")
6868 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6869 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6870 }
6871
6872 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006873 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006874 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6875 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6876 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6877}
Jiyong Park46a512f2020-12-04 18:02:13 +09006878
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006879func TestIndirectTestFor(t *testing.T) {
6880 ctx := testApex(t, `
6881 apex {
6882 name: "myapex",
6883 key: "myapex.key",
6884 native_shared_libs: ["mylib", "myprivlib"],
6885 updatable: false,
6886 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006887
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006888 apex_key {
6889 name: "myapex.key",
6890 public_key: "testkey.avbpubkey",
6891 private_key: "testkey.pem",
6892 }
6893
6894 cc_library {
6895 name: "mylib",
6896 srcs: ["mylib.cpp"],
6897 system_shared_libs: [],
6898 stl: "none",
6899 stubs: {
6900 versions: ["1"],
6901 },
6902 apex_available: ["myapex"],
6903 }
6904
6905 cc_library {
6906 name: "myprivlib",
6907 srcs: ["mylib.cpp"],
6908 system_shared_libs: [],
6909 stl: "none",
6910 shared_libs: ["mylib"],
6911 apex_available: ["myapex"],
6912 }
6913
6914 cc_library {
6915 name: "mytestlib",
6916 srcs: ["mylib.cpp"],
6917 system_shared_libs: [],
6918 shared_libs: ["myprivlib"],
6919 stl: "none",
6920 test_for: ["myapex"],
6921 }
6922 `)
6923
6924 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
6925 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").RelativeToTop().Args["libFlags"], " ")
6926 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6927 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6928 }
6929
6930 // The platform variant of mytestlib links to the platform variant of the
6931 // internal myprivlib.
6932 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6933
6934 // The platform variant of myprivlib links to the platform variant of mylib
6935 // and bypasses its stubs.
6936 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006937}
6938
Martin Stjernholm4d058c82021-03-27 15:18:31 +00006939func TestTestForForLibInOtherApex(t *testing.T) {
6940 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
6941 _ = testApex(t, `
6942 apex {
6943 name: "com.android.art",
6944 key: "myapex.key",
6945 native_shared_libs: ["mylib"],
6946 updatable: false,
6947 }
6948
6949 apex {
6950 name: "com.android.art.debug",
6951 key: "myapex.key",
6952 native_shared_libs: ["mylib", "mytestlib"],
6953 updatable: false,
6954 }
6955
6956 apex_key {
6957 name: "myapex.key",
6958 public_key: "testkey.avbpubkey",
6959 private_key: "testkey.pem",
6960 }
6961
6962 cc_library {
6963 name: "mylib",
6964 srcs: ["mylib.cpp"],
6965 system_shared_libs: [],
6966 stl: "none",
6967 stubs: {
6968 versions: ["1"],
6969 },
6970 apex_available: ["com.android.art", "com.android.art.debug"],
6971 }
6972
6973 cc_library {
6974 name: "mytestlib",
6975 srcs: ["mylib.cpp"],
6976 system_shared_libs: [],
6977 shared_libs: ["mylib"],
6978 stl: "none",
6979 apex_available: ["com.android.art.debug"],
6980 test_for: ["com.android.art"],
6981 }
6982 `,
6983 android.MockFS{
6984 "system/sepolicy/apex/com.android.art-file_contexts": nil,
6985 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
6986 }.AddToFixture())
6987}
6988
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006989// TODO(jungjw): Move this to proptools
6990func intPtr(i int) *int {
6991 return &i
6992}
6993
6994func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006995 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006996 apex_set {
6997 name: "myapex",
6998 set: "myapex.apks",
6999 filename: "foo_v2.apex",
7000 overrides: ["foo"],
7001 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007002 `,
7003 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7004 variables.Platform_sdk_version = intPtr(30)
7005 }),
7006 android.FixtureModifyConfig(func(config android.Config) {
7007 config.Targets[android.Android] = []android.Target{
7008 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7009 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7010 }
7011 }),
7012 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007013
7014 m := ctx.ModuleForTests("myapex", "android_common")
7015
7016 // Check extract_apks tool parameters.
7017 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
7018 actual := extractedApex.Args["abis"]
7019 expected := "ARMEABI_V7A,ARM64_V8A"
7020 if actual != expected {
7021 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7022 }
7023 actual = extractedApex.Args["sdk-version"]
7024 expected = "30"
7025 if actual != expected {
7026 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7027 }
7028
7029 a := m.Module().(*ApexSet)
7030 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007031 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007032 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7033 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7034 }
7035}
7036
Jiyong Park7d95a512020-05-10 15:16:24 +09007037func TestNoStaticLinkingToStubsLib(t *testing.T) {
7038 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7039 apex {
7040 name: "myapex",
7041 key: "myapex.key",
7042 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007043 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007044 }
7045
7046 apex_key {
7047 name: "myapex.key",
7048 public_key: "testkey.avbpubkey",
7049 private_key: "testkey.pem",
7050 }
7051
7052 cc_library {
7053 name: "mylib",
7054 srcs: ["mylib.cpp"],
7055 static_libs: ["otherlib"],
7056 system_shared_libs: [],
7057 stl: "none",
7058 apex_available: [ "myapex" ],
7059 }
7060
7061 cc_library {
7062 name: "otherlib",
7063 srcs: ["mylib.cpp"],
7064 system_shared_libs: [],
7065 stl: "none",
7066 stubs: {
7067 versions: ["1", "2", "3"],
7068 },
7069 apex_available: [ "myapex" ],
7070 }
7071 `)
7072}
7073
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007074func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007075 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007076 apex {
7077 name: "myapex",
7078 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007079 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007080 }
7081
7082 apex_key {
7083 name: "myapex.key",
7084 public_key: "testkey.avbpubkey",
7085 private_key: "testkey.pem",
7086 }
7087
7088 prebuilt_apex {
7089 name: "myapex",
7090 prefer: true,
7091 arch: {
7092 arm64: {
7093 src: "myapex-arm64.apex",
7094 },
7095 arm: {
7096 src: "myapex-arm.apex",
7097 },
7098 },
7099 }
7100
7101 apex_set {
7102 name: "myapex_set",
7103 set: "myapex.apks",
7104 filename: "myapex_set.apex",
7105 overrides: ["myapex"],
7106 }
7107 `)
7108
7109 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7110 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7111 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 +09007112 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 +09007113}
7114
Jooyung Han938b5932020-06-20 12:47:47 +09007115func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007116 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007117 apex {
7118 name: "myapex",
7119 key: "myapex.key",
7120 apps: ["app"],
7121 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007122 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007123 }
7124
7125 apex_key {
7126 name: "myapex.key",
7127 public_key: "testkey.avbpubkey",
7128 private_key: "testkey.pem",
7129 }
7130
7131 android_app {
7132 name: "app",
7133 srcs: ["foo/bar/MyClass.java"],
7134 package_name: "foo",
7135 sdk_version: "none",
7136 system_modules: "none",
7137 apex_available: [ "myapex" ],
7138 }
7139 `, withFiles(map[string][]byte{
7140 "sub/Android.bp": []byte(`
7141 override_apex {
7142 name: "override_myapex",
7143 base: "myapex",
7144 apps: ["override_app"],
7145 allowed_files: ":allowed",
7146 }
7147 // Overridable "path" property should be referenced indirectly
7148 filegroup {
7149 name: "allowed",
7150 srcs: ["allowed.txt"],
7151 }
7152 override_android_app {
7153 name: "override_app",
7154 base: "app",
7155 package_name: "bar",
7156 }
7157 `),
7158 }))
7159
7160 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7161 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7162 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7163 }
7164
7165 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7166 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7167 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7168 }
7169}
7170
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007171func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007172 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007173 apex {
7174 name: "myapex",
7175 key: "myapex.key",
7176 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007177 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007178 }
7179
7180 apex_key {
7181 name: "myapex.key",
7182 public_key: "testkey.avbpubkey",
7183 private_key: "testkey.pem",
7184 }
7185
7186 cc_library {
7187 name: "mylib",
7188 srcs: ["mylib.cpp"],
7189 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007190 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007191 },
7192 apex_available: ["myapex"],
7193 }
7194
7195 cc_prebuilt_library_shared {
7196 name: "mylib",
7197 prefer: false,
7198 srcs: ["prebuilt.so"],
7199 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007200 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007201 },
7202 apex_available: ["myapex"],
7203 }
7204 `)
7205}
7206
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007207func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007208 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007209 apex {
7210 name: "myapex",
7211 key: "myapex.key",
7212 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007213 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007214 }
7215 apex_key {
7216 name: "myapex.key",
7217 public_key: "testkey.avbpubkey",
7218 private_key: "testkey.pem",
7219 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007220 `,
7221 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7222 variables.CompressedApex = proptools.BoolPtr(true)
7223 }),
7224 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007225
7226 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7227 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7228
7229 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7230 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7231
7232 // Make sure output of bundle is .capex
7233 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7234 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7235
7236 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007237 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007238 var builder strings.Builder
7239 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7240 androidMk := builder.String()
7241 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7242}
7243
Martin Stjernholm2856c662020-12-02 15:03:42 +00007244func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007245 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007246 apex {
7247 name: "myapex",
7248 key: "myapex.key",
7249 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007250 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007251 }
7252
7253 apex_key {
7254 name: "myapex.key",
7255 public_key: "testkey.avbpubkey",
7256 private_key: "testkey.pem",
7257 }
7258
7259 cc_library {
7260 name: "mylib",
7261 srcs: ["mylib.cpp"],
7262 apex_available: ["myapex"],
7263 shared_libs: ["otherlib"],
7264 system_shared_libs: [],
7265 }
7266
7267 cc_library {
7268 name: "otherlib",
7269 srcs: ["mylib.cpp"],
7270 stubs: {
7271 versions: ["current"],
7272 },
7273 }
7274
7275 cc_prebuilt_library_shared {
7276 name: "otherlib",
7277 prefer: true,
7278 srcs: ["prebuilt.so"],
7279 stubs: {
7280 versions: ["current"],
7281 },
7282 }
7283 `)
7284
7285 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007286 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007287 var builder strings.Builder
7288 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7289 androidMk := builder.String()
7290
7291 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7292 // a thing there.
7293 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7294}
7295
Jiyong Parke3867542020-12-03 17:28:25 +09007296func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007297 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007298 apex {
7299 name: "myapex",
7300 key: "myapex.key",
7301 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007302 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007303 }
7304
7305 apex_key {
7306 name: "myapex.key",
7307 public_key: "testkey.avbpubkey",
7308 private_key: "testkey.pem",
7309 }
7310
7311 cc_library {
7312 name: "mylib",
7313 srcs: ["mylib.cpp"],
7314 system_shared_libs: [],
7315 stl: "none",
7316 apex_available: ["myapex"],
7317 shared_libs: ["mylib2"],
7318 target: {
7319 apex: {
7320 exclude_shared_libs: ["mylib2"],
7321 },
7322 },
7323 }
7324
7325 cc_library {
7326 name: "mylib2",
7327 srcs: ["mylib.cpp"],
7328 system_shared_libs: [],
7329 stl: "none",
7330 }
7331 `)
7332
7333 // Check if mylib is linked to mylib2 for the non-apex target
7334 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7335 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7336
7337 // Make sure that the link doesn't occur for the apex target
7338 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7339 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7340
7341 // It shouldn't appear in the copy cmd as well.
7342 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7343 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7344}
7345
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007346func TestPrebuiltStubLibDep(t *testing.T) {
7347 bpBase := `
7348 apex {
7349 name: "myapex",
7350 key: "myapex.key",
7351 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007352 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007353 }
7354 apex_key {
7355 name: "myapex.key",
7356 public_key: "testkey.avbpubkey",
7357 private_key: "testkey.pem",
7358 }
7359 cc_library {
7360 name: "mylib",
7361 srcs: ["mylib.cpp"],
7362 apex_available: ["myapex"],
7363 shared_libs: ["stublib"],
7364 system_shared_libs: [],
7365 }
7366 apex {
7367 name: "otherapex",
7368 enabled: %s,
7369 key: "myapex.key",
7370 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007371 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007372 }
7373 `
7374
7375 stublibSourceBp := `
7376 cc_library {
7377 name: "stublib",
7378 srcs: ["mylib.cpp"],
7379 apex_available: ["otherapex"],
7380 system_shared_libs: [],
7381 stl: "none",
7382 stubs: {
7383 versions: ["1"],
7384 },
7385 }
7386 `
7387
7388 stublibPrebuiltBp := `
7389 cc_prebuilt_library_shared {
7390 name: "stublib",
7391 srcs: ["prebuilt.so"],
7392 apex_available: ["otherapex"],
7393 stubs: {
7394 versions: ["1"],
7395 },
7396 %s
7397 }
7398 `
7399
7400 tests := []struct {
7401 name string
7402 stublibBp string
7403 usePrebuilt bool
7404 modNames []string // Modules to collect AndroidMkEntries for
7405 otherApexEnabled []string
7406 }{
7407 {
7408 name: "only_source",
7409 stublibBp: stublibSourceBp,
7410 usePrebuilt: false,
7411 modNames: []string{"stublib"},
7412 otherApexEnabled: []string{"true", "false"},
7413 },
7414 {
7415 name: "source_preferred",
7416 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7417 usePrebuilt: false,
7418 modNames: []string{"stublib", "prebuilt_stublib"},
7419 otherApexEnabled: []string{"true", "false"},
7420 },
7421 {
7422 name: "prebuilt_preferred",
7423 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7424 usePrebuilt: true,
7425 modNames: []string{"stublib", "prebuilt_stublib"},
7426 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7427 },
7428 {
7429 name: "only_prebuilt",
7430 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7431 usePrebuilt: true,
7432 modNames: []string{"stublib"},
7433 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7434 },
7435 }
7436
7437 for _, test := range tests {
7438 t.Run(test.name, func(t *testing.T) {
7439 for _, otherApexEnabled := range test.otherApexEnabled {
7440 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007441 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007442
7443 type modAndMkEntries struct {
7444 mod *cc.Module
7445 mkEntries android.AndroidMkEntries
7446 }
7447 entries := []*modAndMkEntries{}
7448
7449 // Gather shared lib modules that are installable
7450 for _, modName := range test.modNames {
7451 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7452 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7453 continue
7454 }
7455 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007456 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007457 continue
7458 }
Colin Crossaa255532020-07-03 13:18:24 -07007459 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007460 if ent.Disabled {
7461 continue
7462 }
7463 entries = append(entries, &modAndMkEntries{
7464 mod: mod,
7465 mkEntries: ent,
7466 })
7467 }
7468 }
7469 }
7470
7471 var entry *modAndMkEntries = nil
7472 for _, ent := range entries {
7473 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7474 if entry != nil {
7475 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7476 } else {
7477 entry = ent
7478 }
7479 }
7480 }
7481
7482 if entry == nil {
7483 t.Errorf("AndroidMk entry for \"stublib\" missing")
7484 } else {
7485 isPrebuilt := entry.mod.Prebuilt() != nil
7486 if isPrebuilt != test.usePrebuilt {
7487 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7488 }
7489 if !entry.mod.IsStubs() {
7490 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7491 }
7492 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7493 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7494 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007495 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7496 expected := "-D__STUBLIB_API__=1"
7497 if !android.InList(expected, cflags) {
7498 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7499 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007500 }
7501 })
7502 }
7503 })
7504 }
7505}
7506
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007507func TestMain(m *testing.M) {
7508 run := func() int {
7509 setUp()
7510 defer tearDown()
7511
7512 return m.Run()
7513 }
7514
7515 os.Exit(run())
7516}