blob: 898b9e9484d284461334899e3b86995409f11b33 [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 Duffin2be9dcd2021-03-09 13:18:10 +000053func testApexError(t *testing.T, pattern, bp string, handlers ...interface{}) {
Jooyung Han344d5432019-08-23 11:17:39 +090054 t.Helper()
Paul Duffine05480a2021-03-08 15:07:14 +000055 testApexFixtureFactory(bp, handlers).
56 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
57 RunTest(t)
Jooyung Han5c998b92019-06-27 11:30:33 +090058}
59
Paul Duffin2be9dcd2021-03-09 13:18:10 +000060func testApex(t *testing.T, bp string, handlers ...interface{}) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090061 t.Helper()
Paul Duffine05480a2021-03-08 15:07:14 +000062 result := testApexFixtureFactory(bp, handlers).RunTest(t)
63 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090064}
65
Paul Duffine05480a2021-03-08 15:07:14 +000066// apex package specific mechanism for customizing the test configuration.
67//
68// Use FixturePreparer instances instead.
69//
70// deprecated
Jooyung Han344d5432019-08-23 11:17:39 +090071type testCustomizer func(fs map[string][]byte, config android.Config)
72
Paul Duffin810f33d2021-03-09 14:12:32 +000073func withFiles(files android.MockFS) android.FixturePreparer {
74 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090075}
76
Paul Duffin810f33d2021-03-09 14:12:32 +000077func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
78 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090079 for k, v := range targets {
80 config.Targets[k] = v
81 }
Paul Duffin810f33d2021-03-09 14:12:32 +000082 })
Jooyung Han344d5432019-08-23 11:17:39 +090083}
84
Jooyung Han35155c42020-02-06 17:33:20 +090085// withNativeBridgeTargets sets configuration with targets including:
86// - X86_64 (primary)
87// - X86 (secondary)
88// - Arm64 on X86_64 (native bridge)
89// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000090var withNativeBridgeEnabled = android.FixtureModifyConfig(
91 func(config android.Config) {
92 config.Targets[android.Android] = []android.Target{
93 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
94 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
95 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
96 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
97 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
98 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
99 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
100 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
101 }
102 },
103)
104
105func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
106 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
107 variables.ManifestPackageNameOverrides = specs
108 })
Jooyung Han35155c42020-02-06 17:33:20 +0900109}
110
Paul Duffin810f33d2021-03-09 14:12:32 +0000111var withBinder32bit = android.FixtureModifyProductVariables(
112 func(variables android.FixtureProductVariables) {
113 variables.Binder32bit = proptools.BoolPtr(true)
114 },
115)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900116
Paul Duffin810f33d2021-03-09 14:12:32 +0000117var withUnbundledBuild = android.FixtureModifyProductVariables(
118 func(variables android.FixtureProductVariables) {
119 variables.Unbundled_build = proptools.BoolPtr(true)
120 },
121)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900122
Paul Duffina02cae32021-03-09 01:44:06 +0000123var emptyFixtureFactory = android.NewFixtureFactory(&buildDir)
124
Paul Duffin37aad602021-03-08 09:47:16 +0000125var apexFixtureFactory = android.NewFixtureFactory(
126 &buildDir,
127 // General preparers in alphabetical order as test infrastructure will enforce correct
128 // registration order.
129 android.PrepareForTestWithAndroidBuildComponents,
130 bpf.PrepareForTestWithBpf,
131 cc.PrepareForTestWithCcBuildComponents,
132 java.PrepareForTestWithJavaDefaultModules,
133 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
134 rust.PrepareForTestWithRustDefaultModules,
135 sh.PrepareForTestWithShBuildComponents,
136
137 PrepareForTestWithApexBuildComponents,
138
139 // Additional apex test specific preparers.
140 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
141 filegroup {
142 name: "myapex-file_contexts",
143 srcs: [
144 "apex/myapex-file_contexts",
145 ],
146 }
147 `),
148 android.FixtureMergeMockFs(android.MockFS{
149 "a.java": nil,
150 "PrebuiltAppFoo.apk": nil,
151 "PrebuiltAppFooPriv.apk": nil,
152 "build/make/target/product/security": nil,
153 "apex_manifest.json": nil,
154 "AndroidManifest.xml": nil,
155 "system/sepolicy/apex/myapex-file_contexts": nil,
156 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
157 "system/sepolicy/apex/myapex2-file_contexts": nil,
158 "system/sepolicy/apex/otherapex-file_contexts": nil,
159 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
160 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
161 "mylib.cpp": nil,
162 "mytest.cpp": nil,
163 "mytest1.cpp": nil,
164 "mytest2.cpp": nil,
165 "mytest3.cpp": nil,
166 "myprebuilt": nil,
167 "my_include": nil,
168 "foo/bar/MyClass.java": nil,
169 "prebuilt.jar": nil,
170 "prebuilt.so": nil,
171 "vendor/foo/devkeys/test.x509.pem": nil,
172 "vendor/foo/devkeys/test.pk8": nil,
173 "testkey.x509.pem": nil,
174 "testkey.pk8": nil,
175 "testkey.override.x509.pem": nil,
176 "testkey.override.pk8": nil,
177 "vendor/foo/devkeys/testkey.avbpubkey": nil,
178 "vendor/foo/devkeys/testkey.pem": nil,
179 "NOTICE": nil,
180 "custom_notice": nil,
181 "custom_notice_for_static_lib": nil,
182 "testkey2.avbpubkey": nil,
183 "testkey2.pem": nil,
184 "myapex-arm64.apex": nil,
185 "myapex-arm.apex": nil,
186 "myapex.apks": nil,
187 "frameworks/base/api/current.txt": nil,
188 "framework/aidl/a.aidl": nil,
189 "build/make/core/proguard.flags": nil,
190 "build/make/core/proguard_basic_keeps.flags": nil,
191 "dummy.txt": nil,
192 "baz": nil,
193 "bar/baz": nil,
194 "testdata/baz": nil,
195 "AppSet.apks": nil,
196 "foo.rs": nil,
197 "libfoo.jar": nil,
198 "libbar.jar": nil,
199 },
200 ),
201
202 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
203 variables.DeviceVndkVersion = proptools.StringPtr("current")
204 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
205 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
206 variables.Platform_sdk_codename = proptools.StringPtr("Q")
207 variables.Platform_sdk_final = proptools.BoolPtr(false)
208 variables.Platform_version_active_codenames = []string{"Q"}
209 variables.Platform_vndk_version = proptools.StringPtr("VER")
210 }),
211)
212
Paul Duffin2be9dcd2021-03-09 13:18:10 +0000213func testApexFixtureFactory(bp string, handlers []interface{}) android.FixtureFactory {
214 var preparers []android.FixturePreparer
215 for _, handler := range handlers {
216 var preparer android.FixturePreparer
217 if p, ok := handler.(android.FixturePreparer); ok {
218 preparer = p
219 } else {
220 var customizer testCustomizer
221 if c, ok := handler.(testCustomizer); ok {
222 customizer = c
223 } else {
224 customizer = handler.(func(fs map[string][]byte, config android.Config))
Paul Duffine05480a2021-03-08 15:07:14 +0000225 }
Paul Duffin2be9dcd2021-03-09 13:18:10 +0000226 preparer = android.FixtureCustomPreparer(func(fixture android.Fixture) {
227 customizer(fixture.MockFS(), fixture.Config())
228 })
229 }
230 preparers = append(preparers, preparer)
231 }
232 factory := apexFixtureFactory.Extend(preparers...)
Paul Duffine05480a2021-03-08 15:07:14 +0000233 if bp != "" {
234 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
Jooyung Han344d5432019-08-23 11:17:39 +0900235 }
Paul Duffine05480a2021-03-08 15:07:14 +0000236 return factory
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237}
238
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700239func setUp() {
240 var err error
241 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700243 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245}
246
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700247func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700248 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249}
250
Jooyung Han643adc42020-02-27 13:50:06 +0900251// ensure that 'result' equals 'expected'
252func ensureEquals(t *testing.T, result string, expected string) {
253 t.Helper()
254 if result != expected {
255 t.Errorf("%q != %q", expected, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensure that 'result' contains 'expected'
260func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if !strings.Contains(result, expected) {
263 t.Errorf("%q is not found in %q", expected, result)
264 }
265}
266
Liz Kammer5bd365f2020-05-27 15:15:11 -0700267// ensure that 'result' contains 'expected' exactly one time
268func ensureContainsOnce(t *testing.T, result string, expected string) {
269 t.Helper()
270 count := strings.Count(result, expected)
271 if count != 1 {
272 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
273 }
274}
275
Jiyong Park25fc6a92018-11-18 18:02:45 +0900276// ensures that 'result' does not contain 'notExpected'
277func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900278 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 if strings.Contains(result, notExpected) {
280 t.Errorf("%q is found in %q", notExpected, result)
281 }
282}
283
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700284func ensureMatches(t *testing.T, result string, expectedRex string) {
285 ok, err := regexp.MatchString(expectedRex, result)
286 if err != nil {
287 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
288 return
289 }
290 if !ok {
291 t.Errorf("%s does not match regular expession %s", result, expectedRex)
292 }
293}
294
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900296 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 if !android.InList(expected, result) {
298 t.Errorf("%q is not found in %v", expected, result)
299 }
300}
301
302func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900303 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 if android.InList(notExpected, result) {
305 t.Errorf("%q is found in %v", notExpected, result)
306 }
307}
308
Jooyung Hane1633032019-08-01 17:41:43 +0900309func ensureListEmpty(t *testing.T, result []string) {
310 t.Helper()
311 if len(result) > 0 {
312 t.Errorf("%q is expected to be empty", result)
313 }
314}
315
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000316func ensureListNotEmpty(t *testing.T, result []string) {
317 t.Helper()
318 if len(result) == 0 {
319 t.Errorf("%q is expected to be not empty", result)
320 }
321}
322
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323// Minimal test
324func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800325 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900326 apex_defaults {
327 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900328 manifest: ":myapex.manifest",
329 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900331 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900332 native_shared_libs: [
333 "mylib",
334 "libfoo.ffi",
335 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900336 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800337 multilib: {
338 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900339 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800340 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900341 },
Jiyong Park77acec62020-06-01 21:39:15 +0900342 java_libs: [
343 "myjar",
344 "myjar_dex",
345 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000346 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900347 }
348
Jiyong Park30ca9372019-02-07 16:27:23 +0900349 apex {
350 name: "myapex",
351 defaults: ["myapex-defaults"],
352 }
353
Jiyong Park25fc6a92018-11-18 18:02:45 +0900354 apex_key {
355 name: "myapex.key",
356 public_key: "testkey.avbpubkey",
357 private_key: "testkey.pem",
358 }
359
Jiyong Park809bb722019-02-13 21:33:49 +0900360 filegroup {
361 name: "myapex.manifest",
362 srcs: ["apex_manifest.json"],
363 }
364
365 filegroup {
366 name: "myapex.androidmanifest",
367 srcs: ["AndroidManifest.xml"],
368 }
369
Jiyong Park25fc6a92018-11-18 18:02:45 +0900370 cc_library {
371 name: "mylib",
372 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900373 shared_libs: [
374 "mylib2",
375 "libbar.ffi",
376 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900377 system_shared_libs: [],
378 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000379 // TODO: remove //apex_available:platform
380 apex_available: [
381 "//apex_available:platform",
382 "myapex",
383 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900384 }
385
Alex Light3d673592019-01-18 14:37:31 -0800386 cc_binary {
387 name: "foo",
388 srcs: ["mylib.cpp"],
389 compile_multilib: "both",
390 multilib: {
391 lib32: {
392 suffix: "32",
393 },
394 lib64: {
395 suffix: "64",
396 },
397 },
398 symlinks: ["foo_link_"],
399 symlink_preferred_arch: true,
400 system_shared_libs: [],
401 static_executable: true,
402 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700403 apex_available: [ "myapex", "com.android.gki.*" ],
404 }
405
Jiyong Park99644e92020-11-17 22:21:02 +0900406 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000407 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900408 srcs: ["foo.rs"],
409 rlibs: ["libfoo.rlib.rust"],
410 dylibs: ["libfoo.dylib.rust"],
411 apex_available: ["myapex"],
412 }
413
414 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000415 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
421 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000422 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900423 srcs: ["foo.rs"],
424 crate_name: "foo",
425 apex_available: ["myapex"],
426 }
427
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900428 rust_ffi_shared {
429 name: "libfoo.ffi",
430 srcs: ["foo.rs"],
431 crate_name: "foo",
432 apex_available: ["myapex"],
433 }
434
435 rust_ffi_shared {
436 name: "libbar.ffi",
437 srcs: ["foo.rs"],
438 crate_name: "bar",
439 apex_available: ["myapex"],
440 }
441
Yifan Hongd22a84a2020-07-28 17:37:46 -0700442 apex {
443 name: "com.android.gki.fake",
444 binaries: ["foo"],
445 key: "myapex.key",
446 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000447 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800448 }
449
Paul Duffindddd5462020-04-07 15:25:44 +0100450 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900451 name: "mylib2",
452 srcs: ["mylib.cpp"],
453 system_shared_libs: [],
454 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900455 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900456 static_libs: ["libstatic"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Paul Duffindddd5462020-04-07 15:25:44 +0100464 cc_prebuilt_library_shared {
465 name: "mylib2",
466 srcs: ["prebuilt.so"],
467 // TODO: remove //apex_available:platform
468 apex_available: [
469 "//apex_available:platform",
470 "myapex",
471 ],
472 }
473
Jiyong Park9918e1a2020-03-17 19:16:40 +0900474 cc_library_static {
475 name: "libstatic",
476 srcs: ["mylib.cpp"],
477 system_shared_libs: [],
478 stl: "none",
479 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000480 // TODO: remove //apex_available:platform
481 apex_available: [
482 "//apex_available:platform",
483 "myapex",
484 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900485 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486
487 java_library {
488 name: "myjar",
489 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900490 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 sdk_version: "none",
492 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900493 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900494 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000495 // TODO: remove //apex_available:platform
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 }
501
Jiyong Park77acec62020-06-01 21:39:15 +0900502 dex_import {
503 name: "myjar_dex",
504 jars: ["prebuilt.jar"],
505 apex_available: [
506 "//apex_available:platform",
507 "myapex",
508 ],
509 }
510
Jiyong Park7f7766d2019-07-25 22:02:35 +0900511 java_library {
512 name: "myotherjar",
513 srcs: ["foo/bar/MyClass.java"],
514 sdk_version: "none",
515 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900516 // TODO: remove //apex_available:platform
517 apex_available: [
518 "//apex_available:platform",
519 "myapex",
520 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900521 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900522
523 java_library {
524 name: "mysharedjar",
525 srcs: ["foo/bar/MyClass.java"],
526 sdk_version: "none",
527 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900528 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900529 `)
530
Sundong Ahnabb64432019-10-22 13:58:29 +0900531 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900533 // Make sure that Android.mk is created
534 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700535 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900536 var builder strings.Builder
537 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
538
539 androidMk := builder.String()
540 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
541 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
542
Jiyong Park42cca6c2019-04-01 11:15:50 +0900543 optFlags := apexRule.Args["opt_flags"]
544 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700545 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900546 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900547
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548 copyCmds := apexRule.Args["copy_commands"]
549
550 // Ensure that main rule creates an output
551 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
552
553 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700554 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
556 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700561 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
562 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900563 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
564 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900565 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900566
567 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800568 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
569 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900570 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900571 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900572 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900573 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
574 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900575 // .. but not for java libs
576 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900577 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800578
Colin Cross7113d202019-11-20 16:39:12 -0800579 // Ensure that the platform variant ends with _shared or _common
580 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
581 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900582 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
583 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900584 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
585
586 // Ensure that dynamic dependency to java libs are not included
587 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800588
589 // Ensure that all symlinks are present.
590 found_foo_link_64 := false
591 found_foo := false
592 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900593 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800594 if strings.HasSuffix(cmd, "bin/foo") {
595 found_foo = true
596 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
597 found_foo_link_64 = true
598 }
599 }
600 }
601 good := found_foo && found_foo_link_64
602 if !good {
603 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
604 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900605
Sundong Ahnabb64432019-10-22 13:58:29 +0900606 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700607 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900608 if len(noticeInputs) != 3 {
609 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900610 }
611 ensureListContains(t, noticeInputs, "NOTICE")
612 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900613 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900614
Artur Satayeva8bd1132020-04-27 18:07:06 +0100615 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100616 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100617 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
618 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
619 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100620
621 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100622 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100623 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
624 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
625 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800626}
627
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800629 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900630 apex_defaults {
631 name: "myapex-defaults",
632 key: "myapex.key",
633 prebuilts: ["myetc"],
634 native_shared_libs: ["mylib"],
635 java_libs: ["myjar"],
636 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900637 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800638 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000639 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900640 }
641
642 prebuilt_etc {
643 name: "myetc",
644 src: "myprebuilt",
645 }
646
647 apex {
648 name: "myapex",
649 defaults: ["myapex-defaults"],
650 }
651
652 apex_key {
653 name: "myapex.key",
654 public_key: "testkey.avbpubkey",
655 private_key: "testkey.pem",
656 }
657
658 cc_library {
659 name: "mylib",
660 system_shared_libs: [],
661 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000662 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900663 }
664
665 java_library {
666 name: "myjar",
667 srcs: ["foo/bar/MyClass.java"],
668 sdk_version: "none",
669 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000670 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900671 }
672
673 android_app {
674 name: "AppFoo",
675 srcs: ["foo/bar/MyClass.java"],
676 sdk_version: "none",
677 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000678 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900680
681 runtime_resource_overlay {
682 name: "rro",
683 theme: "blue",
684 }
685
markchien2f59ec92020-09-02 16:23:38 +0800686 bpf {
687 name: "bpf",
688 srcs: ["bpf.c", "bpf2.c"],
689 }
690
Jooyung Hanf21c7972019-12-16 22:32:06 +0900691 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000692 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900693 "etc/myetc",
694 "javalib/myjar.jar",
695 "lib64/mylib.so",
696 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900697 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800698 "etc/bpf/bpf.o",
699 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900700 })
701}
702
Jooyung Han01a3ee22019-11-02 02:52:25 +0900703func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800704 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900705 apex {
706 name: "myapex",
707 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000708 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900709 }
710
711 apex_key {
712 name: "myapex.key",
713 public_key: "testkey.avbpubkey",
714 private_key: "testkey.pem",
715 }
716 `)
717
718 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900719 args := module.Rule("apexRule").Args
720 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
721 t.Error("manifest should be apex_manifest.pb, but " + manifest)
722 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900723}
724
Alex Light5098a612018-11-29 17:12:15 -0800725func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800726 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800727 apex {
728 name: "myapex",
729 key: "myapex.key",
730 payload_type: "zip",
731 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000732 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800733 }
734
735 apex_key {
736 name: "myapex.key",
737 public_key: "testkey.avbpubkey",
738 private_key: "testkey.pem",
739 }
740
741 cc_library {
742 name: "mylib",
743 srcs: ["mylib.cpp"],
744 shared_libs: ["mylib2"],
745 system_shared_libs: [],
746 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000747 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800748 }
749
750 cc_library {
751 name: "mylib2",
752 srcs: ["mylib.cpp"],
753 system_shared_libs: [],
754 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000755 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800756 }
757 `)
758
Sundong Ahnabb64432019-10-22 13:58:29 +0900759 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800760 copyCmds := zipApexRule.Args["copy_commands"]
761
762 // Ensure that main rule creates an output
763 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
764
765 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700766 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800767
768 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700769 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800770
771 // Ensure that both direct and indirect deps are copied into apex
772 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
773 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774}
775
776func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800777 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900778 apex {
779 name: "myapex",
780 key: "myapex.key",
781 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000782 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900783 }
784
785 apex_key {
786 name: "myapex.key",
787 public_key: "testkey.avbpubkey",
788 private_key: "testkey.pem",
789 }
790
791 cc_library {
792 name: "mylib",
793 srcs: ["mylib.cpp"],
794 shared_libs: ["mylib2", "mylib3"],
795 system_shared_libs: [],
796 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000797 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798 }
799
800 cc_library {
801 name: "mylib2",
802 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900803 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900804 system_shared_libs: [],
805 stl: "none",
806 stubs: {
807 versions: ["1", "2", "3"],
808 },
809 }
810
811 cc_library {
812 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900813 srcs: ["mylib.cpp"],
814 shared_libs: ["mylib4"],
815 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900816 stl: "none",
817 stubs: {
818 versions: ["10", "11", "12"],
819 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000820 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900821 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900822
823 cc_library {
824 name: "mylib4",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000828 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900829 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900830 `)
831
Sundong Ahnabb64432019-10-22 13:58:29 +0900832 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900833 copyCmds := apexRule.Args["copy_commands"]
834
835 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800836 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837
838 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800839 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840
841 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800842 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900843
Colin Crossaede88c2020-08-11 12:17:01 -0700844 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900845
846 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900847 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900848 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900849 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900850
851 // 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 -0700852 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900853 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700854 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900855
856 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900857 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900858 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900859
860 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700861 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900862
Jooyung Hana57af4a2020-01-23 05:36:59 +0000863 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900864 "lib64/mylib.so",
865 "lib64/mylib3.so",
866 "lib64/mylib4.so",
867 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868}
869
Colin Cross7812fd32020-09-25 12:35:10 -0700870func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
871 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800872 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700873 apex {
874 name: "myapex",
875 key: "myapex.key",
876 native_shared_libs: ["mylib", "mylib3"],
877 min_sdk_version: "29",
878 }
879
880 apex_key {
881 name: "myapex.key",
882 public_key: "testkey.avbpubkey",
883 private_key: "testkey.pem",
884 }
885
886 cc_library {
887 name: "mylib",
888 srcs: ["mylib.cpp"],
889 shared_libs: ["mylib2", "mylib3"],
890 system_shared_libs: [],
891 stl: "none",
892 apex_available: [ "myapex" ],
893 min_sdk_version: "28",
894 }
895
896 cc_library {
897 name: "mylib2",
898 srcs: ["mylib.cpp"],
899 cflags: ["-include mylib.h"],
900 system_shared_libs: [],
901 stl: "none",
902 stubs: {
903 versions: ["28", "29", "30", "current"],
904 },
905 min_sdk_version: "28",
906 }
907
908 cc_library {
909 name: "mylib3",
910 srcs: ["mylib.cpp"],
911 shared_libs: ["mylib4"],
912 system_shared_libs: [],
913 stl: "none",
914 stubs: {
915 versions: ["28", "29", "30", "current"],
916 },
917 apex_available: [ "myapex" ],
918 min_sdk_version: "28",
919 }
920
921 cc_library {
922 name: "mylib4",
923 srcs: ["mylib.cpp"],
924 system_shared_libs: [],
925 stl: "none",
926 apex_available: [ "myapex" ],
927 min_sdk_version: "28",
928 }
929 `)
930
931 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
932 copyCmds := apexRule.Args["copy_commands"]
933
934 // Ensure that direct non-stubs dep is always included
935 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
936
937 // Ensure that indirect stubs dep is not included
938 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
939
940 // Ensure that direct stubs dep is included
941 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
942
943 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
944
Jiyong Park55549df2021-02-26 23:57:23 +0900945 // Ensure that mylib is linking with the latest version of stub for mylib2
946 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700947 // ... and not linking to the non-stub (impl) variant of mylib2
948 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
949
950 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
951 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
952 // .. and not linking to the stubs variant of mylib3
953 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
954
955 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700956 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700957 ensureNotContains(t, mylib2Cflags, "-include ")
958
959 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700960 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700961
962 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
963 "lib64/mylib.so",
964 "lib64/mylib3.so",
965 "lib64/mylib4.so",
966 })
967}
968
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900969func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
970 t.Parallel()
971 // myapex (Z)
972 // mylib -----------------.
973 // |
974 // otherapex (29) |
975 // libstub's versions: 29 Z current
976 // |
977 // <platform> |
978 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800979 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900980 apex {
981 name: "myapex",
982 key: "myapex.key",
983 native_shared_libs: ["mylib"],
984 min_sdk_version: "Z", // non-final
985 }
986
987 cc_library {
988 name: "mylib",
989 srcs: ["mylib.cpp"],
990 shared_libs: ["libstub"],
991 apex_available: ["myapex"],
992 min_sdk_version: "Z",
993 }
994
995 apex_key {
996 name: "myapex.key",
997 public_key: "testkey.avbpubkey",
998 private_key: "testkey.pem",
999 }
1000
1001 apex {
1002 name: "otherapex",
1003 key: "myapex.key",
1004 native_shared_libs: ["libstub"],
1005 min_sdk_version: "29",
1006 }
1007
1008 cc_library {
1009 name: "libstub",
1010 srcs: ["mylib.cpp"],
1011 stubs: {
1012 versions: ["29", "Z", "current"],
1013 },
1014 apex_available: ["otherapex"],
1015 min_sdk_version: "29",
1016 }
1017
1018 // platform module depending on libstub from otherapex should use the latest stub("current")
1019 cc_library {
1020 name: "libplatform",
1021 srcs: ["mylib.cpp"],
1022 shared_libs: ["libstub"],
1023 }
1024 `, func(fs map[string][]byte, config android.Config) {
1025 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Z")
1026 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
1027 config.TestProductVariables.Platform_version_active_codenames = []string{"Z"}
1028 })
1029
Jiyong Park55549df2021-02-26 23:57:23 +09001030 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001031 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001032 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001033 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001034 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001035
1036 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1037 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1038 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1039 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1040 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1041}
1042
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001043func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001044 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001045 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001046 name: "myapex2",
1047 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001048 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001049 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001050 }
1051
1052 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001053 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001054 public_key: "testkey.avbpubkey",
1055 private_key: "testkey.pem",
1056 }
1057
1058 cc_library {
1059 name: "mylib",
1060 srcs: ["mylib.cpp"],
1061 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001062 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001063 system_shared_libs: [],
1064 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001065 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001066 }
1067
1068 cc_library {
1069 name: "libfoo",
1070 srcs: ["mylib.cpp"],
1071 shared_libs: ["libbar"],
1072 system_shared_libs: [],
1073 stl: "none",
1074 stubs: {
1075 versions: ["10", "20", "30"],
1076 },
1077 }
1078
1079 cc_library {
1080 name: "libbar",
1081 srcs: ["mylib.cpp"],
1082 system_shared_libs: [],
1083 stl: "none",
1084 }
1085
Jiyong Park678c8812020-02-07 17:25:49 +09001086 cc_library_static {
1087 name: "libbaz",
1088 srcs: ["mylib.cpp"],
1089 system_shared_libs: [],
1090 stl: "none",
1091 apex_available: [ "myapex2" ],
1092 }
1093
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001094 `)
1095
Jiyong Park83dc74b2020-01-14 18:38:44 +09001096 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001097 copyCmds := apexRule.Args["copy_commands"]
1098
1099 // Ensure that direct non-stubs dep is always included
1100 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1101
1102 // Ensure that indirect stubs dep is not included
1103 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1104
1105 // Ensure that dependency of stubs is not included
1106 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1107
Colin Crossaede88c2020-08-11 12:17:01 -07001108 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001109
1110 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001111 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001112 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001113 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001114
Jiyong Park3ff16992019-12-27 14:11:47 +09001115 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001116
1117 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1118 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001119
Artur Satayeva8bd1132020-04-27 18:07:06 +01001120 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001121 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001122
Artur Satayeva8bd1132020-04-27 18:07:06 +01001123 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001124 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001125}
1126
Jooyung Hand3639552019-08-09 12:57:43 +09001127func TestApexWithRuntimeLibsDependency(t *testing.T) {
1128 /*
1129 myapex
1130 |
1131 v (runtime_libs)
1132 mylib ------+------> libfoo [provides stub]
1133 |
1134 `------> libbar
1135 */
Colin Cross1c460562021-02-16 17:55:47 -08001136 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001137 apex {
1138 name: "myapex",
1139 key: "myapex.key",
1140 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001141 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001142 }
1143
1144 apex_key {
1145 name: "myapex.key",
1146 public_key: "testkey.avbpubkey",
1147 private_key: "testkey.pem",
1148 }
1149
1150 cc_library {
1151 name: "mylib",
1152 srcs: ["mylib.cpp"],
1153 runtime_libs: ["libfoo", "libbar"],
1154 system_shared_libs: [],
1155 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001156 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001157 }
1158
1159 cc_library {
1160 name: "libfoo",
1161 srcs: ["mylib.cpp"],
1162 system_shared_libs: [],
1163 stl: "none",
1164 stubs: {
1165 versions: ["10", "20", "30"],
1166 },
1167 }
1168
1169 cc_library {
1170 name: "libbar",
1171 srcs: ["mylib.cpp"],
1172 system_shared_libs: [],
1173 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001174 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001175 }
1176
1177 `)
1178
Sundong Ahnabb64432019-10-22 13:58:29 +09001179 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001180 copyCmds := apexRule.Args["copy_commands"]
1181
1182 // Ensure that direct non-stubs dep is always included
1183 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1184
1185 // Ensure that indirect stubs dep is not included
1186 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1187
1188 // Ensure that runtime_libs dep in included
1189 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1190
Sundong Ahnabb64432019-10-22 13:58:29 +09001191 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001192 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1193 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001194
1195}
1196
Paul Duffina02cae32021-03-09 01:44:06 +00001197var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1198 cc.PrepareForTestWithCcBuildComponents,
1199 PrepareForTestWithApexBuildComponents,
1200 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001201 apex {
1202 name: "com.android.runtime",
1203 key: "com.android.runtime.key",
1204 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001205 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001206 }
1207
1208 apex_key {
1209 name: "com.android.runtime.key",
1210 public_key: "testkey.avbpubkey",
1211 private_key: "testkey.pem",
1212 }
Paul Duffina02cae32021-03-09 01:44:06 +00001213 `),
1214 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1215)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001216
Paul Duffina02cae32021-03-09 01:44:06 +00001217func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1218 result := emptyFixtureFactory.Extend(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001219 cc_library {
1220 name: "libc",
1221 no_libcrt: true,
1222 nocrt: true,
1223 stl: "none",
1224 system_shared_libs: [],
1225 stubs: { versions: ["1"] },
1226 apex_available: ["com.android.runtime"],
1227
1228 sanitize: {
1229 hwaddress: true,
1230 }
1231 }
1232
1233 cc_prebuilt_library_shared {
1234 name: "libclang_rt.hwasan-aarch64-android",
1235 no_libcrt: true,
1236 nocrt: true,
1237 stl: "none",
1238 system_shared_libs: [],
1239 srcs: [""],
1240 stubs: { versions: ["1"] },
1241
1242 sanitize: {
1243 never: true,
1244 },
Paul Duffina02cae32021-03-09 01:44:06 +00001245 } `)
1246 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001247
1248 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1249 "lib64/bionic/libc.so",
1250 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1251 })
1252
1253 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1254
1255 installed := hwasan.Description("install libclang_rt.hwasan")
1256 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1257
1258 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1259 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1260 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1261}
1262
1263func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffina02cae32021-03-09 01:44:06 +00001264 result := emptyFixtureFactory.Extend(
1265 prepareForTestOfRuntimeApexWithHwasan,
1266 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1267 variables.SanitizeDevice = []string{"hwaddress"}
1268 }),
1269 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001270 cc_library {
1271 name: "libc",
1272 no_libcrt: true,
1273 nocrt: true,
1274 stl: "none",
1275 system_shared_libs: [],
1276 stubs: { versions: ["1"] },
1277 apex_available: ["com.android.runtime"],
1278 }
1279
1280 cc_prebuilt_library_shared {
1281 name: "libclang_rt.hwasan-aarch64-android",
1282 no_libcrt: true,
1283 nocrt: true,
1284 stl: "none",
1285 system_shared_libs: [],
1286 srcs: [""],
1287 stubs: { versions: ["1"] },
1288
1289 sanitize: {
1290 never: true,
1291 },
1292 }
Paul Duffina02cae32021-03-09 01:44:06 +00001293 `)
1294 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001295
1296 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1297 "lib64/bionic/libc.so",
1298 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1299 })
1300
1301 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1302
1303 installed := hwasan.Description("install libclang_rt.hwasan")
1304 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1305
1306 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1307 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1308 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1309}
1310
Jooyung Han61b66e92020-03-21 14:21:46 +00001311func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1312 testcases := []struct {
1313 name string
1314 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001315 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001316 shouldLink string
1317 shouldNotLink []string
1318 }{
1319 {
Jiyong Park55549df2021-02-26 23:57:23 +09001320 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001321 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001322 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001323 shouldLink: "30",
1324 shouldNotLink: []string{"29"},
1325 },
1326 {
Jiyong Park55549df2021-02-26 23:57:23 +09001327 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001328 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001329 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001330 shouldLink: "30",
1331 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001332 },
1333 }
1334 for _, tc := range testcases {
1335 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001336 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001337 apex {
1338 name: "myapex",
1339 key: "myapex.key",
1340 use_vendor: true,
1341 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001342 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001343 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001344 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001345
Jooyung Han61b66e92020-03-21 14:21:46 +00001346 apex_key {
1347 name: "myapex.key",
1348 public_key: "testkey.avbpubkey",
1349 private_key: "testkey.pem",
1350 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001351
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 cc_library {
1353 name: "mylib",
1354 srcs: ["mylib.cpp"],
1355 vendor_available: true,
1356 shared_libs: ["libbar"],
1357 system_shared_libs: [],
1358 stl: "none",
1359 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001360 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001361 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001362
Jooyung Han61b66e92020-03-21 14:21:46 +00001363 cc_library {
1364 name: "libbar",
1365 srcs: ["mylib.cpp"],
1366 system_shared_libs: [],
1367 stl: "none",
1368 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001369 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001370 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001371
Jooyung Han61b66e92020-03-21 14:21:46 +00001372 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001373 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001374 symbol_file: "",
1375 }
1376 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001377 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001378 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001379
Jooyung Han61b66e92020-03-21 14:21:46 +00001380 // Ensure that LLNDK dep is not included
1381 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1382 "lib64/mylib.so",
1383 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001384
Jooyung Han61b66e92020-03-21 14:21:46 +00001385 // Ensure that LLNDK dep is required
1386 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1387 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1388 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001389
Colin Crossaede88c2020-08-11 12:17:01 -07001390 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001391 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001392 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001393 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001394 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001395
Colin Crossaede88c2020-08-11 12:17:01 -07001396 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001397 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1398 })
1399 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001400}
1401
Jiyong Park25fc6a92018-11-18 18:02:45 +09001402func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001403 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001404 apex {
1405 name: "myapex",
1406 key: "myapex.key",
1407 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001408 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001409 }
1410
1411 apex_key {
1412 name: "myapex.key",
1413 public_key: "testkey.avbpubkey",
1414 private_key: "testkey.pem",
1415 }
1416
1417 cc_library {
1418 name: "mylib",
1419 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001420 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001421 shared_libs: ["libdl#27"],
1422 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001423 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001424 }
1425
1426 cc_library_shared {
1427 name: "mylib_shared",
1428 srcs: ["mylib.cpp"],
1429 shared_libs: ["libdl#27"],
1430 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001431 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001432 }
1433
1434 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001435 name: "libBootstrap",
1436 srcs: ["mylib.cpp"],
1437 stl: "none",
1438 bootstrap: true,
1439 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001440 `)
1441
Sundong Ahnabb64432019-10-22 13:58:29 +09001442 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 copyCmds := apexRule.Args["copy_commands"]
1444
1445 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001446 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001447 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1448 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001449
1450 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001451 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452
Colin Crossaede88c2020-08-11 12:17:01 -07001453 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1454 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1455 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001456
1457 // For dependency to libc
1458 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001459 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001460 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001461 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001462 // ... Cflags from stub is correctly exported to mylib
1463 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1464 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1465
1466 // For dependency to libm
1467 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001468 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001469 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001470 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001471 // ... and is not compiling with the stub
1472 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1473 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1474
1475 // For dependency to libdl
1476 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001477 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001478 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001479 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1480 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001482 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001483 // ... Cflags from stub is correctly exported to mylib
1484 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1485 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001486
1487 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001488 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1489 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1490 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1491 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001492}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001493
Jooyung Han749dc692020-04-15 11:03:39 +09001494func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001495 // there are three links between liba --> libz.
1496 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001497 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001498 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001499 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001500 apex {
1501 name: "myapex",
1502 key: "myapex.key",
1503 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001504 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001505 }
1506
1507 apex {
1508 name: "otherapex",
1509 key: "myapex.key",
1510 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001511 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001512 }
1513
1514 apex_key {
1515 name: "myapex.key",
1516 public_key: "testkey.avbpubkey",
1517 private_key: "testkey.pem",
1518 }
1519
1520 cc_library {
1521 name: "libx",
1522 shared_libs: ["liba"],
1523 system_shared_libs: [],
1524 stl: "none",
1525 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001526 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001527 }
1528
1529 cc_library {
1530 name: "liby",
1531 shared_libs: ["liba"],
1532 system_shared_libs: [],
1533 stl: "none",
1534 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001535 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001536 }
1537
1538 cc_library {
1539 name: "liba",
1540 shared_libs: ["libz"],
1541 system_shared_libs: [],
1542 stl: "none",
1543 apex_available: [
1544 "//apex_available:anyapex",
1545 "//apex_available:platform",
1546 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001547 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001548 }
1549
1550 cc_library {
1551 name: "libz",
1552 system_shared_libs: [],
1553 stl: "none",
1554 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001555 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001556 },
1557 }
Jooyung Han749dc692020-04-15 11:03:39 +09001558 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001559
1560 expectLink := func(from, from_variant, to, to_variant string) {
1561 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1562 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1563 }
1564 expectNoLink := func(from, from_variant, to, to_variant string) {
1565 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1566 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1567 }
1568 // platform liba is linked to non-stub version
1569 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001570 // liba in myapex is linked to #30
1571 expectLink("liba", "shared_apex29", "libz", "shared_30")
1572 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001573 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001574 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001575 expectLink("liba", "shared_apex30", "libz", "shared_30")
1576 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1577 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001578}
1579
Jooyung Hanaed150d2020-04-02 01:41:41 +09001580func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001581 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001582 apex {
1583 name: "myapex",
1584 key: "myapex.key",
1585 native_shared_libs: ["libx"],
1586 min_sdk_version: "R",
1587 }
1588
1589 apex_key {
1590 name: "myapex.key",
1591 public_key: "testkey.avbpubkey",
1592 private_key: "testkey.pem",
1593 }
1594
1595 cc_library {
1596 name: "libx",
1597 shared_libs: ["libz"],
1598 system_shared_libs: [],
1599 stl: "none",
1600 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001601 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001602 }
1603
1604 cc_library {
1605 name: "libz",
1606 system_shared_libs: [],
1607 stl: "none",
1608 stubs: {
1609 versions: ["29", "R"],
1610 },
1611 }
1612 `, func(fs map[string][]byte, config android.Config) {
1613 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1614 })
1615
1616 expectLink := func(from, from_variant, to, to_variant string) {
1617 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1618 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1619 }
1620 expectNoLink := func(from, from_variant, to, to_variant string) {
1621 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1622 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1623 }
Dan Albertc8060532020-07-22 22:32:17 -07001624 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001625 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1626 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001627}
1628
Jooyung Han749dc692020-04-15 11:03:39 +09001629func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001630 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001631 apex {
1632 name: "myapex",
1633 key: "myapex.key",
1634 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001635 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001636 }
1637
1638 apex_key {
1639 name: "myapex.key",
1640 public_key: "testkey.avbpubkey",
1641 private_key: "testkey.pem",
1642 }
1643
1644 cc_library {
1645 name: "libx",
1646 shared_libs: ["libz"],
1647 system_shared_libs: [],
1648 stl: "none",
1649 apex_available: [ "myapex" ],
1650 }
1651
1652 cc_library {
1653 name: "libz",
1654 system_shared_libs: [],
1655 stl: "none",
1656 stubs: {
1657 versions: ["1", "2"],
1658 },
1659 }
1660 `)
1661
1662 expectLink := func(from, from_variant, to, to_variant string) {
1663 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1664 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1665 }
1666 expectNoLink := func(from, from_variant, to, to_variant string) {
1667 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1668 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1669 }
Colin Crossaede88c2020-08-11 12:17:01 -07001670 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1671 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1672 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001673}
1674
1675func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001676 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001677 apex {
1678 name: "myapex",
1679 key: "myapex.key",
1680 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001681 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001682 }
1683
1684 apex_key {
1685 name: "myapex.key",
1686 public_key: "testkey.avbpubkey",
1687 private_key: "testkey.pem",
1688 }
1689
1690 cc_library {
1691 name: "libx",
1692 system_shared_libs: [],
1693 stl: "none",
1694 apex_available: [ "myapex" ],
1695 stubs: {
1696 versions: ["1", "2"],
1697 },
1698 }
1699
1700 cc_library {
1701 name: "libz",
1702 shared_libs: ["libx"],
1703 system_shared_libs: [],
1704 stl: "none",
1705 }
1706 `)
1707
1708 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001709 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001710 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1711 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1712 }
1713 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001714 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001715 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1716 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1717 }
1718 expectLink("libz", "shared", "libx", "shared_2")
1719 expectNoLink("libz", "shared", "libz", "shared_1")
1720 expectNoLink("libz", "shared", "libz", "shared")
1721}
1722
Jooyung Han75568392020-03-20 04:29:24 +09001723func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001724 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001725 apex {
1726 name: "myapex",
1727 key: "myapex.key",
1728 native_shared_libs: ["libx"],
1729 min_sdk_version: "29",
1730 }
1731
1732 apex_key {
1733 name: "myapex.key",
1734 public_key: "testkey.avbpubkey",
1735 private_key: "testkey.pem",
1736 }
1737
1738 cc_library {
1739 name: "libx",
1740 shared_libs: ["libbar"],
1741 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001742 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001743 }
1744
1745 cc_library {
1746 name: "libbar",
1747 stubs: {
1748 versions: ["29", "30"],
1749 },
1750 }
Jooyung Han75568392020-03-20 04:29:24 +09001751 `, func(fs map[string][]byte, config android.Config) {
1752 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1753 })
Jooyung Han03b51852020-02-26 22:45:42 +09001754 expectLink := func(from, from_variant, to, to_variant string) {
1755 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1756 libFlags := ld.Args["libFlags"]
1757 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1758 }
Colin Crossaede88c2020-08-11 12:17:01 -07001759 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001760}
1761
Jooyung Han75568392020-03-20 04:29:24 +09001762func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001763 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001764 apex {
1765 name: "myapex",
1766 key: "myapex.key",
1767 native_shared_libs: ["libx"],
1768 min_sdk_version: "29",
1769 }
1770
1771 apex_key {
1772 name: "myapex.key",
1773 public_key: "testkey.avbpubkey",
1774 private_key: "testkey.pem",
1775 }
1776
1777 cc_library {
1778 name: "libx",
1779 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001780 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001781 }
Jooyung Han75568392020-03-20 04:29:24 +09001782 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001783
1784 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001785 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001786 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001787 // note that platform variant is not.
1788 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001789 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001790}
1791
Jooyung Han749dc692020-04-15 11:03:39 +09001792func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1793 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001794 apex {
1795 name: "myapex",
1796 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001797 native_shared_libs: ["mylib"],
1798 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001799 }
1800
1801 apex_key {
1802 name: "myapex.key",
1803 public_key: "testkey.avbpubkey",
1804 private_key: "testkey.pem",
1805 }
Jooyung Han749dc692020-04-15 11:03:39 +09001806
1807 cc_library {
1808 name: "mylib",
1809 srcs: ["mylib.cpp"],
1810 system_shared_libs: [],
1811 stl: "none",
1812 apex_available: [
1813 "myapex",
1814 ],
1815 min_sdk_version: "30",
1816 }
1817 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001818
1819 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1820 apex {
1821 name: "myapex",
1822 key: "myapex.key",
1823 native_shared_libs: ["libfoo.ffi"],
1824 min_sdk_version: "29",
1825 }
1826
1827 apex_key {
1828 name: "myapex.key",
1829 public_key: "testkey.avbpubkey",
1830 private_key: "testkey.pem",
1831 }
1832
1833 rust_ffi_shared {
1834 name: "libfoo.ffi",
1835 srcs: ["foo.rs"],
1836 crate_name: "foo",
1837 apex_available: [
1838 "myapex",
1839 ],
1840 min_sdk_version: "30",
1841 }
1842 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001843}
1844
1845func TestApexMinSdkVersion_Okay(t *testing.T) {
1846 testApex(t, `
1847 apex {
1848 name: "myapex",
1849 key: "myapex.key",
1850 native_shared_libs: ["libfoo"],
1851 java_libs: ["libbar"],
1852 min_sdk_version: "29",
1853 }
1854
1855 apex_key {
1856 name: "myapex.key",
1857 public_key: "testkey.avbpubkey",
1858 private_key: "testkey.pem",
1859 }
1860
1861 cc_library {
1862 name: "libfoo",
1863 srcs: ["mylib.cpp"],
1864 shared_libs: ["libfoo_dep"],
1865 apex_available: ["myapex"],
1866 min_sdk_version: "29",
1867 }
1868
1869 cc_library {
1870 name: "libfoo_dep",
1871 srcs: ["mylib.cpp"],
1872 apex_available: ["myapex"],
1873 min_sdk_version: "29",
1874 }
1875
1876 java_library {
1877 name: "libbar",
1878 sdk_version: "current",
1879 srcs: ["a.java"],
1880 static_libs: ["libbar_dep"],
1881 apex_available: ["myapex"],
1882 min_sdk_version: "29",
1883 }
1884
1885 java_library {
1886 name: "libbar_dep",
1887 sdk_version: "current",
1888 srcs: ["a.java"],
1889 apex_available: ["myapex"],
1890 min_sdk_version: "29",
1891 }
Jooyung Han03b51852020-02-26 22:45:42 +09001892 `)
1893}
1894
Artur Satayev8cf899a2020-04-15 17:29:42 +01001895func TestJavaStableSdkVersion(t *testing.T) {
1896 testCases := []struct {
1897 name string
1898 expectedError string
1899 bp string
1900 }{
1901 {
1902 name: "Non-updatable apex with non-stable dep",
1903 bp: `
1904 apex {
1905 name: "myapex",
1906 java_libs: ["myjar"],
1907 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001908 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001909 }
1910 apex_key {
1911 name: "myapex.key",
1912 public_key: "testkey.avbpubkey",
1913 private_key: "testkey.pem",
1914 }
1915 java_library {
1916 name: "myjar",
1917 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001918 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001919 apex_available: ["myapex"],
1920 }
1921 `,
1922 },
1923 {
1924 name: "Updatable apex with stable dep",
1925 bp: `
1926 apex {
1927 name: "myapex",
1928 java_libs: ["myjar"],
1929 key: "myapex.key",
1930 updatable: true,
1931 min_sdk_version: "29",
1932 }
1933 apex_key {
1934 name: "myapex.key",
1935 public_key: "testkey.avbpubkey",
1936 private_key: "testkey.pem",
1937 }
1938 java_library {
1939 name: "myjar",
1940 srcs: ["foo/bar/MyClass.java"],
1941 sdk_version: "current",
1942 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001943 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001944 }
1945 `,
1946 },
1947 {
1948 name: "Updatable apex with non-stable dep",
1949 expectedError: "cannot depend on \"myjar\"",
1950 bp: `
1951 apex {
1952 name: "myapex",
1953 java_libs: ["myjar"],
1954 key: "myapex.key",
1955 updatable: true,
1956 }
1957 apex_key {
1958 name: "myapex.key",
1959 public_key: "testkey.avbpubkey",
1960 private_key: "testkey.pem",
1961 }
1962 java_library {
1963 name: "myjar",
1964 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001965 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001966 apex_available: ["myapex"],
1967 }
1968 `,
1969 },
1970 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001971 name: "Updatable apex with non-stable transitive dep",
1972 // This is not actually detecting that the transitive dependency is unstable, rather it is
1973 // detecting that the transitive dependency is building against a wider API surface than the
1974 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001975 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001976 bp: `
1977 apex {
1978 name: "myapex",
1979 java_libs: ["myjar"],
1980 key: "myapex.key",
1981 updatable: true,
1982 }
1983 apex_key {
1984 name: "myapex.key",
1985 public_key: "testkey.avbpubkey",
1986 private_key: "testkey.pem",
1987 }
1988 java_library {
1989 name: "myjar",
1990 srcs: ["foo/bar/MyClass.java"],
1991 sdk_version: "current",
1992 apex_available: ["myapex"],
1993 static_libs: ["transitive-jar"],
1994 }
1995 java_library {
1996 name: "transitive-jar",
1997 srcs: ["foo/bar/MyClass.java"],
1998 sdk_version: "core_platform",
1999 apex_available: ["myapex"],
2000 }
2001 `,
2002 },
2003 }
2004
2005 for _, test := range testCases {
2006 t.Run(test.name, func(t *testing.T) {
2007 if test.expectedError == "" {
2008 testApex(t, test.bp)
2009 } else {
2010 testApexError(t, test.expectedError, test.bp)
2011 }
2012 })
2013 }
2014}
2015
Jooyung Han749dc692020-04-15 11:03:39 +09002016func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2017 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2018 apex {
2019 name: "myapex",
2020 key: "myapex.key",
2021 native_shared_libs: ["mylib"],
2022 min_sdk_version: "29",
2023 }
2024
2025 apex_key {
2026 name: "myapex.key",
2027 public_key: "testkey.avbpubkey",
2028 private_key: "testkey.pem",
2029 }
2030
2031 cc_library {
2032 name: "mylib",
2033 srcs: ["mylib.cpp"],
2034 shared_libs: ["mylib2"],
2035 system_shared_libs: [],
2036 stl: "none",
2037 apex_available: [
2038 "myapex",
2039 ],
2040 min_sdk_version: "29",
2041 }
2042
2043 // indirect part of the apex
2044 cc_library {
2045 name: "mylib2",
2046 srcs: ["mylib.cpp"],
2047 system_shared_libs: [],
2048 stl: "none",
2049 apex_available: [
2050 "myapex",
2051 ],
2052 min_sdk_version: "30",
2053 }
2054 `)
2055}
2056
2057func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2058 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2059 apex {
2060 name: "myapex",
2061 key: "myapex.key",
2062 apps: ["AppFoo"],
2063 min_sdk_version: "29",
2064 }
2065
2066 apex_key {
2067 name: "myapex.key",
2068 public_key: "testkey.avbpubkey",
2069 private_key: "testkey.pem",
2070 }
2071
2072 android_app {
2073 name: "AppFoo",
2074 srcs: ["foo/bar/MyClass.java"],
2075 sdk_version: "current",
2076 min_sdk_version: "29",
2077 system_modules: "none",
2078 stl: "none",
2079 static_libs: ["bar"],
2080 apex_available: [ "myapex" ],
2081 }
2082
2083 java_library {
2084 name: "bar",
2085 sdk_version: "current",
2086 srcs: ["a.java"],
2087 apex_available: [ "myapex" ],
2088 }
2089 `)
2090}
2091
2092func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002093 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002094 apex {
2095 name: "myapex",
2096 key: "myapex.key",
2097 native_shared_libs: ["mylib"],
2098 min_sdk_version: "29",
2099 }
2100
2101 apex_key {
2102 name: "myapex.key",
2103 public_key: "testkey.avbpubkey",
2104 private_key: "testkey.pem",
2105 }
2106
Jiyong Park55549df2021-02-26 23:57:23 +09002107 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002108 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2109 cc_library {
2110 name: "mylib",
2111 srcs: ["mylib.cpp"],
2112 shared_libs: ["mylib2"],
2113 system_shared_libs: [],
2114 stl: "none",
2115 apex_available: ["myapex", "otherapex"],
2116 min_sdk_version: "29",
2117 }
2118
2119 cc_library {
2120 name: "mylib2",
2121 srcs: ["mylib.cpp"],
2122 system_shared_libs: [],
2123 stl: "none",
2124 apex_available: ["otherapex"],
2125 stubs: { versions: ["29", "30"] },
2126 min_sdk_version: "30",
2127 }
2128
2129 apex {
2130 name: "otherapex",
2131 key: "myapex.key",
2132 native_shared_libs: ["mylib", "mylib2"],
2133 min_sdk_version: "30",
2134 }
2135 `)
2136 expectLink := func(from, from_variant, to, to_variant string) {
2137 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2138 libFlags := ld.Args["libFlags"]
2139 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2140 }
Jiyong Park55549df2021-02-26 23:57:23 +09002141 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002142 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002143}
2144
Jooyung Haned124c32021-01-26 11:43:46 +09002145func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
2146 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2147 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2148 config.TestProductVariables.Platform_version_active_codenames = []string{"S"}
2149 }
2150 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2151 apex {
2152 name: "myapex",
2153 key: "myapex.key",
2154 native_shared_libs: ["libfoo"],
2155 min_sdk_version: "S",
2156 }
2157 apex_key {
2158 name: "myapex.key",
2159 public_key: "testkey.avbpubkey",
2160 private_key: "testkey.pem",
2161 }
2162 cc_library {
2163 name: "libfoo",
2164 shared_libs: ["libbar"],
2165 apex_available: ["myapex"],
2166 min_sdk_version: "29",
2167 }
2168 cc_library {
2169 name: "libbar",
2170 apex_available: ["myapex"],
2171 }
2172 `, withSAsActiveCodeNames)
2173}
2174
2175func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
2176 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2177 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2178 config.TestProductVariables.Platform_version_active_codenames = []string{"S", "T"}
2179 }
Colin Cross1c460562021-02-16 17:55:47 -08002180 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002181 apex {
2182 name: "myapex",
2183 key: "myapex.key",
2184 native_shared_libs: ["libfoo"],
2185 min_sdk_version: "S",
2186 }
2187 apex_key {
2188 name: "myapex.key",
2189 public_key: "testkey.avbpubkey",
2190 private_key: "testkey.pem",
2191 }
2192 cc_library {
2193 name: "libfoo",
2194 shared_libs: ["libbar"],
2195 apex_available: ["myapex"],
2196 min_sdk_version: "S",
2197 }
2198 cc_library {
2199 name: "libbar",
2200 stubs: {
2201 symbol_file: "libbar.map.txt",
2202 versions: ["30", "S", "T"],
2203 },
2204 }
2205 `, withSAsActiveCodeNames)
2206
2207 // ensure libfoo is linked with "S" version of libbar stub
2208 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2209 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002210 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002211}
2212
Jiyong Park7c2ee712018-12-07 00:42:25 +09002213func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002214 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002215 apex {
2216 name: "myapex",
2217 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002218 native_shared_libs: ["mylib"],
2219 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002220 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002221 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002222 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002223 }
2224
2225 apex_key {
2226 name: "myapex.key",
2227 public_key: "testkey.avbpubkey",
2228 private_key: "testkey.pem",
2229 }
2230
2231 prebuilt_etc {
2232 name: "myetc",
2233 src: "myprebuilt",
2234 sub_dir: "foo/bar",
2235 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002236
2237 cc_library {
2238 name: "mylib",
2239 srcs: ["mylib.cpp"],
2240 relative_install_path: "foo/bar",
2241 system_shared_libs: [],
2242 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002243 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002244 }
2245
2246 cc_binary {
2247 name: "mybin",
2248 srcs: ["mylib.cpp"],
2249 relative_install_path: "foo/bar",
2250 system_shared_libs: [],
2251 static_executable: true,
2252 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002253 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002254 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002255 `)
2256
Sundong Ahnabb64432019-10-22 13:58:29 +09002257 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002258 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2259
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002260 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002261 ensureListContains(t, dirs, "etc")
2262 ensureListContains(t, dirs, "etc/foo")
2263 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002264 ensureListContains(t, dirs, "lib64")
2265 ensureListContains(t, dirs, "lib64/foo")
2266 ensureListContains(t, dirs, "lib64/foo/bar")
2267 ensureListContains(t, dirs, "lib")
2268 ensureListContains(t, dirs, "lib/foo")
2269 ensureListContains(t, dirs, "lib/foo/bar")
2270
Jiyong Parkbd13e442019-03-15 18:10:35 +09002271 ensureListContains(t, dirs, "bin")
2272 ensureListContains(t, dirs, "bin/foo")
2273 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002274}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002275
Jooyung Han35155c42020-02-06 17:33:20 +09002276func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002277 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002278 apex {
2279 name: "myapex",
2280 key: "myapex.key",
2281 multilib: {
2282 both: {
2283 native_shared_libs: ["mylib"],
2284 binaries: ["mybin"],
2285 },
2286 },
2287 compile_multilib: "both",
2288 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002289 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002290 }
2291
2292 apex_key {
2293 name: "myapex.key",
2294 public_key: "testkey.avbpubkey",
2295 private_key: "testkey.pem",
2296 }
2297
2298 cc_library {
2299 name: "mylib",
2300 relative_install_path: "foo/bar",
2301 system_shared_libs: [],
2302 stl: "none",
2303 apex_available: [ "myapex" ],
2304 native_bridge_supported: true,
2305 }
2306
2307 cc_binary {
2308 name: "mybin",
2309 relative_install_path: "foo/bar",
2310 system_shared_libs: [],
2311 static_executable: true,
2312 stl: "none",
2313 apex_available: [ "myapex" ],
2314 native_bridge_supported: true,
2315 compile_multilib: "both", // default is "first" for binary
2316 multilib: {
2317 lib64: {
2318 suffix: "64",
2319 },
2320 },
2321 }
2322 `, withNativeBridgeEnabled)
2323 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2324 "bin/foo/bar/mybin",
2325 "bin/foo/bar/mybin64",
2326 "bin/arm/foo/bar/mybin",
2327 "bin/arm64/foo/bar/mybin64",
2328 "lib/foo/bar/mylib.so",
2329 "lib/arm/foo/bar/mylib.so",
2330 "lib64/foo/bar/mylib.so",
2331 "lib64/arm64/foo/bar/mylib.so",
2332 })
2333}
2334
Jiyong Parkda6eb592018-12-19 17:12:36 +09002335func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002336 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002337 apex {
2338 name: "myapex",
2339 key: "myapex.key",
2340 native_shared_libs: ["mylib"],
2341 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002342 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002343 }
2344
2345 apex_key {
2346 name: "myapex.key",
2347 public_key: "testkey.avbpubkey",
2348 private_key: "testkey.pem",
2349 }
2350
2351 cc_library {
2352 name: "mylib",
2353 srcs: ["mylib.cpp"],
2354 shared_libs: ["mylib2"],
2355 system_shared_libs: [],
2356 vendor_available: true,
2357 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002358 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002359 }
2360
2361 cc_library {
2362 name: "mylib2",
2363 srcs: ["mylib.cpp"],
2364 system_shared_libs: [],
2365 vendor_available: true,
2366 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002367 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002368 }
Jooyung Handc782442019-11-01 03:14:38 +09002369 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002370 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002371 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002372
2373 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002374 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002375 for _, implicit := range i.Implicits {
2376 inputsList = append(inputsList, implicit.String())
2377 }
2378 }
2379 inputsString := strings.Join(inputsList, " ")
2380
2381 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002382 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2383 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002384
2385 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002386 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2387 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002388}
Jiyong Park16e91a02018-12-20 18:18:08 +09002389
Jooyung Han85d61762020-06-24 23:50:26 +09002390func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002391 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2392 apex {
2393 name: "myapex",
2394 key: "myapex.key",
2395 use_vendor: true,
2396 }
2397 apex_key {
2398 name: "myapex.key",
2399 public_key: "testkey.avbpubkey",
2400 private_key: "testkey.pem",
2401 }
2402 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002403 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002404 })
Colin Cross440e0d02020-06-11 11:32:11 -07002405 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002406 testApex(t, `
2407 apex {
2408 name: "myapex",
2409 key: "myapex.key",
2410 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002411 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002412 }
2413 apex_key {
2414 name: "myapex.key",
2415 public_key: "testkey.avbpubkey",
2416 private_key: "testkey.pem",
2417 }
2418 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002419 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002420 })
2421}
2422
Jooyung Han5c998b92019-06-27 11:30:33 +09002423func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2424 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2425 apex {
2426 name: "myapex",
2427 key: "myapex.key",
2428 native_shared_libs: ["mylib"],
2429 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002430 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002431 }
2432
2433 apex_key {
2434 name: "myapex.key",
2435 public_key: "testkey.avbpubkey",
2436 private_key: "testkey.pem",
2437 }
2438
2439 cc_library {
2440 name: "mylib",
2441 srcs: ["mylib.cpp"],
2442 system_shared_libs: [],
2443 stl: "none",
2444 }
2445 `)
2446}
2447
Jooyung Han85d61762020-06-24 23:50:26 +09002448func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002449 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 binaries: ["mybin"],
2454 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002455 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002456 }
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462 cc_binary {
2463 name: "mybin",
2464 vendor: true,
2465 shared_libs: ["libfoo"],
2466 }
2467 cc_library {
2468 name: "libfoo",
2469 proprietary: true,
2470 }
2471 `)
2472
2473 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2474 "bin/mybin",
2475 "lib64/libfoo.so",
2476 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2477 "lib64/libc++.so",
2478 })
2479
2480 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002481 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002482 name := apexBundle.BaseModuleName()
2483 prefix := "TARGET_"
2484 var builder strings.Builder
2485 data.Custom(&builder, name, prefix, "", data)
2486 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002487 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2488 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002489
2490 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2491 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2492 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002493}
2494
Jooyung Handf78e212020-07-22 15:54:47 +09002495func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002496 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002497 apex {
2498 name: "myapex",
2499 key: "myapex.key",
2500 binaries: ["mybin"],
2501 vendor: true,
2502 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002503 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002504 }
2505 apex_key {
2506 name: "myapex.key",
2507 public_key: "testkey.avbpubkey",
2508 private_key: "testkey.pem",
2509 }
2510 cc_binary {
2511 name: "mybin",
2512 vendor: true,
2513 shared_libs: ["libvndk", "libvendor"],
2514 }
2515 cc_library {
2516 name: "libvndk",
2517 vndk: {
2518 enabled: true,
2519 },
2520 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002521 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002522 }
2523 cc_library {
2524 name: "libvendor",
2525 vendor: true,
2526 }
2527 `)
2528
2529 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2530
Colin Crossaede88c2020-08-11 12:17:01 -07002531 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002532 libs := names(ldRule.Args["libFlags"])
2533 // VNDK libs(libvndk/libc++) as they are
2534 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002535 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002536 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002537 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002538
2539 // VNDK libs are not included when use_vndk_as_stable: true
2540 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2541 "bin/mybin",
2542 "lib64/libvendor.so",
2543 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002544
2545 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2546 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2547 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002548}
2549
Justin Yun13decfb2021-03-08 19:25:55 +09002550func TestProductVariant(t *testing.T) {
2551 ctx := testApex(t, `
2552 apex {
2553 name: "myapex",
2554 key: "myapex.key",
2555 updatable: false,
2556 product_specific: true,
2557 binaries: ["foo"],
2558 }
2559
2560 apex_key {
2561 name: "myapex.key",
2562 public_key: "testkey.avbpubkey",
2563 private_key: "testkey.pem",
2564 }
2565
2566 cc_binary {
2567 name: "foo",
2568 product_available: true,
2569 apex_available: ["myapex"],
2570 srcs: ["foo.cpp"],
2571 }
2572 `, func(fs map[string][]byte, config android.Config) {
2573 config.TestProductVariables.ProductVndkVersion = proptools.StringPtr("current")
2574 })
2575
2576 cflags := strings.Fields(
2577 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2578 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2579 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2580 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2581 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2582}
2583
Jooyung Han8e5685d2020-09-21 11:02:57 +09002584func TestApex_withPrebuiltFirmware(t *testing.T) {
2585 testCases := []struct {
2586 name string
2587 additionalProp string
2588 }{
2589 {"system apex with prebuilt_firmware", ""},
2590 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2591 }
2592 for _, tc := range testCases {
2593 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002594 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002595 apex {
2596 name: "myapex",
2597 key: "myapex.key",
2598 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002599 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002600 `+tc.additionalProp+`
2601 }
2602 apex_key {
2603 name: "myapex.key",
2604 public_key: "testkey.avbpubkey",
2605 private_key: "testkey.pem",
2606 }
2607 prebuilt_firmware {
2608 name: "myfirmware",
2609 src: "myfirmware.bin",
2610 filename_from_src: true,
2611 `+tc.additionalProp+`
2612 }
2613 `)
2614 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2615 "etc/firmware/myfirmware.bin",
2616 })
2617 })
2618 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002619}
2620
Jooyung Hanefb184e2020-06-25 17:14:25 +09002621func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002622 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002623 apex {
2624 name: "myapex",
2625 key: "myapex.key",
2626 use_vendor: true,
2627 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002628 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002629 }
2630
2631 apex_key {
2632 name: "myapex.key",
2633 public_key: "testkey.avbpubkey",
2634 private_key: "testkey.pem",
2635 }
2636
2637 cc_library {
2638 name: "mylib",
2639 vendor_available: true,
2640 apex_available: ["myapex"],
2641 }
2642 `, func(fs map[string][]byte, config android.Config) {
2643 setUseVendorAllowListForTest(config, []string{"myapex"})
2644 })
2645
2646 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002647 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002648 name := apexBundle.BaseModuleName()
2649 prefix := "TARGET_"
2650 var builder strings.Builder
2651 data.Custom(&builder, name, prefix, "", data)
2652 androidMk := builder.String()
2653 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2654}
2655
2656func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002657 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002658 apex {
2659 name: "myapex",
2660 key: "myapex.key",
2661 vendor: true,
2662 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002663 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002664 }
2665
2666 apex_key {
2667 name: "myapex.key",
2668 public_key: "testkey.avbpubkey",
2669 private_key: "testkey.pem",
2670 }
2671
2672 cc_library {
2673 name: "mylib",
2674 vendor_available: true,
2675 }
2676 `)
2677
2678 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002679 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002680 name := apexBundle.BaseModuleName()
2681 prefix := "TARGET_"
2682 var builder strings.Builder
2683 data.Custom(&builder, name, prefix, "", data)
2684 androidMk := builder.String()
2685 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2686}
2687
Jooyung Han2ed99d02020-06-24 23:26:26 +09002688func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002689 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002690 apex {
2691 name: "myapex",
2692 key: "myapex.key",
2693 vintf_fragments: ["fragment.xml"],
2694 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002695 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002696 }
2697 apex_key {
2698 name: "myapex.key",
2699 public_key: "testkey.avbpubkey",
2700 private_key: "testkey.pem",
2701 }
2702 cc_binary {
2703 name: "mybin",
2704 }
2705 `)
2706
2707 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002708 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002709 name := apexBundle.BaseModuleName()
2710 prefix := "TARGET_"
2711 var builder strings.Builder
2712 data.Custom(&builder, name, prefix, "", data)
2713 androidMk := builder.String()
2714 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2715 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2716}
2717
Jiyong Park16e91a02018-12-20 18:18:08 +09002718func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002719 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002720 apex {
2721 name: "myapex",
2722 key: "myapex.key",
2723 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002724 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002725 }
2726
2727 apex_key {
2728 name: "myapex.key",
2729 public_key: "testkey.avbpubkey",
2730 private_key: "testkey.pem",
2731 }
2732
2733 cc_library {
2734 name: "mylib",
2735 srcs: ["mylib.cpp"],
2736 system_shared_libs: [],
2737 stl: "none",
2738 stubs: {
2739 versions: ["1", "2", "3"],
2740 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002741 apex_available: [
2742 "//apex_available:platform",
2743 "myapex",
2744 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002745 }
2746
2747 cc_binary {
2748 name: "not_in_apex",
2749 srcs: ["mylib.cpp"],
2750 static_libs: ["mylib"],
2751 static_executable: true,
2752 system_shared_libs: [],
2753 stl: "none",
2754 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002755 `)
2756
Colin Cross7113d202019-11-20 16:39:12 -08002757 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002758
2759 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002760 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002761}
Jiyong Park9335a262018-12-24 11:31:58 +09002762
2763func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002764 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002765 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002766 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002767 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002768 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002769 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002770 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002771 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002772 }
2773
2774 cc_library {
2775 name: "mylib",
2776 srcs: ["mylib.cpp"],
2777 system_shared_libs: [],
2778 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002779 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002780 }
2781
2782 apex_key {
2783 name: "myapex.key",
2784 public_key: "testkey.avbpubkey",
2785 private_key: "testkey.pem",
2786 }
2787
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002788 android_app_certificate {
2789 name: "myapex.certificate",
2790 certificate: "testkey",
2791 }
2792
2793 android_app_certificate {
2794 name: "myapex.certificate.override",
2795 certificate: "testkey.override",
2796 }
2797
Jiyong Park9335a262018-12-24 11:31:58 +09002798 `)
2799
2800 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002801 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002802
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002803 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2804 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002805 "vendor/foo/devkeys/testkey.avbpubkey")
2806 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002807 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2808 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002809 "vendor/foo/devkeys/testkey.pem")
2810 }
2811
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002812 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002813 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002814 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002815 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002816 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002817 }
2818}
Jiyong Park58e364a2019-01-19 19:24:06 +09002819
Jooyung Hanf121a652019-12-17 14:30:11 +09002820func TestCertificate(t *testing.T) {
2821 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002822 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002823 apex {
2824 name: "myapex",
2825 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002826 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002827 }
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }`)
2833 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2834 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2835 if actual := rule.Args["certificates"]; actual != expected {
2836 t.Errorf("certificates should be %q, not %q", expected, actual)
2837 }
2838 })
2839 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002840 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002841 apex {
2842 name: "myapex_keytest",
2843 key: "myapex.key",
2844 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002845 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002846 }
2847 apex_key {
2848 name: "myapex.key",
2849 public_key: "testkey.avbpubkey",
2850 private_key: "testkey.pem",
2851 }
2852 android_app_certificate {
2853 name: "myapex.certificate.override",
2854 certificate: "testkey.override",
2855 }`)
2856 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2857 expected := "testkey.override.x509.pem testkey.override.pk8"
2858 if actual := rule.Args["certificates"]; actual != expected {
2859 t.Errorf("certificates should be %q, not %q", expected, actual)
2860 }
2861 })
2862 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002863 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002864 apex {
2865 name: "myapex",
2866 key: "myapex.key",
2867 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002868 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002869 }
2870 apex_key {
2871 name: "myapex.key",
2872 public_key: "testkey.avbpubkey",
2873 private_key: "testkey.pem",
2874 }
2875 android_app_certificate {
2876 name: "myapex.certificate",
2877 certificate: "testkey",
2878 }`)
2879 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2880 expected := "testkey.x509.pem testkey.pk8"
2881 if actual := rule.Args["certificates"]; actual != expected {
2882 t.Errorf("certificates should be %q, not %q", expected, actual)
2883 }
2884 })
2885 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002886 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002887 apex {
2888 name: "myapex_keytest",
2889 key: "myapex.key",
2890 file_contexts: ":myapex-file_contexts",
2891 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002892 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002893 }
2894 apex_key {
2895 name: "myapex.key",
2896 public_key: "testkey.avbpubkey",
2897 private_key: "testkey.pem",
2898 }
2899 android_app_certificate {
2900 name: "myapex.certificate.override",
2901 certificate: "testkey.override",
2902 }`)
2903 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2904 expected := "testkey.override.x509.pem testkey.override.pk8"
2905 if actual := rule.Args["certificates"]; actual != expected {
2906 t.Errorf("certificates should be %q, not %q", expected, actual)
2907 }
2908 })
2909 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002910 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002911 apex {
2912 name: "myapex",
2913 key: "myapex.key",
2914 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002915 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002916 }
2917 apex_key {
2918 name: "myapex.key",
2919 public_key: "testkey.avbpubkey",
2920 private_key: "testkey.pem",
2921 }`)
2922 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2923 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2924 if actual := rule.Args["certificates"]; actual != expected {
2925 t.Errorf("certificates should be %q, not %q", expected, actual)
2926 }
2927 })
2928 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002929 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002930 apex {
2931 name: "myapex_keytest",
2932 key: "myapex.key",
2933 file_contexts: ":myapex-file_contexts",
2934 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002935 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002936 }
2937 apex_key {
2938 name: "myapex.key",
2939 public_key: "testkey.avbpubkey",
2940 private_key: "testkey.pem",
2941 }
2942 android_app_certificate {
2943 name: "myapex.certificate.override",
2944 certificate: "testkey.override",
2945 }`)
2946 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2947 expected := "testkey.override.x509.pem testkey.override.pk8"
2948 if actual := rule.Args["certificates"]; actual != expected {
2949 t.Errorf("certificates should be %q, not %q", expected, actual)
2950 }
2951 })
2952}
2953
Jiyong Park58e364a2019-01-19 19:24:06 +09002954func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002955 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002956 apex {
2957 name: "myapex",
2958 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002959 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002960 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002961 }
2962
2963 apex {
2964 name: "otherapex",
2965 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002966 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002967 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002968 }
2969
2970 apex_key {
2971 name: "myapex.key",
2972 public_key: "testkey.avbpubkey",
2973 private_key: "testkey.pem",
2974 }
2975
2976 cc_library {
2977 name: "mylib",
2978 srcs: ["mylib.cpp"],
2979 system_shared_libs: [],
2980 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002981 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002982 "myapex",
2983 "otherapex",
2984 ],
Jooyung Han24282772020-03-21 23:20:55 +09002985 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002986 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002987 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002988 cc_library {
2989 name: "mylib2",
2990 srcs: ["mylib.cpp"],
2991 system_shared_libs: [],
2992 stl: "none",
2993 apex_available: [
2994 "myapex",
2995 "otherapex",
2996 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002997 static_libs: ["mylib3"],
2998 recovery_available: true,
2999 min_sdk_version: "29",
3000 }
3001 cc_library {
3002 name: "mylib3",
3003 srcs: ["mylib.cpp"],
3004 system_shared_libs: [],
3005 stl: "none",
3006 apex_available: [
3007 "myapex",
3008 "otherapex",
3009 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003010 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003011 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003012 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003013 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003014 `)
3015
Jooyung Hanc87a0592020-03-02 17:44:33 +09003016 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003017 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003018 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003019 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003020
Jooyung Hanccce2f22020-03-07 03:45:53 +09003021 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003022 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003023 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003024 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003025 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003026
Jooyung Hanccce2f22020-03-07 03:45:53 +09003027 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003028 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003029 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003030 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003031 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003032
Colin Crossaede88c2020-08-11 12:17:01 -07003033 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3034 // each variant defines additional macros to distinguish which apex variant it is built for
3035
3036 // non-APEX variant does not have __ANDROID_APEX__ defined
3037 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3038 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3039
3040 // APEX variant has __ANDROID_APEX__ defined
3041 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3042 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3043 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3044 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3045
3046 // APEX variant has __ANDROID_APEX__ defined
3047 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3048 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3049 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3050 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3051
Dan Albertb19953d2020-11-17 15:29:36 -08003052 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003053 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3054 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003055 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003056
3057 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3058 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003059
3060 // non-APEX variant does not have __ANDROID_APEX__ defined
3061 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3062 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3063
3064 // APEX variant has __ANDROID_APEX__ defined
3065 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003066 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003067 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003068 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003069
Jooyung Hanc87a0592020-03-02 17:44:33 +09003070 // APEX variant has __ANDROID_APEX__ defined
3071 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003072 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003073 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003074 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003075
Dan Albertb19953d2020-11-17 15:29:36 -08003076 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003077 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003078 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003079 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003080}
Jiyong Park7e636d02019-01-28 16:16:54 +09003081
3082func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003083 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003084 apex {
3085 name: "myapex",
3086 key: "myapex.key",
3087 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003088 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003089 }
3090
3091 apex_key {
3092 name: "myapex.key",
3093 public_key: "testkey.avbpubkey",
3094 private_key: "testkey.pem",
3095 }
3096
3097 cc_library_headers {
3098 name: "mylib_headers",
3099 export_include_dirs: ["my_include"],
3100 system_shared_libs: [],
3101 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003102 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003103 }
3104
3105 cc_library {
3106 name: "mylib",
3107 srcs: ["mylib.cpp"],
3108 system_shared_libs: [],
3109 stl: "none",
3110 header_libs: ["mylib_headers"],
3111 export_header_lib_headers: ["mylib_headers"],
3112 stubs: {
3113 versions: ["1", "2", "3"],
3114 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003115 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003116 }
3117
3118 cc_library {
3119 name: "otherlib",
3120 srcs: ["mylib.cpp"],
3121 system_shared_libs: [],
3122 stl: "none",
3123 shared_libs: ["mylib"],
3124 }
3125 `)
3126
Colin Cross7113d202019-11-20 16:39:12 -08003127 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003128
3129 // Ensure that the include path of the header lib is exported to 'otherlib'
3130 ensureContains(t, cFlags, "-Imy_include")
3131}
Alex Light9670d332019-01-29 18:07:33 -08003132
Jiyong Park7cd10e32020-01-14 09:22:18 +09003133type fileInApex struct {
3134 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003135 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003136 isLink bool
3137}
3138
Jooyung Hana57af4a2020-01-23 05:36:59 +00003139func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003140 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003141 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003142 copyCmds := apexRule.Args["copy_commands"]
3143 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003144 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003145 for _, cmd := range strings.Split(copyCmds, "&&") {
3146 cmd = strings.TrimSpace(cmd)
3147 if cmd == "" {
3148 continue
3149 }
3150 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003151 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003152 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003153 switch terms[0] {
3154 case "mkdir":
3155 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003156 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003157 t.Fatal("copyCmds contains invalid cp command", cmd)
3158 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003159 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003160 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003161 isLink = false
3162 case "ln":
3163 if len(terms) != 3 && len(terms) != 4 {
3164 // ln LINK TARGET or ln -s LINK TARGET
3165 t.Fatal("copyCmds contains invalid ln command", cmd)
3166 }
3167 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003168 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003169 isLink = true
3170 default:
3171 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3172 }
3173 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003174 index := strings.Index(dst, imageApexDir)
3175 if index == -1 {
3176 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3177 }
3178 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003179 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003180 }
3181 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003182 return ret
3183}
3184
Jooyung Hana57af4a2020-01-23 05:36:59 +00003185func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3186 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003187 var failed bool
3188 var surplus []string
3189 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003190 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003191 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003192 for _, expected := range files {
3193 if matched, _ := path.Match(expected, file.path); matched {
3194 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003195 mactchFound = true
3196 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003197 }
3198 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003199 if !mactchFound {
3200 surplus = append(surplus, file.path)
3201 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003202 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003203
Jooyung Han31c470b2019-10-18 16:26:59 +09003204 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003205 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003206 t.Log("surplus files", surplus)
3207 failed = true
3208 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003209
3210 if len(files) > len(filesMatched) {
3211 var missing []string
3212 for _, expected := range files {
3213 if !filesMatched[expected] {
3214 missing = append(missing, expected)
3215 }
3216 }
3217 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003218 t.Log("missing files", missing)
3219 failed = true
3220 }
3221 if failed {
3222 t.Fail()
3223 }
3224}
3225
Jooyung Han344d5432019-08-23 11:17:39 +09003226func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003227 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003228 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003229 name: "com.android.vndk.current",
3230 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003231 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003232 }
3233
3234 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003235 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003236 public_key: "testkey.avbpubkey",
3237 private_key: "testkey.pem",
3238 }
3239
3240 cc_library {
3241 name: "libvndk",
3242 srcs: ["mylib.cpp"],
3243 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003244 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003245 vndk: {
3246 enabled: true,
3247 },
3248 system_shared_libs: [],
3249 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003250 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003251 }
3252
3253 cc_library {
3254 name: "libvndksp",
3255 srcs: ["mylib.cpp"],
3256 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003257 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003258 vndk: {
3259 enabled: true,
3260 support_system_process: true,
3261 },
3262 system_shared_libs: [],
3263 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003264 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003265 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003266 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003267
Colin Cross2807f002021-03-02 10:15:29 -08003268 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003269 "lib/libvndk.so",
3270 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003271 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003272 "lib64/libvndk.so",
3273 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003274 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003275 "etc/llndk.libraries.VER.txt",
3276 "etc/vndkcore.libraries.VER.txt",
3277 "etc/vndksp.libraries.VER.txt",
3278 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003279 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003280 })
Jooyung Han344d5432019-08-23 11:17:39 +09003281}
3282
3283func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003284 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003285 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003286 name: "com.android.vndk.current",
3287 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003288 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003289 }
3290
3291 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003292 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003293 public_key: "testkey.avbpubkey",
3294 private_key: "testkey.pem",
3295 }
3296
3297 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003298 name: "libvndk",
3299 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003300 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003301 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003302 vndk: {
3303 enabled: true,
3304 },
3305 system_shared_libs: [],
3306 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003307 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003308 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003309
3310 cc_prebuilt_library_shared {
3311 name: "libvndk.arm",
3312 srcs: ["libvndk.arm.so"],
3313 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003314 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003315 vndk: {
3316 enabled: true,
3317 },
3318 enabled: false,
3319 arch: {
3320 arm: {
3321 enabled: true,
3322 },
3323 },
3324 system_shared_libs: [],
3325 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003326 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003327 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003328 `+vndkLibrariesTxtFiles("current"),
3329 withFiles(map[string][]byte{
3330 "libvndk.so": nil,
3331 "libvndk.arm.so": nil,
3332 }))
Colin Cross2807f002021-03-02 10:15:29 -08003333 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003334 "lib/libvndk.so",
3335 "lib/libvndk.arm.so",
3336 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003337 "lib/libc++.so",
3338 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003339 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003340 })
Jooyung Han344d5432019-08-23 11:17:39 +09003341}
3342
Jooyung Han39edb6c2019-11-06 16:53:07 +09003343func vndkLibrariesTxtFiles(vers ...string) (result string) {
3344 for _, v := range vers {
3345 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003346 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003347 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003348 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003349 name: "` + txt + `.libraries.txt",
3350 }
3351 `
3352 }
3353 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003354 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003355 result += `
3356 prebuilt_etc {
3357 name: "` + txt + `.libraries.` + v + `.txt",
3358 src: "dummy.txt",
3359 }
3360 `
3361 }
3362 }
3363 }
3364 return
3365}
3366
Jooyung Han344d5432019-08-23 11:17:39 +09003367func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003368 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003369 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003370 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003371 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003372 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003373 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003374 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003375 }
3376
3377 apex_key {
3378 name: "myapex.key",
3379 public_key: "testkey.avbpubkey",
3380 private_key: "testkey.pem",
3381 }
3382
Jooyung Han31c470b2019-10-18 16:26:59 +09003383 vndk_prebuilt_shared {
3384 name: "libvndk27",
3385 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003386 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003387 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003388 vndk: {
3389 enabled: true,
3390 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003391 target_arch: "arm64",
3392 arch: {
3393 arm: {
3394 srcs: ["libvndk27_arm.so"],
3395 },
3396 arm64: {
3397 srcs: ["libvndk27_arm64.so"],
3398 },
3399 },
Colin Cross2807f002021-03-02 10:15:29 -08003400 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003401 }
3402
3403 vndk_prebuilt_shared {
3404 name: "libvndk27",
3405 version: "27",
3406 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003407 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003408 vndk: {
3409 enabled: true,
3410 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003411 target_arch: "x86_64",
3412 arch: {
3413 x86: {
3414 srcs: ["libvndk27_x86.so"],
3415 },
3416 x86_64: {
3417 srcs: ["libvndk27_x86_64.so"],
3418 },
3419 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003420 }
3421 `+vndkLibrariesTxtFiles("27"),
3422 withFiles(map[string][]byte{
3423 "libvndk27_arm.so": nil,
3424 "libvndk27_arm64.so": nil,
3425 "libvndk27_x86.so": nil,
3426 "libvndk27_x86_64.so": nil,
3427 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003428
Colin Cross2807f002021-03-02 10:15:29 -08003429 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003430 "lib/libvndk27_arm.so",
3431 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003432 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003433 })
Jooyung Han344d5432019-08-23 11:17:39 +09003434}
3435
Jooyung Han90eee022019-10-01 20:02:42 +09003436func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003437 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003438 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003439 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003440 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003441 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003442 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003443 }
3444 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003445 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003446 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003447 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003448 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003449 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003450 }
3451 apex_key {
3452 name: "myapex.key",
3453 public_key: "testkey.avbpubkey",
3454 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003455 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003456
3457 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003458 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003459 actual := proptools.String(bundle.properties.Apex_name)
3460 if !reflect.DeepEqual(actual, expected) {
3461 t.Errorf("Got '%v', expected '%v'", actual, expected)
3462 }
3463 }
3464
Colin Cross2807f002021-03-02 10:15:29 -08003465 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3466 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003467}
3468
Jooyung Han344d5432019-08-23 11:17:39 +09003469func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003470 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003471 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003472 name: "com.android.vndk.current",
3473 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003474 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003475 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003476 }
3477
3478 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003479 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003480 public_key: "testkey.avbpubkey",
3481 private_key: "testkey.pem",
3482 }
3483
3484 cc_library {
3485 name: "libvndk",
3486 srcs: ["mylib.cpp"],
3487 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003488 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003489 native_bridge_supported: true,
3490 host_supported: true,
3491 vndk: {
3492 enabled: true,
3493 },
3494 system_shared_libs: [],
3495 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003496 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003497 }
Colin Cross2807f002021-03-02 10:15:29 -08003498 `+vndkLibrariesTxtFiles("current"),
3499 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003500
Colin Cross2807f002021-03-02 10:15:29 -08003501 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003502 "lib/libvndk.so",
3503 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003504 "lib/libc++.so",
3505 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003506 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003507 })
Jooyung Han344d5432019-08-23 11:17:39 +09003508}
3509
3510func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003511 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003512 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003513 name: "com.android.vndk.current",
3514 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003515 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003516 native_bridge_supported: true,
3517 }
3518
3519 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003520 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003521 public_key: "testkey.avbpubkey",
3522 private_key: "testkey.pem",
3523 }
3524
3525 cc_library {
3526 name: "libvndk",
3527 srcs: ["mylib.cpp"],
3528 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003529 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003530 native_bridge_supported: true,
3531 host_supported: true,
3532 vndk: {
3533 enabled: true,
3534 },
3535 system_shared_libs: [],
3536 stl: "none",
3537 }
3538 `)
3539}
3540
Jooyung Han31c470b2019-10-18 16:26:59 +09003541func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003542 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003543 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003544 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003545 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003546 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003547 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003548 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003549 }
3550
3551 apex_key {
3552 name: "myapex.key",
3553 public_key: "testkey.avbpubkey",
3554 private_key: "testkey.pem",
3555 }
3556
3557 vndk_prebuilt_shared {
3558 name: "libvndk27",
3559 version: "27",
3560 target_arch: "arm",
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: ["libvndk27.so"],
3569 }
3570 },
3571 }
3572
3573 vndk_prebuilt_shared {
3574 name: "libvndk27",
3575 version: "27",
3576 target_arch: "arm",
3577 binder32bit: true,
3578 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003579 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003580 vndk: {
3581 enabled: true,
3582 },
3583 arch: {
3584 arm: {
3585 srcs: ["libvndk27binder32.so"],
3586 }
3587 },
Colin Cross2807f002021-03-02 10:15:29 -08003588 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003589 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003590 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003591 withFiles(map[string][]byte{
3592 "libvndk27.so": nil,
3593 "libvndk27binder32.so": nil,
3594 }),
3595 withBinder32bit,
3596 withTargets(map[android.OsType][]android.Target{
3597 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003598 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3599 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003600 },
3601 }),
3602 )
3603
Colin Cross2807f002021-03-02 10:15:29 -08003604 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003605 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003606 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003607 })
3608}
3609
Jooyung Han45a96772020-06-15 14:59:42 +09003610func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003611 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003612 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003613 name: "com.android.vndk.current",
3614 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003615 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003616 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003617 }
3618
3619 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003620 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003621 public_key: "testkey.avbpubkey",
3622 private_key: "testkey.pem",
3623 }
3624
3625 cc_library {
3626 name: "libz",
3627 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003628 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003629 vndk: {
3630 enabled: true,
3631 },
3632 stubs: {
3633 symbol_file: "libz.map.txt",
3634 versions: ["30"],
3635 }
3636 }
3637 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3638 "libz.map.txt": nil,
3639 }))
3640
Colin Cross2807f002021-03-02 10:15:29 -08003641 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003642 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3643 ensureListEmpty(t, provideNativeLibs)
3644}
3645
Jooyung Hane1633032019-08-01 17:41:43 +09003646func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003647 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003648 apex {
3649 name: "myapex_nodep",
3650 key: "myapex.key",
3651 native_shared_libs: ["lib_nodep"],
3652 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003653 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003654 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003655 }
3656
3657 apex {
3658 name: "myapex_dep",
3659 key: "myapex.key",
3660 native_shared_libs: ["lib_dep"],
3661 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003662 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003663 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003664 }
3665
3666 apex {
3667 name: "myapex_provider",
3668 key: "myapex.key",
3669 native_shared_libs: ["libfoo"],
3670 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003671 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003672 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003673 }
3674
3675 apex {
3676 name: "myapex_selfcontained",
3677 key: "myapex.key",
3678 native_shared_libs: ["lib_dep", "libfoo"],
3679 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003680 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003681 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003682 }
3683
3684 apex_key {
3685 name: "myapex.key",
3686 public_key: "testkey.avbpubkey",
3687 private_key: "testkey.pem",
3688 }
3689
3690 cc_library {
3691 name: "lib_nodep",
3692 srcs: ["mylib.cpp"],
3693 system_shared_libs: [],
3694 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003695 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003696 }
3697
3698 cc_library {
3699 name: "lib_dep",
3700 srcs: ["mylib.cpp"],
3701 shared_libs: ["libfoo"],
3702 system_shared_libs: [],
3703 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003704 apex_available: [
3705 "myapex_dep",
3706 "myapex_provider",
3707 "myapex_selfcontained",
3708 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003709 }
3710
3711 cc_library {
3712 name: "libfoo",
3713 srcs: ["mytest.cpp"],
3714 stubs: {
3715 versions: ["1"],
3716 },
3717 system_shared_libs: [],
3718 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003719 apex_available: [
3720 "myapex_provider",
3721 "myapex_selfcontained",
3722 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003723 }
3724 `)
3725
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003726 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003727 var provideNativeLibs, requireNativeLibs []string
3728
Sundong Ahnabb64432019-10-22 13:58:29 +09003729 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003730 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3731 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003732 ensureListEmpty(t, provideNativeLibs)
3733 ensureListEmpty(t, requireNativeLibs)
3734
Sundong Ahnabb64432019-10-22 13:58:29 +09003735 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003736 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3737 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003738 ensureListEmpty(t, provideNativeLibs)
3739 ensureListContains(t, requireNativeLibs, "libfoo.so")
3740
Sundong Ahnabb64432019-10-22 13:58:29 +09003741 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003742 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3743 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003744 ensureListContains(t, provideNativeLibs, "libfoo.so")
3745 ensureListEmpty(t, requireNativeLibs)
3746
Sundong Ahnabb64432019-10-22 13:58:29 +09003747 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003748 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3749 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003750 ensureListContains(t, provideNativeLibs, "libfoo.so")
3751 ensureListEmpty(t, requireNativeLibs)
3752}
3753
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003754func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003755 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003756 apex {
3757 name: "myapex",
3758 key: "myapex.key",
3759 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003760 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003761 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003762 }
3763
3764 apex_key {
3765 name: "myapex.key",
3766 public_key: "testkey.avbpubkey",
3767 private_key: "testkey.pem",
3768 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003769
3770 cc_library {
3771 name: "mylib",
3772 srcs: ["mylib.cpp"],
3773 system_shared_libs: [],
3774 stl: "none",
3775 apex_available: [
3776 "//apex_available:platform",
3777 "myapex",
3778 ],
3779 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003780 `)
3781
Sundong Ahnabb64432019-10-22 13:58:29 +09003782 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003783 apexManifestRule := module.Rule("apexManifestRule")
3784 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3785 apexRule := module.Rule("apexRule")
3786 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003787
3788 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003789 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003790 name := apexBundle.BaseModuleName()
3791 prefix := "TARGET_"
3792 var builder strings.Builder
3793 data.Custom(&builder, name, prefix, "", data)
3794 androidMk := builder.String()
3795 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3796 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003797}
3798
Alex Light0851b882019-02-07 13:20:53 -08003799func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003800 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003801 apex {
3802 name: "myapex",
3803 key: "myapex.key",
3804 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003805 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003806 }
3807
3808 apex_key {
3809 name: "myapex.key",
3810 public_key: "testkey.avbpubkey",
3811 private_key: "testkey.pem",
3812 }
3813
3814 cc_library {
3815 name: "mylib_common",
3816 srcs: ["mylib.cpp"],
3817 system_shared_libs: [],
3818 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003819 apex_available: [
3820 "//apex_available:platform",
3821 "myapex",
3822 ],
Alex Light0851b882019-02-07 13:20:53 -08003823 }
3824 `)
3825
Sundong Ahnabb64432019-10-22 13:58:29 +09003826 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003827 apexRule := module.Rule("apexRule")
3828 copyCmds := apexRule.Args["copy_commands"]
3829
3830 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3831 t.Log("Apex was a test apex!")
3832 t.Fail()
3833 }
3834 // Ensure that main rule creates an output
3835 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3836
3837 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003838 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003839
3840 // Ensure that both direct and indirect deps are copied into apex
3841 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3842
Colin Cross7113d202019-11-20 16:39:12 -08003843 // Ensure that the platform variant ends with _shared
3844 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003845
Colin Cross56a83212020-09-15 18:30:11 -07003846 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003847 t.Log("Found mylib_common not in any apex!")
3848 t.Fail()
3849 }
3850}
3851
3852func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003853 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003854 apex_test {
3855 name: "myapex",
3856 key: "myapex.key",
3857 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003858 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003859 }
3860
3861 apex_key {
3862 name: "myapex.key",
3863 public_key: "testkey.avbpubkey",
3864 private_key: "testkey.pem",
3865 }
3866
3867 cc_library {
3868 name: "mylib_common_test",
3869 srcs: ["mylib.cpp"],
3870 system_shared_libs: [],
3871 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003872 // TODO: remove //apex_available:platform
3873 apex_available: [
3874 "//apex_available:platform",
3875 "myapex",
3876 ],
Alex Light0851b882019-02-07 13:20:53 -08003877 }
3878 `)
3879
Sundong Ahnabb64432019-10-22 13:58:29 +09003880 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003881 apexRule := module.Rule("apexRule")
3882 copyCmds := apexRule.Args["copy_commands"]
3883
3884 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3885 t.Log("Apex was not a test apex!")
3886 t.Fail()
3887 }
3888 // Ensure that main rule creates an output
3889 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3890
3891 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003892 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003893
3894 // Ensure that both direct and indirect deps are copied into apex
3895 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3896
Colin Cross7113d202019-11-20 16:39:12 -08003897 // Ensure that the platform variant ends with _shared
3898 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003899}
3900
Alex Light9670d332019-01-29 18:07:33 -08003901func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003902 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003903 apex {
3904 name: "myapex",
3905 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003906 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003907 multilib: {
3908 first: {
3909 native_shared_libs: ["mylib_common"],
3910 }
3911 },
3912 target: {
3913 android: {
3914 multilib: {
3915 first: {
3916 native_shared_libs: ["mylib"],
3917 }
3918 }
3919 },
3920 host: {
3921 multilib: {
3922 first: {
3923 native_shared_libs: ["mylib2"],
3924 }
3925 }
3926 }
3927 }
3928 }
3929
3930 apex_key {
3931 name: "myapex.key",
3932 public_key: "testkey.avbpubkey",
3933 private_key: "testkey.pem",
3934 }
3935
3936 cc_library {
3937 name: "mylib",
3938 srcs: ["mylib.cpp"],
3939 system_shared_libs: [],
3940 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003941 // TODO: remove //apex_available:platform
3942 apex_available: [
3943 "//apex_available:platform",
3944 "myapex",
3945 ],
Alex Light9670d332019-01-29 18:07:33 -08003946 }
3947
3948 cc_library {
3949 name: "mylib_common",
3950 srcs: ["mylib.cpp"],
3951 system_shared_libs: [],
3952 stl: "none",
3953 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003954 // TODO: remove //apex_available:platform
3955 apex_available: [
3956 "//apex_available:platform",
3957 "myapex",
3958 ],
Alex Light9670d332019-01-29 18:07:33 -08003959 }
3960
3961 cc_library {
3962 name: "mylib2",
3963 srcs: ["mylib.cpp"],
3964 system_shared_libs: [],
3965 stl: "none",
3966 compile_multilib: "first",
3967 }
3968 `)
3969
Sundong Ahnabb64432019-10-22 13:58:29 +09003970 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003971 copyCmds := apexRule.Args["copy_commands"]
3972
3973 // Ensure that main rule creates an output
3974 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3975
3976 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003977 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3978 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3979 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003980
3981 // Ensure that both direct and indirect deps are copied into apex
3982 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3983 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3984 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3985
Colin Cross7113d202019-11-20 16:39:12 -08003986 // Ensure that the platform variant ends with _shared
3987 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3988 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3989 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003990}
Jiyong Park04480cf2019-02-06 00:16:29 +09003991
Jiyong Park59140302020-12-14 18:44:04 +09003992func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003993 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003994 apex {
3995 name: "myapex",
3996 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003997 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003998 arch: {
3999 arm64: {
4000 native_shared_libs: ["mylib.arm64"],
4001 },
4002 x86_64: {
4003 native_shared_libs: ["mylib.x64"],
4004 },
4005 }
4006 }
4007
4008 apex_key {
4009 name: "myapex.key",
4010 public_key: "testkey.avbpubkey",
4011 private_key: "testkey.pem",
4012 }
4013
4014 cc_library {
4015 name: "mylib.arm64",
4016 srcs: ["mylib.cpp"],
4017 system_shared_libs: [],
4018 stl: "none",
4019 // TODO: remove //apex_available:platform
4020 apex_available: [
4021 "//apex_available:platform",
4022 "myapex",
4023 ],
4024 }
4025
4026 cc_library {
4027 name: "mylib.x64",
4028 srcs: ["mylib.cpp"],
4029 system_shared_libs: [],
4030 stl: "none",
4031 // TODO: remove //apex_available:platform
4032 apex_available: [
4033 "//apex_available:platform",
4034 "myapex",
4035 ],
4036 }
4037 `)
4038
4039 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4040 copyCmds := apexRule.Args["copy_commands"]
4041
4042 // Ensure that apex variant is created for the direct dep
4043 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4044 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4045
4046 // Ensure that both direct and indirect deps are copied into apex
4047 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4048 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4049}
4050
Jiyong Park04480cf2019-02-06 00:16:29 +09004051func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004052 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004053 apex {
4054 name: "myapex",
4055 key: "myapex.key",
4056 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004057 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004058 }
4059
4060 apex_key {
4061 name: "myapex.key",
4062 public_key: "testkey.avbpubkey",
4063 private_key: "testkey.pem",
4064 }
4065
4066 sh_binary {
4067 name: "myscript",
4068 src: "mylib.cpp",
4069 filename: "myscript.sh",
4070 sub_dir: "script",
4071 }
4072 `)
4073
Sundong Ahnabb64432019-10-22 13:58:29 +09004074 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004075 copyCmds := apexRule.Args["copy_commands"]
4076
4077 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4078}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004079
Jooyung Han91df2082019-11-20 01:49:42 +09004080func TestApexInVariousPartition(t *testing.T) {
4081 testcases := []struct {
4082 propName, parition, flattenedPartition string
4083 }{
4084 {"", "system", "system_ext"},
4085 {"product_specific: true", "product", "product"},
4086 {"soc_specific: true", "vendor", "vendor"},
4087 {"proprietary: true", "vendor", "vendor"},
4088 {"vendor: true", "vendor", "vendor"},
4089 {"system_ext_specific: true", "system_ext", "system_ext"},
4090 }
4091 for _, tc := range testcases {
4092 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004093 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004094 apex {
4095 name: "myapex",
4096 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004097 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004098 `+tc.propName+`
4099 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004100
Jooyung Han91df2082019-11-20 01:49:42 +09004101 apex_key {
4102 name: "myapex.key",
4103 public_key: "testkey.avbpubkey",
4104 private_key: "testkey.pem",
4105 }
4106 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004107
Jooyung Han91df2082019-11-20 01:49:42 +09004108 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4109 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4110 actual := apex.installDir.String()
4111 if actual != expected {
4112 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4113 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004114
Jooyung Han91df2082019-11-20 01:49:42 +09004115 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4116 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4117 actual = flattened.installDir.String()
4118 if actual != expected {
4119 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4120 }
4121 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004122 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004123}
Jiyong Park67882562019-03-21 01:11:21 +09004124
Jooyung Han580eb4f2020-06-24 19:33:06 +09004125func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004126 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004127 apex {
4128 name: "myapex",
4129 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004130 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004131 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004132
Jooyung Han580eb4f2020-06-24 19:33:06 +09004133 apex_key {
4134 name: "myapex.key",
4135 public_key: "testkey.avbpubkey",
4136 private_key: "testkey.pem",
4137 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004138 `)
4139 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004140 rule := module.Output("file_contexts")
4141 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4142}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004143
Jooyung Han580eb4f2020-06-24 19:33:06 +09004144func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004145 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004146 apex {
4147 name: "myapex",
4148 key: "myapex.key",
4149 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004150 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004151 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004152
Jooyung Han580eb4f2020-06-24 19:33:06 +09004153 apex_key {
4154 name: "myapex.key",
4155 public_key: "testkey.avbpubkey",
4156 private_key: "testkey.pem",
4157 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004158 `, withFiles(map[string][]byte{
4159 "my_own_file_contexts": nil,
4160 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004161}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004162
Jooyung Han580eb4f2020-06-24 19:33:06 +09004163func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004164 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004165 apex {
4166 name: "myapex",
4167 key: "myapex.key",
4168 product_specific: true,
4169 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004170 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004171 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004172
Jooyung Han580eb4f2020-06-24 19:33:06 +09004173 apex_key {
4174 name: "myapex.key",
4175 public_key: "testkey.avbpubkey",
4176 private_key: "testkey.pem",
4177 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004178 `)
4179
Colin Cross1c460562021-02-16 17:55:47 -08004180 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004181 apex {
4182 name: "myapex",
4183 key: "myapex.key",
4184 product_specific: true,
4185 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004186 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004187 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004188
Jooyung Han580eb4f2020-06-24 19:33:06 +09004189 apex_key {
4190 name: "myapex.key",
4191 public_key: "testkey.avbpubkey",
4192 private_key: "testkey.pem",
4193 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004194 `, withFiles(map[string][]byte{
4195 "product_specific_file_contexts": nil,
4196 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004197 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4198 rule := module.Output("file_contexts")
4199 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4200}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004201
Jooyung Han580eb4f2020-06-24 19:33:06 +09004202func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004203 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004204 apex {
4205 name: "myapex",
4206 key: "myapex.key",
4207 product_specific: true,
4208 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004209 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004210 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004211
Jooyung Han580eb4f2020-06-24 19:33:06 +09004212 apex_key {
4213 name: "myapex.key",
4214 public_key: "testkey.avbpubkey",
4215 private_key: "testkey.pem",
4216 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004217
Jooyung Han580eb4f2020-06-24 19:33:06 +09004218 filegroup {
4219 name: "my-file-contexts",
4220 srcs: ["product_specific_file_contexts"],
4221 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004222 `, withFiles(map[string][]byte{
4223 "product_specific_file_contexts": nil,
4224 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004225 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4226 rule := module.Output("file_contexts")
4227 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004228}
4229
Jiyong Park67882562019-03-21 01:11:21 +09004230func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004231 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004232 apex_key {
4233 name: "myapex.key",
4234 public_key: ":my.avbpubkey",
4235 private_key: ":my.pem",
4236 product_specific: true,
4237 }
4238
4239 filegroup {
4240 name: "my.avbpubkey",
4241 srcs: ["testkey2.avbpubkey"],
4242 }
4243
4244 filegroup {
4245 name: "my.pem",
4246 srcs: ["testkey2.pem"],
4247 }
4248 `)
4249
4250 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4251 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004252 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004253 if actual_pubkey != expected_pubkey {
4254 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4255 }
4256 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004257 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004258 if actual_privkey != expected_privkey {
4259 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4260 }
4261}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004262
4263func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004264 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004265 prebuilt_apex {
4266 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004267 arch: {
4268 arm64: {
4269 src: "myapex-arm64.apex",
4270 },
4271 arm: {
4272 src: "myapex-arm.apex",
4273 },
4274 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004275 }
4276 `)
4277
4278 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4279
Jiyong Parkc95714e2019-03-29 14:23:10 +09004280 expectedInput := "myapex-arm64.apex"
4281 if prebuilt.inputApex.String() != expectedInput {
4282 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4283 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004284}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004285
4286func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004287 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004288 prebuilt_apex {
4289 name: "myapex",
4290 src: "myapex-arm.apex",
4291 filename: "notmyapex.apex",
4292 }
4293 `)
4294
4295 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4296
4297 expected := "notmyapex.apex"
4298 if p.installFilename != expected {
4299 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4300 }
4301}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004302
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004303func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004304 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004305 prebuilt_apex {
4306 name: "myapex.prebuilt",
4307 src: "myapex-arm.apex",
4308 overrides: [
4309 "myapex",
4310 ],
4311 }
4312 `)
4313
4314 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4315
4316 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004317 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004318 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004319 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004320 }
4321}
4322
Paul Duffin092153d2021-01-26 11:42:39 +00004323// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4324// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004325func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4326 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004327 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004328 }
4329
Paul Duffin89886cb2021-02-05 16:44:03 +00004330 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004331 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004332 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004333 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004334 stem := android.RemoveOptionalPrebuiltPrefix(name)
4335 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004336 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4337 }
4338 }
4339
Paul Duffin39853512021-02-26 11:09:39 +00004340 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004341 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004342 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004343 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4344 }
4345 }
4346
4347 t.Run("prebuilt only", func(t *testing.T) {
4348 bp := `
4349 prebuilt_apex {
4350 name: "myapex",
4351 arch: {
4352 arm64: {
4353 src: "myapex-arm64.apex",
4354 },
4355 arm: {
4356 src: "myapex-arm.apex",
4357 },
4358 },
Paul Duffin39853512021-02-26 11:09:39 +00004359 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004360 }
4361
4362 java_import {
4363 name: "libfoo",
4364 jars: ["libfoo.jar"],
4365 }
Paul Duffin39853512021-02-26 11:09:39 +00004366
4367 java_sdk_library_import {
4368 name: "libbar",
4369 public: {
4370 jars: ["libbar.jar"],
4371 },
4372 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004373 `
4374
4375 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4376 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4377
Paul Duffinf6932af2021-02-26 18:21:56 +00004378 // Make sure that the deapexer has the correct input APEX.
4379 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4380 rule := deapexer.Rule("deapexer")
4381 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4382 t.Errorf("expected: %q, found: %q", expected, actual)
4383 }
4384
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004385 // Make sure that the prebuilt_apex has the correct input APEX.
4386 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4387 rule = prebuiltApex.Rule("android/soong/android.Cp")
4388 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4389 t.Errorf("expected: %q, found: %q", expected, actual)
4390 }
4391
Paul Duffin89886cb2021-02-05 16:44:03 +00004392 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004393
4394 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004395 })
4396
4397 t.Run("prebuilt with source preferred", func(t *testing.T) {
4398
4399 bp := `
4400 prebuilt_apex {
4401 name: "myapex",
4402 arch: {
4403 arm64: {
4404 src: "myapex-arm64.apex",
4405 },
4406 arm: {
4407 src: "myapex-arm.apex",
4408 },
4409 },
Paul Duffin39853512021-02-26 11:09:39 +00004410 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004411 }
4412
4413 java_import {
4414 name: "libfoo",
4415 jars: ["libfoo.jar"],
4416 }
4417
4418 java_library {
4419 name: "libfoo",
4420 }
Paul Duffin39853512021-02-26 11:09:39 +00004421
4422 java_sdk_library_import {
4423 name: "libbar",
4424 public: {
4425 jars: ["libbar.jar"],
4426 },
4427 }
4428
4429 java_sdk_library {
4430 name: "libbar",
4431 srcs: ["foo/bar/MyClass.java"],
4432 unsafe_ignore_missing_latest_api: true,
4433 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004434 `
4435
4436 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4437 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4438
Paul Duffin89886cb2021-02-05 16:44:03 +00004439 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004440 ensureNoSourceVariant(t, ctx, "libfoo")
4441
4442 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4443 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004444 })
4445
4446 t.Run("prebuilt preferred with source", func(t *testing.T) {
4447 bp := `
4448 prebuilt_apex {
4449 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004450 arch: {
4451 arm64: {
4452 src: "myapex-arm64.apex",
4453 },
4454 arm: {
4455 src: "myapex-arm.apex",
4456 },
4457 },
Paul Duffin39853512021-02-26 11:09:39 +00004458 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004459 }
4460
4461 java_import {
4462 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004463 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004464 jars: ["libfoo.jar"],
4465 }
4466
4467 java_library {
4468 name: "libfoo",
4469 }
Paul Duffin39853512021-02-26 11:09:39 +00004470
4471 java_sdk_library_import {
4472 name: "libbar",
4473 prefer: true,
4474 public: {
4475 jars: ["libbar.jar"],
4476 },
4477 }
4478
4479 java_sdk_library {
4480 name: "libbar",
4481 srcs: ["foo/bar/MyClass.java"],
4482 unsafe_ignore_missing_latest_api: true,
4483 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004484 `
4485
4486 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4487 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4488
Paul Duffin89886cb2021-02-05 16:44:03 +00004489 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004490 ensureNoSourceVariant(t, ctx, "libfoo")
4491
4492 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4493 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004494 })
4495}
4496
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004497func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4498 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004499 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004500 }
4501
Paul Duffin37856732021-02-26 14:24:15 +00004502 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4503 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004504 s := ctx.SingletonForTests("dex_bootjars")
4505 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004506 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004507 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004508 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004509 foundLibfooJar = true
4510 buildRule := s.Output(output)
4511 actual := android.NormalizePathForTesting(buildRule.Input)
4512 if actual != bootDexJarPath {
4513 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4514 }
4515 }
4516 }
4517 if !foundLibfooJar {
4518 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4519 }
4520 }
4521
Paul Duffin4fd997b2021-02-03 20:06:33 +00004522 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004523 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004524 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4525 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4526 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4527 }
4528
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004529 t.Run("prebuilt only", func(t *testing.T) {
4530 bp := `
4531 prebuilt_apex {
4532 name: "myapex",
4533 arch: {
4534 arm64: {
4535 src: "myapex-arm64.apex",
4536 },
4537 arm: {
4538 src: "myapex-arm.apex",
4539 },
4540 },
Paul Duffin37856732021-02-26 14:24:15 +00004541 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004542 }
4543
4544 java_import {
4545 name: "libfoo",
4546 jars: ["libfoo.jar"],
4547 apex_available: ["myapex"],
4548 }
Paul Duffin37856732021-02-26 14:24:15 +00004549
4550 java_sdk_library_import {
4551 name: "libbar",
4552 public: {
4553 jars: ["libbar.jar"],
4554 },
4555 apex_available: ["myapex"],
4556 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004557 `
4558
4559 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004560 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4561 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004562
Paul Duffin9d67ca62021-02-03 20:06:33 +00004563 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4564 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004565.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004566.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4567`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004568 })
4569
4570 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4571 bp := `
4572 prebuilt_apex {
4573 name: "myapex",
4574 arch: {
4575 arm64: {
4576 src: "myapex-arm64.apex",
4577 },
4578 arm: {
4579 src: "myapex-arm.apex",
4580 },
4581 },
Paul Duffin37856732021-02-26 14:24:15 +00004582 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004583 }
4584
4585 java_import {
4586 name: "libfoo",
4587 jars: ["libfoo.jar"],
4588 apex_available: ["myapex"],
4589 }
4590
4591 java_library {
4592 name: "libfoo",
4593 srcs: ["foo/bar/MyClass.java"],
4594 apex_available: ["myapex"],
4595 }
Paul Duffin37856732021-02-26 14:24:15 +00004596
4597 java_sdk_library_import {
4598 name: "libbar",
4599 public: {
4600 jars: ["libbar.jar"],
4601 },
4602 apex_available: ["myapex"],
4603 }
4604
4605 java_sdk_library {
4606 name: "libbar",
4607 srcs: ["foo/bar/MyClass.java"],
4608 unsafe_ignore_missing_latest_api: true,
4609 apex_available: ["myapex"],
4610 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004611 `
4612
4613 // In this test the source (java_library) libfoo is active since the
4614 // prebuilt (java_import) defaults to prefer:false. However the
4615 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4616 // find the dex boot jar in it. We either need to disable the source libfoo
4617 // or make the prebuilt libfoo preferred.
4618 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4619 })
4620
4621 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4622 bp := `
4623 prebuilt_apex {
4624 name: "myapex",
4625 arch: {
4626 arm64: {
4627 src: "myapex-arm64.apex",
4628 },
4629 arm: {
4630 src: "myapex-arm.apex",
4631 },
4632 },
Paul Duffin37856732021-02-26 14:24:15 +00004633 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004634 }
4635
4636 java_import {
4637 name: "libfoo",
4638 prefer: true,
4639 jars: ["libfoo.jar"],
4640 apex_available: ["myapex"],
4641 }
4642
4643 java_library {
4644 name: "libfoo",
4645 srcs: ["foo/bar/MyClass.java"],
4646 apex_available: ["myapex"],
4647 }
Paul Duffin37856732021-02-26 14:24:15 +00004648
4649 java_sdk_library_import {
4650 name: "libbar",
4651 prefer: true,
4652 public: {
4653 jars: ["libbar.jar"],
4654 },
4655 apex_available: ["myapex"],
4656 }
4657
4658 java_sdk_library {
4659 name: "libbar",
4660 srcs: ["foo/bar/MyClass.java"],
4661 unsafe_ignore_missing_latest_api: true,
4662 apex_available: ["myapex"],
4663 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004664 `
4665
4666 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004667 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4668 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004669
Paul Duffin9d67ca62021-02-03 20:06:33 +00004670 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4671 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004672.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004673.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4674`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004675 })
4676
4677 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4678 bp := `
4679 apex {
4680 name: "myapex",
4681 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004682 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004683 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004684 }
4685
4686 apex_key {
4687 name: "myapex.key",
4688 public_key: "testkey.avbpubkey",
4689 private_key: "testkey.pem",
4690 }
4691
4692 prebuilt_apex {
4693 name: "myapex",
4694 arch: {
4695 arm64: {
4696 src: "myapex-arm64.apex",
4697 },
4698 arm: {
4699 src: "myapex-arm.apex",
4700 },
4701 },
Paul Duffin37856732021-02-26 14:24:15 +00004702 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004703 }
4704
4705 java_import {
4706 name: "libfoo",
4707 jars: ["libfoo.jar"],
4708 apex_available: ["myapex"],
4709 }
4710
4711 java_library {
4712 name: "libfoo",
4713 srcs: ["foo/bar/MyClass.java"],
4714 apex_available: ["myapex"],
4715 }
Paul Duffin37856732021-02-26 14:24:15 +00004716
4717 java_sdk_library_import {
4718 name: "libbar",
4719 public: {
4720 jars: ["libbar.jar"],
4721 },
4722 apex_available: ["myapex"],
4723 }
4724
4725 java_sdk_library {
4726 name: "libbar",
4727 srcs: ["foo/bar/MyClass.java"],
4728 unsafe_ignore_missing_latest_api: true,
4729 apex_available: ["myapex"],
4730 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004731 `
4732
4733 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004734 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4735 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004736
4737 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4738 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004739.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004740.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4741`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004742 })
4743
4744 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4745 bp := `
4746 apex {
4747 name: "myapex",
4748 enabled: false,
4749 key: "myapex.key",
4750 java_libs: ["libfoo"],
4751 }
4752
4753 apex_key {
4754 name: "myapex.key",
4755 public_key: "testkey.avbpubkey",
4756 private_key: "testkey.pem",
4757 }
4758
4759 prebuilt_apex {
4760 name: "myapex",
4761 arch: {
4762 arm64: {
4763 src: "myapex-arm64.apex",
4764 },
4765 arm: {
4766 src: "myapex-arm.apex",
4767 },
4768 },
Paul Duffin37856732021-02-26 14:24:15 +00004769 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004770 }
4771
4772 java_import {
4773 name: "libfoo",
4774 prefer: true,
4775 jars: ["libfoo.jar"],
4776 apex_available: ["myapex"],
4777 }
4778
4779 java_library {
4780 name: "libfoo",
4781 srcs: ["foo/bar/MyClass.java"],
4782 apex_available: ["myapex"],
4783 }
Paul Duffin37856732021-02-26 14:24:15 +00004784
4785 java_sdk_library_import {
4786 name: "libbar",
4787 prefer: true,
4788 public: {
4789 jars: ["libbar.jar"],
4790 },
4791 apex_available: ["myapex"],
4792 }
4793
4794 java_sdk_library {
4795 name: "libbar",
4796 srcs: ["foo/bar/MyClass.java"],
4797 unsafe_ignore_missing_latest_api: true,
4798 apex_available: ["myapex"],
4799 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004800 `
4801
4802 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004803 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4804 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004805
Paul Duffin9d67ca62021-02-03 20:06:33 +00004806 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4807 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004808.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004809.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4810`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004811 })
4812}
4813
Roland Levillain630846d2019-06-26 12:48:34 +01004814func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004815 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004816 apex_test {
4817 name: "myapex",
4818 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004819 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004820 tests: [
4821 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004822 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004823 ],
4824 }
4825
4826 apex_key {
4827 name: "myapex.key",
4828 public_key: "testkey.avbpubkey",
4829 private_key: "testkey.pem",
4830 }
4831
Liz Kammer1c14a212020-05-12 15:26:55 -07004832 filegroup {
4833 name: "fg",
4834 srcs: [
4835 "baz",
4836 "bar/baz"
4837 ],
4838 }
4839
Roland Levillain630846d2019-06-26 12:48:34 +01004840 cc_test {
4841 name: "mytest",
4842 gtest: false,
4843 srcs: ["mytest.cpp"],
4844 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004845 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004846 system_shared_libs: [],
4847 static_executable: true,
4848 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004849 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004850 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004851
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004852 cc_library {
4853 name: "mylib",
4854 srcs: ["mylib.cpp"],
4855 system_shared_libs: [],
4856 stl: "none",
4857 }
4858
Liz Kammer5bd365f2020-05-27 15:15:11 -07004859 filegroup {
4860 name: "fg2",
4861 srcs: [
4862 "testdata/baz"
4863 ],
4864 }
4865
Roland Levillain9b5fde92019-06-28 15:41:19 +01004866 cc_test {
4867 name: "mytests",
4868 gtest: false,
4869 srcs: [
4870 "mytest1.cpp",
4871 "mytest2.cpp",
4872 "mytest3.cpp",
4873 ],
4874 test_per_src: true,
4875 relative_install_path: "test",
4876 system_shared_libs: [],
4877 static_executable: true,
4878 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004879 data: [
4880 ":fg",
4881 ":fg2",
4882 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004883 }
Roland Levillain630846d2019-06-26 12:48:34 +01004884 `)
4885
Sundong Ahnabb64432019-10-22 13:58:29 +09004886 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004887 copyCmds := apexRule.Args["copy_commands"]
4888
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004889 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004890 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004891 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004892
Liz Kammer1c14a212020-05-12 15:26:55 -07004893 //Ensure that test data are copied into apex.
4894 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4895 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4896
Roland Levillain9b5fde92019-06-28 15:41:19 +01004897 // Ensure that test deps built with `test_per_src` are copied into apex.
4898 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4899 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4900 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004901
4902 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004903 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004904 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004905 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004906 prefix := "TARGET_"
4907 var builder strings.Builder
4908 data.Custom(&builder, name, prefix, "", data)
4909 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004910 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4911 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4912 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4913 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004914 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004915 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004916 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004917
4918 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004919 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004920 data.Custom(&builder, name, prefix, "", data)
4921 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004922 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4923 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004924}
4925
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004926func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004927 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004928 apex {
4929 name: "myapex",
4930 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004931 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004932 }
4933 apex_key {
4934 name: "myapex.key",
4935 public_key: "testkey.avbpubkey",
4936 private_key: "testkey.pem",
4937 }
4938 `, func(fs map[string][]byte, config android.Config) {
4939 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4940 })
4941 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004942 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004943 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004944 var builder strings.Builder
4945 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4946 androidMk := builder.String()
4947 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4948}
4949
Jooyung Hand48f3c32019-08-23 11:18:57 +09004950func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4951 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4952 apex {
4953 name: "myapex",
4954 key: "myapex.key",
4955 native_shared_libs: ["libfoo"],
4956 }
4957
4958 apex_key {
4959 name: "myapex.key",
4960 public_key: "testkey.avbpubkey",
4961 private_key: "testkey.pem",
4962 }
4963
4964 cc_library {
4965 name: "libfoo",
4966 stl: "none",
4967 system_shared_libs: [],
4968 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004969 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004970 }
4971 `)
4972 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4973 apex {
4974 name: "myapex",
4975 key: "myapex.key",
4976 java_libs: ["myjar"],
4977 }
4978
4979 apex_key {
4980 name: "myapex.key",
4981 public_key: "testkey.avbpubkey",
4982 private_key: "testkey.pem",
4983 }
4984
4985 java_library {
4986 name: "myjar",
4987 srcs: ["foo/bar/MyClass.java"],
4988 sdk_version: "none",
4989 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004990 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004991 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004992 }
4993 `)
4994}
4995
Bill Peckhama41a6962021-01-11 10:58:54 -08004996func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004997 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004998 apex {
4999 name: "myapex",
5000 key: "myapex.key",
5001 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005002 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005003 }
5004
5005 apex_key {
5006 name: "myapex.key",
5007 public_key: "testkey.avbpubkey",
5008 private_key: "testkey.pem",
5009 }
5010
5011 java_import {
5012 name: "myjavaimport",
5013 apex_available: ["myapex"],
5014 jars: ["my.jar"],
5015 compile_dex: true,
5016 }
5017 `)
5018
5019 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5020 apexRule := module.Rule("apexRule")
5021 copyCmds := apexRule.Args["copy_commands"]
5022 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5023}
5024
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005025func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005026 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005027 apex {
5028 name: "myapex",
5029 key: "myapex.key",
5030 apps: [
5031 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005032 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005033 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005034 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005035 }
5036
5037 apex_key {
5038 name: "myapex.key",
5039 public_key: "testkey.avbpubkey",
5040 private_key: "testkey.pem",
5041 }
5042
5043 android_app {
5044 name: "AppFoo",
5045 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005046 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005047 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005048 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005049 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005050 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005051 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005052
5053 android_app {
5054 name: "AppFooPriv",
5055 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005056 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005057 system_modules: "none",
5058 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005059 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005060 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005061 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005062
5063 cc_library_shared {
5064 name: "libjni",
5065 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005066 shared_libs: ["libfoo"],
5067 stl: "none",
5068 system_shared_libs: [],
5069 apex_available: [ "myapex" ],
5070 sdk_version: "current",
5071 }
5072
5073 cc_library_shared {
5074 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005075 stl: "none",
5076 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005077 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005078 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005079 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005080 `)
5081
Sundong Ahnabb64432019-10-22 13:58:29 +09005082 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005083 apexRule := module.Rule("apexRule")
5084 copyCmds := apexRule.Args["copy_commands"]
5085
5086 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005087 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005088
Colin Crossaede88c2020-08-11 12:17:01 -07005089 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005090 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005091 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005092 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005093 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005094 // JNI libraries including transitive deps are
5095 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005096 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005097 // ... embedded inside APK (jnilibs.zip)
5098 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5099 // ... and not directly inside the APEX
5100 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5101 }
Dario Frenicde2a032019-10-27 00:29:22 +01005102}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005103
Dario Frenicde2a032019-10-27 00:29:22 +01005104func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005105 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005106 apex {
5107 name: "myapex",
5108 key: "myapex.key",
5109 apps: [
5110 "AppFooPrebuilt",
5111 "AppFooPrivPrebuilt",
5112 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005113 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005114 }
5115
5116 apex_key {
5117 name: "myapex.key",
5118 public_key: "testkey.avbpubkey",
5119 private_key: "testkey.pem",
5120 }
5121
5122 android_app_import {
5123 name: "AppFooPrebuilt",
5124 apk: "PrebuiltAppFoo.apk",
5125 presigned: true,
5126 dex_preopt: {
5127 enabled: false,
5128 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005129 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005130 }
5131
5132 android_app_import {
5133 name: "AppFooPrivPrebuilt",
5134 apk: "PrebuiltAppFooPriv.apk",
5135 privileged: true,
5136 presigned: true,
5137 dex_preopt: {
5138 enabled: false,
5139 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005140 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005141 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005142 }
5143 `)
5144
Sundong Ahnabb64432019-10-22 13:58:29 +09005145 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005146 apexRule := module.Rule("apexRule")
5147 copyCmds := apexRule.Args["copy_commands"]
5148
5149 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005150 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5151}
5152
5153func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005154 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005155 apex {
5156 name: "myapex",
5157 key: "myapex.key",
5158 apps: [
5159 "AppFoo",
5160 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005161 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005162 }
5163
5164 apex_key {
5165 name: "myapex.key",
5166 public_key: "testkey.avbpubkey",
5167 private_key: "testkey.pem",
5168 }
5169
5170 android_app {
5171 name: "AppFoo",
5172 srcs: ["foo/bar/MyClass.java"],
5173 sdk_version: "none",
5174 system_modules: "none",
5175 apex_available: [ "myapex" ],
5176 }
5177
5178 android_app_import {
5179 name: "AppFoo",
5180 apk: "AppFooPrebuilt.apk",
5181 filename: "AppFooPrebuilt.apk",
5182 presigned: true,
5183 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005184 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005185 }
5186 `, withFiles(map[string][]byte{
5187 "AppFooPrebuilt.apk": nil,
5188 }))
5189
5190 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005191 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005192 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005193}
5194
Dario Freni6f3937c2019-12-20 22:58:03 +00005195func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005196 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005197 apex {
5198 name: "myapex",
5199 key: "myapex.key",
5200 apps: [
5201 "TesterHelpAppFoo",
5202 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005203 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005204 }
5205
5206 apex_key {
5207 name: "myapex.key",
5208 public_key: "testkey.avbpubkey",
5209 private_key: "testkey.pem",
5210 }
5211
5212 android_test_helper_app {
5213 name: "TesterHelpAppFoo",
5214 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005215 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005216 }
5217
5218 `)
5219
5220 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5221 apexRule := module.Rule("apexRule")
5222 copyCmds := apexRule.Args["copy_commands"]
5223
5224 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5225}
5226
Jooyung Han18020ea2019-11-13 10:50:48 +09005227func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5228 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005229 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005230 apex {
5231 name: "myapex",
5232 key: "myapex.key",
5233 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005234 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005235 }
5236
5237 apex_key {
5238 name: "myapex.key",
5239 public_key: "testkey.avbpubkey",
5240 private_key: "testkey.pem",
5241 }
5242
5243 apex {
5244 name: "otherapex",
5245 key: "myapex.key",
5246 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005247 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005248 }
5249
5250 cc_defaults {
5251 name: "libfoo-defaults",
5252 apex_available: ["otherapex"],
5253 }
5254
5255 cc_library {
5256 name: "libfoo",
5257 defaults: ["libfoo-defaults"],
5258 stl: "none",
5259 system_shared_libs: [],
5260 }`)
5261}
5262
Paul Duffine52e66f2020-03-30 17:54:29 +01005263func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005264 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005265 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005266 apex {
5267 name: "myapex",
5268 key: "myapex.key",
5269 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005270 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005271 }
5272
5273 apex_key {
5274 name: "myapex.key",
5275 public_key: "testkey.avbpubkey",
5276 private_key: "testkey.pem",
5277 }
5278
5279 apex {
5280 name: "otherapex",
5281 key: "otherapex.key",
5282 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005283 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005284 }
5285
5286 apex_key {
5287 name: "otherapex.key",
5288 public_key: "testkey.avbpubkey",
5289 private_key: "testkey.pem",
5290 }
5291
5292 cc_library {
5293 name: "libfoo",
5294 stl: "none",
5295 system_shared_libs: [],
5296 apex_available: ["otherapex"],
5297 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005298}
Jiyong Park127b40b2019-09-30 16:04:35 +09005299
Paul Duffine52e66f2020-03-30 17:54:29 +01005300func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005301 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005302 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005303.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005304.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005305.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005306.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005307.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005308.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005309 apex {
5310 name: "myapex",
5311 key: "myapex.key",
5312 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005313 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005314 }
5315
5316 apex_key {
5317 name: "myapex.key",
5318 public_key: "testkey.avbpubkey",
5319 private_key: "testkey.pem",
5320 }
5321
Jiyong Park127b40b2019-09-30 16:04:35 +09005322 cc_library {
5323 name: "libfoo",
5324 stl: "none",
5325 shared_libs: ["libbar"],
5326 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005327 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005328 }
5329
5330 cc_library {
5331 name: "libbar",
5332 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005333 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005334 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005335 apex_available: ["myapex"],
5336 }
5337
5338 cc_library {
5339 name: "libbaz",
5340 stl: "none",
5341 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005342 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005343}
Jiyong Park127b40b2019-09-30 16:04:35 +09005344
Paul Duffine52e66f2020-03-30 17:54:29 +01005345func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005346 testApexError(t, "\"otherapex\" is not a valid module name", `
5347 apex {
5348 name: "myapex",
5349 key: "myapex.key",
5350 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005351 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005352 }
5353
5354 apex_key {
5355 name: "myapex.key",
5356 public_key: "testkey.avbpubkey",
5357 private_key: "testkey.pem",
5358 }
5359
5360 cc_library {
5361 name: "libfoo",
5362 stl: "none",
5363 system_shared_libs: [],
5364 apex_available: ["otherapex"],
5365 }`)
5366
Paul Duffine52e66f2020-03-30 17:54:29 +01005367 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005368 apex {
5369 name: "myapex",
5370 key: "myapex.key",
5371 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005372 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005373 }
5374
5375 apex_key {
5376 name: "myapex.key",
5377 public_key: "testkey.avbpubkey",
5378 private_key: "testkey.pem",
5379 }
5380
5381 cc_library {
5382 name: "libfoo",
5383 stl: "none",
5384 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005385 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005386 apex_available: ["myapex"],
5387 }
5388
5389 cc_library {
5390 name: "libbar",
5391 stl: "none",
5392 system_shared_libs: [],
5393 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005394 }
5395
5396 cc_library {
5397 name: "libbaz",
5398 stl: "none",
5399 system_shared_libs: [],
5400 stubs: {
5401 versions: ["10", "20", "30"],
5402 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005403 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005404}
Jiyong Park127b40b2019-09-30 16:04:35 +09005405
Jiyong Park89e850a2020-04-07 16:37:39 +09005406func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005407 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005408 apex {
5409 name: "myapex",
5410 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005411 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005412 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005413 }
5414
5415 apex_key {
5416 name: "myapex.key",
5417 public_key: "testkey.avbpubkey",
5418 private_key: "testkey.pem",
5419 }
5420
5421 cc_library {
5422 name: "libfoo",
5423 stl: "none",
5424 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005425 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005426 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005427 }
5428
5429 cc_library {
5430 name: "libfoo2",
5431 stl: "none",
5432 system_shared_libs: [],
5433 shared_libs: ["libbaz"],
5434 apex_available: ["//apex_available:platform"],
5435 }
5436
5437 cc_library {
5438 name: "libbar",
5439 stl: "none",
5440 system_shared_libs: [],
5441 apex_available: ["myapex"],
5442 }
5443
5444 cc_library {
5445 name: "libbaz",
5446 stl: "none",
5447 system_shared_libs: [],
5448 apex_available: ["myapex"],
5449 stubs: {
5450 versions: ["1"],
5451 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005452 }`)
5453
Jiyong Park89e850a2020-04-07 16:37:39 +09005454 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5455 // because it depends on libbar which isn't available to platform
5456 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5457 if libfoo.NotAvailableForPlatform() != true {
5458 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5459 }
5460
5461 // libfoo2 however can be available to platform because it depends on libbaz which provides
5462 // stubs
5463 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5464 if libfoo2.NotAvailableForPlatform() == true {
5465 t.Errorf("%q should be available to platform", libfoo2.String())
5466 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005467}
Jiyong Parka90ca002019-10-07 15:47:24 +09005468
Paul Duffine52e66f2020-03-30 17:54:29 +01005469func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005470 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005471 apex {
5472 name: "myapex",
5473 key: "myapex.key",
5474 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005475 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005476 }
5477
5478 apex_key {
5479 name: "myapex.key",
5480 public_key: "testkey.avbpubkey",
5481 private_key: "testkey.pem",
5482 }
5483
5484 cc_library {
5485 name: "libfoo",
5486 stl: "none",
5487 system_shared_libs: [],
5488 apex_available: ["myapex"],
5489 static: {
5490 apex_available: ["//apex_available:platform"],
5491 },
5492 }`)
5493
Jiyong Park89e850a2020-04-07 16:37:39 +09005494 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5495 if libfooShared.NotAvailableForPlatform() != true {
5496 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5497 }
5498 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5499 if libfooStatic.NotAvailableForPlatform() != false {
5500 t.Errorf("%q should be available to platform", libfooStatic.String())
5501 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005502}
5503
Jiyong Park5d790c32019-11-15 18:40:32 +09005504func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005505 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005506 apex {
5507 name: "myapex",
5508 key: "myapex.key",
5509 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005510 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005511 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005512 }
5513
5514 override_apex {
5515 name: "override_myapex",
5516 base: "myapex",
5517 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005518 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005519 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005520 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005521 }
5522
5523 apex_key {
5524 name: "myapex.key",
5525 public_key: "testkey.avbpubkey",
5526 private_key: "testkey.pem",
5527 }
5528
5529 android_app {
5530 name: "app",
5531 srcs: ["foo/bar/MyClass.java"],
5532 package_name: "foo",
5533 sdk_version: "none",
5534 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005535 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005536 }
5537
5538 override_android_app {
5539 name: "override_app",
5540 base: "app",
5541 package_name: "bar",
5542 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005543 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005544
Jiyong Park317645e2019-12-05 13:20:58 +09005545 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5546 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5547 if originalVariant.GetOverriddenBy() != "" {
5548 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5549 }
5550 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5551 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5552 }
5553
Jiyong Park5d790c32019-11-15 18:40:32 +09005554 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5555 apexRule := module.Rule("apexRule")
5556 copyCmds := apexRule.Args["copy_commands"]
5557
5558 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005559 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005560
5561 apexBundle := module.Module().(*apexBundle)
5562 name := apexBundle.Name()
5563 if name != "override_myapex" {
5564 t.Errorf("name should be \"override_myapex\", but was %q", name)
5565 }
5566
Baligh Uddin004d7172020-02-19 21:29:28 -08005567 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5568 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5569 }
5570
Jiyong Park20bacab2020-03-03 11:45:41 +09005571 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005572 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005573
Colin Crossaa255532020-07-03 13:18:24 -07005574 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005575 var builder strings.Builder
5576 data.Custom(&builder, name, "TARGET_", "", data)
5577 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005578 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005579 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5580 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005581 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005582 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005583 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005584 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5585 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005586}
5587
Jooyung Han214bf372019-11-12 13:03:50 +09005588func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005589 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005590 apex {
5591 name: "myapex",
5592 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005593 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005594 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005595 }
5596
5597 apex_key {
5598 name: "myapex.key",
5599 public_key: "testkey.avbpubkey",
5600 private_key: "testkey.pem",
5601 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005602
5603 cc_library {
5604 name: "mylib",
5605 srcs: ["mylib.cpp"],
5606 stl: "libc++",
5607 system_shared_libs: [],
5608 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005609 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005610 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005611 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005612
5613 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5614 args := module.Rule("apexRule").Args
5615 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005616 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005617
5618 // The copies of the libraries in the apex should have one more dependency than
5619 // the ones outside the apex, namely the unwinder. Ideally we should check
5620 // the dependency names directly here but for some reason the names are blank in
5621 // this test.
5622 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005623 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005624 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5625 if len(apexImplicits) != len(nonApexImplicits)+1 {
5626 t.Errorf("%q missing unwinder dep", lib)
5627 }
5628 }
Jooyung Han214bf372019-11-12 13:03:50 +09005629}
5630
Paul Duffine05480a2021-03-08 15:07:14 +00005631var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005632 "api/current.txt": nil,
5633 "api/removed.txt": nil,
5634 "api/system-current.txt": nil,
5635 "api/system-removed.txt": nil,
5636 "api/test-current.txt": nil,
5637 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005638
Anton Hanssondff2c782020-12-21 17:10:01 +00005639 "100/public/api/foo.txt": nil,
5640 "100/public/api/foo-removed.txt": nil,
5641 "100/system/api/foo.txt": nil,
5642 "100/system/api/foo-removed.txt": nil,
5643
Paul Duffineedc5d52020-06-12 17:46:39 +01005644 // For java_sdk_library_import
5645 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005646}
5647
Jooyung Han58f26ab2019-12-18 15:34:32 +09005648func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005649 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005650 apex {
5651 name: "myapex",
5652 key: "myapex.key",
5653 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005654 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005655 }
5656
5657 apex_key {
5658 name: "myapex.key",
5659 public_key: "testkey.avbpubkey",
5660 private_key: "testkey.pem",
5661 }
5662
5663 java_sdk_library {
5664 name: "foo",
5665 srcs: ["a.java"],
5666 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005667 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005668 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005669
5670 prebuilt_apis {
5671 name: "sdk",
5672 api_dirs: ["100"],
5673 }
Paul Duffin9b879592020-05-26 13:21:35 +01005674 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005675
5676 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005677 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005678 "javalib/foo.jar",
5679 "etc/permissions/foo.xml",
5680 })
5681 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005682 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5683 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005684}
5685
Paul Duffin9b879592020-05-26 13:21:35 +01005686func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005687 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005688 apex {
5689 name: "myapex",
5690 key: "myapex.key",
5691 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005692 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005693 }
5694
5695 apex_key {
5696 name: "myapex.key",
5697 public_key: "testkey.avbpubkey",
5698 private_key: "testkey.pem",
5699 }
5700
5701 java_sdk_library {
5702 name: "foo",
5703 srcs: ["a.java"],
5704 api_packages: ["foo"],
5705 apex_available: ["myapex"],
5706 sdk_version: "none",
5707 system_modules: "none",
5708 }
5709
5710 java_library {
5711 name: "bar",
5712 srcs: ["a.java"],
5713 libs: ["foo"],
5714 apex_available: ["myapex"],
5715 sdk_version: "none",
5716 system_modules: "none",
5717 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005718
5719 prebuilt_apis {
5720 name: "sdk",
5721 api_dirs: ["100"],
5722 }
Paul Duffin9b879592020-05-26 13:21:35 +01005723 `, withFiles(filesForSdkLibrary))
5724
5725 // java_sdk_library installs both impl jar and permission XML
5726 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5727 "javalib/bar.jar",
5728 "javalib/foo.jar",
5729 "etc/permissions/foo.xml",
5730 })
5731
5732 // The bar library should depend on the implementation jar.
5733 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5734 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5735 t.Errorf("expected %q, found %#q", expected, actual)
5736 }
5737}
5738
5739func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005740 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005741 apex {
5742 name: "myapex",
5743 key: "myapex.key",
5744 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005745 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005746 }
5747
5748 apex_key {
5749 name: "myapex.key",
5750 public_key: "testkey.avbpubkey",
5751 private_key: "testkey.pem",
5752 }
5753
5754 java_sdk_library {
5755 name: "foo",
5756 srcs: ["a.java"],
5757 api_packages: ["foo"],
5758 apex_available: ["myapex"],
5759 sdk_version: "none",
5760 system_modules: "none",
5761 }
5762
5763 java_library {
5764 name: "bar",
5765 srcs: ["a.java"],
5766 libs: ["foo"],
5767 sdk_version: "none",
5768 system_modules: "none",
5769 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005770
5771 prebuilt_apis {
5772 name: "sdk",
5773 api_dirs: ["100"],
5774 }
Paul Duffin9b879592020-05-26 13:21:35 +01005775 `, withFiles(filesForSdkLibrary))
5776
5777 // java_sdk_library installs both impl jar and permission XML
5778 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5779 "javalib/foo.jar",
5780 "etc/permissions/foo.xml",
5781 })
5782
5783 // The bar library should depend on the stubs jar.
5784 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5785 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5786 t.Errorf("expected %q, found %#q", expected, actual)
5787 }
5788}
5789
Paul Duffineedc5d52020-06-12 17:46:39 +01005790func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005791 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005792 prebuilt_apis {
5793 name: "sdk",
5794 api_dirs: ["100"],
5795 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005796 withFiles(map[string][]byte{
5797 "apex/a.java": nil,
5798 "apex/apex_manifest.json": nil,
5799 "apex/Android.bp": []byte(`
5800 package {
5801 default_visibility: ["//visibility:private"],
5802 }
5803
5804 apex {
5805 name: "myapex",
5806 key: "myapex.key",
5807 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005808 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005809 }
5810
5811 apex_key {
5812 name: "myapex.key",
5813 public_key: "testkey.avbpubkey",
5814 private_key: "testkey.pem",
5815 }
5816
5817 java_library {
5818 name: "bar",
5819 srcs: ["a.java"],
5820 libs: ["foo"],
5821 apex_available: ["myapex"],
5822 sdk_version: "none",
5823 system_modules: "none",
5824 }
5825`),
5826 "source/a.java": nil,
5827 "source/api/current.txt": nil,
5828 "source/api/removed.txt": nil,
5829 "source/Android.bp": []byte(`
5830 package {
5831 default_visibility: ["//visibility:private"],
5832 }
5833
5834 java_sdk_library {
5835 name: "foo",
5836 visibility: ["//apex"],
5837 srcs: ["a.java"],
5838 api_packages: ["foo"],
5839 apex_available: ["myapex"],
5840 sdk_version: "none",
5841 system_modules: "none",
5842 public: {
5843 enabled: true,
5844 },
5845 }
5846`),
5847 "prebuilt/a.jar": nil,
5848 "prebuilt/Android.bp": []byte(`
5849 package {
5850 default_visibility: ["//visibility:private"],
5851 }
5852
5853 java_sdk_library_import {
5854 name: "foo",
5855 visibility: ["//apex", "//source"],
5856 apex_available: ["myapex"],
5857 prefer: true,
5858 public: {
5859 jars: ["a.jar"],
5860 },
5861 }
5862`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005863 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005864 )
5865
5866 // java_sdk_library installs both impl jar and permission XML
5867 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5868 "javalib/bar.jar",
5869 "javalib/foo.jar",
5870 "etc/permissions/foo.xml",
5871 })
5872
5873 // The bar library should depend on the implementation jar.
5874 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5875 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5876 t.Errorf("expected %q, found %#q", expected, actual)
5877 }
5878}
5879
5880func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5881 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5882 apex {
5883 name: "myapex",
5884 key: "myapex.key",
5885 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005886 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005887 }
5888
5889 apex_key {
5890 name: "myapex.key",
5891 public_key: "testkey.avbpubkey",
5892 private_key: "testkey.pem",
5893 }
5894
5895 java_sdk_library_import {
5896 name: "foo",
5897 apex_available: ["myapex"],
5898 prefer: true,
5899 public: {
5900 jars: ["a.jar"],
5901 },
5902 }
5903
5904 `, withFiles(filesForSdkLibrary))
5905}
5906
atrost6e126252020-01-27 17:01:16 +00005907func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005908 result := apexFixtureFactory.
5909 Extend(java.PrepareForTestWithPlatformCompatConfig).
5910 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005911 apex {
5912 name: "myapex",
5913 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005914 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005915 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005916 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005917 }
5918
5919 apex_key {
5920 name: "myapex.key",
5921 public_key: "testkey.avbpubkey",
5922 private_key: "testkey.pem",
5923 }
5924
5925 platform_compat_config {
5926 name: "myjar-platform-compat-config",
5927 src: ":myjar",
5928 }
5929
5930 java_library {
5931 name: "myjar",
5932 srcs: ["foo/bar/MyClass.java"],
5933 sdk_version: "none",
5934 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005935 apex_available: [ "myapex" ],
5936 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005937
5938 // Make sure that a preferred prebuilt does not affect the apex contents.
5939 prebuilt_platform_compat_config {
5940 name: "myjar-platform-compat-config",
5941 metadata: "compat-config/metadata.xml",
5942 prefer: true,
5943 }
atrost6e126252020-01-27 17:01:16 +00005944 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005945 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005946 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5947 "etc/compatconfig/myjar-platform-compat-config.xml",
5948 "javalib/myjar.jar",
5949 })
5950}
5951
Jiyong Park479321d2019-12-16 11:47:12 +09005952func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5953 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5954 apex {
5955 name: "myapex",
5956 key: "myapex.key",
5957 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005958 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005959 }
5960
5961 apex_key {
5962 name: "myapex.key",
5963 public_key: "testkey.avbpubkey",
5964 private_key: "testkey.pem",
5965 }
5966
5967 java_library {
5968 name: "myjar",
5969 srcs: ["foo/bar/MyClass.java"],
5970 sdk_version: "none",
5971 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005972 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005973 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005974 }
5975 `)
5976}
5977
Jiyong Park7afd1072019-12-30 16:56:33 +09005978func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005979 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005980 apex {
5981 name: "myapex",
5982 key: "myapex.key",
5983 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005984 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005985 }
5986
5987 apex_key {
5988 name: "myapex.key",
5989 public_key: "testkey.avbpubkey",
5990 private_key: "testkey.pem",
5991 }
5992
5993 cc_library {
5994 name: "mylib",
5995 srcs: ["mylib.cpp"],
5996 system_shared_libs: [],
5997 stl: "none",
5998 required: ["a", "b"],
5999 host_required: ["c", "d"],
6000 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006001 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006002 }
6003 `)
6004
6005 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006006 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006007 name := apexBundle.BaseModuleName()
6008 prefix := "TARGET_"
6009 var builder strings.Builder
6010 data.Custom(&builder, name, prefix, "", data)
6011 androidMk := builder.String()
6012 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6013 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6014 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6015}
6016
Jiyong Park7cd10e32020-01-14 09:22:18 +09006017func TestSymlinksFromApexToSystem(t *testing.T) {
6018 bp := `
6019 apex {
6020 name: "myapex",
6021 key: "myapex.key",
6022 native_shared_libs: ["mylib"],
6023 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006024 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006025 }
6026
Jiyong Park9d677202020-02-19 16:29:35 +09006027 apex {
6028 name: "myapex.updatable",
6029 key: "myapex.key",
6030 native_shared_libs: ["mylib"],
6031 java_libs: ["myjar"],
6032 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006033 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006034 }
6035
Jiyong Park7cd10e32020-01-14 09:22:18 +09006036 apex_key {
6037 name: "myapex.key",
6038 public_key: "testkey.avbpubkey",
6039 private_key: "testkey.pem",
6040 }
6041
6042 cc_library {
6043 name: "mylib",
6044 srcs: ["mylib.cpp"],
6045 shared_libs: ["myotherlib"],
6046 system_shared_libs: [],
6047 stl: "none",
6048 apex_available: [
6049 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006050 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006051 "//apex_available:platform",
6052 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006053 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006054 }
6055
6056 cc_library {
6057 name: "myotherlib",
6058 srcs: ["mylib.cpp"],
6059 system_shared_libs: [],
6060 stl: "none",
6061 apex_available: [
6062 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006063 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006064 "//apex_available:platform",
6065 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006066 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006067 }
6068
6069 java_library {
6070 name: "myjar",
6071 srcs: ["foo/bar/MyClass.java"],
6072 sdk_version: "none",
6073 system_modules: "none",
6074 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006075 apex_available: [
6076 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006077 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006078 "//apex_available:platform",
6079 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006080 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006081 }
6082
6083 java_library {
6084 name: "myotherjar",
6085 srcs: ["foo/bar/MyClass.java"],
6086 sdk_version: "none",
6087 system_modules: "none",
6088 apex_available: [
6089 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006090 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006091 "//apex_available:platform",
6092 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006093 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006094 }
6095 `
6096
6097 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6098 for _, f := range files {
6099 if f.path == file {
6100 if f.isLink {
6101 t.Errorf("%q is not a real file", file)
6102 }
6103 return
6104 }
6105 }
6106 t.Errorf("%q is not found", file)
6107 }
6108
6109 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6110 for _, f := range files {
6111 if f.path == file {
6112 if !f.isLink {
6113 t.Errorf("%q is not a symlink", file)
6114 }
6115 return
6116 }
6117 }
6118 t.Errorf("%q is not found", file)
6119 }
6120
Jiyong Park9d677202020-02-19 16:29:35 +09006121 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6122 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006123 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006124 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006125 ensureRealfileExists(t, files, "javalib/myjar.jar")
6126 ensureRealfileExists(t, files, "lib64/mylib.so")
6127 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6128
Jiyong Park9d677202020-02-19 16:29:35 +09006129 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6130 ensureRealfileExists(t, files, "javalib/myjar.jar")
6131 ensureRealfileExists(t, files, "lib64/mylib.so")
6132 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6133
6134 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006135 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006136 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006137 ensureRealfileExists(t, files, "javalib/myjar.jar")
6138 ensureRealfileExists(t, files, "lib64/mylib.so")
6139 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006140
6141 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6142 ensureRealfileExists(t, files, "javalib/myjar.jar")
6143 ensureRealfileExists(t, files, "lib64/mylib.so")
6144 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006145}
6146
Yo Chiange8128052020-07-23 20:09:18 +08006147func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006148 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006149 apex {
6150 name: "myapex",
6151 key: "myapex.key",
6152 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006153 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006154 }
6155
6156 apex_key {
6157 name: "myapex.key",
6158 public_key: "testkey.avbpubkey",
6159 private_key: "testkey.pem",
6160 }
6161
6162 cc_library_shared {
6163 name: "mylib",
6164 srcs: ["mylib.cpp"],
6165 shared_libs: ["myotherlib"],
6166 system_shared_libs: [],
6167 stl: "none",
6168 apex_available: [
6169 "myapex",
6170 "//apex_available:platform",
6171 ],
6172 }
6173
6174 cc_prebuilt_library_shared {
6175 name: "myotherlib",
6176 srcs: ["prebuilt.so"],
6177 system_shared_libs: [],
6178 stl: "none",
6179 apex_available: [
6180 "myapex",
6181 "//apex_available:platform",
6182 ],
6183 }
6184 `)
6185
6186 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006187 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006188 var builder strings.Builder
6189 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6190 androidMk := builder.String()
6191 // `myotherlib` is added to `myapex` as symlink
6192 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6193 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6194 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6195 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006196 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 +08006197}
6198
Jooyung Han643adc42020-02-27 13:50:06 +09006199func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006200 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006201 apex {
6202 name: "myapex",
6203 key: "myapex.key",
6204 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006205 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006206 }
6207
6208 apex_key {
6209 name: "myapex.key",
6210 public_key: "testkey.avbpubkey",
6211 private_key: "testkey.pem",
6212 }
6213
6214 cc_library {
6215 name: "mylib",
6216 srcs: ["mylib.cpp"],
6217 shared_libs: ["mylib2"],
6218 system_shared_libs: [],
6219 stl: "none",
6220 apex_available: [ "myapex" ],
6221 }
6222
6223 cc_library {
6224 name: "mylib2",
6225 srcs: ["mylib.cpp"],
6226 system_shared_libs: [],
6227 stl: "none",
6228 apex_available: [ "myapex" ],
6229 }
6230 `)
6231
6232 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6233 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6234 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6235 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6236 "lib64/mylib.so",
6237 "lib64/mylib2.so",
6238 })
6239}
6240
Jooyung Han49f67012020-04-17 13:43:10 +09006241func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006242 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006243 apex {
6244 name: "myapex",
6245 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006246 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006247 }
6248 apex_key {
6249 name: "myapex.key",
6250 public_key: "testkey.avbpubkey",
6251 private_key: "testkey.pem",
6252 }
6253 `, func(fs map[string][]byte, config android.Config) {
6254 delete(config.Targets, android.Android)
6255 config.AndroidCommonTarget = android.Target{}
6256 })
6257
6258 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6259 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6260 }
6261}
6262
Jiyong Parkbd159612020-02-28 15:22:21 +09006263func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006264 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006265 apex {
6266 name: "myapex",
6267 key: "myapex.key",
6268 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006269 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006270 }
6271
6272 apex_key {
6273 name: "myapex.key",
6274 public_key: "testkey.avbpubkey",
6275 private_key: "testkey.pem",
6276 }
6277
6278 android_app {
6279 name: "AppFoo",
6280 srcs: ["foo/bar/MyClass.java"],
6281 sdk_version: "none",
6282 system_modules: "none",
6283 apex_available: [ "myapex" ],
6284 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006285 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006286
Colin Crosscf371cc2020-11-13 11:48:42 -08006287 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006288 content := bundleConfigRule.Args["content"]
6289
6290 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006291 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 +09006292}
6293
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006294func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006295 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006296 apex {
6297 name: "myapex",
6298 key: "myapex.key",
6299 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006300 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006301 }
6302
6303 apex_key {
6304 name: "myapex.key",
6305 public_key: "testkey.avbpubkey",
6306 private_key: "testkey.pem",
6307 }
6308
6309 android_app_set {
6310 name: "AppSet",
6311 set: "AppSet.apks",
6312 }`)
6313 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006314 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006315 content := bundleConfigRule.Args["content"]
6316 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6317 s := mod.Rule("apexRule").Args["copy_commands"]
6318 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6319 if len(copyCmds) != 3 {
6320 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6321 }
6322 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6323 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6324 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6325}
6326
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006327func TestAppSetBundlePrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006328 ctx := testApex(t, "", func(fs map[string][]byte, config android.Config) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006329 bp := `
6330 apex_set {
6331 name: "myapex",
6332 filename: "foo_v2.apex",
6333 sanitized: {
6334 none: { set: "myapex.apks", },
6335 hwaddress: { set: "myapex.hwasan.apks", },
6336 },
6337 }`
6338 fs["Android.bp"] = []byte(bp)
6339
6340 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
6341 })
6342
6343 m := ctx.ModuleForTests("myapex", "android_common")
6344 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6345
6346 actual := extractedApex.Inputs
6347 if len(actual) != 1 {
6348 t.Errorf("expected a single input")
6349 }
6350
6351 expected := "myapex.hwasan.apks"
6352 if actual[0].String() != expected {
6353 t.Errorf("expected %s, got %s", expected, actual[0].String())
6354 }
6355}
6356
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006357func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006358 t.Helper()
6359
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006360 bp := `
6361 java_library {
6362 name: "some-updatable-apex-lib",
6363 srcs: ["a.java"],
6364 sdk_version: "current",
6365 apex_available: [
6366 "some-updatable-apex",
6367 ],
6368 }
6369
6370 java_library {
6371 name: "some-non-updatable-apex-lib",
6372 srcs: ["a.java"],
6373 apex_available: [
6374 "some-non-updatable-apex",
6375 ],
6376 }
6377
6378 java_library {
6379 name: "some-platform-lib",
6380 srcs: ["a.java"],
6381 sdk_version: "current",
6382 installable: true,
6383 }
6384
6385 java_library {
6386 name: "some-art-lib",
6387 srcs: ["a.java"],
6388 sdk_version: "current",
6389 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006390 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006391 ],
6392 hostdex: true,
6393 }
6394
6395 apex {
6396 name: "some-updatable-apex",
6397 key: "some-updatable-apex.key",
6398 java_libs: ["some-updatable-apex-lib"],
6399 updatable: true,
6400 min_sdk_version: "current",
6401 }
6402
6403 apex {
6404 name: "some-non-updatable-apex",
6405 key: "some-non-updatable-apex.key",
6406 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006407 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006408 }
6409
6410 apex_key {
6411 name: "some-updatable-apex.key",
6412 }
6413
6414 apex_key {
6415 name: "some-non-updatable-apex.key",
6416 }
6417
6418 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006419 name: "com.android.art.debug",
6420 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006421 java_libs: ["some-art-lib"],
6422 updatable: true,
6423 min_sdk_version: "current",
6424 }
6425
6426 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006427 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006428 }
6429
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006430 filegroup {
6431 name: "some-updatable-apex-file_contexts",
6432 srcs: [
6433 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6434 ],
6435 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006436
6437 filegroup {
6438 name: "some-non-updatable-apex-file_contexts",
6439 srcs: [
6440 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6441 ],
6442 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006443 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006444
6445 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6446}
6447
Paul Duffin064b70c2020-11-02 17:32:38 +00006448func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006449 t.Helper()
6450
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006451 bp += cc.GatherRequiredDepsForTest(android.Android)
6452 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006453
6454 fs := map[string][]byte{
6455 "a.java": nil,
6456 "a.jar": nil,
6457 "build/make/target/product/security": nil,
6458 "apex_manifest.json": nil,
6459 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006460 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006461 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6462 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6463 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006464 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006465 }
6466 cc.GatherRequiredFilesForTest(fs)
6467
Paul Duffin39853512021-02-26 11:09:39 +00006468 for k, v := range filesForSdkLibrary {
6469 fs[k] = v
6470 }
Colin Crossae8600b2020-10-29 17:09:13 -07006471 config := android.TestArchConfig(buildDir, nil, bp, fs)
6472
6473 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006474 ctx.RegisterModuleType("apex", BundleFactory)
6475 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006476 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006477 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006478 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006479 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006480 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006481 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006482 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006483 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006484 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6485 ctx.PreDepsMutators(RegisterPreDepsMutators)
6486 ctx.PostDepsMutators(RegisterPostDepsMutators)
6487
Colin Crossae8600b2020-10-29 17:09:13 -07006488 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006489
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006490 pathCtx := android.PathContextForTesting(config)
6491 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6492 transformDexpreoptConfig(dexpreoptConfig)
6493 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6494
Paul Duffinf38931c2021-02-05 16:58:28 +00006495 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006496 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006497 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6498 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6499
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006500 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6501 android.FailIfErrored(t, errs)
6502
6503 _, errs = ctx.PrepareBuildActions(config)
6504 if errmsg == "" {
6505 android.FailIfErrored(t, errs)
6506 } else if len(errs) > 0 {
6507 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006508 } else {
6509 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6510 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006511
6512 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006513}
6514
Jooyung Han548640b2020-04-27 12:10:30 +09006515func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6516 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6517 apex {
6518 name: "myapex",
6519 key: "myapex.key",
6520 updatable: true,
6521 }
6522
6523 apex_key {
6524 name: "myapex.key",
6525 public_key: "testkey.avbpubkey",
6526 private_key: "testkey.pem",
6527 }
6528 `)
6529}
6530
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006531func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6532 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6533 apex {
6534 name: "myapex",
6535 key: "myapex.key",
6536 }
6537
6538 apex_key {
6539 name: "myapex.key",
6540 public_key: "testkey.avbpubkey",
6541 private_key: "testkey.pem",
6542 }
6543 `)
6544}
6545
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006546func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006547 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006548 var transform func(*dexpreopt.GlobalConfig)
6549
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006550 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6551 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006552 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006553 }
6554 testNoUpdatableJarsInBootImage(t, "", transform)
6555 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006556
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006557 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006558 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 +01006559 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006560 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006561 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006562 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006563 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006564
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006565 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 -07006566 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 +01006567 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006568 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006569 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006570 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006572
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006573 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 -07006574 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006576 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006577 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006578 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006579 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006580
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006581 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 -07006582 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 +01006583 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006584 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006585 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006586 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006588
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6590 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006591 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006592 }
6593 testNoUpdatableJarsInBootImage(t, "", transform)
6594 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006595
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006597 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006599 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006600 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006601 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006602 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006603
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006604 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006605 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006606 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006607 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006608 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006609 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006610 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006611
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006612 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006613 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006614 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006615 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006616 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006617 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006618 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006619
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006620 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6621 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006622 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006623 }
6624 testNoUpdatableJarsInBootImage(t, "", transform)
6625 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006626
6627}
6628
6629func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6630 transform := func(config *dexpreopt.GlobalConfig) {
6631 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6632 }
6633 t.Run("prebuilt no source", func(t *testing.T) {
6634 testDexpreoptWithApexes(t, `
6635 prebuilt_apex {
6636 name: "myapex" ,
6637 arch: {
6638 arm64: {
6639 src: "myapex-arm64.apex",
6640 },
6641 arm: {
6642 src: "myapex-arm.apex",
6643 },
6644 },
6645 exported_java_libs: ["libfoo"],
6646 }
6647
6648 java_import {
6649 name: "libfoo",
6650 jars: ["libfoo.jar"],
6651 }
6652`, "", transform)
6653 })
6654
6655 t.Run("prebuilt no source", func(t *testing.T) {
6656 testDexpreoptWithApexes(t, `
6657 prebuilt_apex {
6658 name: "myapex" ,
6659 arch: {
6660 arm64: {
6661 src: "myapex-arm64.apex",
6662 },
6663 arm: {
6664 src: "myapex-arm.apex",
6665 },
6666 },
6667 exported_java_libs: ["libfoo"],
6668 }
6669
6670 java_import {
6671 name: "libfoo",
6672 jars: ["libfoo.jar"],
6673 }
6674`, "", transform)
6675 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006676}
6677
Andrei Onea115e7e72020-06-05 21:14:03 +01006678func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6679 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006680 bp += `
6681 apex_key {
6682 name: "myapex.key",
6683 public_key: "testkey.avbpubkey",
6684 private_key: "testkey.pem",
6685 }`
6686 fs := map[string][]byte{
6687 "lib1/src/A.java": nil,
6688 "lib2/src/B.java": nil,
6689 "system/sepolicy/apex/myapex-file_contexts": nil,
6690 }
6691
Colin Crossae8600b2020-10-29 17:09:13 -07006692 config := android.TestArchConfig(buildDir, nil, bp, fs)
6693 android.SetTestNeverallowRules(config, rules)
6694 updatableBootJars := make([]string, 0, len(apexBootJars))
6695 for _, apexBootJar := range apexBootJars {
6696 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6697 }
6698 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6699
6700 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006701 ctx.RegisterModuleType("apex", BundleFactory)
6702 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6703 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6704 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006705 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006706 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6707 ctx.PreDepsMutators(RegisterPreDepsMutators)
6708 ctx.PostDepsMutators(RegisterPostDepsMutators)
6709 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6710
Colin Crossae8600b2020-10-29 17:09:13 -07006711 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006712
6713 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6714 android.FailIfErrored(t, errs)
6715
6716 _, errs = ctx.PrepareBuildActions(config)
6717 if errmsg == "" {
6718 android.FailIfErrored(t, errs)
6719 } else if len(errs) > 0 {
6720 android.FailIfNoMatchingErrors(t, errmsg, errs)
6721 return
6722 } else {
6723 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6724 }
6725}
6726
6727func TestApexPermittedPackagesRules(t *testing.T) {
6728 testcases := []struct {
6729 name string
6730 expectedError string
6731 bp string
6732 bootJars []string
6733 modulesPackages map[string][]string
6734 }{
6735
6736 {
6737 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6738 expectedError: "",
6739 bp: `
6740 java_library {
6741 name: "bcp_lib1",
6742 srcs: ["lib1/src/*.java"],
6743 permitted_packages: ["foo.bar"],
6744 apex_available: ["myapex"],
6745 sdk_version: "none",
6746 system_modules: "none",
6747 }
6748 java_library {
6749 name: "nonbcp_lib2",
6750 srcs: ["lib2/src/*.java"],
6751 apex_available: ["myapex"],
6752 permitted_packages: ["a.b"],
6753 sdk_version: "none",
6754 system_modules: "none",
6755 }
6756 apex {
6757 name: "myapex",
6758 key: "myapex.key",
6759 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006760 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006761 }`,
6762 bootJars: []string{"bcp_lib1"},
6763 modulesPackages: map[string][]string{
6764 "myapex": []string{
6765 "foo.bar",
6766 },
6767 },
6768 },
6769 {
6770 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6771 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.`,
6772 bp: `
6773 java_library {
6774 name: "bcp_lib1",
6775 srcs: ["lib1/src/*.java"],
6776 apex_available: ["myapex"],
6777 permitted_packages: ["foo.bar"],
6778 sdk_version: "none",
6779 system_modules: "none",
6780 }
6781 java_library {
6782 name: "bcp_lib2",
6783 srcs: ["lib2/src/*.java"],
6784 apex_available: ["myapex"],
6785 permitted_packages: ["foo.bar", "bar.baz"],
6786 sdk_version: "none",
6787 system_modules: "none",
6788 }
6789 apex {
6790 name: "myapex",
6791 key: "myapex.key",
6792 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006793 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006794 }
6795 `,
6796 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6797 modulesPackages: map[string][]string{
6798 "myapex": []string{
6799 "foo.bar",
6800 },
6801 },
6802 },
6803 }
6804 for _, tc := range testcases {
6805 t.Run(tc.name, func(t *testing.T) {
6806 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6807 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6808 })
6809 }
6810}
6811
Jiyong Park62304bb2020-04-13 16:19:48 +09006812func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006813 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006814 apex {
6815 name: "myapex",
6816 key: "myapex.key",
6817 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006818 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006819 }
6820
6821 apex_key {
6822 name: "myapex.key",
6823 public_key: "testkey.avbpubkey",
6824 private_key: "testkey.pem",
6825 }
6826
6827 cc_library {
6828 name: "mylib",
6829 srcs: ["mylib.cpp"],
6830 system_shared_libs: [],
6831 stl: "none",
6832 stubs: {
6833 versions: ["1"],
6834 },
6835 apex_available: ["myapex"],
6836 }
6837
6838 cc_library {
6839 name: "myprivlib",
6840 srcs: ["mylib.cpp"],
6841 system_shared_libs: [],
6842 stl: "none",
6843 apex_available: ["myapex"],
6844 }
6845
6846
6847 cc_test {
6848 name: "mytest",
6849 gtest: false,
6850 srcs: ["mylib.cpp"],
6851 system_shared_libs: [],
6852 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006853 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006854 test_for: ["myapex"]
6855 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006856
6857 cc_library {
6858 name: "mytestlib",
6859 srcs: ["mylib.cpp"],
6860 system_shared_libs: [],
6861 shared_libs: ["mylib", "myprivlib"],
6862 stl: "none",
6863 test_for: ["myapex"],
6864 }
6865
6866 cc_benchmark {
6867 name: "mybench",
6868 srcs: ["mylib.cpp"],
6869 system_shared_libs: [],
6870 shared_libs: ["mylib", "myprivlib"],
6871 stl: "none",
6872 test_for: ["myapex"],
6873 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006874 `)
6875
6876 // the test 'mytest' is a test for the apex, therefore is linked to the
6877 // actual implementation of mylib instead of its stub.
6878 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6879 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6880 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006881
6882 // The same should be true for cc_library
6883 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6884 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6885 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6886
6887 // ... and for cc_benchmark
6888 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6889 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6890 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006891}
6892
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006893// TODO(jungjw): Move this to proptools
6894func intPtr(i int) *int {
6895 return &i
6896}
6897
6898func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006899 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006900 apex_set {
6901 name: "myapex",
6902 set: "myapex.apks",
6903 filename: "foo_v2.apex",
6904 overrides: ["foo"],
6905 }
6906 `, func(fs map[string][]byte, config android.Config) {
6907 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006908 config.Targets[android.Android] = []android.Target{
6909 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6910 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6911 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006912 })
6913
6914 m := ctx.ModuleForTests("myapex", "android_common")
6915
6916 // Check extract_apks tool parameters.
6917 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6918 actual := extractedApex.Args["abis"]
6919 expected := "ARMEABI_V7A,ARM64_V8A"
6920 if actual != expected {
6921 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6922 }
6923 actual = extractedApex.Args["sdk-version"]
6924 expected = "30"
6925 if actual != expected {
6926 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6927 }
6928
6929 a := m.Module().(*ApexSet)
6930 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006931 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006932 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6933 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6934 }
6935}
6936
Jiyong Park7d95a512020-05-10 15:16:24 +09006937func TestNoStaticLinkingToStubsLib(t *testing.T) {
6938 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6939 apex {
6940 name: "myapex",
6941 key: "myapex.key",
6942 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006943 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006944 }
6945
6946 apex_key {
6947 name: "myapex.key",
6948 public_key: "testkey.avbpubkey",
6949 private_key: "testkey.pem",
6950 }
6951
6952 cc_library {
6953 name: "mylib",
6954 srcs: ["mylib.cpp"],
6955 static_libs: ["otherlib"],
6956 system_shared_libs: [],
6957 stl: "none",
6958 apex_available: [ "myapex" ],
6959 }
6960
6961 cc_library {
6962 name: "otherlib",
6963 srcs: ["mylib.cpp"],
6964 system_shared_libs: [],
6965 stl: "none",
6966 stubs: {
6967 versions: ["1", "2", "3"],
6968 },
6969 apex_available: [ "myapex" ],
6970 }
6971 `)
6972}
6973
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006974func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006975 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006976 apex {
6977 name: "myapex",
6978 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006979 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006980 }
6981
6982 apex_key {
6983 name: "myapex.key",
6984 public_key: "testkey.avbpubkey",
6985 private_key: "testkey.pem",
6986 }
6987
6988 prebuilt_apex {
6989 name: "myapex",
6990 prefer: true,
6991 arch: {
6992 arm64: {
6993 src: "myapex-arm64.apex",
6994 },
6995 arm: {
6996 src: "myapex-arm.apex",
6997 },
6998 },
6999 }
7000
7001 apex_set {
7002 name: "myapex_set",
7003 set: "myapex.apks",
7004 filename: "myapex_set.apex",
7005 overrides: ["myapex"],
7006 }
7007 `)
7008
7009 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7010 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7011 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 +09007012 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 +09007013}
7014
Jooyung Han938b5932020-06-20 12:47:47 +09007015func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007016 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007017 apex {
7018 name: "myapex",
7019 key: "myapex.key",
7020 apps: ["app"],
7021 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007022 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007023 }
7024
7025 apex_key {
7026 name: "myapex.key",
7027 public_key: "testkey.avbpubkey",
7028 private_key: "testkey.pem",
7029 }
7030
7031 android_app {
7032 name: "app",
7033 srcs: ["foo/bar/MyClass.java"],
7034 package_name: "foo",
7035 sdk_version: "none",
7036 system_modules: "none",
7037 apex_available: [ "myapex" ],
7038 }
7039 `, withFiles(map[string][]byte{
7040 "sub/Android.bp": []byte(`
7041 override_apex {
7042 name: "override_myapex",
7043 base: "myapex",
7044 apps: ["override_app"],
7045 allowed_files: ":allowed",
7046 }
7047 // Overridable "path" property should be referenced indirectly
7048 filegroup {
7049 name: "allowed",
7050 srcs: ["allowed.txt"],
7051 }
7052 override_android_app {
7053 name: "override_app",
7054 base: "app",
7055 package_name: "bar",
7056 }
7057 `),
7058 }))
7059
7060 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7061 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7062 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7063 }
7064
7065 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7066 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7067 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7068 }
7069}
7070
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007071func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007072 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007073 apex {
7074 name: "myapex",
7075 key: "myapex.key",
7076 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007077 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007078 }
7079
7080 apex_key {
7081 name: "myapex.key",
7082 public_key: "testkey.avbpubkey",
7083 private_key: "testkey.pem",
7084 }
7085
7086 cc_library {
7087 name: "mylib",
7088 srcs: ["mylib.cpp"],
7089 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007090 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007091 },
7092 apex_available: ["myapex"],
7093 }
7094
7095 cc_prebuilt_library_shared {
7096 name: "mylib",
7097 prefer: false,
7098 srcs: ["prebuilt.so"],
7099 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007100 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007101 },
7102 apex_available: ["myapex"],
7103 }
7104 `)
7105}
7106
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007107func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007108 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007109 apex {
7110 name: "myapex",
7111 key: "myapex.key",
7112 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007113 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007114 }
7115 apex_key {
7116 name: "myapex.key",
7117 public_key: "testkey.avbpubkey",
7118 private_key: "testkey.pem",
7119 }
7120 `, func(fs map[string][]byte, config android.Config) {
7121 config.TestProductVariables.CompressedApex = proptools.BoolPtr(true)
7122 })
7123
7124 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7125 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7126
7127 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7128 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7129
7130 // Make sure output of bundle is .capex
7131 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7132 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7133
7134 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007135 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007136 var builder strings.Builder
7137 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7138 androidMk := builder.String()
7139 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7140}
7141
Martin Stjernholm2856c662020-12-02 15:03:42 +00007142func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007143 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007144 apex {
7145 name: "myapex",
7146 key: "myapex.key",
7147 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007148 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007149 }
7150
7151 apex_key {
7152 name: "myapex.key",
7153 public_key: "testkey.avbpubkey",
7154 private_key: "testkey.pem",
7155 }
7156
7157 cc_library {
7158 name: "mylib",
7159 srcs: ["mylib.cpp"],
7160 apex_available: ["myapex"],
7161 shared_libs: ["otherlib"],
7162 system_shared_libs: [],
7163 }
7164
7165 cc_library {
7166 name: "otherlib",
7167 srcs: ["mylib.cpp"],
7168 stubs: {
7169 versions: ["current"],
7170 },
7171 }
7172
7173 cc_prebuilt_library_shared {
7174 name: "otherlib",
7175 prefer: true,
7176 srcs: ["prebuilt.so"],
7177 stubs: {
7178 versions: ["current"],
7179 },
7180 }
7181 `)
7182
7183 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007184 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007185 var builder strings.Builder
7186 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7187 androidMk := builder.String()
7188
7189 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7190 // a thing there.
7191 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7192}
7193
Jiyong Parke3867542020-12-03 17:28:25 +09007194func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007195 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007196 apex {
7197 name: "myapex",
7198 key: "myapex.key",
7199 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007200 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007201 }
7202
7203 apex_key {
7204 name: "myapex.key",
7205 public_key: "testkey.avbpubkey",
7206 private_key: "testkey.pem",
7207 }
7208
7209 cc_library {
7210 name: "mylib",
7211 srcs: ["mylib.cpp"],
7212 system_shared_libs: [],
7213 stl: "none",
7214 apex_available: ["myapex"],
7215 shared_libs: ["mylib2"],
7216 target: {
7217 apex: {
7218 exclude_shared_libs: ["mylib2"],
7219 },
7220 },
7221 }
7222
7223 cc_library {
7224 name: "mylib2",
7225 srcs: ["mylib.cpp"],
7226 system_shared_libs: [],
7227 stl: "none",
7228 }
7229 `)
7230
7231 // Check if mylib is linked to mylib2 for the non-apex target
7232 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7233 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7234
7235 // Make sure that the link doesn't occur for the apex target
7236 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7237 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7238
7239 // It shouldn't appear in the copy cmd as well.
7240 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7241 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7242}
7243
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007244func TestPrebuiltStubLibDep(t *testing.T) {
7245 bpBase := `
7246 apex {
7247 name: "myapex",
7248 key: "myapex.key",
7249 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007250 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007251 }
7252 apex_key {
7253 name: "myapex.key",
7254 public_key: "testkey.avbpubkey",
7255 private_key: "testkey.pem",
7256 }
7257 cc_library {
7258 name: "mylib",
7259 srcs: ["mylib.cpp"],
7260 apex_available: ["myapex"],
7261 shared_libs: ["stublib"],
7262 system_shared_libs: [],
7263 }
7264 apex {
7265 name: "otherapex",
7266 enabled: %s,
7267 key: "myapex.key",
7268 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007269 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007270 }
7271 `
7272
7273 stublibSourceBp := `
7274 cc_library {
7275 name: "stublib",
7276 srcs: ["mylib.cpp"],
7277 apex_available: ["otherapex"],
7278 system_shared_libs: [],
7279 stl: "none",
7280 stubs: {
7281 versions: ["1"],
7282 },
7283 }
7284 `
7285
7286 stublibPrebuiltBp := `
7287 cc_prebuilt_library_shared {
7288 name: "stublib",
7289 srcs: ["prebuilt.so"],
7290 apex_available: ["otherapex"],
7291 stubs: {
7292 versions: ["1"],
7293 },
7294 %s
7295 }
7296 `
7297
7298 tests := []struct {
7299 name string
7300 stublibBp string
7301 usePrebuilt bool
7302 modNames []string // Modules to collect AndroidMkEntries for
7303 otherApexEnabled []string
7304 }{
7305 {
7306 name: "only_source",
7307 stublibBp: stublibSourceBp,
7308 usePrebuilt: false,
7309 modNames: []string{"stublib"},
7310 otherApexEnabled: []string{"true", "false"},
7311 },
7312 {
7313 name: "source_preferred",
7314 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7315 usePrebuilt: false,
7316 modNames: []string{"stublib", "prebuilt_stublib"},
7317 otherApexEnabled: []string{"true", "false"},
7318 },
7319 {
7320 name: "prebuilt_preferred",
7321 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7322 usePrebuilt: true,
7323 modNames: []string{"stublib", "prebuilt_stublib"},
7324 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7325 },
7326 {
7327 name: "only_prebuilt",
7328 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7329 usePrebuilt: true,
7330 modNames: []string{"stublib"},
7331 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7332 },
7333 }
7334
7335 for _, test := range tests {
7336 t.Run(test.name, func(t *testing.T) {
7337 for _, otherApexEnabled := range test.otherApexEnabled {
7338 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007339 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007340
7341 type modAndMkEntries struct {
7342 mod *cc.Module
7343 mkEntries android.AndroidMkEntries
7344 }
7345 entries := []*modAndMkEntries{}
7346
7347 // Gather shared lib modules that are installable
7348 for _, modName := range test.modNames {
7349 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7350 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7351 continue
7352 }
7353 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007354 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007355 continue
7356 }
Colin Crossaa255532020-07-03 13:18:24 -07007357 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007358 if ent.Disabled {
7359 continue
7360 }
7361 entries = append(entries, &modAndMkEntries{
7362 mod: mod,
7363 mkEntries: ent,
7364 })
7365 }
7366 }
7367 }
7368
7369 var entry *modAndMkEntries = nil
7370 for _, ent := range entries {
7371 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7372 if entry != nil {
7373 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7374 } else {
7375 entry = ent
7376 }
7377 }
7378 }
7379
7380 if entry == nil {
7381 t.Errorf("AndroidMk entry for \"stublib\" missing")
7382 } else {
7383 isPrebuilt := entry.mod.Prebuilt() != nil
7384 if isPrebuilt != test.usePrebuilt {
7385 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7386 }
7387 if !entry.mod.IsStubs() {
7388 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7389 }
7390 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7391 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7392 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007393 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7394 expected := "-D__STUBLIB_API__=1"
7395 if !android.InList(expected, cflags) {
7396 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7397 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007398 }
7399 })
7400 }
7401 })
7402 }
7403}
7404
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007405func TestMain(m *testing.M) {
7406 run := func() int {
7407 setUp()
7408 defer tearDown()
7409
7410 return m.Run()
7411 }
7412
7413 os.Exit(run())
7414}