blob: b15966085babe45d73877fc0e24e2a88268d15ed [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 `),
143 android.FixtureMergeMockFs(android.MockFS{
144 "a.java": nil,
145 "PrebuiltAppFoo.apk": nil,
146 "PrebuiltAppFooPriv.apk": nil,
147 "build/make/target/product/security": nil,
148 "apex_manifest.json": nil,
149 "AndroidManifest.xml": nil,
150 "system/sepolicy/apex/myapex-file_contexts": nil,
151 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
152 "system/sepolicy/apex/myapex2-file_contexts": nil,
153 "system/sepolicy/apex/otherapex-file_contexts": nil,
154 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
155 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
156 "mylib.cpp": nil,
157 "mytest.cpp": nil,
158 "mytest1.cpp": nil,
159 "mytest2.cpp": nil,
160 "mytest3.cpp": nil,
161 "myprebuilt": nil,
162 "my_include": nil,
163 "foo/bar/MyClass.java": nil,
164 "prebuilt.jar": nil,
165 "prebuilt.so": nil,
166 "vendor/foo/devkeys/test.x509.pem": nil,
167 "vendor/foo/devkeys/test.pk8": nil,
168 "testkey.x509.pem": nil,
169 "testkey.pk8": nil,
170 "testkey.override.x509.pem": nil,
171 "testkey.override.pk8": nil,
172 "vendor/foo/devkeys/testkey.avbpubkey": nil,
173 "vendor/foo/devkeys/testkey.pem": nil,
174 "NOTICE": nil,
175 "custom_notice": nil,
176 "custom_notice_for_static_lib": nil,
177 "testkey2.avbpubkey": nil,
178 "testkey2.pem": nil,
179 "myapex-arm64.apex": nil,
180 "myapex-arm.apex": nil,
181 "myapex.apks": nil,
182 "frameworks/base/api/current.txt": nil,
183 "framework/aidl/a.aidl": nil,
184 "build/make/core/proguard.flags": nil,
185 "build/make/core/proguard_basic_keeps.flags": nil,
186 "dummy.txt": nil,
187 "baz": nil,
188 "bar/baz": nil,
189 "testdata/baz": nil,
190 "AppSet.apks": nil,
191 "foo.rs": nil,
192 "libfoo.jar": nil,
193 "libbar.jar": nil,
194 },
195 ),
196
197 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
198 variables.DeviceVndkVersion = proptools.StringPtr("current")
199 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
200 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
201 variables.Platform_sdk_codename = proptools.StringPtr("Q")
202 variables.Platform_sdk_final = proptools.BoolPtr(false)
203 variables.Platform_version_active_codenames = []string{"Q"}
204 variables.Platform_vndk_version = proptools.StringPtr("VER")
205 }),
206)
207
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700208func setUp() {
209 var err error
210 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900211 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700212 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214}
215
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700216func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700217 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900218}
219
Jooyung Han643adc42020-02-27 13:50:06 +0900220// ensure that 'result' equals 'expected'
221func ensureEquals(t *testing.T, result string, expected string) {
222 t.Helper()
223 if result != expected {
224 t.Errorf("%q != %q", expected, result)
225 }
226}
227
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228// ensure that 'result' contains 'expected'
229func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900230 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231 if !strings.Contains(result, expected) {
232 t.Errorf("%q is not found in %q", expected, result)
233 }
234}
235
Liz Kammer5bd365f2020-05-27 15:15:11 -0700236// ensure that 'result' contains 'expected' exactly one time
237func ensureContainsOnce(t *testing.T, result string, expected string) {
238 t.Helper()
239 count := strings.Count(result, expected)
240 if count != 1 {
241 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
242 }
243}
244
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245// ensures that 'result' does not contain 'notExpected'
246func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900247 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 if strings.Contains(result, notExpected) {
249 t.Errorf("%q is found in %q", notExpected, result)
250 }
251}
252
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700253func ensureMatches(t *testing.T, result string, expectedRex string) {
254 ok, err := regexp.MatchString(expectedRex, result)
255 if err != nil {
256 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
257 return
258 }
259 if !ok {
260 t.Errorf("%s does not match regular expession %s", result, expectedRex)
261 }
262}
263
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900265 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266 if !android.InList(expected, result) {
267 t.Errorf("%q is not found in %v", expected, result)
268 }
269}
270
271func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if android.InList(notExpected, result) {
274 t.Errorf("%q is found in %v", notExpected, result)
275 }
276}
277
Jooyung Hane1633032019-08-01 17:41:43 +0900278func ensureListEmpty(t *testing.T, result []string) {
279 t.Helper()
280 if len(result) > 0 {
281 t.Errorf("%q is expected to be empty", result)
282 }
283}
284
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000285func ensureListNotEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) == 0 {
288 t.Errorf("%q is expected to be not empty", result)
289 }
290}
291
Jiyong Park25fc6a92018-11-18 18:02:45 +0900292// Minimal test
293func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800294 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900295 apex_defaults {
296 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900297 manifest: ":myapex.manifest",
298 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900300 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900301 native_shared_libs: [
302 "mylib",
303 "libfoo.ffi",
304 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900305 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800306 multilib: {
307 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900308 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800309 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900310 },
Jiyong Park77acec62020-06-01 21:39:15 +0900311 java_libs: [
312 "myjar",
313 "myjar_dex",
314 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000315 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316 }
317
Jiyong Park30ca9372019-02-07 16:27:23 +0900318 apex {
319 name: "myapex",
320 defaults: ["myapex-defaults"],
321 }
322
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 apex_key {
324 name: "myapex.key",
325 public_key: "testkey.avbpubkey",
326 private_key: "testkey.pem",
327 }
328
Jiyong Park809bb722019-02-13 21:33:49 +0900329 filegroup {
330 name: "myapex.manifest",
331 srcs: ["apex_manifest.json"],
332 }
333
334 filegroup {
335 name: "myapex.androidmanifest",
336 srcs: ["AndroidManifest.xml"],
337 }
338
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 cc_library {
340 name: "mylib",
341 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900342 shared_libs: [
343 "mylib2",
344 "libbar.ffi",
345 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 system_shared_libs: [],
347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000348 // TODO: remove //apex_available:platform
349 apex_available: [
350 "//apex_available:platform",
351 "myapex",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 }
354
Alex Light3d673592019-01-18 14:37:31 -0800355 cc_binary {
356 name: "foo",
357 srcs: ["mylib.cpp"],
358 compile_multilib: "both",
359 multilib: {
360 lib32: {
361 suffix: "32",
362 },
363 lib64: {
364 suffix: "64",
365 },
366 },
367 symlinks: ["foo_link_"],
368 symlink_preferred_arch: true,
369 system_shared_libs: [],
370 static_executable: true,
371 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700372 apex_available: [ "myapex", "com.android.gki.*" ],
373 }
374
Jiyong Park99644e92020-11-17 22:21:02 +0900375 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000376 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900377 srcs: ["foo.rs"],
378 rlibs: ["libfoo.rlib.rust"],
379 dylibs: ["libfoo.dylib.rust"],
380 apex_available: ["myapex"],
381 }
382
383 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000384 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900385 srcs: ["foo.rs"],
386 crate_name: "foo",
387 apex_available: ["myapex"],
388 }
389
390 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 crate_name: "foo",
394 apex_available: ["myapex"],
395 }
396
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900397 rust_ffi_shared {
398 name: "libfoo.ffi",
399 srcs: ["foo.rs"],
400 crate_name: "foo",
401 apex_available: ["myapex"],
402 }
403
404 rust_ffi_shared {
405 name: "libbar.ffi",
406 srcs: ["foo.rs"],
407 crate_name: "bar",
408 apex_available: ["myapex"],
409 }
410
Yifan Hongd22a84a2020-07-28 17:37:46 -0700411 apex {
412 name: "com.android.gki.fake",
413 binaries: ["foo"],
414 key: "myapex.key",
415 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000416 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800417 }
418
Paul Duffindddd5462020-04-07 15:25:44 +0100419 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900420 name: "mylib2",
421 srcs: ["mylib.cpp"],
422 system_shared_libs: [],
423 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900424 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900425 static_libs: ["libstatic"],
426 // TODO: remove //apex_available:platform
427 apex_available: [
428 "//apex_available:platform",
429 "myapex",
430 ],
431 }
432
Paul Duffindddd5462020-04-07 15:25:44 +0100433 cc_prebuilt_library_shared {
434 name: "mylib2",
435 srcs: ["prebuilt.so"],
436 // TODO: remove //apex_available:platform
437 apex_available: [
438 "//apex_available:platform",
439 "myapex",
440 ],
441 }
442
Jiyong Park9918e1a2020-03-17 19:16:40 +0900443 cc_library_static {
444 name: "libstatic",
445 srcs: ["mylib.cpp"],
446 system_shared_libs: [],
447 stl: "none",
448 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000449 // TODO: remove //apex_available:platform
450 apex_available: [
451 "//apex_available:platform",
452 "myapex",
453 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900454 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900455
456 java_library {
457 name: "myjar",
458 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900459 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900460 sdk_version: "none",
461 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900462 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900463 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000464 // TODO: remove //apex_available:platform
465 apex_available: [
466 "//apex_available:platform",
467 "myapex",
468 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900469 }
470
Jiyong Park77acec62020-06-01 21:39:15 +0900471 dex_import {
472 name: "myjar_dex",
473 jars: ["prebuilt.jar"],
474 apex_available: [
475 "//apex_available:platform",
476 "myapex",
477 ],
478 }
479
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 java_library {
481 name: "myotherjar",
482 srcs: ["foo/bar/MyClass.java"],
483 sdk_version: "none",
484 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900485 // TODO: remove //apex_available:platform
486 apex_available: [
487 "//apex_available:platform",
488 "myapex",
489 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900491
492 java_library {
493 name: "mysharedjar",
494 srcs: ["foo/bar/MyClass.java"],
495 sdk_version: "none",
496 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900497 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900498 `)
499
Sundong Ahnabb64432019-10-22 13:58:29 +0900500 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900501
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900502 // Make sure that Android.mk is created
503 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700504 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900505 var builder strings.Builder
506 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
507
508 androidMk := builder.String()
509 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
510 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
511
Jiyong Park42cca6c2019-04-01 11:15:50 +0900512 optFlags := apexRule.Args["opt_flags"]
513 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700514 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900515 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900516
Jiyong Park25fc6a92018-11-18 18:02:45 +0900517 copyCmds := apexRule.Args["copy_commands"]
518
519 // Ensure that main rule creates an output
520 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
521
522 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700523 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
524 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
525 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900526 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900527 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900528
529 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700530 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
531 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900532 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
533 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900534 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900535
536 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800537 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
538 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900539 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900540 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900541 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900542 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
543 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900544 // .. but not for java libs
545 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900546 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800547
Colin Cross7113d202019-11-20 16:39:12 -0800548 // Ensure that the platform variant ends with _shared or _common
549 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
552 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900553 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
554
555 // Ensure that dynamic dependency to java libs are not included
556 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800557
558 // Ensure that all symlinks are present.
559 found_foo_link_64 := false
560 found_foo := false
561 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900562 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800563 if strings.HasSuffix(cmd, "bin/foo") {
564 found_foo = true
565 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
566 found_foo_link_64 = true
567 }
568 }
569 }
570 good := found_foo && found_foo_link_64
571 if !good {
572 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
573 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900574
Sundong Ahnabb64432019-10-22 13:58:29 +0900575 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700576 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900577 if len(noticeInputs) != 3 {
578 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900579 }
580 ensureListContains(t, noticeInputs, "NOTICE")
581 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900582 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900583
Artur Satayeva8bd1132020-04-27 18:07:06 +0100584 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100585 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100586 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
587 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
588 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100589
590 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100591 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100592 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
593 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
594 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800595}
596
Jooyung Hanf21c7972019-12-16 22:32:06 +0900597func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800598 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900599 apex_defaults {
600 name: "myapex-defaults",
601 key: "myapex.key",
602 prebuilts: ["myetc"],
603 native_shared_libs: ["mylib"],
604 java_libs: ["myjar"],
605 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900606 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800607 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000608 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900609 }
610
611 prebuilt_etc {
612 name: "myetc",
613 src: "myprebuilt",
614 }
615
616 apex {
617 name: "myapex",
618 defaults: ["myapex-defaults"],
619 }
620
621 apex_key {
622 name: "myapex.key",
623 public_key: "testkey.avbpubkey",
624 private_key: "testkey.pem",
625 }
626
627 cc_library {
628 name: "mylib",
629 system_shared_libs: [],
630 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000631 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900632 }
633
634 java_library {
635 name: "myjar",
636 srcs: ["foo/bar/MyClass.java"],
637 sdk_version: "none",
638 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000639 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900640 }
641
642 android_app {
643 name: "AppFoo",
644 srcs: ["foo/bar/MyClass.java"],
645 sdk_version: "none",
646 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000647 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900648 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900649
650 runtime_resource_overlay {
651 name: "rro",
652 theme: "blue",
653 }
654
markchien2f59ec92020-09-02 16:23:38 +0800655 bpf {
656 name: "bpf",
657 srcs: ["bpf.c", "bpf2.c"],
658 }
659
Jooyung Hanf21c7972019-12-16 22:32:06 +0900660 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000661 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900662 "etc/myetc",
663 "javalib/myjar.jar",
664 "lib64/mylib.so",
665 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900666 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800667 "etc/bpf/bpf.o",
668 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900669 })
670}
671
Jooyung Han01a3ee22019-11-02 02:52:25 +0900672func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800673 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900674 apex {
675 name: "myapex",
676 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000677 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900678 }
679
680 apex_key {
681 name: "myapex.key",
682 public_key: "testkey.avbpubkey",
683 private_key: "testkey.pem",
684 }
685 `)
686
687 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900688 args := module.Rule("apexRule").Args
689 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
690 t.Error("manifest should be apex_manifest.pb, but " + manifest)
691 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692}
693
Alex Light5098a612018-11-29 17:12:15 -0800694func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800695 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800696 apex {
697 name: "myapex",
698 key: "myapex.key",
699 payload_type: "zip",
700 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000701 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800702 }
703
704 apex_key {
705 name: "myapex.key",
706 public_key: "testkey.avbpubkey",
707 private_key: "testkey.pem",
708 }
709
710 cc_library {
711 name: "mylib",
712 srcs: ["mylib.cpp"],
713 shared_libs: ["mylib2"],
714 system_shared_libs: [],
715 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000716 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800717 }
718
719 cc_library {
720 name: "mylib2",
721 srcs: ["mylib.cpp"],
722 system_shared_libs: [],
723 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000724 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800725 }
726 `)
727
Sundong Ahnabb64432019-10-22 13:58:29 +0900728 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800729 copyCmds := zipApexRule.Args["copy_commands"]
730
731 // Ensure that main rule creates an output
732 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
733
734 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700735 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800736
737 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700738 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800739
740 // Ensure that both direct and indirect deps are copied into apex
741 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
742 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900743}
744
745func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800746 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900747 apex {
748 name: "myapex",
749 key: "myapex.key",
750 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000751 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900752 }
753
754 apex_key {
755 name: "myapex.key",
756 public_key: "testkey.avbpubkey",
757 private_key: "testkey.pem",
758 }
759
760 cc_library {
761 name: "mylib",
762 srcs: ["mylib.cpp"],
763 shared_libs: ["mylib2", "mylib3"],
764 system_shared_libs: [],
765 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000766 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900767 }
768
769 cc_library {
770 name: "mylib2",
771 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900772 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900773 system_shared_libs: [],
774 stl: "none",
775 stubs: {
776 versions: ["1", "2", "3"],
777 },
778 }
779
780 cc_library {
781 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900782 srcs: ["mylib.cpp"],
783 shared_libs: ["mylib4"],
784 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900785 stl: "none",
786 stubs: {
787 versions: ["10", "11", "12"],
788 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000789 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900790 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900791
792 cc_library {
793 name: "mylib4",
794 srcs: ["mylib.cpp"],
795 system_shared_libs: [],
796 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000797 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900798 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900799 `)
800
Sundong Ahnabb64432019-10-22 13:58:29 +0900801 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900802 copyCmds := apexRule.Args["copy_commands"]
803
804 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800805 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900806
807 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800808 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900809
810 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800811 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900812
Colin Crossaede88c2020-08-11 12:17:01 -0700813 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900814
815 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900816 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900817 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900818 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900819
820 // 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 -0700821 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900822 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700823 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900824
825 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900826 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900827 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900828
829 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700830 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900831
Jooyung Hana57af4a2020-01-23 05:36:59 +0000832 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900833 "lib64/mylib.so",
834 "lib64/mylib3.so",
835 "lib64/mylib4.so",
836 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837}
838
Colin Cross7812fd32020-09-25 12:35:10 -0700839func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
840 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800841 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700842 apex {
843 name: "myapex",
844 key: "myapex.key",
845 native_shared_libs: ["mylib", "mylib3"],
846 min_sdk_version: "29",
847 }
848
849 apex_key {
850 name: "myapex.key",
851 public_key: "testkey.avbpubkey",
852 private_key: "testkey.pem",
853 }
854
855 cc_library {
856 name: "mylib",
857 srcs: ["mylib.cpp"],
858 shared_libs: ["mylib2", "mylib3"],
859 system_shared_libs: [],
860 stl: "none",
861 apex_available: [ "myapex" ],
862 min_sdk_version: "28",
863 }
864
865 cc_library {
866 name: "mylib2",
867 srcs: ["mylib.cpp"],
868 cflags: ["-include mylib.h"],
869 system_shared_libs: [],
870 stl: "none",
871 stubs: {
872 versions: ["28", "29", "30", "current"],
873 },
874 min_sdk_version: "28",
875 }
876
877 cc_library {
878 name: "mylib3",
879 srcs: ["mylib.cpp"],
880 shared_libs: ["mylib4"],
881 system_shared_libs: [],
882 stl: "none",
883 stubs: {
884 versions: ["28", "29", "30", "current"],
885 },
886 apex_available: [ "myapex" ],
887 min_sdk_version: "28",
888 }
889
890 cc_library {
891 name: "mylib4",
892 srcs: ["mylib.cpp"],
893 system_shared_libs: [],
894 stl: "none",
895 apex_available: [ "myapex" ],
896 min_sdk_version: "28",
897 }
898 `)
899
900 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
901 copyCmds := apexRule.Args["copy_commands"]
902
903 // Ensure that direct non-stubs dep is always included
904 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
905
906 // Ensure that indirect stubs dep is not included
907 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
908
909 // Ensure that direct stubs dep is included
910 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
911
912 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
913
Jiyong Park55549df2021-02-26 23:57:23 +0900914 // Ensure that mylib is linking with the latest version of stub for mylib2
915 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700916 // ... and not linking to the non-stub (impl) variant of mylib2
917 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
918
919 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
920 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
921 // .. and not linking to the stubs variant of mylib3
922 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
923
924 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700925 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700926 ensureNotContains(t, mylib2Cflags, "-include ")
927
928 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700929 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700930
931 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
932 "lib64/mylib.so",
933 "lib64/mylib3.so",
934 "lib64/mylib4.so",
935 })
936}
937
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900938func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
939 t.Parallel()
940 // myapex (Z)
941 // mylib -----------------.
942 // |
943 // otherapex (29) |
944 // libstub's versions: 29 Z current
945 // |
946 // <platform> |
947 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800948 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900949 apex {
950 name: "myapex",
951 key: "myapex.key",
952 native_shared_libs: ["mylib"],
953 min_sdk_version: "Z", // non-final
954 }
955
956 cc_library {
957 name: "mylib",
958 srcs: ["mylib.cpp"],
959 shared_libs: ["libstub"],
960 apex_available: ["myapex"],
961 min_sdk_version: "Z",
962 }
963
964 apex_key {
965 name: "myapex.key",
966 public_key: "testkey.avbpubkey",
967 private_key: "testkey.pem",
968 }
969
970 apex {
971 name: "otherapex",
972 key: "myapex.key",
973 native_shared_libs: ["libstub"],
974 min_sdk_version: "29",
975 }
976
977 cc_library {
978 name: "libstub",
979 srcs: ["mylib.cpp"],
980 stubs: {
981 versions: ["29", "Z", "current"],
982 },
983 apex_available: ["otherapex"],
984 min_sdk_version: "29",
985 }
986
987 // platform module depending on libstub from otherapex should use the latest stub("current")
988 cc_library {
989 name: "libplatform",
990 srcs: ["mylib.cpp"],
991 shared_libs: ["libstub"],
992 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +0000993 `,
994 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
995 variables.Platform_sdk_codename = proptools.StringPtr("Z")
996 variables.Platform_sdk_final = proptools.BoolPtr(false)
997 variables.Platform_version_active_codenames = []string{"Z"}
998 }),
999 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001000
Jiyong Park55549df2021-02-26 23:57:23 +09001001 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001002 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001003 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001004 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001005 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001006
1007 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1008 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1009 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1010 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1011 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1012}
1013
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001014func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001015 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001016 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001017 name: "myapex2",
1018 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001019 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001020 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001021 }
1022
1023 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001024 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 cc_library {
1030 name: "mylib",
1031 srcs: ["mylib.cpp"],
1032 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001033 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001034 system_shared_libs: [],
1035 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001036 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001037 }
1038
1039 cc_library {
1040 name: "libfoo",
1041 srcs: ["mylib.cpp"],
1042 shared_libs: ["libbar"],
1043 system_shared_libs: [],
1044 stl: "none",
1045 stubs: {
1046 versions: ["10", "20", "30"],
1047 },
1048 }
1049
1050 cc_library {
1051 name: "libbar",
1052 srcs: ["mylib.cpp"],
1053 system_shared_libs: [],
1054 stl: "none",
1055 }
1056
Jiyong Park678c8812020-02-07 17:25:49 +09001057 cc_library_static {
1058 name: "libbaz",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 apex_available: [ "myapex2" ],
1063 }
1064
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001065 `)
1066
Jiyong Park83dc74b2020-01-14 18:38:44 +09001067 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001068 copyCmds := apexRule.Args["copy_commands"]
1069
1070 // Ensure that direct non-stubs dep is always included
1071 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1072
1073 // Ensure that indirect stubs dep is not included
1074 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1075
1076 // Ensure that dependency of stubs is not included
1077 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1078
Colin Crossaede88c2020-08-11 12:17:01 -07001079 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001080
1081 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001082 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001083 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001084 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001085
Jiyong Park3ff16992019-12-27 14:11:47 +09001086 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001087
1088 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1089 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001090
Artur Satayeva8bd1132020-04-27 18:07:06 +01001091 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001092 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001093
Artur Satayeva8bd1132020-04-27 18:07:06 +01001094 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001095 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001096}
1097
Jooyung Hand3639552019-08-09 12:57:43 +09001098func TestApexWithRuntimeLibsDependency(t *testing.T) {
1099 /*
1100 myapex
1101 |
1102 v (runtime_libs)
1103 mylib ------+------> libfoo [provides stub]
1104 |
1105 `------> libbar
1106 */
Colin Cross1c460562021-02-16 17:55:47 -08001107 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001108 apex {
1109 name: "myapex",
1110 key: "myapex.key",
1111 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001112 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001113 }
1114
1115 apex_key {
1116 name: "myapex.key",
1117 public_key: "testkey.avbpubkey",
1118 private_key: "testkey.pem",
1119 }
1120
1121 cc_library {
1122 name: "mylib",
1123 srcs: ["mylib.cpp"],
1124 runtime_libs: ["libfoo", "libbar"],
1125 system_shared_libs: [],
1126 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001127 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001128 }
1129
1130 cc_library {
1131 name: "libfoo",
1132 srcs: ["mylib.cpp"],
1133 system_shared_libs: [],
1134 stl: "none",
1135 stubs: {
1136 versions: ["10", "20", "30"],
1137 },
1138 }
1139
1140 cc_library {
1141 name: "libbar",
1142 srcs: ["mylib.cpp"],
1143 system_shared_libs: [],
1144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001145 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001146 }
1147
1148 `)
1149
Sundong Ahnabb64432019-10-22 13:58:29 +09001150 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001151 copyCmds := apexRule.Args["copy_commands"]
1152
1153 // Ensure that direct non-stubs dep is always included
1154 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1155
1156 // Ensure that indirect stubs dep is not included
1157 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1158
1159 // Ensure that runtime_libs dep in included
1160 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1161
Sundong Ahnabb64432019-10-22 13:58:29 +09001162 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001163 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1164 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001165
1166}
1167
Paul Duffina02cae32021-03-09 01:44:06 +00001168var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1169 cc.PrepareForTestWithCcBuildComponents,
1170 PrepareForTestWithApexBuildComponents,
1171 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001172 apex {
1173 name: "com.android.runtime",
1174 key: "com.android.runtime.key",
1175 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001176 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001177 }
1178
1179 apex_key {
1180 name: "com.android.runtime.key",
1181 public_key: "testkey.avbpubkey",
1182 private_key: "testkey.pem",
1183 }
Paul Duffina02cae32021-03-09 01:44:06 +00001184 `),
1185 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1186)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001187
Paul Duffina02cae32021-03-09 01:44:06 +00001188func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001189 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001190 cc_library {
1191 name: "libc",
1192 no_libcrt: true,
1193 nocrt: true,
1194 stl: "none",
1195 system_shared_libs: [],
1196 stubs: { versions: ["1"] },
1197 apex_available: ["com.android.runtime"],
1198
1199 sanitize: {
1200 hwaddress: true,
1201 }
1202 }
1203
1204 cc_prebuilt_library_shared {
1205 name: "libclang_rt.hwasan-aarch64-android",
1206 no_libcrt: true,
1207 nocrt: true,
1208 stl: "none",
1209 system_shared_libs: [],
1210 srcs: [""],
1211 stubs: { versions: ["1"] },
1212
1213 sanitize: {
1214 never: true,
1215 },
Paul Duffina02cae32021-03-09 01:44:06 +00001216 } `)
1217 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001218
1219 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1220 "lib64/bionic/libc.so",
1221 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1222 })
1223
1224 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1225
1226 installed := hwasan.Description("install libclang_rt.hwasan")
1227 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1228
1229 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1230 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1231 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1232}
1233
1234func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001235 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001236 prepareForTestOfRuntimeApexWithHwasan,
1237 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1238 variables.SanitizeDevice = []string{"hwaddress"}
1239 }),
1240 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001241 cc_library {
1242 name: "libc",
1243 no_libcrt: true,
1244 nocrt: true,
1245 stl: "none",
1246 system_shared_libs: [],
1247 stubs: { versions: ["1"] },
1248 apex_available: ["com.android.runtime"],
1249 }
1250
1251 cc_prebuilt_library_shared {
1252 name: "libclang_rt.hwasan-aarch64-android",
1253 no_libcrt: true,
1254 nocrt: true,
1255 stl: "none",
1256 system_shared_libs: [],
1257 srcs: [""],
1258 stubs: { versions: ["1"] },
1259
1260 sanitize: {
1261 never: true,
1262 },
1263 }
Paul Duffina02cae32021-03-09 01:44:06 +00001264 `)
1265 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001266
1267 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1268 "lib64/bionic/libc.so",
1269 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1270 })
1271
1272 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1273
1274 installed := hwasan.Description("install libclang_rt.hwasan")
1275 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1276
1277 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1278 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1279 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1280}
1281
Jooyung Han61b66e92020-03-21 14:21:46 +00001282func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1283 testcases := []struct {
1284 name string
1285 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001286 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001287 shouldLink string
1288 shouldNotLink []string
1289 }{
1290 {
Jiyong Park55549df2021-02-26 23:57:23 +09001291 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001292 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001293 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001294 shouldLink: "30",
1295 shouldNotLink: []string{"29"},
1296 },
1297 {
Jiyong Park55549df2021-02-26 23:57:23 +09001298 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001299 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001300 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001301 shouldLink: "30",
1302 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001303 },
1304 }
1305 for _, tc := range testcases {
1306 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001307 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001308 apex {
1309 name: "myapex",
1310 key: "myapex.key",
1311 use_vendor: true,
1312 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001313 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001314 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001315 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001316
Jooyung Han61b66e92020-03-21 14:21:46 +00001317 apex_key {
1318 name: "myapex.key",
1319 public_key: "testkey.avbpubkey",
1320 private_key: "testkey.pem",
1321 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001322
Jooyung Han61b66e92020-03-21 14:21:46 +00001323 cc_library {
1324 name: "mylib",
1325 srcs: ["mylib.cpp"],
1326 vendor_available: true,
1327 shared_libs: ["libbar"],
1328 system_shared_libs: [],
1329 stl: "none",
1330 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001331 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001332 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001333
Jooyung Han61b66e92020-03-21 14:21:46 +00001334 cc_library {
1335 name: "libbar",
1336 srcs: ["mylib.cpp"],
1337 system_shared_libs: [],
1338 stl: "none",
1339 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001340 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001341 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001342
Jooyung Han61b66e92020-03-21 14:21:46 +00001343 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001344 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001345 symbol_file: "",
1346 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001347 `,
1348 setUseVendorAllowListForTest([]string{"myapex"}),
1349 withUnbundledBuild,
1350 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001351
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 // Ensure that LLNDK dep is not included
1353 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1354 "lib64/mylib.so",
1355 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001356
Jooyung Han61b66e92020-03-21 14:21:46 +00001357 // Ensure that LLNDK dep is required
1358 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1359 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1360 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001361
Colin Crossaede88c2020-08-11 12:17:01 -07001362 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001363 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001364 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001365 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001366 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001367
Colin Crossaede88c2020-08-11 12:17:01 -07001368 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001369 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1370 })
1371 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001372}
1373
Jiyong Park25fc6a92018-11-18 18:02:45 +09001374func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001375 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001376 apex {
1377 name: "myapex",
1378 key: "myapex.key",
1379 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001380 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001381 }
1382
1383 apex_key {
1384 name: "myapex.key",
1385 public_key: "testkey.avbpubkey",
1386 private_key: "testkey.pem",
1387 }
1388
1389 cc_library {
1390 name: "mylib",
1391 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001392 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001393 shared_libs: ["libdl#27"],
1394 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001395 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001396 }
1397
1398 cc_library_shared {
1399 name: "mylib_shared",
1400 srcs: ["mylib.cpp"],
1401 shared_libs: ["libdl#27"],
1402 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001403 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001404 }
1405
1406 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001407 name: "libBootstrap",
1408 srcs: ["mylib.cpp"],
1409 stl: "none",
1410 bootstrap: true,
1411 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001412 `)
1413
Sundong Ahnabb64432019-10-22 13:58:29 +09001414 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001415 copyCmds := apexRule.Args["copy_commands"]
1416
1417 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001418 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001419 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1420 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001421
1422 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001423 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001424
Colin Crossaede88c2020-08-11 12:17:01 -07001425 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1426 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1427 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001428
1429 // For dependency to libc
1430 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001431 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001432 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001433 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001434 // ... Cflags from stub is correctly exported to mylib
1435 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1436 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1437
1438 // For dependency to libm
1439 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001440 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001441 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001442 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 // ... and is not compiling with the stub
1444 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1445 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1446
1447 // For dependency to libdl
1448 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001449 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001450 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001451 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1452 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001453 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001454 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001455 // ... Cflags from stub is correctly exported to mylib
1456 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1457 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001458
1459 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001460 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1461 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1462 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1463 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001464}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001465
Jooyung Han749dc692020-04-15 11:03:39 +09001466func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001467 // there are three links between liba --> libz.
1468 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001469 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001470 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001471 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001472 apex {
1473 name: "myapex",
1474 key: "myapex.key",
1475 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001476 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001477 }
1478
1479 apex {
1480 name: "otherapex",
1481 key: "myapex.key",
1482 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001483 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001484 }
1485
1486 apex_key {
1487 name: "myapex.key",
1488 public_key: "testkey.avbpubkey",
1489 private_key: "testkey.pem",
1490 }
1491
1492 cc_library {
1493 name: "libx",
1494 shared_libs: ["liba"],
1495 system_shared_libs: [],
1496 stl: "none",
1497 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001498 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001499 }
1500
1501 cc_library {
1502 name: "liby",
1503 shared_libs: ["liba"],
1504 system_shared_libs: [],
1505 stl: "none",
1506 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001507 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001508 }
1509
1510 cc_library {
1511 name: "liba",
1512 shared_libs: ["libz"],
1513 system_shared_libs: [],
1514 stl: "none",
1515 apex_available: [
1516 "//apex_available:anyapex",
1517 "//apex_available:platform",
1518 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001519 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001520 }
1521
1522 cc_library {
1523 name: "libz",
1524 system_shared_libs: [],
1525 stl: "none",
1526 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001527 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001528 },
1529 }
Jooyung Han749dc692020-04-15 11:03:39 +09001530 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001531
1532 expectLink := func(from, from_variant, to, to_variant string) {
1533 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1534 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1535 }
1536 expectNoLink := func(from, from_variant, to, to_variant string) {
1537 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1538 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1539 }
1540 // platform liba is linked to non-stub version
1541 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001542 // liba in myapex is linked to #30
1543 expectLink("liba", "shared_apex29", "libz", "shared_30")
1544 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001545 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001546 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001547 expectLink("liba", "shared_apex30", "libz", "shared_30")
1548 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1549 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001550}
1551
Jooyung Hanaed150d2020-04-02 01:41:41 +09001552func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001553 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001554 apex {
1555 name: "myapex",
1556 key: "myapex.key",
1557 native_shared_libs: ["libx"],
1558 min_sdk_version: "R",
1559 }
1560
1561 apex_key {
1562 name: "myapex.key",
1563 public_key: "testkey.avbpubkey",
1564 private_key: "testkey.pem",
1565 }
1566
1567 cc_library {
1568 name: "libx",
1569 shared_libs: ["libz"],
1570 system_shared_libs: [],
1571 stl: "none",
1572 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001573 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001574 }
1575
1576 cc_library {
1577 name: "libz",
1578 system_shared_libs: [],
1579 stl: "none",
1580 stubs: {
1581 versions: ["29", "R"],
1582 },
1583 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001584 `,
1585 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1586 variables.Platform_version_active_codenames = []string{"R"}
1587 }),
1588 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001589
1590 expectLink := func(from, from_variant, to, to_variant string) {
1591 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1592 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1593 }
1594 expectNoLink := func(from, from_variant, to, to_variant string) {
1595 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1596 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1597 }
Dan Albertc8060532020-07-22 22:32:17 -07001598 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001599 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1600 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001601}
1602
Jooyung Han749dc692020-04-15 11:03:39 +09001603func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001604 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001605 apex {
1606 name: "myapex",
1607 key: "myapex.key",
1608 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001609 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001610 }
1611
1612 apex_key {
1613 name: "myapex.key",
1614 public_key: "testkey.avbpubkey",
1615 private_key: "testkey.pem",
1616 }
1617
1618 cc_library {
1619 name: "libx",
1620 shared_libs: ["libz"],
1621 system_shared_libs: [],
1622 stl: "none",
1623 apex_available: [ "myapex" ],
1624 }
1625
1626 cc_library {
1627 name: "libz",
1628 system_shared_libs: [],
1629 stl: "none",
1630 stubs: {
1631 versions: ["1", "2"],
1632 },
1633 }
1634 `)
1635
1636 expectLink := func(from, from_variant, to, to_variant string) {
1637 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1638 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1639 }
1640 expectNoLink := func(from, from_variant, to, to_variant string) {
1641 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1642 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1643 }
Colin Crossaede88c2020-08-11 12:17:01 -07001644 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1645 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1646 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001647}
1648
1649func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001650 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001651 apex {
1652 name: "myapex",
1653 key: "myapex.key",
1654 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001655 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001656 }
1657
1658 apex_key {
1659 name: "myapex.key",
1660 public_key: "testkey.avbpubkey",
1661 private_key: "testkey.pem",
1662 }
1663
1664 cc_library {
1665 name: "libx",
1666 system_shared_libs: [],
1667 stl: "none",
1668 apex_available: [ "myapex" ],
1669 stubs: {
1670 versions: ["1", "2"],
1671 },
1672 }
1673
1674 cc_library {
1675 name: "libz",
1676 shared_libs: ["libx"],
1677 system_shared_libs: [],
1678 stl: "none",
1679 }
1680 `)
1681
1682 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001683 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001684 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1685 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1686 }
1687 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001688 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001689 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1690 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1691 }
1692 expectLink("libz", "shared", "libx", "shared_2")
1693 expectNoLink("libz", "shared", "libz", "shared_1")
1694 expectNoLink("libz", "shared", "libz", "shared")
1695}
1696
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001697var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1698 func(variables android.FixtureProductVariables) {
1699 variables.SanitizeDevice = []string{"hwaddress"}
1700 },
1701)
1702
Jooyung Han75568392020-03-20 04:29:24 +09001703func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001704 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001705 apex {
1706 name: "myapex",
1707 key: "myapex.key",
1708 native_shared_libs: ["libx"],
1709 min_sdk_version: "29",
1710 }
1711
1712 apex_key {
1713 name: "myapex.key",
1714 public_key: "testkey.avbpubkey",
1715 private_key: "testkey.pem",
1716 }
1717
1718 cc_library {
1719 name: "libx",
1720 shared_libs: ["libbar"],
1721 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001722 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001723 }
1724
1725 cc_library {
1726 name: "libbar",
1727 stubs: {
1728 versions: ["29", "30"],
1729 },
1730 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001731 `,
1732 prepareForTestWithSantitizeHwaddress,
1733 )
Jooyung Han03b51852020-02-26 22:45:42 +09001734 expectLink := func(from, from_variant, to, to_variant string) {
1735 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1736 libFlags := ld.Args["libFlags"]
1737 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1738 }
Colin Crossaede88c2020-08-11 12:17:01 -07001739 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001740}
1741
Jooyung Han75568392020-03-20 04:29:24 +09001742func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001743 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001744 apex {
1745 name: "myapex",
1746 key: "myapex.key",
1747 native_shared_libs: ["libx"],
1748 min_sdk_version: "29",
1749 }
1750
1751 apex_key {
1752 name: "myapex.key",
1753 public_key: "testkey.avbpubkey",
1754 private_key: "testkey.pem",
1755 }
1756
1757 cc_library {
1758 name: "libx",
1759 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001760 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001761 }
Jooyung Han75568392020-03-20 04:29:24 +09001762 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001763
1764 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001765 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001766 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001767 // note that platform variant is not.
1768 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001769 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001770}
1771
Jooyung Han749dc692020-04-15 11:03:39 +09001772func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1773 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001774 apex {
1775 name: "myapex",
1776 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001777 native_shared_libs: ["mylib"],
1778 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001779 }
1780
1781 apex_key {
1782 name: "myapex.key",
1783 public_key: "testkey.avbpubkey",
1784 private_key: "testkey.pem",
1785 }
Jooyung Han749dc692020-04-15 11:03:39 +09001786
1787 cc_library {
1788 name: "mylib",
1789 srcs: ["mylib.cpp"],
1790 system_shared_libs: [],
1791 stl: "none",
1792 apex_available: [
1793 "myapex",
1794 ],
1795 min_sdk_version: "30",
1796 }
1797 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001798
1799 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1800 apex {
1801 name: "myapex",
1802 key: "myapex.key",
1803 native_shared_libs: ["libfoo.ffi"],
1804 min_sdk_version: "29",
1805 }
1806
1807 apex_key {
1808 name: "myapex.key",
1809 public_key: "testkey.avbpubkey",
1810 private_key: "testkey.pem",
1811 }
1812
1813 rust_ffi_shared {
1814 name: "libfoo.ffi",
1815 srcs: ["foo.rs"],
1816 crate_name: "foo",
1817 apex_available: [
1818 "myapex",
1819 ],
1820 min_sdk_version: "30",
1821 }
1822 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001823}
1824
1825func TestApexMinSdkVersion_Okay(t *testing.T) {
1826 testApex(t, `
1827 apex {
1828 name: "myapex",
1829 key: "myapex.key",
1830 native_shared_libs: ["libfoo"],
1831 java_libs: ["libbar"],
1832 min_sdk_version: "29",
1833 }
1834
1835 apex_key {
1836 name: "myapex.key",
1837 public_key: "testkey.avbpubkey",
1838 private_key: "testkey.pem",
1839 }
1840
1841 cc_library {
1842 name: "libfoo",
1843 srcs: ["mylib.cpp"],
1844 shared_libs: ["libfoo_dep"],
1845 apex_available: ["myapex"],
1846 min_sdk_version: "29",
1847 }
1848
1849 cc_library {
1850 name: "libfoo_dep",
1851 srcs: ["mylib.cpp"],
1852 apex_available: ["myapex"],
1853 min_sdk_version: "29",
1854 }
1855
1856 java_library {
1857 name: "libbar",
1858 sdk_version: "current",
1859 srcs: ["a.java"],
1860 static_libs: ["libbar_dep"],
1861 apex_available: ["myapex"],
1862 min_sdk_version: "29",
1863 }
1864
1865 java_library {
1866 name: "libbar_dep",
1867 sdk_version: "current",
1868 srcs: ["a.java"],
1869 apex_available: ["myapex"],
1870 min_sdk_version: "29",
1871 }
Jooyung Han03b51852020-02-26 22:45:42 +09001872 `)
1873}
1874
Artur Satayev8cf899a2020-04-15 17:29:42 +01001875func TestJavaStableSdkVersion(t *testing.T) {
1876 testCases := []struct {
1877 name string
1878 expectedError string
1879 bp string
1880 }{
1881 {
1882 name: "Non-updatable apex with non-stable dep",
1883 bp: `
1884 apex {
1885 name: "myapex",
1886 java_libs: ["myjar"],
1887 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001888 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001889 }
1890 apex_key {
1891 name: "myapex.key",
1892 public_key: "testkey.avbpubkey",
1893 private_key: "testkey.pem",
1894 }
1895 java_library {
1896 name: "myjar",
1897 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001898 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001899 apex_available: ["myapex"],
1900 }
1901 `,
1902 },
1903 {
1904 name: "Updatable apex with stable dep",
1905 bp: `
1906 apex {
1907 name: "myapex",
1908 java_libs: ["myjar"],
1909 key: "myapex.key",
1910 updatable: true,
1911 min_sdk_version: "29",
1912 }
1913 apex_key {
1914 name: "myapex.key",
1915 public_key: "testkey.avbpubkey",
1916 private_key: "testkey.pem",
1917 }
1918 java_library {
1919 name: "myjar",
1920 srcs: ["foo/bar/MyClass.java"],
1921 sdk_version: "current",
1922 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001923 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001924 }
1925 `,
1926 },
1927 {
1928 name: "Updatable apex with non-stable dep",
1929 expectedError: "cannot depend on \"myjar\"",
1930 bp: `
1931 apex {
1932 name: "myapex",
1933 java_libs: ["myjar"],
1934 key: "myapex.key",
1935 updatable: true,
1936 }
1937 apex_key {
1938 name: "myapex.key",
1939 public_key: "testkey.avbpubkey",
1940 private_key: "testkey.pem",
1941 }
1942 java_library {
1943 name: "myjar",
1944 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001945 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001946 apex_available: ["myapex"],
1947 }
1948 `,
1949 },
1950 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001951 name: "Updatable apex with non-stable transitive dep",
1952 // This is not actually detecting that the transitive dependency is unstable, rather it is
1953 // detecting that the transitive dependency is building against a wider API surface than the
1954 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001955 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001956 bp: `
1957 apex {
1958 name: "myapex",
1959 java_libs: ["myjar"],
1960 key: "myapex.key",
1961 updatable: true,
1962 }
1963 apex_key {
1964 name: "myapex.key",
1965 public_key: "testkey.avbpubkey",
1966 private_key: "testkey.pem",
1967 }
1968 java_library {
1969 name: "myjar",
1970 srcs: ["foo/bar/MyClass.java"],
1971 sdk_version: "current",
1972 apex_available: ["myapex"],
1973 static_libs: ["transitive-jar"],
1974 }
1975 java_library {
1976 name: "transitive-jar",
1977 srcs: ["foo/bar/MyClass.java"],
1978 sdk_version: "core_platform",
1979 apex_available: ["myapex"],
1980 }
1981 `,
1982 },
1983 }
1984
1985 for _, test := range testCases {
1986 t.Run(test.name, func(t *testing.T) {
1987 if test.expectedError == "" {
1988 testApex(t, test.bp)
1989 } else {
1990 testApexError(t, test.expectedError, test.bp)
1991 }
1992 })
1993 }
1994}
1995
Jooyung Han749dc692020-04-15 11:03:39 +09001996func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1997 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
1998 apex {
1999 name: "myapex",
2000 key: "myapex.key",
2001 native_shared_libs: ["mylib"],
2002 min_sdk_version: "29",
2003 }
2004
2005 apex_key {
2006 name: "myapex.key",
2007 public_key: "testkey.avbpubkey",
2008 private_key: "testkey.pem",
2009 }
2010
2011 cc_library {
2012 name: "mylib",
2013 srcs: ["mylib.cpp"],
2014 shared_libs: ["mylib2"],
2015 system_shared_libs: [],
2016 stl: "none",
2017 apex_available: [
2018 "myapex",
2019 ],
2020 min_sdk_version: "29",
2021 }
2022
2023 // indirect part of the apex
2024 cc_library {
2025 name: "mylib2",
2026 srcs: ["mylib.cpp"],
2027 system_shared_libs: [],
2028 stl: "none",
2029 apex_available: [
2030 "myapex",
2031 ],
2032 min_sdk_version: "30",
2033 }
2034 `)
2035}
2036
2037func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2038 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2039 apex {
2040 name: "myapex",
2041 key: "myapex.key",
2042 apps: ["AppFoo"],
2043 min_sdk_version: "29",
2044 }
2045
2046 apex_key {
2047 name: "myapex.key",
2048 public_key: "testkey.avbpubkey",
2049 private_key: "testkey.pem",
2050 }
2051
2052 android_app {
2053 name: "AppFoo",
2054 srcs: ["foo/bar/MyClass.java"],
2055 sdk_version: "current",
2056 min_sdk_version: "29",
2057 system_modules: "none",
2058 stl: "none",
2059 static_libs: ["bar"],
2060 apex_available: [ "myapex" ],
2061 }
2062
2063 java_library {
2064 name: "bar",
2065 sdk_version: "current",
2066 srcs: ["a.java"],
2067 apex_available: [ "myapex" ],
2068 }
2069 `)
2070}
2071
2072func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002073 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002074 apex {
2075 name: "myapex",
2076 key: "myapex.key",
2077 native_shared_libs: ["mylib"],
2078 min_sdk_version: "29",
2079 }
2080
2081 apex_key {
2082 name: "myapex.key",
2083 public_key: "testkey.avbpubkey",
2084 private_key: "testkey.pem",
2085 }
2086
Jiyong Park55549df2021-02-26 23:57:23 +09002087 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002088 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2089 cc_library {
2090 name: "mylib",
2091 srcs: ["mylib.cpp"],
2092 shared_libs: ["mylib2"],
2093 system_shared_libs: [],
2094 stl: "none",
2095 apex_available: ["myapex", "otherapex"],
2096 min_sdk_version: "29",
2097 }
2098
2099 cc_library {
2100 name: "mylib2",
2101 srcs: ["mylib.cpp"],
2102 system_shared_libs: [],
2103 stl: "none",
2104 apex_available: ["otherapex"],
2105 stubs: { versions: ["29", "30"] },
2106 min_sdk_version: "30",
2107 }
2108
2109 apex {
2110 name: "otherapex",
2111 key: "myapex.key",
2112 native_shared_libs: ["mylib", "mylib2"],
2113 min_sdk_version: "30",
2114 }
2115 `)
2116 expectLink := func(from, from_variant, to, to_variant string) {
2117 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2118 libFlags := ld.Args["libFlags"]
2119 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2120 }
Jiyong Park55549df2021-02-26 23:57:23 +09002121 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002122 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002123}
2124
Jooyung Haned124c32021-01-26 11:43:46 +09002125func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002126 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2127 func(variables android.FixtureProductVariables) {
2128 variables.Platform_sdk_codename = proptools.StringPtr("S")
2129 variables.Platform_version_active_codenames = []string{"S"}
2130 },
2131 )
Jooyung Haned124c32021-01-26 11:43:46 +09002132 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2133 apex {
2134 name: "myapex",
2135 key: "myapex.key",
2136 native_shared_libs: ["libfoo"],
2137 min_sdk_version: "S",
2138 }
2139 apex_key {
2140 name: "myapex.key",
2141 public_key: "testkey.avbpubkey",
2142 private_key: "testkey.pem",
2143 }
2144 cc_library {
2145 name: "libfoo",
2146 shared_libs: ["libbar"],
2147 apex_available: ["myapex"],
2148 min_sdk_version: "29",
2149 }
2150 cc_library {
2151 name: "libbar",
2152 apex_available: ["myapex"],
2153 }
2154 `, withSAsActiveCodeNames)
2155}
2156
2157func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002158 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2159 variables.Platform_sdk_codename = proptools.StringPtr("S")
2160 variables.Platform_version_active_codenames = []string{"S", "T"}
2161 })
Colin Cross1c460562021-02-16 17:55:47 -08002162 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002163 apex {
2164 name: "myapex",
2165 key: "myapex.key",
2166 native_shared_libs: ["libfoo"],
2167 min_sdk_version: "S",
2168 }
2169 apex_key {
2170 name: "myapex.key",
2171 public_key: "testkey.avbpubkey",
2172 private_key: "testkey.pem",
2173 }
2174 cc_library {
2175 name: "libfoo",
2176 shared_libs: ["libbar"],
2177 apex_available: ["myapex"],
2178 min_sdk_version: "S",
2179 }
2180 cc_library {
2181 name: "libbar",
2182 stubs: {
2183 symbol_file: "libbar.map.txt",
2184 versions: ["30", "S", "T"],
2185 },
2186 }
2187 `, withSAsActiveCodeNames)
2188
2189 // ensure libfoo is linked with "S" version of libbar stub
2190 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2191 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002192 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002193}
2194
Jiyong Park7c2ee712018-12-07 00:42:25 +09002195func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002196 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002197 apex {
2198 name: "myapex",
2199 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002200 native_shared_libs: ["mylib"],
2201 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002202 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002203 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002204 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002205 }
2206
2207 apex_key {
2208 name: "myapex.key",
2209 public_key: "testkey.avbpubkey",
2210 private_key: "testkey.pem",
2211 }
2212
2213 prebuilt_etc {
2214 name: "myetc",
2215 src: "myprebuilt",
2216 sub_dir: "foo/bar",
2217 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002218
2219 cc_library {
2220 name: "mylib",
2221 srcs: ["mylib.cpp"],
2222 relative_install_path: "foo/bar",
2223 system_shared_libs: [],
2224 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002225 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002226 }
2227
2228 cc_binary {
2229 name: "mybin",
2230 srcs: ["mylib.cpp"],
2231 relative_install_path: "foo/bar",
2232 system_shared_libs: [],
2233 static_executable: true,
2234 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002235 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002236 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002237 `)
2238
Sundong Ahnabb64432019-10-22 13:58:29 +09002239 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002240 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2241
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002242 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002243 ensureListContains(t, dirs, "etc")
2244 ensureListContains(t, dirs, "etc/foo")
2245 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002246 ensureListContains(t, dirs, "lib64")
2247 ensureListContains(t, dirs, "lib64/foo")
2248 ensureListContains(t, dirs, "lib64/foo/bar")
2249 ensureListContains(t, dirs, "lib")
2250 ensureListContains(t, dirs, "lib/foo")
2251 ensureListContains(t, dirs, "lib/foo/bar")
2252
Jiyong Parkbd13e442019-03-15 18:10:35 +09002253 ensureListContains(t, dirs, "bin")
2254 ensureListContains(t, dirs, "bin/foo")
2255 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002256}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002257
Jooyung Han35155c42020-02-06 17:33:20 +09002258func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002259 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002260 apex {
2261 name: "myapex",
2262 key: "myapex.key",
2263 multilib: {
2264 both: {
2265 native_shared_libs: ["mylib"],
2266 binaries: ["mybin"],
2267 },
2268 },
2269 compile_multilib: "both",
2270 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002271 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002272 }
2273
2274 apex_key {
2275 name: "myapex.key",
2276 public_key: "testkey.avbpubkey",
2277 private_key: "testkey.pem",
2278 }
2279
2280 cc_library {
2281 name: "mylib",
2282 relative_install_path: "foo/bar",
2283 system_shared_libs: [],
2284 stl: "none",
2285 apex_available: [ "myapex" ],
2286 native_bridge_supported: true,
2287 }
2288
2289 cc_binary {
2290 name: "mybin",
2291 relative_install_path: "foo/bar",
2292 system_shared_libs: [],
2293 static_executable: true,
2294 stl: "none",
2295 apex_available: [ "myapex" ],
2296 native_bridge_supported: true,
2297 compile_multilib: "both", // default is "first" for binary
2298 multilib: {
2299 lib64: {
2300 suffix: "64",
2301 },
2302 },
2303 }
2304 `, withNativeBridgeEnabled)
2305 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2306 "bin/foo/bar/mybin",
2307 "bin/foo/bar/mybin64",
2308 "bin/arm/foo/bar/mybin",
2309 "bin/arm64/foo/bar/mybin64",
2310 "lib/foo/bar/mylib.so",
2311 "lib/arm/foo/bar/mylib.so",
2312 "lib64/foo/bar/mylib.so",
2313 "lib64/arm64/foo/bar/mylib.so",
2314 })
2315}
2316
Jiyong Parkda6eb592018-12-19 17:12:36 +09002317func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002318 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002319 apex {
2320 name: "myapex",
2321 key: "myapex.key",
2322 native_shared_libs: ["mylib"],
2323 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002324 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002325 }
2326
2327 apex_key {
2328 name: "myapex.key",
2329 public_key: "testkey.avbpubkey",
2330 private_key: "testkey.pem",
2331 }
2332
2333 cc_library {
2334 name: "mylib",
2335 srcs: ["mylib.cpp"],
2336 shared_libs: ["mylib2"],
2337 system_shared_libs: [],
2338 vendor_available: true,
2339 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002340 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002341 }
2342
2343 cc_library {
2344 name: "mylib2",
2345 srcs: ["mylib.cpp"],
2346 system_shared_libs: [],
2347 vendor_available: true,
2348 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002349 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002350 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002351 `,
2352 setUseVendorAllowListForTest([]string{"myapex"}),
2353 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002354
2355 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002356 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002357 for _, implicit := range i.Implicits {
2358 inputsList = append(inputsList, implicit.String())
2359 }
2360 }
2361 inputsString := strings.Join(inputsList, " ")
2362
2363 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002364 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2365 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002366
2367 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002368 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2369 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002370}
Jiyong Park16e91a02018-12-20 18:18:08 +09002371
Jooyung Han85d61762020-06-24 23:50:26 +09002372func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002373 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2374 apex {
2375 name: "myapex",
2376 key: "myapex.key",
2377 use_vendor: true,
2378 }
2379 apex_key {
2380 name: "myapex.key",
2381 public_key: "testkey.avbpubkey",
2382 private_key: "testkey.pem",
2383 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002384 `,
2385 setUseVendorAllowListForTest([]string{""}),
2386 )
Colin Cross440e0d02020-06-11 11:32:11 -07002387 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002388 testApex(t, `
2389 apex {
2390 name: "myapex",
2391 key: "myapex.key",
2392 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002393 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002394 }
2395 apex_key {
2396 name: "myapex.key",
2397 public_key: "testkey.avbpubkey",
2398 private_key: "testkey.pem",
2399 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002400 `,
2401 setUseVendorAllowListForTest([]string{"myapex"}),
2402 )
Jooyung Handc782442019-11-01 03:14:38 +09002403}
2404
Jooyung Han5c998b92019-06-27 11:30:33 +09002405func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2406 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2407 apex {
2408 name: "myapex",
2409 key: "myapex.key",
2410 native_shared_libs: ["mylib"],
2411 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002412 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002413 }
2414
2415 apex_key {
2416 name: "myapex.key",
2417 public_key: "testkey.avbpubkey",
2418 private_key: "testkey.pem",
2419 }
2420
2421 cc_library {
2422 name: "mylib",
2423 srcs: ["mylib.cpp"],
2424 system_shared_libs: [],
2425 stl: "none",
2426 }
2427 `)
2428}
2429
Jooyung Han85d61762020-06-24 23:50:26 +09002430func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002431 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002432 apex {
2433 name: "myapex",
2434 key: "myapex.key",
2435 binaries: ["mybin"],
2436 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002437 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002438 }
2439 apex_key {
2440 name: "myapex.key",
2441 public_key: "testkey.avbpubkey",
2442 private_key: "testkey.pem",
2443 }
2444 cc_binary {
2445 name: "mybin",
2446 vendor: true,
2447 shared_libs: ["libfoo"],
2448 }
2449 cc_library {
2450 name: "libfoo",
2451 proprietary: true,
2452 }
2453 `)
2454
2455 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2456 "bin/mybin",
2457 "lib64/libfoo.so",
2458 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2459 "lib64/libc++.so",
2460 })
2461
2462 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002463 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002464 name := apexBundle.BaseModuleName()
2465 prefix := "TARGET_"
2466 var builder strings.Builder
2467 data.Custom(&builder, name, prefix, "", data)
2468 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002469 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2470 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002471
2472 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2473 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2474 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002475}
2476
Jooyung Handf78e212020-07-22 15:54:47 +09002477func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002478 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002479 apex {
2480 name: "myapex",
2481 key: "myapex.key",
2482 binaries: ["mybin"],
2483 vendor: true,
2484 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002485 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002486 }
2487 apex_key {
2488 name: "myapex.key",
2489 public_key: "testkey.avbpubkey",
2490 private_key: "testkey.pem",
2491 }
2492 cc_binary {
2493 name: "mybin",
2494 vendor: true,
2495 shared_libs: ["libvndk", "libvendor"],
2496 }
2497 cc_library {
2498 name: "libvndk",
2499 vndk: {
2500 enabled: true,
2501 },
2502 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002503 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002504 }
2505 cc_library {
2506 name: "libvendor",
2507 vendor: true,
2508 }
2509 `)
2510
2511 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2512
Colin Crossaede88c2020-08-11 12:17:01 -07002513 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002514 libs := names(ldRule.Args["libFlags"])
2515 // VNDK libs(libvndk/libc++) as they are
2516 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002517 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002518 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002519 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002520
2521 // VNDK libs are not included when use_vndk_as_stable: true
2522 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2523 "bin/mybin",
2524 "lib64/libvendor.so",
2525 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002526
2527 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2528 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2529 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002530}
2531
Justin Yun13decfb2021-03-08 19:25:55 +09002532func TestProductVariant(t *testing.T) {
2533 ctx := testApex(t, `
2534 apex {
2535 name: "myapex",
2536 key: "myapex.key",
2537 updatable: false,
2538 product_specific: true,
2539 binaries: ["foo"],
2540 }
2541
2542 apex_key {
2543 name: "myapex.key",
2544 public_key: "testkey.avbpubkey",
2545 private_key: "testkey.pem",
2546 }
2547
2548 cc_binary {
2549 name: "foo",
2550 product_available: true,
2551 apex_available: ["myapex"],
2552 srcs: ["foo.cpp"],
2553 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002554 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2555 variables.ProductVndkVersion = proptools.StringPtr("current")
2556 }),
2557 )
Justin Yun13decfb2021-03-08 19:25:55 +09002558
2559 cflags := strings.Fields(
2560 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2561 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2562 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2563 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2564 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2565}
2566
Jooyung Han8e5685d2020-09-21 11:02:57 +09002567func TestApex_withPrebuiltFirmware(t *testing.T) {
2568 testCases := []struct {
2569 name string
2570 additionalProp string
2571 }{
2572 {"system apex with prebuilt_firmware", ""},
2573 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2574 }
2575 for _, tc := range testCases {
2576 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002577 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002578 apex {
2579 name: "myapex",
2580 key: "myapex.key",
2581 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002582 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002583 `+tc.additionalProp+`
2584 }
2585 apex_key {
2586 name: "myapex.key",
2587 public_key: "testkey.avbpubkey",
2588 private_key: "testkey.pem",
2589 }
2590 prebuilt_firmware {
2591 name: "myfirmware",
2592 src: "myfirmware.bin",
2593 filename_from_src: true,
2594 `+tc.additionalProp+`
2595 }
2596 `)
2597 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2598 "etc/firmware/myfirmware.bin",
2599 })
2600 })
2601 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002602}
2603
Jooyung Hanefb184e2020-06-25 17:14:25 +09002604func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002605 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002606 apex {
2607 name: "myapex",
2608 key: "myapex.key",
2609 use_vendor: true,
2610 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002611 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002612 }
2613
2614 apex_key {
2615 name: "myapex.key",
2616 public_key: "testkey.avbpubkey",
2617 private_key: "testkey.pem",
2618 }
2619
2620 cc_library {
2621 name: "mylib",
2622 vendor_available: true,
2623 apex_available: ["myapex"],
2624 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002625 `,
2626 setUseVendorAllowListForTest([]string{"myapex"}),
2627 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002628
2629 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002630 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002631 name := apexBundle.BaseModuleName()
2632 prefix := "TARGET_"
2633 var builder strings.Builder
2634 data.Custom(&builder, name, prefix, "", data)
2635 androidMk := builder.String()
2636 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2637}
2638
2639func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002640 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002641 apex {
2642 name: "myapex",
2643 key: "myapex.key",
2644 vendor: true,
2645 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002646 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002647 }
2648
2649 apex_key {
2650 name: "myapex.key",
2651 public_key: "testkey.avbpubkey",
2652 private_key: "testkey.pem",
2653 }
2654
2655 cc_library {
2656 name: "mylib",
2657 vendor_available: true,
2658 }
2659 `)
2660
2661 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002662 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002663 name := apexBundle.BaseModuleName()
2664 prefix := "TARGET_"
2665 var builder strings.Builder
2666 data.Custom(&builder, name, prefix, "", data)
2667 androidMk := builder.String()
2668 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2669}
2670
Jooyung Han2ed99d02020-06-24 23:26:26 +09002671func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002672 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002673 apex {
2674 name: "myapex",
2675 key: "myapex.key",
2676 vintf_fragments: ["fragment.xml"],
2677 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002678 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002679 }
2680 apex_key {
2681 name: "myapex.key",
2682 public_key: "testkey.avbpubkey",
2683 private_key: "testkey.pem",
2684 }
2685 cc_binary {
2686 name: "mybin",
2687 }
2688 `)
2689
2690 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002691 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002692 name := apexBundle.BaseModuleName()
2693 prefix := "TARGET_"
2694 var builder strings.Builder
2695 data.Custom(&builder, name, prefix, "", data)
2696 androidMk := builder.String()
2697 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2698 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2699}
2700
Jiyong Park16e91a02018-12-20 18:18:08 +09002701func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002702 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002703 apex {
2704 name: "myapex",
2705 key: "myapex.key",
2706 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002707 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002708 }
2709
2710 apex_key {
2711 name: "myapex.key",
2712 public_key: "testkey.avbpubkey",
2713 private_key: "testkey.pem",
2714 }
2715
2716 cc_library {
2717 name: "mylib",
2718 srcs: ["mylib.cpp"],
2719 system_shared_libs: [],
2720 stl: "none",
2721 stubs: {
2722 versions: ["1", "2", "3"],
2723 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002724 apex_available: [
2725 "//apex_available:platform",
2726 "myapex",
2727 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002728 }
2729
2730 cc_binary {
2731 name: "not_in_apex",
2732 srcs: ["mylib.cpp"],
2733 static_libs: ["mylib"],
2734 static_executable: true,
2735 system_shared_libs: [],
2736 stl: "none",
2737 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002738 `)
2739
Colin Cross7113d202019-11-20 16:39:12 -08002740 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002741
2742 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002743 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002744}
Jiyong Park9335a262018-12-24 11:31:58 +09002745
2746func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002747 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002748 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002749 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002750 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002751 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002752 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002753 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002754 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002755 }
2756
2757 cc_library {
2758 name: "mylib",
2759 srcs: ["mylib.cpp"],
2760 system_shared_libs: [],
2761 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002762 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002763 }
2764
2765 apex_key {
2766 name: "myapex.key",
2767 public_key: "testkey.avbpubkey",
2768 private_key: "testkey.pem",
2769 }
2770
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002771 android_app_certificate {
2772 name: "myapex.certificate",
2773 certificate: "testkey",
2774 }
2775
2776 android_app_certificate {
2777 name: "myapex.certificate.override",
2778 certificate: "testkey.override",
2779 }
2780
Jiyong Park9335a262018-12-24 11:31:58 +09002781 `)
2782
2783 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002784 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002785
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002786 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2787 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002788 "vendor/foo/devkeys/testkey.avbpubkey")
2789 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002790 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2791 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002792 "vendor/foo/devkeys/testkey.pem")
2793 }
2794
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002795 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002796 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002797 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002798 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002799 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002800 }
2801}
Jiyong Park58e364a2019-01-19 19:24:06 +09002802
Jooyung Hanf121a652019-12-17 14:30:11 +09002803func TestCertificate(t *testing.T) {
2804 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002805 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002806 apex {
2807 name: "myapex",
2808 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002809 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002810 }
2811 apex_key {
2812 name: "myapex.key",
2813 public_key: "testkey.avbpubkey",
2814 private_key: "testkey.pem",
2815 }`)
2816 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2817 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2818 if actual := rule.Args["certificates"]; actual != expected {
2819 t.Errorf("certificates should be %q, not %q", expected, actual)
2820 }
2821 })
2822 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002823 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002824 apex {
2825 name: "myapex_keytest",
2826 key: "myapex.key",
2827 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002828 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002829 }
2830 apex_key {
2831 name: "myapex.key",
2832 public_key: "testkey.avbpubkey",
2833 private_key: "testkey.pem",
2834 }
2835 android_app_certificate {
2836 name: "myapex.certificate.override",
2837 certificate: "testkey.override",
2838 }`)
2839 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2840 expected := "testkey.override.x509.pem testkey.override.pk8"
2841 if actual := rule.Args["certificates"]; actual != expected {
2842 t.Errorf("certificates should be %q, not %q", expected, actual)
2843 }
2844 })
2845 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002846 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002847 apex {
2848 name: "myapex",
2849 key: "myapex.key",
2850 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002851 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002852 }
2853 apex_key {
2854 name: "myapex.key",
2855 public_key: "testkey.avbpubkey",
2856 private_key: "testkey.pem",
2857 }
2858 android_app_certificate {
2859 name: "myapex.certificate",
2860 certificate: "testkey",
2861 }`)
2862 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2863 expected := "testkey.x509.pem testkey.pk8"
2864 if actual := rule.Args["certificates"]; actual != expected {
2865 t.Errorf("certificates should be %q, not %q", expected, actual)
2866 }
2867 })
2868 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002869 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002870 apex {
2871 name: "myapex_keytest",
2872 key: "myapex.key",
2873 file_contexts: ":myapex-file_contexts",
2874 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002875 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002876 }
2877 apex_key {
2878 name: "myapex.key",
2879 public_key: "testkey.avbpubkey",
2880 private_key: "testkey.pem",
2881 }
2882 android_app_certificate {
2883 name: "myapex.certificate.override",
2884 certificate: "testkey.override",
2885 }`)
2886 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2887 expected := "testkey.override.x509.pem testkey.override.pk8"
2888 if actual := rule.Args["certificates"]; actual != expected {
2889 t.Errorf("certificates should be %q, not %q", expected, actual)
2890 }
2891 })
2892 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002893 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002894 apex {
2895 name: "myapex",
2896 key: "myapex.key",
2897 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002898 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002899 }
2900 apex_key {
2901 name: "myapex.key",
2902 public_key: "testkey.avbpubkey",
2903 private_key: "testkey.pem",
2904 }`)
2905 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2906 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2907 if actual := rule.Args["certificates"]; actual != expected {
2908 t.Errorf("certificates should be %q, not %q", expected, actual)
2909 }
2910 })
2911 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002912 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002913 apex {
2914 name: "myapex_keytest",
2915 key: "myapex.key",
2916 file_contexts: ":myapex-file_contexts",
2917 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002918 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002919 }
2920 apex_key {
2921 name: "myapex.key",
2922 public_key: "testkey.avbpubkey",
2923 private_key: "testkey.pem",
2924 }
2925 android_app_certificate {
2926 name: "myapex.certificate.override",
2927 certificate: "testkey.override",
2928 }`)
2929 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2930 expected := "testkey.override.x509.pem testkey.override.pk8"
2931 if actual := rule.Args["certificates"]; actual != expected {
2932 t.Errorf("certificates should be %q, not %q", expected, actual)
2933 }
2934 })
2935}
2936
Jiyong Park58e364a2019-01-19 19:24:06 +09002937func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002938 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002939 apex {
2940 name: "myapex",
2941 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002942 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002943 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002944 }
2945
2946 apex {
2947 name: "otherapex",
2948 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002949 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002950 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002951 }
2952
2953 apex_key {
2954 name: "myapex.key",
2955 public_key: "testkey.avbpubkey",
2956 private_key: "testkey.pem",
2957 }
2958
2959 cc_library {
2960 name: "mylib",
2961 srcs: ["mylib.cpp"],
2962 system_shared_libs: [],
2963 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002964 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002965 "myapex",
2966 "otherapex",
2967 ],
Jooyung Han24282772020-03-21 23:20:55 +09002968 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002969 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002970 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002971 cc_library {
2972 name: "mylib2",
2973 srcs: ["mylib.cpp"],
2974 system_shared_libs: [],
2975 stl: "none",
2976 apex_available: [
2977 "myapex",
2978 "otherapex",
2979 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002980 static_libs: ["mylib3"],
2981 recovery_available: true,
2982 min_sdk_version: "29",
2983 }
2984 cc_library {
2985 name: "mylib3",
2986 srcs: ["mylib.cpp"],
2987 system_shared_libs: [],
2988 stl: "none",
2989 apex_available: [
2990 "myapex",
2991 "otherapex",
2992 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002993 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002994 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002995 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002996 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002997 `)
2998
Jooyung Hanc87a0592020-03-02 17:44:33 +09002999 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003000 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003001 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003002 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003003
Jooyung Hanccce2f22020-03-07 03:45:53 +09003004 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003005 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003006 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003007 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003008 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003009
Jooyung Hanccce2f22020-03-07 03:45:53 +09003010 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003011 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003012 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003013 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003014 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003015
Colin Crossaede88c2020-08-11 12:17:01 -07003016 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3017 // each variant defines additional macros to distinguish which apex variant it is built for
3018
3019 // non-APEX variant does not have __ANDROID_APEX__ defined
3020 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3021 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3022
3023 // APEX variant has __ANDROID_APEX__ defined
3024 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3025 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3026 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3027 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3028
3029 // APEX variant has __ANDROID_APEX__ defined
3030 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3031 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3032 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3033 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3034
Dan Albertb19953d2020-11-17 15:29:36 -08003035 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003036 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3037 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003038 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003039
3040 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3041 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003042
3043 // non-APEX variant does not have __ANDROID_APEX__ defined
3044 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3045 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3046
3047 // APEX variant has __ANDROID_APEX__ defined
3048 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003049 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003050 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003051 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003052
Jooyung Hanc87a0592020-03-02 17:44:33 +09003053 // APEX variant has __ANDROID_APEX__ defined
3054 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003055 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003056 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003057 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003058
Dan Albertb19953d2020-11-17 15:29:36 -08003059 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003060 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003061 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003062 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003063}
Jiyong Park7e636d02019-01-28 16:16:54 +09003064
3065func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003066 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003067 apex {
3068 name: "myapex",
3069 key: "myapex.key",
3070 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003071 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003072 }
3073
3074 apex_key {
3075 name: "myapex.key",
3076 public_key: "testkey.avbpubkey",
3077 private_key: "testkey.pem",
3078 }
3079
3080 cc_library_headers {
3081 name: "mylib_headers",
3082 export_include_dirs: ["my_include"],
3083 system_shared_libs: [],
3084 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003085 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003086 }
3087
3088 cc_library {
3089 name: "mylib",
3090 srcs: ["mylib.cpp"],
3091 system_shared_libs: [],
3092 stl: "none",
3093 header_libs: ["mylib_headers"],
3094 export_header_lib_headers: ["mylib_headers"],
3095 stubs: {
3096 versions: ["1", "2", "3"],
3097 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003098 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003099 }
3100
3101 cc_library {
3102 name: "otherlib",
3103 srcs: ["mylib.cpp"],
3104 system_shared_libs: [],
3105 stl: "none",
3106 shared_libs: ["mylib"],
3107 }
3108 `)
3109
Colin Cross7113d202019-11-20 16:39:12 -08003110 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003111
3112 // Ensure that the include path of the header lib is exported to 'otherlib'
3113 ensureContains(t, cFlags, "-Imy_include")
3114}
Alex Light9670d332019-01-29 18:07:33 -08003115
Jiyong Park7cd10e32020-01-14 09:22:18 +09003116type fileInApex struct {
3117 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003118 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003119 isLink bool
3120}
3121
Jooyung Hana57af4a2020-01-23 05:36:59 +00003122func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003123 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003124 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003125 copyCmds := apexRule.Args["copy_commands"]
3126 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003127 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003128 for _, cmd := range strings.Split(copyCmds, "&&") {
3129 cmd = strings.TrimSpace(cmd)
3130 if cmd == "" {
3131 continue
3132 }
3133 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003134 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003135 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003136 switch terms[0] {
3137 case "mkdir":
3138 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003139 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003140 t.Fatal("copyCmds contains invalid cp command", cmd)
3141 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003142 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003143 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003144 isLink = false
3145 case "ln":
3146 if len(terms) != 3 && len(terms) != 4 {
3147 // ln LINK TARGET or ln -s LINK TARGET
3148 t.Fatal("copyCmds contains invalid ln command", cmd)
3149 }
3150 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003151 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003152 isLink = true
3153 default:
3154 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3155 }
3156 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003157 index := strings.Index(dst, imageApexDir)
3158 if index == -1 {
3159 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3160 }
3161 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003162 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003163 }
3164 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003165 return ret
3166}
3167
Jooyung Hana57af4a2020-01-23 05:36:59 +00003168func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3169 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003170 var failed bool
3171 var surplus []string
3172 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003173 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003174 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003175 for _, expected := range files {
3176 if matched, _ := path.Match(expected, file.path); matched {
3177 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003178 mactchFound = true
3179 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003180 }
3181 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003182 if !mactchFound {
3183 surplus = append(surplus, file.path)
3184 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003185 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003186
Jooyung Han31c470b2019-10-18 16:26:59 +09003187 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003188 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 t.Log("surplus files", surplus)
3190 failed = true
3191 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003192
3193 if len(files) > len(filesMatched) {
3194 var missing []string
3195 for _, expected := range files {
3196 if !filesMatched[expected] {
3197 missing = append(missing, expected)
3198 }
3199 }
3200 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003201 t.Log("missing files", missing)
3202 failed = true
3203 }
3204 if failed {
3205 t.Fail()
3206 }
3207}
3208
Jooyung Han344d5432019-08-23 11:17:39 +09003209func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003210 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003211 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003212 name: "com.android.vndk.current",
3213 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003214 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003215 }
3216
3217 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003218 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003219 public_key: "testkey.avbpubkey",
3220 private_key: "testkey.pem",
3221 }
3222
3223 cc_library {
3224 name: "libvndk",
3225 srcs: ["mylib.cpp"],
3226 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003227 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003228 vndk: {
3229 enabled: true,
3230 },
3231 system_shared_libs: [],
3232 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003233 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003234 }
3235
3236 cc_library {
3237 name: "libvndksp",
3238 srcs: ["mylib.cpp"],
3239 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003240 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003241 vndk: {
3242 enabled: true,
3243 support_system_process: true,
3244 },
3245 system_shared_libs: [],
3246 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003247 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003248 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003249 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003250
Colin Cross2807f002021-03-02 10:15:29 -08003251 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003252 "lib/libvndk.so",
3253 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003254 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003255 "lib64/libvndk.so",
3256 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003257 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003258 "etc/llndk.libraries.VER.txt",
3259 "etc/vndkcore.libraries.VER.txt",
3260 "etc/vndksp.libraries.VER.txt",
3261 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003262 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003263 })
Jooyung Han344d5432019-08-23 11:17:39 +09003264}
3265
3266func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003267 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003268 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003269 name: "com.android.vndk.current",
3270 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003271 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003272 }
3273
3274 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003275 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003276 public_key: "testkey.avbpubkey",
3277 private_key: "testkey.pem",
3278 }
3279
3280 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003281 name: "libvndk",
3282 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003283 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003284 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003285 vndk: {
3286 enabled: true,
3287 },
3288 system_shared_libs: [],
3289 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003290 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003291 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003292
3293 cc_prebuilt_library_shared {
3294 name: "libvndk.arm",
3295 srcs: ["libvndk.arm.so"],
3296 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003297 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003298 vndk: {
3299 enabled: true,
3300 },
3301 enabled: false,
3302 arch: {
3303 arm: {
3304 enabled: true,
3305 },
3306 },
3307 system_shared_libs: [],
3308 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003309 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003310 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003311 `+vndkLibrariesTxtFiles("current"),
3312 withFiles(map[string][]byte{
3313 "libvndk.so": nil,
3314 "libvndk.arm.so": nil,
3315 }))
Colin Cross2807f002021-03-02 10:15:29 -08003316 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003317 "lib/libvndk.so",
3318 "lib/libvndk.arm.so",
3319 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003320 "lib/libc++.so",
3321 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003322 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003323 })
Jooyung Han344d5432019-08-23 11:17:39 +09003324}
3325
Jooyung Han39edb6c2019-11-06 16:53:07 +09003326func vndkLibrariesTxtFiles(vers ...string) (result string) {
3327 for _, v := range vers {
3328 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003329 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003330 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003331 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003332 name: "` + txt + `.libraries.txt",
3333 }
3334 `
3335 }
3336 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003337 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003338 result += `
3339 prebuilt_etc {
3340 name: "` + txt + `.libraries.` + v + `.txt",
3341 src: "dummy.txt",
3342 }
3343 `
3344 }
3345 }
3346 }
3347 return
3348}
3349
Jooyung Han344d5432019-08-23 11:17:39 +09003350func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003351 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003352 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003353 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003354 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003355 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003356 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003357 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003358 }
3359
3360 apex_key {
3361 name: "myapex.key",
3362 public_key: "testkey.avbpubkey",
3363 private_key: "testkey.pem",
3364 }
3365
Jooyung Han31c470b2019-10-18 16:26:59 +09003366 vndk_prebuilt_shared {
3367 name: "libvndk27",
3368 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003369 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003370 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003371 vndk: {
3372 enabled: true,
3373 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003374 target_arch: "arm64",
3375 arch: {
3376 arm: {
3377 srcs: ["libvndk27_arm.so"],
3378 },
3379 arm64: {
3380 srcs: ["libvndk27_arm64.so"],
3381 },
3382 },
Colin Cross2807f002021-03-02 10:15:29 -08003383 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003384 }
3385
3386 vndk_prebuilt_shared {
3387 name: "libvndk27",
3388 version: "27",
3389 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003390 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003391 vndk: {
3392 enabled: true,
3393 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003394 target_arch: "x86_64",
3395 arch: {
3396 x86: {
3397 srcs: ["libvndk27_x86.so"],
3398 },
3399 x86_64: {
3400 srcs: ["libvndk27_x86_64.so"],
3401 },
3402 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003403 }
3404 `+vndkLibrariesTxtFiles("27"),
3405 withFiles(map[string][]byte{
3406 "libvndk27_arm.so": nil,
3407 "libvndk27_arm64.so": nil,
3408 "libvndk27_x86.so": nil,
3409 "libvndk27_x86_64.so": nil,
3410 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003411
Colin Cross2807f002021-03-02 10:15:29 -08003412 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003413 "lib/libvndk27_arm.so",
3414 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003415 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003416 })
Jooyung Han344d5432019-08-23 11:17:39 +09003417}
3418
Jooyung Han90eee022019-10-01 20:02:42 +09003419func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003420 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003421 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003422 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003423 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003424 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003425 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003426 }
3427 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003428 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003429 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003430 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003431 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003432 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003433 }
3434 apex_key {
3435 name: "myapex.key",
3436 public_key: "testkey.avbpubkey",
3437 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003438 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003439
3440 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003441 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003442 actual := proptools.String(bundle.properties.Apex_name)
3443 if !reflect.DeepEqual(actual, expected) {
3444 t.Errorf("Got '%v', expected '%v'", actual, expected)
3445 }
3446 }
3447
Colin Cross2807f002021-03-02 10:15:29 -08003448 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3449 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003450}
3451
Jooyung Han344d5432019-08-23 11:17:39 +09003452func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003453 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003454 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003455 name: "com.android.vndk.current",
3456 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003457 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003458 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003459 }
3460
3461 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003462 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003463 public_key: "testkey.avbpubkey",
3464 private_key: "testkey.pem",
3465 }
3466
3467 cc_library {
3468 name: "libvndk",
3469 srcs: ["mylib.cpp"],
3470 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003471 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003472 native_bridge_supported: true,
3473 host_supported: true,
3474 vndk: {
3475 enabled: true,
3476 },
3477 system_shared_libs: [],
3478 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003479 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003480 }
Colin Cross2807f002021-03-02 10:15:29 -08003481 `+vndkLibrariesTxtFiles("current"),
3482 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003483
Colin Cross2807f002021-03-02 10:15:29 -08003484 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003485 "lib/libvndk.so",
3486 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003487 "lib/libc++.so",
3488 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003489 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003490 })
Jooyung Han344d5432019-08-23 11:17:39 +09003491}
3492
3493func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003494 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003495 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003496 name: "com.android.vndk.current",
3497 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003498 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003499 native_bridge_supported: true,
3500 }
3501
3502 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003503 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003504 public_key: "testkey.avbpubkey",
3505 private_key: "testkey.pem",
3506 }
3507
3508 cc_library {
3509 name: "libvndk",
3510 srcs: ["mylib.cpp"],
3511 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003512 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003513 native_bridge_supported: true,
3514 host_supported: true,
3515 vndk: {
3516 enabled: true,
3517 },
3518 system_shared_libs: [],
3519 stl: "none",
3520 }
3521 `)
3522}
3523
Jooyung Han31c470b2019-10-18 16:26:59 +09003524func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003525 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003526 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003527 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003528 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003529 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003530 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003531 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003532 }
3533
3534 apex_key {
3535 name: "myapex.key",
3536 public_key: "testkey.avbpubkey",
3537 private_key: "testkey.pem",
3538 }
3539
3540 vndk_prebuilt_shared {
3541 name: "libvndk27",
3542 version: "27",
3543 target_arch: "arm",
3544 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003545 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003546 vndk: {
3547 enabled: true,
3548 },
3549 arch: {
3550 arm: {
3551 srcs: ["libvndk27.so"],
3552 }
3553 },
3554 }
3555
3556 vndk_prebuilt_shared {
3557 name: "libvndk27",
3558 version: "27",
3559 target_arch: "arm",
3560 binder32bit: true,
3561 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003562 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003563 vndk: {
3564 enabled: true,
3565 },
3566 arch: {
3567 arm: {
3568 srcs: ["libvndk27binder32.so"],
3569 }
3570 },
Colin Cross2807f002021-03-02 10:15:29 -08003571 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003572 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003573 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003574 withFiles(map[string][]byte{
3575 "libvndk27.so": nil,
3576 "libvndk27binder32.so": nil,
3577 }),
3578 withBinder32bit,
3579 withTargets(map[android.OsType][]android.Target{
3580 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003581 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3582 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003583 },
3584 }),
3585 )
3586
Colin Cross2807f002021-03-02 10:15:29 -08003587 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003588 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003589 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003590 })
3591}
3592
Jooyung Han45a96772020-06-15 14:59:42 +09003593func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003594 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003595 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003596 name: "com.android.vndk.current",
3597 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003598 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003599 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003600 }
3601
3602 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003603 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003604 public_key: "testkey.avbpubkey",
3605 private_key: "testkey.pem",
3606 }
3607
3608 cc_library {
3609 name: "libz",
3610 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003611 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003612 vndk: {
3613 enabled: true,
3614 },
3615 stubs: {
3616 symbol_file: "libz.map.txt",
3617 versions: ["30"],
3618 }
3619 }
3620 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3621 "libz.map.txt": nil,
3622 }))
3623
Colin Cross2807f002021-03-02 10:15:29 -08003624 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003625 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3626 ensureListEmpty(t, provideNativeLibs)
3627}
3628
Jooyung Hane1633032019-08-01 17:41:43 +09003629func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003630 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003631 apex {
3632 name: "myapex_nodep",
3633 key: "myapex.key",
3634 native_shared_libs: ["lib_nodep"],
3635 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003636 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003637 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003638 }
3639
3640 apex {
3641 name: "myapex_dep",
3642 key: "myapex.key",
3643 native_shared_libs: ["lib_dep"],
3644 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003645 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003646 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003647 }
3648
3649 apex {
3650 name: "myapex_provider",
3651 key: "myapex.key",
3652 native_shared_libs: ["libfoo"],
3653 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003654 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003655 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003656 }
3657
3658 apex {
3659 name: "myapex_selfcontained",
3660 key: "myapex.key",
3661 native_shared_libs: ["lib_dep", "libfoo"],
3662 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003663 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003664 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003665 }
3666
3667 apex_key {
3668 name: "myapex.key",
3669 public_key: "testkey.avbpubkey",
3670 private_key: "testkey.pem",
3671 }
3672
3673 cc_library {
3674 name: "lib_nodep",
3675 srcs: ["mylib.cpp"],
3676 system_shared_libs: [],
3677 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003678 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003679 }
3680
3681 cc_library {
3682 name: "lib_dep",
3683 srcs: ["mylib.cpp"],
3684 shared_libs: ["libfoo"],
3685 system_shared_libs: [],
3686 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003687 apex_available: [
3688 "myapex_dep",
3689 "myapex_provider",
3690 "myapex_selfcontained",
3691 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003692 }
3693
3694 cc_library {
3695 name: "libfoo",
3696 srcs: ["mytest.cpp"],
3697 stubs: {
3698 versions: ["1"],
3699 },
3700 system_shared_libs: [],
3701 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003702 apex_available: [
3703 "myapex_provider",
3704 "myapex_selfcontained",
3705 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003706 }
3707 `)
3708
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003709 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003710 var provideNativeLibs, requireNativeLibs []string
3711
Sundong Ahnabb64432019-10-22 13:58:29 +09003712 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003713 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3714 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003715 ensureListEmpty(t, provideNativeLibs)
3716 ensureListEmpty(t, requireNativeLibs)
3717
Sundong Ahnabb64432019-10-22 13:58:29 +09003718 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003719 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3720 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003721 ensureListEmpty(t, provideNativeLibs)
3722 ensureListContains(t, requireNativeLibs, "libfoo.so")
3723
Sundong Ahnabb64432019-10-22 13:58:29 +09003724 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003725 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3726 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003727 ensureListContains(t, provideNativeLibs, "libfoo.so")
3728 ensureListEmpty(t, requireNativeLibs)
3729
Sundong Ahnabb64432019-10-22 13:58:29 +09003730 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003731 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3732 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003733 ensureListContains(t, provideNativeLibs, "libfoo.so")
3734 ensureListEmpty(t, requireNativeLibs)
3735}
3736
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003737func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003738 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003739 apex {
3740 name: "myapex",
3741 key: "myapex.key",
3742 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003743 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003744 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003745 }
3746
3747 apex_key {
3748 name: "myapex.key",
3749 public_key: "testkey.avbpubkey",
3750 private_key: "testkey.pem",
3751 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003752
3753 cc_library {
3754 name: "mylib",
3755 srcs: ["mylib.cpp"],
3756 system_shared_libs: [],
3757 stl: "none",
3758 apex_available: [
3759 "//apex_available:platform",
3760 "myapex",
3761 ],
3762 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003763 `)
3764
Sundong Ahnabb64432019-10-22 13:58:29 +09003765 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003766 apexManifestRule := module.Rule("apexManifestRule")
3767 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3768 apexRule := module.Rule("apexRule")
3769 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003770
3771 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003772 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003773 name := apexBundle.BaseModuleName()
3774 prefix := "TARGET_"
3775 var builder strings.Builder
3776 data.Custom(&builder, name, prefix, "", data)
3777 androidMk := builder.String()
3778 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3779 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003780}
3781
Alex Light0851b882019-02-07 13:20:53 -08003782func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003783 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003784 apex {
3785 name: "myapex",
3786 key: "myapex.key",
3787 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003788 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003789 }
3790
3791 apex_key {
3792 name: "myapex.key",
3793 public_key: "testkey.avbpubkey",
3794 private_key: "testkey.pem",
3795 }
3796
3797 cc_library {
3798 name: "mylib_common",
3799 srcs: ["mylib.cpp"],
3800 system_shared_libs: [],
3801 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003802 apex_available: [
3803 "//apex_available:platform",
3804 "myapex",
3805 ],
Alex Light0851b882019-02-07 13:20:53 -08003806 }
3807 `)
3808
Sundong Ahnabb64432019-10-22 13:58:29 +09003809 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003810 apexRule := module.Rule("apexRule")
3811 copyCmds := apexRule.Args["copy_commands"]
3812
3813 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3814 t.Log("Apex was a test apex!")
3815 t.Fail()
3816 }
3817 // Ensure that main rule creates an output
3818 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3819
3820 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003821 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003822
3823 // Ensure that both direct and indirect deps are copied into apex
3824 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3825
Colin Cross7113d202019-11-20 16:39:12 -08003826 // Ensure that the platform variant ends with _shared
3827 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003828
Colin Cross56a83212020-09-15 18:30:11 -07003829 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003830 t.Log("Found mylib_common not in any apex!")
3831 t.Fail()
3832 }
3833}
3834
3835func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003836 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003837 apex_test {
3838 name: "myapex",
3839 key: "myapex.key",
3840 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003841 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003842 }
3843
3844 apex_key {
3845 name: "myapex.key",
3846 public_key: "testkey.avbpubkey",
3847 private_key: "testkey.pem",
3848 }
3849
3850 cc_library {
3851 name: "mylib_common_test",
3852 srcs: ["mylib.cpp"],
3853 system_shared_libs: [],
3854 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003855 // TODO: remove //apex_available:platform
3856 apex_available: [
3857 "//apex_available:platform",
3858 "myapex",
3859 ],
Alex Light0851b882019-02-07 13:20:53 -08003860 }
3861 `)
3862
Sundong Ahnabb64432019-10-22 13:58:29 +09003863 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003864 apexRule := module.Rule("apexRule")
3865 copyCmds := apexRule.Args["copy_commands"]
3866
3867 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3868 t.Log("Apex was not a test apex!")
3869 t.Fail()
3870 }
3871 // Ensure that main rule creates an output
3872 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3873
3874 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003875 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003876
3877 // Ensure that both direct and indirect deps are copied into apex
3878 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3879
Colin Cross7113d202019-11-20 16:39:12 -08003880 // Ensure that the platform variant ends with _shared
3881 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003882}
3883
Alex Light9670d332019-01-29 18:07:33 -08003884func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003885 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003886 apex {
3887 name: "myapex",
3888 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003889 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003890 multilib: {
3891 first: {
3892 native_shared_libs: ["mylib_common"],
3893 }
3894 },
3895 target: {
3896 android: {
3897 multilib: {
3898 first: {
3899 native_shared_libs: ["mylib"],
3900 }
3901 }
3902 },
3903 host: {
3904 multilib: {
3905 first: {
3906 native_shared_libs: ["mylib2"],
3907 }
3908 }
3909 }
3910 }
3911 }
3912
3913 apex_key {
3914 name: "myapex.key",
3915 public_key: "testkey.avbpubkey",
3916 private_key: "testkey.pem",
3917 }
3918
3919 cc_library {
3920 name: "mylib",
3921 srcs: ["mylib.cpp"],
3922 system_shared_libs: [],
3923 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003924 // TODO: remove //apex_available:platform
3925 apex_available: [
3926 "//apex_available:platform",
3927 "myapex",
3928 ],
Alex Light9670d332019-01-29 18:07:33 -08003929 }
3930
3931 cc_library {
3932 name: "mylib_common",
3933 srcs: ["mylib.cpp"],
3934 system_shared_libs: [],
3935 stl: "none",
3936 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003937 // TODO: remove //apex_available:platform
3938 apex_available: [
3939 "//apex_available:platform",
3940 "myapex",
3941 ],
Alex Light9670d332019-01-29 18:07:33 -08003942 }
3943
3944 cc_library {
3945 name: "mylib2",
3946 srcs: ["mylib.cpp"],
3947 system_shared_libs: [],
3948 stl: "none",
3949 compile_multilib: "first",
3950 }
3951 `)
3952
Sundong Ahnabb64432019-10-22 13:58:29 +09003953 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003954 copyCmds := apexRule.Args["copy_commands"]
3955
3956 // Ensure that main rule creates an output
3957 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3958
3959 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003960 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3961 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3962 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003963
3964 // Ensure that both direct and indirect deps are copied into apex
3965 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3966 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3967 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3968
Colin Cross7113d202019-11-20 16:39:12 -08003969 // Ensure that the platform variant ends with _shared
3970 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3971 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3972 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003973}
Jiyong Park04480cf2019-02-06 00:16:29 +09003974
Jiyong Park59140302020-12-14 18:44:04 +09003975func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003976 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003977 apex {
3978 name: "myapex",
3979 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003980 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003981 arch: {
3982 arm64: {
3983 native_shared_libs: ["mylib.arm64"],
3984 },
3985 x86_64: {
3986 native_shared_libs: ["mylib.x64"],
3987 },
3988 }
3989 }
3990
3991 apex_key {
3992 name: "myapex.key",
3993 public_key: "testkey.avbpubkey",
3994 private_key: "testkey.pem",
3995 }
3996
3997 cc_library {
3998 name: "mylib.arm64",
3999 srcs: ["mylib.cpp"],
4000 system_shared_libs: [],
4001 stl: "none",
4002 // TODO: remove //apex_available:platform
4003 apex_available: [
4004 "//apex_available:platform",
4005 "myapex",
4006 ],
4007 }
4008
4009 cc_library {
4010 name: "mylib.x64",
4011 srcs: ["mylib.cpp"],
4012 system_shared_libs: [],
4013 stl: "none",
4014 // TODO: remove //apex_available:platform
4015 apex_available: [
4016 "//apex_available:platform",
4017 "myapex",
4018 ],
4019 }
4020 `)
4021
4022 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4023 copyCmds := apexRule.Args["copy_commands"]
4024
4025 // Ensure that apex variant is created for the direct dep
4026 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4027 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4028
4029 // Ensure that both direct and indirect deps are copied into apex
4030 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4031 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4032}
4033
Jiyong Park04480cf2019-02-06 00:16:29 +09004034func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004035 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004036 apex {
4037 name: "myapex",
4038 key: "myapex.key",
4039 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004040 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004041 }
4042
4043 apex_key {
4044 name: "myapex.key",
4045 public_key: "testkey.avbpubkey",
4046 private_key: "testkey.pem",
4047 }
4048
4049 sh_binary {
4050 name: "myscript",
4051 src: "mylib.cpp",
4052 filename: "myscript.sh",
4053 sub_dir: "script",
4054 }
4055 `)
4056
Sundong Ahnabb64432019-10-22 13:58:29 +09004057 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004058 copyCmds := apexRule.Args["copy_commands"]
4059
4060 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4061}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004062
Jooyung Han91df2082019-11-20 01:49:42 +09004063func TestApexInVariousPartition(t *testing.T) {
4064 testcases := []struct {
4065 propName, parition, flattenedPartition string
4066 }{
4067 {"", "system", "system_ext"},
4068 {"product_specific: true", "product", "product"},
4069 {"soc_specific: true", "vendor", "vendor"},
4070 {"proprietary: true", "vendor", "vendor"},
4071 {"vendor: true", "vendor", "vendor"},
4072 {"system_ext_specific: true", "system_ext", "system_ext"},
4073 }
4074 for _, tc := range testcases {
4075 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004076 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004077 apex {
4078 name: "myapex",
4079 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004080 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004081 `+tc.propName+`
4082 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004083
Jooyung Han91df2082019-11-20 01:49:42 +09004084 apex_key {
4085 name: "myapex.key",
4086 public_key: "testkey.avbpubkey",
4087 private_key: "testkey.pem",
4088 }
4089 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004090
Jooyung Han91df2082019-11-20 01:49:42 +09004091 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4092 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4093 actual := apex.installDir.String()
4094 if actual != expected {
4095 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4096 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004097
Jooyung Han91df2082019-11-20 01:49:42 +09004098 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4099 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4100 actual = flattened.installDir.String()
4101 if actual != expected {
4102 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4103 }
4104 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004105 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004106}
Jiyong Park67882562019-03-21 01:11:21 +09004107
Jooyung Han580eb4f2020-06-24 19:33:06 +09004108func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004109 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004110 apex {
4111 name: "myapex",
4112 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004113 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004114 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004115
Jooyung Han580eb4f2020-06-24 19:33:06 +09004116 apex_key {
4117 name: "myapex.key",
4118 public_key: "testkey.avbpubkey",
4119 private_key: "testkey.pem",
4120 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004121 `)
4122 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004123 rule := module.Output("file_contexts")
4124 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4125}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004126
Jooyung Han580eb4f2020-06-24 19:33:06 +09004127func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004128 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004129 apex {
4130 name: "myapex",
4131 key: "myapex.key",
4132 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004133 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004134 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004135
Jooyung Han580eb4f2020-06-24 19:33:06 +09004136 apex_key {
4137 name: "myapex.key",
4138 public_key: "testkey.avbpubkey",
4139 private_key: "testkey.pem",
4140 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004141 `, withFiles(map[string][]byte{
4142 "my_own_file_contexts": nil,
4143 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004144}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004145
Jooyung Han580eb4f2020-06-24 19:33:06 +09004146func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004147 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004148 apex {
4149 name: "myapex",
4150 key: "myapex.key",
4151 product_specific: true,
4152 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004153 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004154 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004155
Jooyung Han580eb4f2020-06-24 19:33:06 +09004156 apex_key {
4157 name: "myapex.key",
4158 public_key: "testkey.avbpubkey",
4159 private_key: "testkey.pem",
4160 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004161 `)
4162
Colin Cross1c460562021-02-16 17:55:47 -08004163 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004164 apex {
4165 name: "myapex",
4166 key: "myapex.key",
4167 product_specific: true,
4168 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004169 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004170 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004171
Jooyung Han580eb4f2020-06-24 19:33:06 +09004172 apex_key {
4173 name: "myapex.key",
4174 public_key: "testkey.avbpubkey",
4175 private_key: "testkey.pem",
4176 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004177 `, withFiles(map[string][]byte{
4178 "product_specific_file_contexts": nil,
4179 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004180 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4181 rule := module.Output("file_contexts")
4182 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4183}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004184
Jooyung Han580eb4f2020-06-24 19:33:06 +09004185func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004186 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004187 apex {
4188 name: "myapex",
4189 key: "myapex.key",
4190 product_specific: true,
4191 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004192 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004193 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004194
Jooyung Han580eb4f2020-06-24 19:33:06 +09004195 apex_key {
4196 name: "myapex.key",
4197 public_key: "testkey.avbpubkey",
4198 private_key: "testkey.pem",
4199 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004200
Jooyung Han580eb4f2020-06-24 19:33:06 +09004201 filegroup {
4202 name: "my-file-contexts",
4203 srcs: ["product_specific_file_contexts"],
4204 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004205 `, withFiles(map[string][]byte{
4206 "product_specific_file_contexts": nil,
4207 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004208 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4209 rule := module.Output("file_contexts")
4210 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004211}
4212
Jiyong Park67882562019-03-21 01:11:21 +09004213func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004214 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004215 apex_key {
4216 name: "myapex.key",
4217 public_key: ":my.avbpubkey",
4218 private_key: ":my.pem",
4219 product_specific: true,
4220 }
4221
4222 filegroup {
4223 name: "my.avbpubkey",
4224 srcs: ["testkey2.avbpubkey"],
4225 }
4226
4227 filegroup {
4228 name: "my.pem",
4229 srcs: ["testkey2.pem"],
4230 }
4231 `)
4232
4233 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4234 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004235 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004236 if actual_pubkey != expected_pubkey {
4237 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4238 }
4239 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004240 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004241 if actual_privkey != expected_privkey {
4242 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4243 }
4244}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004245
4246func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004247 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004248 prebuilt_apex {
4249 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004250 arch: {
4251 arm64: {
4252 src: "myapex-arm64.apex",
4253 },
4254 arm: {
4255 src: "myapex-arm.apex",
4256 },
4257 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004258 }
4259 `)
4260
4261 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4262
Jiyong Parkc95714e2019-03-29 14:23:10 +09004263 expectedInput := "myapex-arm64.apex"
4264 if prebuilt.inputApex.String() != expectedInput {
4265 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4266 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004267}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004268
4269func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004270 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004271 prebuilt_apex {
4272 name: "myapex",
4273 src: "myapex-arm.apex",
4274 filename: "notmyapex.apex",
4275 }
4276 `)
4277
4278 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4279
4280 expected := "notmyapex.apex"
4281 if p.installFilename != expected {
4282 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4283 }
4284}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004285
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004286func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004287 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004288 prebuilt_apex {
4289 name: "myapex.prebuilt",
4290 src: "myapex-arm.apex",
4291 overrides: [
4292 "myapex",
4293 ],
4294 }
4295 `)
4296
4297 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4298
4299 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004300 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004301 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004302 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004303 }
4304}
4305
Paul Duffin092153d2021-01-26 11:42:39 +00004306// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4307// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004308func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4309 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004310 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004311 }
4312
Paul Duffin89886cb2021-02-05 16:44:03 +00004313 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004314 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004315 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004316 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004317 stem := android.RemoveOptionalPrebuiltPrefix(name)
4318 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004319 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4320 }
4321 }
4322
Paul Duffin39853512021-02-26 11:09:39 +00004323 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004324 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004325 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004326 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4327 }
4328 }
4329
4330 t.Run("prebuilt only", func(t *testing.T) {
4331 bp := `
4332 prebuilt_apex {
4333 name: "myapex",
4334 arch: {
4335 arm64: {
4336 src: "myapex-arm64.apex",
4337 },
4338 arm: {
4339 src: "myapex-arm.apex",
4340 },
4341 },
Paul Duffin39853512021-02-26 11:09:39 +00004342 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004343 }
4344
4345 java_import {
4346 name: "libfoo",
4347 jars: ["libfoo.jar"],
4348 }
Paul Duffin39853512021-02-26 11:09:39 +00004349
4350 java_sdk_library_import {
4351 name: "libbar",
4352 public: {
4353 jars: ["libbar.jar"],
4354 },
4355 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004356 `
4357
4358 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4359 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4360
Paul Duffinf6932af2021-02-26 18:21:56 +00004361 // Make sure that the deapexer has the correct input APEX.
4362 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4363 rule := deapexer.Rule("deapexer")
4364 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4365 t.Errorf("expected: %q, found: %q", expected, actual)
4366 }
4367
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004368 // Make sure that the prebuilt_apex has the correct input APEX.
4369 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4370 rule = prebuiltApex.Rule("android/soong/android.Cp")
4371 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4372 t.Errorf("expected: %q, found: %q", expected, actual)
4373 }
4374
Paul Duffin89886cb2021-02-05 16:44:03 +00004375 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004376
4377 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004378 })
4379
4380 t.Run("prebuilt with source preferred", func(t *testing.T) {
4381
4382 bp := `
4383 prebuilt_apex {
4384 name: "myapex",
4385 arch: {
4386 arm64: {
4387 src: "myapex-arm64.apex",
4388 },
4389 arm: {
4390 src: "myapex-arm.apex",
4391 },
4392 },
Paul Duffin39853512021-02-26 11:09:39 +00004393 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004394 }
4395
4396 java_import {
4397 name: "libfoo",
4398 jars: ["libfoo.jar"],
4399 }
4400
4401 java_library {
4402 name: "libfoo",
4403 }
Paul Duffin39853512021-02-26 11:09:39 +00004404
4405 java_sdk_library_import {
4406 name: "libbar",
4407 public: {
4408 jars: ["libbar.jar"],
4409 },
4410 }
4411
4412 java_sdk_library {
4413 name: "libbar",
4414 srcs: ["foo/bar/MyClass.java"],
4415 unsafe_ignore_missing_latest_api: true,
4416 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004417 `
4418
4419 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4420 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4421
Paul Duffin89886cb2021-02-05 16:44:03 +00004422 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004423 ensureNoSourceVariant(t, ctx, "libfoo")
4424
4425 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4426 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004427 })
4428
4429 t.Run("prebuilt preferred with source", func(t *testing.T) {
4430 bp := `
4431 prebuilt_apex {
4432 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004433 arch: {
4434 arm64: {
4435 src: "myapex-arm64.apex",
4436 },
4437 arm: {
4438 src: "myapex-arm.apex",
4439 },
4440 },
Paul Duffin39853512021-02-26 11:09:39 +00004441 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004442 }
4443
4444 java_import {
4445 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004446 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004447 jars: ["libfoo.jar"],
4448 }
4449
4450 java_library {
4451 name: "libfoo",
4452 }
Paul Duffin39853512021-02-26 11:09:39 +00004453
4454 java_sdk_library_import {
4455 name: "libbar",
4456 prefer: true,
4457 public: {
4458 jars: ["libbar.jar"],
4459 },
4460 }
4461
4462 java_sdk_library {
4463 name: "libbar",
4464 srcs: ["foo/bar/MyClass.java"],
4465 unsafe_ignore_missing_latest_api: true,
4466 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004467 `
4468
4469 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4470 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4471
Paul Duffin89886cb2021-02-05 16:44:03 +00004472 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004473 ensureNoSourceVariant(t, ctx, "libfoo")
4474
4475 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4476 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004477 })
4478}
4479
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004480func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4481 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004482 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004483 }
4484
Paul Duffin37856732021-02-26 14:24:15 +00004485 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4486 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004487 s := ctx.SingletonForTests("dex_bootjars")
4488 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004489 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004490 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004491 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004492 foundLibfooJar = true
4493 buildRule := s.Output(output)
4494 actual := android.NormalizePathForTesting(buildRule.Input)
4495 if actual != bootDexJarPath {
4496 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4497 }
4498 }
4499 }
4500 if !foundLibfooJar {
4501 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4502 }
4503 }
4504
Paul Duffin4fd997b2021-02-03 20:06:33 +00004505 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004506 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004507 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4508 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4509 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4510 }
4511
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004512 t.Run("prebuilt only", func(t *testing.T) {
4513 bp := `
4514 prebuilt_apex {
4515 name: "myapex",
4516 arch: {
4517 arm64: {
4518 src: "myapex-arm64.apex",
4519 },
4520 arm: {
4521 src: "myapex-arm.apex",
4522 },
4523 },
Paul Duffin37856732021-02-26 14:24:15 +00004524 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004525 }
4526
4527 java_import {
4528 name: "libfoo",
4529 jars: ["libfoo.jar"],
4530 apex_available: ["myapex"],
4531 }
Paul Duffin37856732021-02-26 14:24:15 +00004532
4533 java_sdk_library_import {
4534 name: "libbar",
4535 public: {
4536 jars: ["libbar.jar"],
4537 },
4538 apex_available: ["myapex"],
4539 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004540 `
4541
4542 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004543 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4544 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004545
Paul Duffin9d67ca62021-02-03 20:06:33 +00004546 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4547 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004548.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004549.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4550`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004551 })
4552
4553 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4554 bp := `
4555 prebuilt_apex {
4556 name: "myapex",
4557 arch: {
4558 arm64: {
4559 src: "myapex-arm64.apex",
4560 },
4561 arm: {
4562 src: "myapex-arm.apex",
4563 },
4564 },
Paul Duffin37856732021-02-26 14:24:15 +00004565 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004566 }
4567
4568 java_import {
4569 name: "libfoo",
4570 jars: ["libfoo.jar"],
4571 apex_available: ["myapex"],
4572 }
4573
4574 java_library {
4575 name: "libfoo",
4576 srcs: ["foo/bar/MyClass.java"],
4577 apex_available: ["myapex"],
4578 }
Paul Duffin37856732021-02-26 14:24:15 +00004579
4580 java_sdk_library_import {
4581 name: "libbar",
4582 public: {
4583 jars: ["libbar.jar"],
4584 },
4585 apex_available: ["myapex"],
4586 }
4587
4588 java_sdk_library {
4589 name: "libbar",
4590 srcs: ["foo/bar/MyClass.java"],
4591 unsafe_ignore_missing_latest_api: true,
4592 apex_available: ["myapex"],
4593 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004594 `
4595
4596 // In this test the source (java_library) libfoo is active since the
4597 // prebuilt (java_import) defaults to prefer:false. However the
4598 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4599 // find the dex boot jar in it. We either need to disable the source libfoo
4600 // or make the prebuilt libfoo preferred.
4601 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4602 })
4603
4604 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4605 bp := `
4606 prebuilt_apex {
4607 name: "myapex",
4608 arch: {
4609 arm64: {
4610 src: "myapex-arm64.apex",
4611 },
4612 arm: {
4613 src: "myapex-arm.apex",
4614 },
4615 },
Paul Duffin37856732021-02-26 14:24:15 +00004616 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004617 }
4618
4619 java_import {
4620 name: "libfoo",
4621 prefer: true,
4622 jars: ["libfoo.jar"],
4623 apex_available: ["myapex"],
4624 }
4625
4626 java_library {
4627 name: "libfoo",
4628 srcs: ["foo/bar/MyClass.java"],
4629 apex_available: ["myapex"],
4630 }
Paul Duffin37856732021-02-26 14:24:15 +00004631
4632 java_sdk_library_import {
4633 name: "libbar",
4634 prefer: true,
4635 public: {
4636 jars: ["libbar.jar"],
4637 },
4638 apex_available: ["myapex"],
4639 }
4640
4641 java_sdk_library {
4642 name: "libbar",
4643 srcs: ["foo/bar/MyClass.java"],
4644 unsafe_ignore_missing_latest_api: true,
4645 apex_available: ["myapex"],
4646 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004647 `
4648
4649 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004650 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4651 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004652
Paul Duffin9d67ca62021-02-03 20:06:33 +00004653 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4654 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004655.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004656.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4657`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004658 })
4659
4660 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4661 bp := `
4662 apex {
4663 name: "myapex",
4664 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004665 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004666 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004667 }
4668
4669 apex_key {
4670 name: "myapex.key",
4671 public_key: "testkey.avbpubkey",
4672 private_key: "testkey.pem",
4673 }
4674
4675 prebuilt_apex {
4676 name: "myapex",
4677 arch: {
4678 arm64: {
4679 src: "myapex-arm64.apex",
4680 },
4681 arm: {
4682 src: "myapex-arm.apex",
4683 },
4684 },
Paul Duffin37856732021-02-26 14:24:15 +00004685 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004686 }
4687
4688 java_import {
4689 name: "libfoo",
4690 jars: ["libfoo.jar"],
4691 apex_available: ["myapex"],
4692 }
4693
4694 java_library {
4695 name: "libfoo",
4696 srcs: ["foo/bar/MyClass.java"],
4697 apex_available: ["myapex"],
4698 }
Paul Duffin37856732021-02-26 14:24:15 +00004699
4700 java_sdk_library_import {
4701 name: "libbar",
4702 public: {
4703 jars: ["libbar.jar"],
4704 },
4705 apex_available: ["myapex"],
4706 }
4707
4708 java_sdk_library {
4709 name: "libbar",
4710 srcs: ["foo/bar/MyClass.java"],
4711 unsafe_ignore_missing_latest_api: true,
4712 apex_available: ["myapex"],
4713 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004714 `
4715
4716 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004717 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4718 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004719
4720 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4721 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004722.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004723.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4724`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004725 })
4726
4727 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4728 bp := `
4729 apex {
4730 name: "myapex",
4731 enabled: false,
4732 key: "myapex.key",
4733 java_libs: ["libfoo"],
4734 }
4735
4736 apex_key {
4737 name: "myapex.key",
4738 public_key: "testkey.avbpubkey",
4739 private_key: "testkey.pem",
4740 }
4741
4742 prebuilt_apex {
4743 name: "myapex",
4744 arch: {
4745 arm64: {
4746 src: "myapex-arm64.apex",
4747 },
4748 arm: {
4749 src: "myapex-arm.apex",
4750 },
4751 },
Paul Duffin37856732021-02-26 14:24:15 +00004752 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004753 }
4754
4755 java_import {
4756 name: "libfoo",
4757 prefer: true,
4758 jars: ["libfoo.jar"],
4759 apex_available: ["myapex"],
4760 }
4761
4762 java_library {
4763 name: "libfoo",
4764 srcs: ["foo/bar/MyClass.java"],
4765 apex_available: ["myapex"],
4766 }
Paul Duffin37856732021-02-26 14:24:15 +00004767
4768 java_sdk_library_import {
4769 name: "libbar",
4770 prefer: true,
4771 public: {
4772 jars: ["libbar.jar"],
4773 },
4774 apex_available: ["myapex"],
4775 }
4776
4777 java_sdk_library {
4778 name: "libbar",
4779 srcs: ["foo/bar/MyClass.java"],
4780 unsafe_ignore_missing_latest_api: true,
4781 apex_available: ["myapex"],
4782 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004783 `
4784
4785 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004786 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4787 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004788
Paul Duffin9d67ca62021-02-03 20:06:33 +00004789 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4790 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004791.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004792.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4793`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004794 })
4795}
4796
Roland Levillain630846d2019-06-26 12:48:34 +01004797func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004798 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004799 apex_test {
4800 name: "myapex",
4801 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004802 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004803 tests: [
4804 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004805 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004806 ],
4807 }
4808
4809 apex_key {
4810 name: "myapex.key",
4811 public_key: "testkey.avbpubkey",
4812 private_key: "testkey.pem",
4813 }
4814
Liz Kammer1c14a212020-05-12 15:26:55 -07004815 filegroup {
4816 name: "fg",
4817 srcs: [
4818 "baz",
4819 "bar/baz"
4820 ],
4821 }
4822
Roland Levillain630846d2019-06-26 12:48:34 +01004823 cc_test {
4824 name: "mytest",
4825 gtest: false,
4826 srcs: ["mytest.cpp"],
4827 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004828 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004829 system_shared_libs: [],
4830 static_executable: true,
4831 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004832 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004833 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004834
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004835 cc_library {
4836 name: "mylib",
4837 srcs: ["mylib.cpp"],
4838 system_shared_libs: [],
4839 stl: "none",
4840 }
4841
Liz Kammer5bd365f2020-05-27 15:15:11 -07004842 filegroup {
4843 name: "fg2",
4844 srcs: [
4845 "testdata/baz"
4846 ],
4847 }
4848
Roland Levillain9b5fde92019-06-28 15:41:19 +01004849 cc_test {
4850 name: "mytests",
4851 gtest: false,
4852 srcs: [
4853 "mytest1.cpp",
4854 "mytest2.cpp",
4855 "mytest3.cpp",
4856 ],
4857 test_per_src: true,
4858 relative_install_path: "test",
4859 system_shared_libs: [],
4860 static_executable: true,
4861 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004862 data: [
4863 ":fg",
4864 ":fg2",
4865 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004866 }
Roland Levillain630846d2019-06-26 12:48:34 +01004867 `)
4868
Sundong Ahnabb64432019-10-22 13:58:29 +09004869 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004870 copyCmds := apexRule.Args["copy_commands"]
4871
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004872 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004873 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004874 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004875
Liz Kammer1c14a212020-05-12 15:26:55 -07004876 //Ensure that test data are copied into apex.
4877 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4878 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4879
Roland Levillain9b5fde92019-06-28 15:41:19 +01004880 // Ensure that test deps built with `test_per_src` are copied into apex.
4881 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4882 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4883 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004884
4885 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004886 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004887 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004888 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004889 prefix := "TARGET_"
4890 var builder strings.Builder
4891 data.Custom(&builder, name, prefix, "", data)
4892 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004893 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4894 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4895 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4896 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004897 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004898 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004899 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004900
4901 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004902 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004903 data.Custom(&builder, name, prefix, "", data)
4904 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004905 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4906 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004907}
4908
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004909func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004910 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004911 apex {
4912 name: "myapex",
4913 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004914 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004915 }
4916 apex_key {
4917 name: "myapex.key",
4918 public_key: "testkey.avbpubkey",
4919 private_key: "testkey.pem",
4920 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004921 `,
4922 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4923 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4924 }),
4925 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004926 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004927 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004928 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004929 var builder strings.Builder
4930 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4931 androidMk := builder.String()
4932 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4933}
4934
Jooyung Hand48f3c32019-08-23 11:18:57 +09004935func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4936 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4937 apex {
4938 name: "myapex",
4939 key: "myapex.key",
4940 native_shared_libs: ["libfoo"],
4941 }
4942
4943 apex_key {
4944 name: "myapex.key",
4945 public_key: "testkey.avbpubkey",
4946 private_key: "testkey.pem",
4947 }
4948
4949 cc_library {
4950 name: "libfoo",
4951 stl: "none",
4952 system_shared_libs: [],
4953 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004954 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004955 }
4956 `)
4957 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4958 apex {
4959 name: "myapex",
4960 key: "myapex.key",
4961 java_libs: ["myjar"],
4962 }
4963
4964 apex_key {
4965 name: "myapex.key",
4966 public_key: "testkey.avbpubkey",
4967 private_key: "testkey.pem",
4968 }
4969
4970 java_library {
4971 name: "myjar",
4972 srcs: ["foo/bar/MyClass.java"],
4973 sdk_version: "none",
4974 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004975 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004976 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004977 }
4978 `)
4979}
4980
Bill Peckhama41a6962021-01-11 10:58:54 -08004981func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004982 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004983 apex {
4984 name: "myapex",
4985 key: "myapex.key",
4986 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004987 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004988 }
4989
4990 apex_key {
4991 name: "myapex.key",
4992 public_key: "testkey.avbpubkey",
4993 private_key: "testkey.pem",
4994 }
4995
4996 java_import {
4997 name: "myjavaimport",
4998 apex_available: ["myapex"],
4999 jars: ["my.jar"],
5000 compile_dex: true,
5001 }
5002 `)
5003
5004 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5005 apexRule := module.Rule("apexRule")
5006 copyCmds := apexRule.Args["copy_commands"]
5007 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5008}
5009
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005010func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005011 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005012 apex {
5013 name: "myapex",
5014 key: "myapex.key",
5015 apps: [
5016 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005017 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005018 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005019 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005020 }
5021
5022 apex_key {
5023 name: "myapex.key",
5024 public_key: "testkey.avbpubkey",
5025 private_key: "testkey.pem",
5026 }
5027
5028 android_app {
5029 name: "AppFoo",
5030 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005031 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005032 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005033 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005034 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005035 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005036 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005037
5038 android_app {
5039 name: "AppFooPriv",
5040 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005041 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005042 system_modules: "none",
5043 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005044 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005045 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005046 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005047
5048 cc_library_shared {
5049 name: "libjni",
5050 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005051 shared_libs: ["libfoo"],
5052 stl: "none",
5053 system_shared_libs: [],
5054 apex_available: [ "myapex" ],
5055 sdk_version: "current",
5056 }
5057
5058 cc_library_shared {
5059 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005060 stl: "none",
5061 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005062 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005063 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005064 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005065 `)
5066
Sundong Ahnabb64432019-10-22 13:58:29 +09005067 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005068 apexRule := module.Rule("apexRule")
5069 copyCmds := apexRule.Args["copy_commands"]
5070
5071 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005072 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005073
Colin Crossaede88c2020-08-11 12:17:01 -07005074 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005075 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005076 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005077 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005078 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005079 // JNI libraries including transitive deps are
5080 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005081 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005082 // ... embedded inside APK (jnilibs.zip)
5083 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5084 // ... and not directly inside the APEX
5085 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5086 }
Dario Frenicde2a032019-10-27 00:29:22 +01005087}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005088
Dario Frenicde2a032019-10-27 00:29:22 +01005089func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005090 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005091 apex {
5092 name: "myapex",
5093 key: "myapex.key",
5094 apps: [
5095 "AppFooPrebuilt",
5096 "AppFooPrivPrebuilt",
5097 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005098 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005099 }
5100
5101 apex_key {
5102 name: "myapex.key",
5103 public_key: "testkey.avbpubkey",
5104 private_key: "testkey.pem",
5105 }
5106
5107 android_app_import {
5108 name: "AppFooPrebuilt",
5109 apk: "PrebuiltAppFoo.apk",
5110 presigned: true,
5111 dex_preopt: {
5112 enabled: false,
5113 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005114 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005115 }
5116
5117 android_app_import {
5118 name: "AppFooPrivPrebuilt",
5119 apk: "PrebuiltAppFooPriv.apk",
5120 privileged: true,
5121 presigned: true,
5122 dex_preopt: {
5123 enabled: false,
5124 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005125 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005126 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005127 }
5128 `)
5129
Sundong Ahnabb64432019-10-22 13:58:29 +09005130 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005131 apexRule := module.Rule("apexRule")
5132 copyCmds := apexRule.Args["copy_commands"]
5133
5134 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005135 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5136}
5137
5138func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005139 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005140 apex {
5141 name: "myapex",
5142 key: "myapex.key",
5143 apps: [
5144 "AppFoo",
5145 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005146 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005147 }
5148
5149 apex_key {
5150 name: "myapex.key",
5151 public_key: "testkey.avbpubkey",
5152 private_key: "testkey.pem",
5153 }
5154
5155 android_app {
5156 name: "AppFoo",
5157 srcs: ["foo/bar/MyClass.java"],
5158 sdk_version: "none",
5159 system_modules: "none",
5160 apex_available: [ "myapex" ],
5161 }
5162
5163 android_app_import {
5164 name: "AppFoo",
5165 apk: "AppFooPrebuilt.apk",
5166 filename: "AppFooPrebuilt.apk",
5167 presigned: true,
5168 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005169 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005170 }
5171 `, withFiles(map[string][]byte{
5172 "AppFooPrebuilt.apk": nil,
5173 }))
5174
5175 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005176 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005177 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005178}
5179
Dario Freni6f3937c2019-12-20 22:58:03 +00005180func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005181 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005182 apex {
5183 name: "myapex",
5184 key: "myapex.key",
5185 apps: [
5186 "TesterHelpAppFoo",
5187 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005188 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005189 }
5190
5191 apex_key {
5192 name: "myapex.key",
5193 public_key: "testkey.avbpubkey",
5194 private_key: "testkey.pem",
5195 }
5196
5197 android_test_helper_app {
5198 name: "TesterHelpAppFoo",
5199 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005200 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005201 }
5202
5203 `)
5204
5205 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5206 apexRule := module.Rule("apexRule")
5207 copyCmds := apexRule.Args["copy_commands"]
5208
5209 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5210}
5211
Jooyung Han18020ea2019-11-13 10:50:48 +09005212func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5213 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005214 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005215 apex {
5216 name: "myapex",
5217 key: "myapex.key",
5218 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005219 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005220 }
5221
5222 apex_key {
5223 name: "myapex.key",
5224 public_key: "testkey.avbpubkey",
5225 private_key: "testkey.pem",
5226 }
5227
5228 apex {
5229 name: "otherapex",
5230 key: "myapex.key",
5231 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005232 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005233 }
5234
5235 cc_defaults {
5236 name: "libfoo-defaults",
5237 apex_available: ["otherapex"],
5238 }
5239
5240 cc_library {
5241 name: "libfoo",
5242 defaults: ["libfoo-defaults"],
5243 stl: "none",
5244 system_shared_libs: [],
5245 }`)
5246}
5247
Paul Duffine52e66f2020-03-30 17:54:29 +01005248func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005249 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005250 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005251 apex {
5252 name: "myapex",
5253 key: "myapex.key",
5254 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005255 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005256 }
5257
5258 apex_key {
5259 name: "myapex.key",
5260 public_key: "testkey.avbpubkey",
5261 private_key: "testkey.pem",
5262 }
5263
5264 apex {
5265 name: "otherapex",
5266 key: "otherapex.key",
5267 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005268 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005269 }
5270
5271 apex_key {
5272 name: "otherapex.key",
5273 public_key: "testkey.avbpubkey",
5274 private_key: "testkey.pem",
5275 }
5276
5277 cc_library {
5278 name: "libfoo",
5279 stl: "none",
5280 system_shared_libs: [],
5281 apex_available: ["otherapex"],
5282 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005283}
Jiyong Park127b40b2019-09-30 16:04:35 +09005284
Paul Duffine52e66f2020-03-30 17:54:29 +01005285func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005286 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005287 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005288.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005289.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005290.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005291.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005292.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005293.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005294 apex {
5295 name: "myapex",
5296 key: "myapex.key",
5297 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005298 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005299 }
5300
5301 apex_key {
5302 name: "myapex.key",
5303 public_key: "testkey.avbpubkey",
5304 private_key: "testkey.pem",
5305 }
5306
Jiyong Park127b40b2019-09-30 16:04:35 +09005307 cc_library {
5308 name: "libfoo",
5309 stl: "none",
5310 shared_libs: ["libbar"],
5311 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005312 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005313 }
5314
5315 cc_library {
5316 name: "libbar",
5317 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005318 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005319 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005320 apex_available: ["myapex"],
5321 }
5322
5323 cc_library {
5324 name: "libbaz",
5325 stl: "none",
5326 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005327 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005328}
Jiyong Park127b40b2019-09-30 16:04:35 +09005329
Paul Duffine52e66f2020-03-30 17:54:29 +01005330func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005331 testApexError(t, "\"otherapex\" is not a valid module name", `
5332 apex {
5333 name: "myapex",
5334 key: "myapex.key",
5335 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005336 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005337 }
5338
5339 apex_key {
5340 name: "myapex.key",
5341 public_key: "testkey.avbpubkey",
5342 private_key: "testkey.pem",
5343 }
5344
5345 cc_library {
5346 name: "libfoo",
5347 stl: "none",
5348 system_shared_libs: [],
5349 apex_available: ["otherapex"],
5350 }`)
5351
Paul Duffine52e66f2020-03-30 17:54:29 +01005352 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005353 apex {
5354 name: "myapex",
5355 key: "myapex.key",
5356 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005357 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005358 }
5359
5360 apex_key {
5361 name: "myapex.key",
5362 public_key: "testkey.avbpubkey",
5363 private_key: "testkey.pem",
5364 }
5365
5366 cc_library {
5367 name: "libfoo",
5368 stl: "none",
5369 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005370 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005371 apex_available: ["myapex"],
5372 }
5373
5374 cc_library {
5375 name: "libbar",
5376 stl: "none",
5377 system_shared_libs: [],
5378 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005379 }
5380
5381 cc_library {
5382 name: "libbaz",
5383 stl: "none",
5384 system_shared_libs: [],
5385 stubs: {
5386 versions: ["10", "20", "30"],
5387 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005388 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005389}
Jiyong Park127b40b2019-09-30 16:04:35 +09005390
Jiyong Park89e850a2020-04-07 16:37:39 +09005391func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005392 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005393 apex {
5394 name: "myapex",
5395 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005396 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005397 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005398 }
5399
5400 apex_key {
5401 name: "myapex.key",
5402 public_key: "testkey.avbpubkey",
5403 private_key: "testkey.pem",
5404 }
5405
5406 cc_library {
5407 name: "libfoo",
5408 stl: "none",
5409 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005410 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005411 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005412 }
5413
5414 cc_library {
5415 name: "libfoo2",
5416 stl: "none",
5417 system_shared_libs: [],
5418 shared_libs: ["libbaz"],
5419 apex_available: ["//apex_available:platform"],
5420 }
5421
5422 cc_library {
5423 name: "libbar",
5424 stl: "none",
5425 system_shared_libs: [],
5426 apex_available: ["myapex"],
5427 }
5428
5429 cc_library {
5430 name: "libbaz",
5431 stl: "none",
5432 system_shared_libs: [],
5433 apex_available: ["myapex"],
5434 stubs: {
5435 versions: ["1"],
5436 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005437 }`)
5438
Jiyong Park89e850a2020-04-07 16:37:39 +09005439 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5440 // because it depends on libbar which isn't available to platform
5441 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5442 if libfoo.NotAvailableForPlatform() != true {
5443 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5444 }
5445
5446 // libfoo2 however can be available to platform because it depends on libbaz which provides
5447 // stubs
5448 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5449 if libfoo2.NotAvailableForPlatform() == true {
5450 t.Errorf("%q should be available to platform", libfoo2.String())
5451 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005452}
Jiyong Parka90ca002019-10-07 15:47:24 +09005453
Paul Duffine52e66f2020-03-30 17:54:29 +01005454func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005455 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005456 apex {
5457 name: "myapex",
5458 key: "myapex.key",
5459 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005460 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005461 }
5462
5463 apex_key {
5464 name: "myapex.key",
5465 public_key: "testkey.avbpubkey",
5466 private_key: "testkey.pem",
5467 }
5468
5469 cc_library {
5470 name: "libfoo",
5471 stl: "none",
5472 system_shared_libs: [],
5473 apex_available: ["myapex"],
5474 static: {
5475 apex_available: ["//apex_available:platform"],
5476 },
5477 }`)
5478
Jiyong Park89e850a2020-04-07 16:37:39 +09005479 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5480 if libfooShared.NotAvailableForPlatform() != true {
5481 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5482 }
5483 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5484 if libfooStatic.NotAvailableForPlatform() != false {
5485 t.Errorf("%q should be available to platform", libfooStatic.String())
5486 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005487}
5488
Jiyong Park5d790c32019-11-15 18:40:32 +09005489func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005490 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005491 apex {
5492 name: "myapex",
5493 key: "myapex.key",
5494 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005495 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005496 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005497 }
5498
5499 override_apex {
5500 name: "override_myapex",
5501 base: "myapex",
5502 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005503 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005504 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005505 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005506 }
5507
5508 apex_key {
5509 name: "myapex.key",
5510 public_key: "testkey.avbpubkey",
5511 private_key: "testkey.pem",
5512 }
5513
5514 android_app {
5515 name: "app",
5516 srcs: ["foo/bar/MyClass.java"],
5517 package_name: "foo",
5518 sdk_version: "none",
5519 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005520 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005521 }
5522
5523 override_android_app {
5524 name: "override_app",
5525 base: "app",
5526 package_name: "bar",
5527 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005528 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005529
Jiyong Park317645e2019-12-05 13:20:58 +09005530 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5531 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5532 if originalVariant.GetOverriddenBy() != "" {
5533 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5534 }
5535 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5536 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5537 }
5538
Jiyong Park5d790c32019-11-15 18:40:32 +09005539 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5540 apexRule := module.Rule("apexRule")
5541 copyCmds := apexRule.Args["copy_commands"]
5542
5543 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005544 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005545
5546 apexBundle := module.Module().(*apexBundle)
5547 name := apexBundle.Name()
5548 if name != "override_myapex" {
5549 t.Errorf("name should be \"override_myapex\", but was %q", name)
5550 }
5551
Baligh Uddin004d7172020-02-19 21:29:28 -08005552 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5553 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5554 }
5555
Jiyong Park20bacab2020-03-03 11:45:41 +09005556 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005557 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005558
Colin Crossaa255532020-07-03 13:18:24 -07005559 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005560 var builder strings.Builder
5561 data.Custom(&builder, name, "TARGET_", "", data)
5562 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005563 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005564 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5565 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005566 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005567 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005568 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005569 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5570 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005571}
5572
Jooyung Han214bf372019-11-12 13:03:50 +09005573func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005574 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005575 apex {
5576 name: "myapex",
5577 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005578 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005579 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005580 }
5581
5582 apex_key {
5583 name: "myapex.key",
5584 public_key: "testkey.avbpubkey",
5585 private_key: "testkey.pem",
5586 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005587
5588 cc_library {
5589 name: "mylib",
5590 srcs: ["mylib.cpp"],
5591 stl: "libc++",
5592 system_shared_libs: [],
5593 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005594 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005595 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005596 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005597
5598 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5599 args := module.Rule("apexRule").Args
5600 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005601 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005602
5603 // The copies of the libraries in the apex should have one more dependency than
5604 // the ones outside the apex, namely the unwinder. Ideally we should check
5605 // the dependency names directly here but for some reason the names are blank in
5606 // this test.
5607 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005608 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005609 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5610 if len(apexImplicits) != len(nonApexImplicits)+1 {
5611 t.Errorf("%q missing unwinder dep", lib)
5612 }
5613 }
Jooyung Han214bf372019-11-12 13:03:50 +09005614}
5615
Paul Duffine05480a2021-03-08 15:07:14 +00005616var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005617 "api/current.txt": nil,
5618 "api/removed.txt": nil,
5619 "api/system-current.txt": nil,
5620 "api/system-removed.txt": nil,
5621 "api/test-current.txt": nil,
5622 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005623
Anton Hanssondff2c782020-12-21 17:10:01 +00005624 "100/public/api/foo.txt": nil,
5625 "100/public/api/foo-removed.txt": nil,
5626 "100/system/api/foo.txt": nil,
5627 "100/system/api/foo-removed.txt": nil,
5628
Paul Duffineedc5d52020-06-12 17:46:39 +01005629 // For java_sdk_library_import
5630 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005631}
5632
Jooyung Han58f26ab2019-12-18 15:34:32 +09005633func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005634 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005635 apex {
5636 name: "myapex",
5637 key: "myapex.key",
5638 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005639 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005640 }
5641
5642 apex_key {
5643 name: "myapex.key",
5644 public_key: "testkey.avbpubkey",
5645 private_key: "testkey.pem",
5646 }
5647
5648 java_sdk_library {
5649 name: "foo",
5650 srcs: ["a.java"],
5651 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005652 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005653 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005654
5655 prebuilt_apis {
5656 name: "sdk",
5657 api_dirs: ["100"],
5658 }
Paul Duffin9b879592020-05-26 13:21:35 +01005659 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005660
5661 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005662 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005663 "javalib/foo.jar",
5664 "etc/permissions/foo.xml",
5665 })
5666 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005667 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5668 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005669}
5670
Paul Duffin9b879592020-05-26 13:21:35 +01005671func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005672 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005673 apex {
5674 name: "myapex",
5675 key: "myapex.key",
5676 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005677 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005678 }
5679
5680 apex_key {
5681 name: "myapex.key",
5682 public_key: "testkey.avbpubkey",
5683 private_key: "testkey.pem",
5684 }
5685
5686 java_sdk_library {
5687 name: "foo",
5688 srcs: ["a.java"],
5689 api_packages: ["foo"],
5690 apex_available: ["myapex"],
5691 sdk_version: "none",
5692 system_modules: "none",
5693 }
5694
5695 java_library {
5696 name: "bar",
5697 srcs: ["a.java"],
5698 libs: ["foo"],
5699 apex_available: ["myapex"],
5700 sdk_version: "none",
5701 system_modules: "none",
5702 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005703
5704 prebuilt_apis {
5705 name: "sdk",
5706 api_dirs: ["100"],
5707 }
Paul Duffin9b879592020-05-26 13:21:35 +01005708 `, withFiles(filesForSdkLibrary))
5709
5710 // java_sdk_library installs both impl jar and permission XML
5711 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5712 "javalib/bar.jar",
5713 "javalib/foo.jar",
5714 "etc/permissions/foo.xml",
5715 })
5716
5717 // The bar library should depend on the implementation jar.
5718 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5719 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5720 t.Errorf("expected %q, found %#q", expected, actual)
5721 }
5722}
5723
5724func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005725 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005726 apex {
5727 name: "myapex",
5728 key: "myapex.key",
5729 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005730 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005731 }
5732
5733 apex_key {
5734 name: "myapex.key",
5735 public_key: "testkey.avbpubkey",
5736 private_key: "testkey.pem",
5737 }
5738
5739 java_sdk_library {
5740 name: "foo",
5741 srcs: ["a.java"],
5742 api_packages: ["foo"],
5743 apex_available: ["myapex"],
5744 sdk_version: "none",
5745 system_modules: "none",
5746 }
5747
5748 java_library {
5749 name: "bar",
5750 srcs: ["a.java"],
5751 libs: ["foo"],
5752 sdk_version: "none",
5753 system_modules: "none",
5754 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005755
5756 prebuilt_apis {
5757 name: "sdk",
5758 api_dirs: ["100"],
5759 }
Paul Duffin9b879592020-05-26 13:21:35 +01005760 `, withFiles(filesForSdkLibrary))
5761
5762 // java_sdk_library installs both impl jar and permission XML
5763 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5764 "javalib/foo.jar",
5765 "etc/permissions/foo.xml",
5766 })
5767
5768 // The bar library should depend on the stubs jar.
5769 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5770 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5771 t.Errorf("expected %q, found %#q", expected, actual)
5772 }
5773}
5774
Paul Duffineedc5d52020-06-12 17:46:39 +01005775func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005776 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005777 prebuilt_apis {
5778 name: "sdk",
5779 api_dirs: ["100"],
5780 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005781 withFiles(map[string][]byte{
5782 "apex/a.java": nil,
5783 "apex/apex_manifest.json": nil,
5784 "apex/Android.bp": []byte(`
5785 package {
5786 default_visibility: ["//visibility:private"],
5787 }
5788
5789 apex {
5790 name: "myapex",
5791 key: "myapex.key",
5792 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005793 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005794 }
5795
5796 apex_key {
5797 name: "myapex.key",
5798 public_key: "testkey.avbpubkey",
5799 private_key: "testkey.pem",
5800 }
5801
5802 java_library {
5803 name: "bar",
5804 srcs: ["a.java"],
5805 libs: ["foo"],
5806 apex_available: ["myapex"],
5807 sdk_version: "none",
5808 system_modules: "none",
5809 }
5810`),
5811 "source/a.java": nil,
5812 "source/api/current.txt": nil,
5813 "source/api/removed.txt": nil,
5814 "source/Android.bp": []byte(`
5815 package {
5816 default_visibility: ["//visibility:private"],
5817 }
5818
5819 java_sdk_library {
5820 name: "foo",
5821 visibility: ["//apex"],
5822 srcs: ["a.java"],
5823 api_packages: ["foo"],
5824 apex_available: ["myapex"],
5825 sdk_version: "none",
5826 system_modules: "none",
5827 public: {
5828 enabled: true,
5829 },
5830 }
5831`),
5832 "prebuilt/a.jar": nil,
5833 "prebuilt/Android.bp": []byte(`
5834 package {
5835 default_visibility: ["//visibility:private"],
5836 }
5837
5838 java_sdk_library_import {
5839 name: "foo",
5840 visibility: ["//apex", "//source"],
5841 apex_available: ["myapex"],
5842 prefer: true,
5843 public: {
5844 jars: ["a.jar"],
5845 },
5846 }
5847`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005848 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005849 )
5850
5851 // java_sdk_library installs both impl jar and permission XML
5852 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5853 "javalib/bar.jar",
5854 "javalib/foo.jar",
5855 "etc/permissions/foo.xml",
5856 })
5857
5858 // The bar library should depend on the implementation jar.
5859 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5860 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5861 t.Errorf("expected %q, found %#q", expected, actual)
5862 }
5863}
5864
5865func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5866 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5867 apex {
5868 name: "myapex",
5869 key: "myapex.key",
5870 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005871 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005872 }
5873
5874 apex_key {
5875 name: "myapex.key",
5876 public_key: "testkey.avbpubkey",
5877 private_key: "testkey.pem",
5878 }
5879
5880 java_sdk_library_import {
5881 name: "foo",
5882 apex_available: ["myapex"],
5883 prefer: true,
5884 public: {
5885 jars: ["a.jar"],
5886 },
5887 }
5888
5889 `, withFiles(filesForSdkLibrary))
5890}
5891
atrost6e126252020-01-27 17:01:16 +00005892func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005893 result := apexFixtureFactory.
5894 Extend(java.PrepareForTestWithPlatformCompatConfig).
5895 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005896 apex {
5897 name: "myapex",
5898 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005899 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005900 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005901 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005902 }
5903
5904 apex_key {
5905 name: "myapex.key",
5906 public_key: "testkey.avbpubkey",
5907 private_key: "testkey.pem",
5908 }
5909
5910 platform_compat_config {
5911 name: "myjar-platform-compat-config",
5912 src: ":myjar",
5913 }
5914
5915 java_library {
5916 name: "myjar",
5917 srcs: ["foo/bar/MyClass.java"],
5918 sdk_version: "none",
5919 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005920 apex_available: [ "myapex" ],
5921 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005922
5923 // Make sure that a preferred prebuilt does not affect the apex contents.
5924 prebuilt_platform_compat_config {
5925 name: "myjar-platform-compat-config",
5926 metadata: "compat-config/metadata.xml",
5927 prefer: true,
5928 }
atrost6e126252020-01-27 17:01:16 +00005929 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005930 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005931 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5932 "etc/compatconfig/myjar-platform-compat-config.xml",
5933 "javalib/myjar.jar",
5934 })
5935}
5936
Jiyong Park479321d2019-12-16 11:47:12 +09005937func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5938 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5939 apex {
5940 name: "myapex",
5941 key: "myapex.key",
5942 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005943 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005944 }
5945
5946 apex_key {
5947 name: "myapex.key",
5948 public_key: "testkey.avbpubkey",
5949 private_key: "testkey.pem",
5950 }
5951
5952 java_library {
5953 name: "myjar",
5954 srcs: ["foo/bar/MyClass.java"],
5955 sdk_version: "none",
5956 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005957 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005958 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005959 }
5960 `)
5961}
5962
Jiyong Park7afd1072019-12-30 16:56:33 +09005963func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005964 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005965 apex {
5966 name: "myapex",
5967 key: "myapex.key",
5968 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005969 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005970 }
5971
5972 apex_key {
5973 name: "myapex.key",
5974 public_key: "testkey.avbpubkey",
5975 private_key: "testkey.pem",
5976 }
5977
5978 cc_library {
5979 name: "mylib",
5980 srcs: ["mylib.cpp"],
5981 system_shared_libs: [],
5982 stl: "none",
5983 required: ["a", "b"],
5984 host_required: ["c", "d"],
5985 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005986 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005987 }
5988 `)
5989
5990 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005991 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005992 name := apexBundle.BaseModuleName()
5993 prefix := "TARGET_"
5994 var builder strings.Builder
5995 data.Custom(&builder, name, prefix, "", data)
5996 androidMk := builder.String()
5997 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5998 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5999 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6000}
6001
Jiyong Park7cd10e32020-01-14 09:22:18 +09006002func TestSymlinksFromApexToSystem(t *testing.T) {
6003 bp := `
6004 apex {
6005 name: "myapex",
6006 key: "myapex.key",
6007 native_shared_libs: ["mylib"],
6008 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006009 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006010 }
6011
Jiyong Park9d677202020-02-19 16:29:35 +09006012 apex {
6013 name: "myapex.updatable",
6014 key: "myapex.key",
6015 native_shared_libs: ["mylib"],
6016 java_libs: ["myjar"],
6017 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006018 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006019 }
6020
Jiyong Park7cd10e32020-01-14 09:22:18 +09006021 apex_key {
6022 name: "myapex.key",
6023 public_key: "testkey.avbpubkey",
6024 private_key: "testkey.pem",
6025 }
6026
6027 cc_library {
6028 name: "mylib",
6029 srcs: ["mylib.cpp"],
6030 shared_libs: ["myotherlib"],
6031 system_shared_libs: [],
6032 stl: "none",
6033 apex_available: [
6034 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006035 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006036 "//apex_available:platform",
6037 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006038 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006039 }
6040
6041 cc_library {
6042 name: "myotherlib",
6043 srcs: ["mylib.cpp"],
6044 system_shared_libs: [],
6045 stl: "none",
6046 apex_available: [
6047 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006048 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006049 "//apex_available:platform",
6050 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006051 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006052 }
6053
6054 java_library {
6055 name: "myjar",
6056 srcs: ["foo/bar/MyClass.java"],
6057 sdk_version: "none",
6058 system_modules: "none",
6059 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006060 apex_available: [
6061 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006062 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006063 "//apex_available:platform",
6064 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006065 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006066 }
6067
6068 java_library {
6069 name: "myotherjar",
6070 srcs: ["foo/bar/MyClass.java"],
6071 sdk_version: "none",
6072 system_modules: "none",
6073 apex_available: [
6074 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006075 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006076 "//apex_available:platform",
6077 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006078 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006079 }
6080 `
6081
6082 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6083 for _, f := range files {
6084 if f.path == file {
6085 if f.isLink {
6086 t.Errorf("%q is not a real file", file)
6087 }
6088 return
6089 }
6090 }
6091 t.Errorf("%q is not found", file)
6092 }
6093
6094 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6095 for _, f := range files {
6096 if f.path == file {
6097 if !f.isLink {
6098 t.Errorf("%q is not a symlink", file)
6099 }
6100 return
6101 }
6102 }
6103 t.Errorf("%q is not found", file)
6104 }
6105
Jiyong Park9d677202020-02-19 16:29:35 +09006106 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6107 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006108 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006109 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006110 ensureRealfileExists(t, files, "javalib/myjar.jar")
6111 ensureRealfileExists(t, files, "lib64/mylib.so")
6112 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6113
Jiyong Park9d677202020-02-19 16:29:35 +09006114 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6115 ensureRealfileExists(t, files, "javalib/myjar.jar")
6116 ensureRealfileExists(t, files, "lib64/mylib.so")
6117 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6118
6119 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006120 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006121 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006122 ensureRealfileExists(t, files, "javalib/myjar.jar")
6123 ensureRealfileExists(t, files, "lib64/mylib.so")
6124 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006125
6126 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6127 ensureRealfileExists(t, files, "javalib/myjar.jar")
6128 ensureRealfileExists(t, files, "lib64/mylib.so")
6129 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006130}
6131
Yo Chiange8128052020-07-23 20:09:18 +08006132func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006133 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006134 apex {
6135 name: "myapex",
6136 key: "myapex.key",
6137 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006138 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006139 }
6140
6141 apex_key {
6142 name: "myapex.key",
6143 public_key: "testkey.avbpubkey",
6144 private_key: "testkey.pem",
6145 }
6146
6147 cc_library_shared {
6148 name: "mylib",
6149 srcs: ["mylib.cpp"],
6150 shared_libs: ["myotherlib"],
6151 system_shared_libs: [],
6152 stl: "none",
6153 apex_available: [
6154 "myapex",
6155 "//apex_available:platform",
6156 ],
6157 }
6158
6159 cc_prebuilt_library_shared {
6160 name: "myotherlib",
6161 srcs: ["prebuilt.so"],
6162 system_shared_libs: [],
6163 stl: "none",
6164 apex_available: [
6165 "myapex",
6166 "//apex_available:platform",
6167 ],
6168 }
6169 `)
6170
6171 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006172 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006173 var builder strings.Builder
6174 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6175 androidMk := builder.String()
6176 // `myotherlib` is added to `myapex` as symlink
6177 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6178 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6179 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6180 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006181 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 +08006182}
6183
Jooyung Han643adc42020-02-27 13:50:06 +09006184func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006185 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006186 apex {
6187 name: "myapex",
6188 key: "myapex.key",
6189 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006190 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006191 }
6192
6193 apex_key {
6194 name: "myapex.key",
6195 public_key: "testkey.avbpubkey",
6196 private_key: "testkey.pem",
6197 }
6198
6199 cc_library {
6200 name: "mylib",
6201 srcs: ["mylib.cpp"],
6202 shared_libs: ["mylib2"],
6203 system_shared_libs: [],
6204 stl: "none",
6205 apex_available: [ "myapex" ],
6206 }
6207
6208 cc_library {
6209 name: "mylib2",
6210 srcs: ["mylib.cpp"],
6211 system_shared_libs: [],
6212 stl: "none",
6213 apex_available: [ "myapex" ],
6214 }
6215 `)
6216
6217 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6218 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6219 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6220 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6221 "lib64/mylib.so",
6222 "lib64/mylib2.so",
6223 })
6224}
6225
Jooyung Han49f67012020-04-17 13:43:10 +09006226func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006227 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006228 apex {
6229 name: "myapex",
6230 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006231 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006232 }
6233 apex_key {
6234 name: "myapex.key",
6235 public_key: "testkey.avbpubkey",
6236 private_key: "testkey.pem",
6237 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006238 `,
6239 android.FixtureModifyConfig(func(config android.Config) {
6240 delete(config.Targets, android.Android)
6241 config.AndroidCommonTarget = android.Target{}
6242 }),
6243 )
Jooyung Han49f67012020-04-17 13:43:10 +09006244
6245 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6246 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6247 }
6248}
6249
Jiyong Parkbd159612020-02-28 15:22:21 +09006250func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006251 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006252 apex {
6253 name: "myapex",
6254 key: "myapex.key",
6255 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006256 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006257 }
6258
6259 apex_key {
6260 name: "myapex.key",
6261 public_key: "testkey.avbpubkey",
6262 private_key: "testkey.pem",
6263 }
6264
6265 android_app {
6266 name: "AppFoo",
6267 srcs: ["foo/bar/MyClass.java"],
6268 sdk_version: "none",
6269 system_modules: "none",
6270 apex_available: [ "myapex" ],
6271 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006272 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006273
Colin Crosscf371cc2020-11-13 11:48:42 -08006274 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006275 content := bundleConfigRule.Args["content"]
6276
6277 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006278 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 +09006279}
6280
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006281func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006282 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006283 apex {
6284 name: "myapex",
6285 key: "myapex.key",
6286 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006287 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006288 }
6289
6290 apex_key {
6291 name: "myapex.key",
6292 public_key: "testkey.avbpubkey",
6293 private_key: "testkey.pem",
6294 }
6295
6296 android_app_set {
6297 name: "AppSet",
6298 set: "AppSet.apks",
6299 }`)
6300 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006301 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006302 content := bundleConfigRule.Args["content"]
6303 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6304 s := mod.Rule("apexRule").Args["copy_commands"]
6305 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6306 if len(copyCmds) != 3 {
6307 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6308 }
6309 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6310 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6311 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6312}
6313
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006314func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006315 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006316 bp := `
6317 apex_set {
6318 name: "myapex",
6319 filename: "foo_v2.apex",
6320 sanitized: {
6321 none: { set: "myapex.apks", },
6322 hwaddress: { set: "myapex.hwasan.apks", },
6323 },
6324 }`
6325 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006326 }),
6327 prepareForTestWithSantitizeHwaddress,
6328 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006329
6330 m := ctx.ModuleForTests("myapex", "android_common")
6331 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6332
6333 actual := extractedApex.Inputs
6334 if len(actual) != 1 {
6335 t.Errorf("expected a single input")
6336 }
6337
6338 expected := "myapex.hwasan.apks"
6339 if actual[0].String() != expected {
6340 t.Errorf("expected %s, got %s", expected, actual[0].String())
6341 }
6342}
6343
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006344func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006345 t.Helper()
6346
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006347 bp := `
6348 java_library {
6349 name: "some-updatable-apex-lib",
6350 srcs: ["a.java"],
6351 sdk_version: "current",
6352 apex_available: [
6353 "some-updatable-apex",
6354 ],
6355 }
6356
6357 java_library {
6358 name: "some-non-updatable-apex-lib",
6359 srcs: ["a.java"],
6360 apex_available: [
6361 "some-non-updatable-apex",
6362 ],
6363 }
6364
6365 java_library {
6366 name: "some-platform-lib",
6367 srcs: ["a.java"],
6368 sdk_version: "current",
6369 installable: true,
6370 }
6371
6372 java_library {
6373 name: "some-art-lib",
6374 srcs: ["a.java"],
6375 sdk_version: "current",
6376 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006377 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006378 ],
6379 hostdex: true,
6380 }
6381
6382 apex {
6383 name: "some-updatable-apex",
6384 key: "some-updatable-apex.key",
6385 java_libs: ["some-updatable-apex-lib"],
6386 updatable: true,
6387 min_sdk_version: "current",
6388 }
6389
6390 apex {
6391 name: "some-non-updatable-apex",
6392 key: "some-non-updatable-apex.key",
6393 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006394 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006395 }
6396
6397 apex_key {
6398 name: "some-updatable-apex.key",
6399 }
6400
6401 apex_key {
6402 name: "some-non-updatable-apex.key",
6403 }
6404
6405 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006406 name: "com.android.art.debug",
6407 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006408 java_libs: ["some-art-lib"],
6409 updatable: true,
6410 min_sdk_version: "current",
6411 }
6412
6413 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006414 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006415 }
6416
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006417 filegroup {
6418 name: "some-updatable-apex-file_contexts",
6419 srcs: [
6420 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6421 ],
6422 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006423
6424 filegroup {
6425 name: "some-non-updatable-apex-file_contexts",
6426 srcs: [
6427 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6428 ],
6429 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006430 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006431
6432 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6433}
6434
Paul Duffin064b70c2020-11-02 17:32:38 +00006435func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006436 t.Helper()
6437
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006438 bp += cc.GatherRequiredDepsForTest(android.Android)
6439 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006440
6441 fs := map[string][]byte{
6442 "a.java": nil,
6443 "a.jar": nil,
6444 "build/make/target/product/security": nil,
6445 "apex_manifest.json": nil,
6446 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006447 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006448 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6449 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6450 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006451 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006452 }
6453 cc.GatherRequiredFilesForTest(fs)
6454
Paul Duffin39853512021-02-26 11:09:39 +00006455 for k, v := range filesForSdkLibrary {
6456 fs[k] = v
6457 }
Colin Crossae8600b2020-10-29 17:09:13 -07006458 config := android.TestArchConfig(buildDir, nil, bp, fs)
6459
6460 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006461 ctx.RegisterModuleType("apex", BundleFactory)
6462 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006463 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006464 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006465 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006466 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006467 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006468 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006469 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006470 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006471 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6472 ctx.PreDepsMutators(RegisterPreDepsMutators)
6473 ctx.PostDepsMutators(RegisterPostDepsMutators)
6474
Colin Crossae8600b2020-10-29 17:09:13 -07006475 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006476
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006477 pathCtx := android.PathContextForTesting(config)
6478 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6479 transformDexpreoptConfig(dexpreoptConfig)
6480 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6481
Paul Duffinf38931c2021-02-05 16:58:28 +00006482 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006483 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006484 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6485 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6486
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006487 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6488 android.FailIfErrored(t, errs)
6489
6490 _, errs = ctx.PrepareBuildActions(config)
6491 if errmsg == "" {
6492 android.FailIfErrored(t, errs)
6493 } else if len(errs) > 0 {
6494 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006495 } else {
6496 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6497 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006498
6499 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006500}
6501
Jooyung Han548640b2020-04-27 12:10:30 +09006502func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6503 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6504 apex {
6505 name: "myapex",
6506 key: "myapex.key",
6507 updatable: true,
6508 }
6509
6510 apex_key {
6511 name: "myapex.key",
6512 public_key: "testkey.avbpubkey",
6513 private_key: "testkey.pem",
6514 }
6515 `)
6516}
6517
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006518func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6519 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6520 apex {
6521 name: "myapex",
6522 key: "myapex.key",
6523 }
6524
6525 apex_key {
6526 name: "myapex.key",
6527 public_key: "testkey.avbpubkey",
6528 private_key: "testkey.pem",
6529 }
6530 `)
6531}
6532
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006533func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006534 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006535 var transform func(*dexpreopt.GlobalConfig)
6536
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006537 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6538 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006539 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006540 }
6541 testNoUpdatableJarsInBootImage(t, "", transform)
6542 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006543
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006544 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006545 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 +01006546 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006547 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006548 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006549 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006550 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006551
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006552 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 -07006553 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 +01006554 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006555 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006556 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006557 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006558 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006559
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006560 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 -07006561 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006562 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006563 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006564 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006565 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006566 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006567
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006568 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 -07006569 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 +01006570 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006571 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006572 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006573 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006574 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006575
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006576 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6577 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006578 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006579 }
6580 testNoUpdatableJarsInBootImage(t, "", transform)
6581 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006582
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006583 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006584 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006585 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006586 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006588 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006590
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006591 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006592 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006593 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006594 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006595 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006596 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006597 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006598
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006599 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006600 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006601 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006602 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006603 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006604 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006605 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006606
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006607 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6608 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006609 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006610 }
6611 testNoUpdatableJarsInBootImage(t, "", transform)
6612 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006613
6614}
6615
6616func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6617 transform := func(config *dexpreopt.GlobalConfig) {
6618 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6619 }
6620 t.Run("prebuilt no source", func(t *testing.T) {
6621 testDexpreoptWithApexes(t, `
6622 prebuilt_apex {
6623 name: "myapex" ,
6624 arch: {
6625 arm64: {
6626 src: "myapex-arm64.apex",
6627 },
6628 arm: {
6629 src: "myapex-arm.apex",
6630 },
6631 },
6632 exported_java_libs: ["libfoo"],
6633 }
6634
6635 java_import {
6636 name: "libfoo",
6637 jars: ["libfoo.jar"],
6638 }
6639`, "", transform)
6640 })
6641
6642 t.Run("prebuilt no source", func(t *testing.T) {
6643 testDexpreoptWithApexes(t, `
6644 prebuilt_apex {
6645 name: "myapex" ,
6646 arch: {
6647 arm64: {
6648 src: "myapex-arm64.apex",
6649 },
6650 arm: {
6651 src: "myapex-arm.apex",
6652 },
6653 },
6654 exported_java_libs: ["libfoo"],
6655 }
6656
6657 java_import {
6658 name: "libfoo",
6659 jars: ["libfoo.jar"],
6660 }
6661`, "", transform)
6662 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006663}
6664
Andrei Onea115e7e72020-06-05 21:14:03 +01006665func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6666 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006667 bp += `
6668 apex_key {
6669 name: "myapex.key",
6670 public_key: "testkey.avbpubkey",
6671 private_key: "testkey.pem",
6672 }`
6673 fs := map[string][]byte{
6674 "lib1/src/A.java": nil,
6675 "lib2/src/B.java": nil,
6676 "system/sepolicy/apex/myapex-file_contexts": nil,
6677 }
6678
Colin Crossae8600b2020-10-29 17:09:13 -07006679 config := android.TestArchConfig(buildDir, nil, bp, fs)
6680 android.SetTestNeverallowRules(config, rules)
6681 updatableBootJars := make([]string, 0, len(apexBootJars))
6682 for _, apexBootJar := range apexBootJars {
6683 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6684 }
6685 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6686
6687 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006688 ctx.RegisterModuleType("apex", BundleFactory)
6689 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6690 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6691 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006692 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006693 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6694 ctx.PreDepsMutators(RegisterPreDepsMutators)
6695 ctx.PostDepsMutators(RegisterPostDepsMutators)
6696 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6697
Colin Crossae8600b2020-10-29 17:09:13 -07006698 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006699
6700 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6701 android.FailIfErrored(t, errs)
6702
6703 _, errs = ctx.PrepareBuildActions(config)
6704 if errmsg == "" {
6705 android.FailIfErrored(t, errs)
6706 } else if len(errs) > 0 {
6707 android.FailIfNoMatchingErrors(t, errmsg, errs)
6708 return
6709 } else {
6710 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6711 }
6712}
6713
6714func TestApexPermittedPackagesRules(t *testing.T) {
6715 testcases := []struct {
6716 name string
6717 expectedError string
6718 bp string
6719 bootJars []string
6720 modulesPackages map[string][]string
6721 }{
6722
6723 {
6724 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6725 expectedError: "",
6726 bp: `
6727 java_library {
6728 name: "bcp_lib1",
6729 srcs: ["lib1/src/*.java"],
6730 permitted_packages: ["foo.bar"],
6731 apex_available: ["myapex"],
6732 sdk_version: "none",
6733 system_modules: "none",
6734 }
6735 java_library {
6736 name: "nonbcp_lib2",
6737 srcs: ["lib2/src/*.java"],
6738 apex_available: ["myapex"],
6739 permitted_packages: ["a.b"],
6740 sdk_version: "none",
6741 system_modules: "none",
6742 }
6743 apex {
6744 name: "myapex",
6745 key: "myapex.key",
6746 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006747 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006748 }`,
6749 bootJars: []string{"bcp_lib1"},
6750 modulesPackages: map[string][]string{
6751 "myapex": []string{
6752 "foo.bar",
6753 },
6754 },
6755 },
6756 {
6757 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6758 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.`,
6759 bp: `
6760 java_library {
6761 name: "bcp_lib1",
6762 srcs: ["lib1/src/*.java"],
6763 apex_available: ["myapex"],
6764 permitted_packages: ["foo.bar"],
6765 sdk_version: "none",
6766 system_modules: "none",
6767 }
6768 java_library {
6769 name: "bcp_lib2",
6770 srcs: ["lib2/src/*.java"],
6771 apex_available: ["myapex"],
6772 permitted_packages: ["foo.bar", "bar.baz"],
6773 sdk_version: "none",
6774 system_modules: "none",
6775 }
6776 apex {
6777 name: "myapex",
6778 key: "myapex.key",
6779 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006780 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006781 }
6782 `,
6783 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6784 modulesPackages: map[string][]string{
6785 "myapex": []string{
6786 "foo.bar",
6787 },
6788 },
6789 },
6790 }
6791 for _, tc := range testcases {
6792 t.Run(tc.name, func(t *testing.T) {
6793 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6794 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6795 })
6796 }
6797}
6798
Jiyong Park62304bb2020-04-13 16:19:48 +09006799func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006800 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006801 apex {
6802 name: "myapex",
6803 key: "myapex.key",
6804 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006805 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006806 }
6807
6808 apex_key {
6809 name: "myapex.key",
6810 public_key: "testkey.avbpubkey",
6811 private_key: "testkey.pem",
6812 }
6813
6814 cc_library {
6815 name: "mylib",
6816 srcs: ["mylib.cpp"],
6817 system_shared_libs: [],
6818 stl: "none",
6819 stubs: {
6820 versions: ["1"],
6821 },
6822 apex_available: ["myapex"],
6823 }
6824
6825 cc_library {
6826 name: "myprivlib",
6827 srcs: ["mylib.cpp"],
6828 system_shared_libs: [],
6829 stl: "none",
6830 apex_available: ["myapex"],
6831 }
6832
6833
6834 cc_test {
6835 name: "mytest",
6836 gtest: false,
6837 srcs: ["mylib.cpp"],
6838 system_shared_libs: [],
6839 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006840 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006841 test_for: ["myapex"]
6842 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006843
6844 cc_library {
6845 name: "mytestlib",
6846 srcs: ["mylib.cpp"],
6847 system_shared_libs: [],
6848 shared_libs: ["mylib", "myprivlib"],
6849 stl: "none",
6850 test_for: ["myapex"],
6851 }
6852
6853 cc_benchmark {
6854 name: "mybench",
6855 srcs: ["mylib.cpp"],
6856 system_shared_libs: [],
6857 shared_libs: ["mylib", "myprivlib"],
6858 stl: "none",
6859 test_for: ["myapex"],
6860 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006861 `)
6862
6863 // the test 'mytest' is a test for the apex, therefore is linked to the
6864 // actual implementation of mylib instead of its stub.
6865 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6866 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6867 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006868
6869 // The same should be true for cc_library
6870 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6871 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6872 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6873
6874 // ... and for cc_benchmark
6875 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6876 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6877 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006878}
6879
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006880// TODO(jungjw): Move this to proptools
6881func intPtr(i int) *int {
6882 return &i
6883}
6884
6885func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006886 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006887 apex_set {
6888 name: "myapex",
6889 set: "myapex.apks",
6890 filename: "foo_v2.apex",
6891 overrides: ["foo"],
6892 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006893 `,
6894 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6895 variables.Platform_sdk_version = intPtr(30)
6896 }),
6897 android.FixtureModifyConfig(func(config android.Config) {
6898 config.Targets[android.Android] = []android.Target{
6899 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6900 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6901 }
6902 }),
6903 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006904
6905 m := ctx.ModuleForTests("myapex", "android_common")
6906
6907 // Check extract_apks tool parameters.
6908 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6909 actual := extractedApex.Args["abis"]
6910 expected := "ARMEABI_V7A,ARM64_V8A"
6911 if actual != expected {
6912 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6913 }
6914 actual = extractedApex.Args["sdk-version"]
6915 expected = "30"
6916 if actual != expected {
6917 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6918 }
6919
6920 a := m.Module().(*ApexSet)
6921 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006922 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006923 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6924 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6925 }
6926}
6927
Jiyong Park7d95a512020-05-10 15:16:24 +09006928func TestNoStaticLinkingToStubsLib(t *testing.T) {
6929 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6930 apex {
6931 name: "myapex",
6932 key: "myapex.key",
6933 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006934 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006935 }
6936
6937 apex_key {
6938 name: "myapex.key",
6939 public_key: "testkey.avbpubkey",
6940 private_key: "testkey.pem",
6941 }
6942
6943 cc_library {
6944 name: "mylib",
6945 srcs: ["mylib.cpp"],
6946 static_libs: ["otherlib"],
6947 system_shared_libs: [],
6948 stl: "none",
6949 apex_available: [ "myapex" ],
6950 }
6951
6952 cc_library {
6953 name: "otherlib",
6954 srcs: ["mylib.cpp"],
6955 system_shared_libs: [],
6956 stl: "none",
6957 stubs: {
6958 versions: ["1", "2", "3"],
6959 },
6960 apex_available: [ "myapex" ],
6961 }
6962 `)
6963}
6964
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006965func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006966 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006967 apex {
6968 name: "myapex",
6969 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006970 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006971 }
6972
6973 apex_key {
6974 name: "myapex.key",
6975 public_key: "testkey.avbpubkey",
6976 private_key: "testkey.pem",
6977 }
6978
6979 prebuilt_apex {
6980 name: "myapex",
6981 prefer: true,
6982 arch: {
6983 arm64: {
6984 src: "myapex-arm64.apex",
6985 },
6986 arm: {
6987 src: "myapex-arm.apex",
6988 },
6989 },
6990 }
6991
6992 apex_set {
6993 name: "myapex_set",
6994 set: "myapex.apks",
6995 filename: "myapex_set.apex",
6996 overrides: ["myapex"],
6997 }
6998 `)
6999
7000 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7001 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7002 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 +09007003 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 +09007004}
7005
Jooyung Han938b5932020-06-20 12:47:47 +09007006func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007007 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007008 apex {
7009 name: "myapex",
7010 key: "myapex.key",
7011 apps: ["app"],
7012 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007013 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007014 }
7015
7016 apex_key {
7017 name: "myapex.key",
7018 public_key: "testkey.avbpubkey",
7019 private_key: "testkey.pem",
7020 }
7021
7022 android_app {
7023 name: "app",
7024 srcs: ["foo/bar/MyClass.java"],
7025 package_name: "foo",
7026 sdk_version: "none",
7027 system_modules: "none",
7028 apex_available: [ "myapex" ],
7029 }
7030 `, withFiles(map[string][]byte{
7031 "sub/Android.bp": []byte(`
7032 override_apex {
7033 name: "override_myapex",
7034 base: "myapex",
7035 apps: ["override_app"],
7036 allowed_files: ":allowed",
7037 }
7038 // Overridable "path" property should be referenced indirectly
7039 filegroup {
7040 name: "allowed",
7041 srcs: ["allowed.txt"],
7042 }
7043 override_android_app {
7044 name: "override_app",
7045 base: "app",
7046 package_name: "bar",
7047 }
7048 `),
7049 }))
7050
7051 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7052 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7053 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7054 }
7055
7056 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7057 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7058 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7059 }
7060}
7061
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007062func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007063 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007064 apex {
7065 name: "myapex",
7066 key: "myapex.key",
7067 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007068 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007069 }
7070
7071 apex_key {
7072 name: "myapex.key",
7073 public_key: "testkey.avbpubkey",
7074 private_key: "testkey.pem",
7075 }
7076
7077 cc_library {
7078 name: "mylib",
7079 srcs: ["mylib.cpp"],
7080 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007081 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007082 },
7083 apex_available: ["myapex"],
7084 }
7085
7086 cc_prebuilt_library_shared {
7087 name: "mylib",
7088 prefer: false,
7089 srcs: ["prebuilt.so"],
7090 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007091 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007092 },
7093 apex_available: ["myapex"],
7094 }
7095 `)
7096}
7097
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007098func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007099 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007100 apex {
7101 name: "myapex",
7102 key: "myapex.key",
7103 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007104 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007105 }
7106 apex_key {
7107 name: "myapex.key",
7108 public_key: "testkey.avbpubkey",
7109 private_key: "testkey.pem",
7110 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007111 `,
7112 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7113 variables.CompressedApex = proptools.BoolPtr(true)
7114 }),
7115 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007116
7117 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7118 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7119
7120 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7121 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7122
7123 // Make sure output of bundle is .capex
7124 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7125 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7126
7127 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007128 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007129 var builder strings.Builder
7130 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7131 androidMk := builder.String()
7132 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7133}
7134
Martin Stjernholm2856c662020-12-02 15:03:42 +00007135func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007136 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007137 apex {
7138 name: "myapex",
7139 key: "myapex.key",
7140 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007141 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007142 }
7143
7144 apex_key {
7145 name: "myapex.key",
7146 public_key: "testkey.avbpubkey",
7147 private_key: "testkey.pem",
7148 }
7149
7150 cc_library {
7151 name: "mylib",
7152 srcs: ["mylib.cpp"],
7153 apex_available: ["myapex"],
7154 shared_libs: ["otherlib"],
7155 system_shared_libs: [],
7156 }
7157
7158 cc_library {
7159 name: "otherlib",
7160 srcs: ["mylib.cpp"],
7161 stubs: {
7162 versions: ["current"],
7163 },
7164 }
7165
7166 cc_prebuilt_library_shared {
7167 name: "otherlib",
7168 prefer: true,
7169 srcs: ["prebuilt.so"],
7170 stubs: {
7171 versions: ["current"],
7172 },
7173 }
7174 `)
7175
7176 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007177 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007178 var builder strings.Builder
7179 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7180 androidMk := builder.String()
7181
7182 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7183 // a thing there.
7184 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7185}
7186
Jiyong Parke3867542020-12-03 17:28:25 +09007187func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007188 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007189 apex {
7190 name: "myapex",
7191 key: "myapex.key",
7192 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007193 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007194 }
7195
7196 apex_key {
7197 name: "myapex.key",
7198 public_key: "testkey.avbpubkey",
7199 private_key: "testkey.pem",
7200 }
7201
7202 cc_library {
7203 name: "mylib",
7204 srcs: ["mylib.cpp"],
7205 system_shared_libs: [],
7206 stl: "none",
7207 apex_available: ["myapex"],
7208 shared_libs: ["mylib2"],
7209 target: {
7210 apex: {
7211 exclude_shared_libs: ["mylib2"],
7212 },
7213 },
7214 }
7215
7216 cc_library {
7217 name: "mylib2",
7218 srcs: ["mylib.cpp"],
7219 system_shared_libs: [],
7220 stl: "none",
7221 }
7222 `)
7223
7224 // Check if mylib is linked to mylib2 for the non-apex target
7225 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7226 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7227
7228 // Make sure that the link doesn't occur for the apex target
7229 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7230 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7231
7232 // It shouldn't appear in the copy cmd as well.
7233 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7234 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7235}
7236
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007237func TestPrebuiltStubLibDep(t *testing.T) {
7238 bpBase := `
7239 apex {
7240 name: "myapex",
7241 key: "myapex.key",
7242 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007243 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007244 }
7245 apex_key {
7246 name: "myapex.key",
7247 public_key: "testkey.avbpubkey",
7248 private_key: "testkey.pem",
7249 }
7250 cc_library {
7251 name: "mylib",
7252 srcs: ["mylib.cpp"],
7253 apex_available: ["myapex"],
7254 shared_libs: ["stublib"],
7255 system_shared_libs: [],
7256 }
7257 apex {
7258 name: "otherapex",
7259 enabled: %s,
7260 key: "myapex.key",
7261 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007262 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007263 }
7264 `
7265
7266 stublibSourceBp := `
7267 cc_library {
7268 name: "stublib",
7269 srcs: ["mylib.cpp"],
7270 apex_available: ["otherapex"],
7271 system_shared_libs: [],
7272 stl: "none",
7273 stubs: {
7274 versions: ["1"],
7275 },
7276 }
7277 `
7278
7279 stublibPrebuiltBp := `
7280 cc_prebuilt_library_shared {
7281 name: "stublib",
7282 srcs: ["prebuilt.so"],
7283 apex_available: ["otherapex"],
7284 stubs: {
7285 versions: ["1"],
7286 },
7287 %s
7288 }
7289 `
7290
7291 tests := []struct {
7292 name string
7293 stublibBp string
7294 usePrebuilt bool
7295 modNames []string // Modules to collect AndroidMkEntries for
7296 otherApexEnabled []string
7297 }{
7298 {
7299 name: "only_source",
7300 stublibBp: stublibSourceBp,
7301 usePrebuilt: false,
7302 modNames: []string{"stublib"},
7303 otherApexEnabled: []string{"true", "false"},
7304 },
7305 {
7306 name: "source_preferred",
7307 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7308 usePrebuilt: false,
7309 modNames: []string{"stublib", "prebuilt_stublib"},
7310 otherApexEnabled: []string{"true", "false"},
7311 },
7312 {
7313 name: "prebuilt_preferred",
7314 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7315 usePrebuilt: true,
7316 modNames: []string{"stublib", "prebuilt_stublib"},
7317 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7318 },
7319 {
7320 name: "only_prebuilt",
7321 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7322 usePrebuilt: true,
7323 modNames: []string{"stublib"},
7324 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7325 },
7326 }
7327
7328 for _, test := range tests {
7329 t.Run(test.name, func(t *testing.T) {
7330 for _, otherApexEnabled := range test.otherApexEnabled {
7331 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007332 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007333
7334 type modAndMkEntries struct {
7335 mod *cc.Module
7336 mkEntries android.AndroidMkEntries
7337 }
7338 entries := []*modAndMkEntries{}
7339
7340 // Gather shared lib modules that are installable
7341 for _, modName := range test.modNames {
7342 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7343 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7344 continue
7345 }
7346 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007347 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007348 continue
7349 }
Colin Crossaa255532020-07-03 13:18:24 -07007350 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007351 if ent.Disabled {
7352 continue
7353 }
7354 entries = append(entries, &modAndMkEntries{
7355 mod: mod,
7356 mkEntries: ent,
7357 })
7358 }
7359 }
7360 }
7361
7362 var entry *modAndMkEntries = nil
7363 for _, ent := range entries {
7364 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7365 if entry != nil {
7366 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7367 } else {
7368 entry = ent
7369 }
7370 }
7371 }
7372
7373 if entry == nil {
7374 t.Errorf("AndroidMk entry for \"stublib\" missing")
7375 } else {
7376 isPrebuilt := entry.mod.Prebuilt() != nil
7377 if isPrebuilt != test.usePrebuilt {
7378 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7379 }
7380 if !entry.mod.IsStubs() {
7381 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7382 }
7383 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7384 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7385 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007386 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7387 expected := "-D__STUBLIB_API__=1"
7388 if !android.InList(expected, cflags) {
7389 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7390 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007391 }
7392 })
7393 }
7394 })
7395 }
7396}
7397
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007398func TestMain(m *testing.M) {
7399 run := func() int {
7400 setUp()
7401 defer tearDown()
7402
7403 return m.Run()
7404 }
7405
7406 os.Exit(run())
7407}