blob: faf7ae57941c6cc631d3a86d623ba7d79a8c713d [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 Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jiyong Park7cd10e32020-01-14 09:22:18 +090094func withUnbundledBuild(fs map[string][]byte, config android.Config) {
95 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
96}
97
Jooyung Han344d5432019-08-23 11:17:39 +090098func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090099 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100
101 bp = bp + `
102 toolchain_library {
103 name: "libcompiler_rt-extras",
104 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
109 toolchain_library {
110 name: "libatomic",
111 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 vendor_available: true,
113 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900114 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900115 }
116
117 toolchain_library {
118 name: "libgcc",
119 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900120 vendor_available: true,
121 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900122 }
123
124 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700125 name: "libgcc_stripped",
126 src: "",
127 vendor_available: true,
128 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900129 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700130 }
131
132 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900133 name: "libclang_rt.builtins-aarch64-android",
134 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900135 vendor_available: true,
136 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900137 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138 }
139
140 toolchain_library {
141 name: "libclang_rt.builtins-arm-android",
142 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 vendor_available: true,
144 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900145 native_bridge_supported: true,
146 }
147
148 toolchain_library {
149 name: "libclang_rt.builtins-x86_64-android",
150 src: "",
151 vendor_available: true,
152 recovery_available: true,
153 native_bridge_supported: true,
154 }
155
156 toolchain_library {
157 name: "libclang_rt.builtins-i686-android",
158 src: "",
159 vendor_available: true,
160 recovery_available: true,
161 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900162 }
163
164 cc_object {
165 name: "crtbegin_so",
166 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900167 vendor_available: true,
168 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900169 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900170 }
171
172 cc_object {
173 name: "crtend_so",
174 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900175 vendor_available: true,
176 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900177 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900178 }
179
Alex Light3d673592019-01-18 14:37:31 -0800180 cc_object {
181 name: "crtbegin_static",
182 stl: "none",
183 }
184
185 cc_object {
186 name: "crtend_android",
187 stl: "none",
188 }
189
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 llndk_library {
191 name: "libc",
192 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900193 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900194 }
195
196 llndk_library {
197 name: "libm",
198 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900199 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900200 }
201
202 llndk_library {
203 name: "libdl",
204 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900205 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900206 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900207
208 filegroup {
209 name: "myapex-file_contexts",
210 srcs: [
211 "system/sepolicy/apex/myapex-file_contexts",
212 ],
213 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800215
Dario Frenicde2a032019-10-27 00:29:22 +0100216 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217
Jooyung Han344d5432019-08-23 11:17:39 +0900218 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900219 "a.java": nil,
220 "PrebuiltAppFoo.apk": nil,
221 "PrebuiltAppFooPriv.apk": nil,
222 "build/make/target/product/security": nil,
223 "apex_manifest.json": nil,
224 "AndroidManifest.xml": nil,
225 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900226 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900227 "system/sepolicy/apex/otherapex-file_contexts": nil,
228 "system/sepolicy/apex/commonapex-file_contexts": nil,
229 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800230 "mylib.cpp": nil,
231 "mylib_common.cpp": nil,
232 "mytest.cpp": nil,
233 "mytest1.cpp": nil,
234 "mytest2.cpp": nil,
235 "mytest3.cpp": nil,
236 "myprebuilt": nil,
237 "my_include": nil,
238 "foo/bar/MyClass.java": nil,
239 "prebuilt.jar": nil,
240 "vendor/foo/devkeys/test.x509.pem": nil,
241 "vendor/foo/devkeys/test.pk8": nil,
242 "testkey.x509.pem": nil,
243 "testkey.pk8": nil,
244 "testkey.override.x509.pem": nil,
245 "testkey.override.pk8": nil,
246 "vendor/foo/devkeys/testkey.avbpubkey": nil,
247 "vendor/foo/devkeys/testkey.pem": nil,
248 "NOTICE": nil,
249 "custom_notice": nil,
250 "testkey2.avbpubkey": nil,
251 "testkey2.pem": nil,
252 "myapex-arm64.apex": nil,
253 "myapex-arm.apex": nil,
254 "frameworks/base/api/current.txt": nil,
255 "framework/aidl/a.aidl": nil,
256 "build/make/core/proguard.flags": nil,
257 "build/make/core/proguard_basic_keeps.flags": nil,
258 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900259 }
260
261 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800262 // The fs now needs to be populated before creating the config, call handlers twice
263 // for now, once to get any fs changes, and later after the config was created to
264 // set product variables or targets.
265 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
266 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900267 }
268
Colin Cross98be1bb2019-12-13 20:41:13 -0800269 config := android.TestArchConfig(buildDir, nil, bp, fs)
270 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
271 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
272 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
273 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
274 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
275 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
276
277 for _, handler := range handlers {
278 // The fs now needs to be populated before creating the config, call handlers twice
279 // for now, earlier to get any fs changes, and now after the config was created to
280 // set product variables or targets.
281 tempFS := map[string][]byte{}
282 handler(tempFS, config)
283 }
284
285 ctx := android.NewTestArchContext()
286 ctx.RegisterModuleType("apex", BundleFactory)
287 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
288 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
289 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
290 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
291 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
292 ctx.RegisterModuleType("override_apex", overrideApexFactory)
293
Jooyung Hana57af4a2020-01-23 05:36:59 +0000294 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
295 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
296
Paul Duffin77980a82019-12-19 16:01:36 +0000297 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800298 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800299 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
300 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000302 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800304 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000305 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000306 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000307 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900308 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800309
Colin Cross98be1bb2019-12-13 20:41:13 -0800310 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800311 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800312
313 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314
Jooyung Han5c998b92019-06-27 11:30:33 +0900315 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316}
317
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700318func setUp() {
319 var err error
320 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700322 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324}
325
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700326func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 os.RemoveAll(buildDir)
328}
329
330// ensure that 'result' contains 'expected'
331func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900332 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 if !strings.Contains(result, expected) {
334 t.Errorf("%q is not found in %q", expected, result)
335 }
336}
337
338// ensures that 'result' does not contain 'notExpected'
339func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900340 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 if strings.Contains(result, notExpected) {
342 t.Errorf("%q is found in %q", notExpected, result)
343 }
344}
345
346func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900347 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 if !android.InList(expected, result) {
349 t.Errorf("%q is not found in %v", expected, result)
350 }
351}
352
353func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900354 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 if android.InList(notExpected, result) {
356 t.Errorf("%q is found in %v", notExpected, result)
357 }
358}
359
Jooyung Hane1633032019-08-01 17:41:43 +0900360func ensureListEmpty(t *testing.T, result []string) {
361 t.Helper()
362 if len(result) > 0 {
363 t.Errorf("%q is expected to be empty", result)
364 }
365}
366
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367// Minimal test
368func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700369 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900370 apex_defaults {
371 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900372 manifest: ":myapex.manifest",
373 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900374 key: "myapex.key",
375 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800376 multilib: {
377 both: {
378 binaries: ["foo",],
379 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900381 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900382 }
383
Jiyong Park30ca9372019-02-07 16:27:23 +0900384 apex {
385 name: "myapex",
386 defaults: ["myapex-defaults"],
387 }
388
Jiyong Park25fc6a92018-11-18 18:02:45 +0900389 apex_key {
390 name: "myapex.key",
391 public_key: "testkey.avbpubkey",
392 private_key: "testkey.pem",
393 }
394
Jiyong Park809bb722019-02-13 21:33:49 +0900395 filegroup {
396 name: "myapex.manifest",
397 srcs: ["apex_manifest.json"],
398 }
399
400 filegroup {
401 name: "myapex.androidmanifest",
402 srcs: ["AndroidManifest.xml"],
403 }
404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 cc_library {
406 name: "mylib",
407 srcs: ["mylib.cpp"],
408 shared_libs: ["mylib2"],
409 system_shared_libs: [],
410 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000411 // TODO: remove //apex_available:platform
412 apex_available: [
413 "//apex_available:platform",
414 "myapex",
415 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900416 }
417
Alex Light3d673592019-01-18 14:37:31 -0800418 cc_binary {
419 name: "foo",
420 srcs: ["mylib.cpp"],
421 compile_multilib: "both",
422 multilib: {
423 lib32: {
424 suffix: "32",
425 },
426 lib64: {
427 suffix: "64",
428 },
429 },
430 symlinks: ["foo_link_"],
431 symlink_preferred_arch: true,
432 system_shared_libs: [],
433 static_executable: true,
434 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000435 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800436 }
437
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 cc_library {
439 name: "mylib2",
440 srcs: ["mylib.cpp"],
441 system_shared_libs: [],
442 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900443 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000444 // TODO: remove //apex_available:platform
445 apex_available: [
446 "//apex_available:platform",
447 "myapex",
448 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900450
451 java_library {
452 name: "myjar",
453 srcs: ["foo/bar/MyClass.java"],
454 sdk_version: "none",
455 system_modules: "none",
456 compile_dex: true,
457 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900458 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900464 }
465
466 java_library {
467 name: "myotherjar",
468 srcs: ["foo/bar/MyClass.java"],
469 sdk_version: "none",
470 system_modules: "none",
471 compile_dex: true,
472 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900473
474 java_library {
475 name: "mysharedjar",
476 srcs: ["foo/bar/MyClass.java"],
477 sdk_version: "none",
478 system_modules: "none",
479 compile_dex: true,
480 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900481 `)
482
Sundong Ahnabb64432019-10-22 13:58:29 +0900483 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900484
485 optFlags := apexRule.Args["opt_flags"]
486 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700487 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900488 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900489
Jiyong Park25fc6a92018-11-18 18:02:45 +0900490 copyCmds := apexRule.Args["copy_commands"]
491
492 // Ensure that main rule creates an output
493 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
494
495 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800496 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900497 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900498
499 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800500 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900501 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900502
503 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800504 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
505 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
507 // .. but not for java libs
508 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900509 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800510
Colin Cross7113d202019-11-20 16:39:12 -0800511 // Ensure that the platform variant ends with _shared or _common
512 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
513 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900514 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
515 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900516 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
517
518 // Ensure that dynamic dependency to java libs are not included
519 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800520
521 // Ensure that all symlinks are present.
522 found_foo_link_64 := false
523 found_foo := false
524 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900525 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800526 if strings.HasSuffix(cmd, "bin/foo") {
527 found_foo = true
528 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
529 found_foo_link_64 = true
530 }
531 }
532 }
533 good := found_foo && found_foo_link_64
534 if !good {
535 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
536 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900537
Sundong Ahnabb64432019-10-22 13:58:29 +0900538 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700539 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700540 if len(noticeInputs) != 2 {
541 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900542 }
543 ensureListContains(t, noticeInputs, "NOTICE")
544 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900545
546 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
547 ensureListContains(t, depsInfo, "internal myjar")
548 ensureListContains(t, depsInfo, "internal mylib")
549 ensureListContains(t, depsInfo, "internal mylib2")
550 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800551}
552
Jooyung Hanf21c7972019-12-16 22:32:06 +0900553func TestDefaults(t *testing.T) {
554 ctx, _ := testApex(t, `
555 apex_defaults {
556 name: "myapex-defaults",
557 key: "myapex.key",
558 prebuilts: ["myetc"],
559 native_shared_libs: ["mylib"],
560 java_libs: ["myjar"],
561 apps: ["AppFoo"],
562 }
563
564 prebuilt_etc {
565 name: "myetc",
566 src: "myprebuilt",
567 }
568
569 apex {
570 name: "myapex",
571 defaults: ["myapex-defaults"],
572 }
573
574 apex_key {
575 name: "myapex.key",
576 public_key: "testkey.avbpubkey",
577 private_key: "testkey.pem",
578 }
579
580 cc_library {
581 name: "mylib",
582 system_shared_libs: [],
583 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000584 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900585 }
586
587 java_library {
588 name: "myjar",
589 srcs: ["foo/bar/MyClass.java"],
590 sdk_version: "none",
591 system_modules: "none",
592 compile_dex: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000593 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900594 }
595
596 android_app {
597 name: "AppFoo",
598 srcs: ["foo/bar/MyClass.java"],
599 sdk_version: "none",
600 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000601 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900602 }
603 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000604 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900605 "etc/myetc",
606 "javalib/myjar.jar",
607 "lib64/mylib.so",
608 "app/AppFoo/AppFoo.apk",
609 })
610}
611
Jooyung Han01a3ee22019-11-02 02:52:25 +0900612func TestApexManifest(t *testing.T) {
613 ctx, _ := testApex(t, `
614 apex {
615 name: "myapex",
616 key: "myapex.key",
617 }
618
619 apex_key {
620 name: "myapex.key",
621 public_key: "testkey.avbpubkey",
622 private_key: "testkey.pem",
623 }
624 `)
625
626 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900627 args := module.Rule("apexRule").Args
628 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
629 t.Error("manifest should be apex_manifest.pb, but " + manifest)
630 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900631}
632
Alex Light5098a612018-11-29 17:12:15 -0800633func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700634 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800635 apex {
636 name: "myapex",
637 key: "myapex.key",
638 payload_type: "zip",
639 native_shared_libs: ["mylib"],
640 }
641
642 apex_key {
643 name: "myapex.key",
644 public_key: "testkey.avbpubkey",
645 private_key: "testkey.pem",
646 }
647
648 cc_library {
649 name: "mylib",
650 srcs: ["mylib.cpp"],
651 shared_libs: ["mylib2"],
652 system_shared_libs: [],
653 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000654 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800655 }
656
657 cc_library {
658 name: "mylib2",
659 srcs: ["mylib.cpp"],
660 system_shared_libs: [],
661 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000662 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800663 }
664 `)
665
Sundong Ahnabb64432019-10-22 13:58:29 +0900666 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800667 copyCmds := zipApexRule.Args["copy_commands"]
668
669 // Ensure that main rule creates an output
670 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
671
672 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800673 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800674
675 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800676 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800677
678 // Ensure that both direct and indirect deps are copied into apex
679 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
680 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900681}
682
683func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700684 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900685 apex {
686 name: "myapex",
687 key: "myapex.key",
688 native_shared_libs: ["mylib", "mylib3"],
689 }
690
691 apex_key {
692 name: "myapex.key",
693 public_key: "testkey.avbpubkey",
694 private_key: "testkey.pem",
695 }
696
697 cc_library {
698 name: "mylib",
699 srcs: ["mylib.cpp"],
700 shared_libs: ["mylib2", "mylib3"],
701 system_shared_libs: [],
702 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000703 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704 }
705
706 cc_library {
707 name: "mylib2",
708 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900709 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710 system_shared_libs: [],
711 stl: "none",
712 stubs: {
713 versions: ["1", "2", "3"],
714 },
715 }
716
717 cc_library {
718 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900719 srcs: ["mylib.cpp"],
720 shared_libs: ["mylib4"],
721 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900722 stl: "none",
723 stubs: {
724 versions: ["10", "11", "12"],
725 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000726 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900728
729 cc_library {
730 name: "mylib4",
731 srcs: ["mylib.cpp"],
732 system_shared_libs: [],
733 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000734 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900735 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900736 `)
737
Sundong Ahnabb64432019-10-22 13:58:29 +0900738 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900739 copyCmds := apexRule.Args["copy_commands"]
740
741 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800742 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900743
744 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800745 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900746
747 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800748 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900749
Colin Cross7113d202019-11-20 16:39:12 -0800750 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900751
752 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900753 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900755 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900756
757 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800758 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900759 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800760 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900761
762 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800763 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900764 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900765
766 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900767 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900768
Jooyung Hana57af4a2020-01-23 05:36:59 +0000769 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900770 "lib64/mylib.so",
771 "lib64/mylib3.so",
772 "lib64/mylib4.so",
773 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900774}
775
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900776func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700777 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900778 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900779 name: "myapex2",
780 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781 native_shared_libs: ["mylib"],
782 }
783
784 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900785 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900786 public_key: "testkey.avbpubkey",
787 private_key: "testkey.pem",
788 }
789
790 cc_library {
791 name: "mylib",
792 srcs: ["mylib.cpp"],
793 shared_libs: ["libfoo#10"],
794 system_shared_libs: [],
795 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000796 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900797 }
798
799 cc_library {
800 name: "libfoo",
801 srcs: ["mylib.cpp"],
802 shared_libs: ["libbar"],
803 system_shared_libs: [],
804 stl: "none",
805 stubs: {
806 versions: ["10", "20", "30"],
807 },
808 }
809
810 cc_library {
811 name: "libbar",
812 srcs: ["mylib.cpp"],
813 system_shared_libs: [],
814 stl: "none",
815 }
816
817 `)
818
Jiyong Park83dc74b2020-01-14 18:38:44 +0900819 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900820 copyCmds := apexRule.Args["copy_commands"]
821
822 // Ensure that direct non-stubs dep is always included
823 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
824
825 // Ensure that indirect stubs dep is not included
826 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
827
828 // Ensure that dependency of stubs is not included
829 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
830
Jiyong Park83dc74b2020-01-14 18:38:44 +0900831 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900832
833 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900834 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900835 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900836 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900837
Jiyong Park3ff16992019-12-27 14:11:47 +0900838 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900839
840 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
841 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900842
843 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
844 ensureListContains(t, depsInfo, "internal mylib")
845 ensureListContains(t, depsInfo, "external libfoo")
846 ensureListNotContains(t, depsInfo, "internal libfoo")
847 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900848}
849
Jooyung Hand3639552019-08-09 12:57:43 +0900850func TestApexWithRuntimeLibsDependency(t *testing.T) {
851 /*
852 myapex
853 |
854 v (runtime_libs)
855 mylib ------+------> libfoo [provides stub]
856 |
857 `------> libbar
858 */
859 ctx, _ := testApex(t, `
860 apex {
861 name: "myapex",
862 key: "myapex.key",
863 native_shared_libs: ["mylib"],
864 }
865
866 apex_key {
867 name: "myapex.key",
868 public_key: "testkey.avbpubkey",
869 private_key: "testkey.pem",
870 }
871
872 cc_library {
873 name: "mylib",
874 srcs: ["mylib.cpp"],
875 runtime_libs: ["libfoo", "libbar"],
876 system_shared_libs: [],
877 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000878 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900879 }
880
881 cc_library {
882 name: "libfoo",
883 srcs: ["mylib.cpp"],
884 system_shared_libs: [],
885 stl: "none",
886 stubs: {
887 versions: ["10", "20", "30"],
888 },
889 }
890
891 cc_library {
892 name: "libbar",
893 srcs: ["mylib.cpp"],
894 system_shared_libs: [],
895 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000896 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900897 }
898
899 `)
900
Sundong Ahnabb64432019-10-22 13:58:29 +0900901 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900902 copyCmds := apexRule.Args["copy_commands"]
903
904 // Ensure that direct non-stubs dep is always included
905 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
906
907 // Ensure that indirect stubs dep is not included
908 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
909
910 // Ensure that runtime_libs dep in included
911 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
912
Sundong Ahnabb64432019-10-22 13:58:29 +0900913 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900914 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
915 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900916
917}
918
Jooyung Han9c80bae2019-08-20 17:30:57 +0900919func TestApexDependencyToLLNDK(t *testing.T) {
920 ctx, _ := testApex(t, `
921 apex {
922 name: "myapex",
923 key: "myapex.key",
924 use_vendor: true,
925 native_shared_libs: ["mylib"],
926 }
927
928 apex_key {
929 name: "myapex.key",
930 public_key: "testkey.avbpubkey",
931 private_key: "testkey.pem",
932 }
933
934 cc_library {
935 name: "mylib",
936 srcs: ["mylib.cpp"],
937 vendor_available: true,
938 shared_libs: ["libbar"],
939 system_shared_libs: [],
940 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000941 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900942 }
943
944 cc_library {
945 name: "libbar",
946 srcs: ["mylib.cpp"],
947 system_shared_libs: [],
948 stl: "none",
949 }
950
951 llndk_library {
952 name: "libbar",
953 symbol_file: "",
954 }
Jooyung Handc782442019-11-01 03:14:38 +0900955 `, func(fs map[string][]byte, config android.Config) {
956 setUseVendorWhitelistForTest(config, []string{"myapex"})
957 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900958
Sundong Ahnabb64432019-10-22 13:58:29 +0900959 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900960 copyCmds := apexRule.Args["copy_commands"]
961
962 // Ensure that LLNDK dep is not included
963 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
964
Sundong Ahnabb64432019-10-22 13:58:29 +0900965 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900966 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900967
968 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900969 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900970
971}
972
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700974 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 apex {
976 name: "myapex",
977 key: "myapex.key",
978 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
979 }
980
981 apex_key {
982 name: "myapex.key",
983 public_key: "testkey.avbpubkey",
984 private_key: "testkey.pem",
985 }
986
987 cc_library {
988 name: "mylib",
989 srcs: ["mylib.cpp"],
990 shared_libs: ["libdl#27"],
991 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000992 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 }
994
995 cc_library_shared {
996 name: "mylib_shared",
997 srcs: ["mylib.cpp"],
998 shared_libs: ["libdl#27"],
999 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001000 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001 }
1002
1003 cc_library {
1004 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001005 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006 nocrt: true,
1007 system_shared_libs: [],
1008 stl: "none",
1009 stubs: {
1010 versions: ["27", "28", "29"],
1011 },
1012 }
1013
1014 cc_library {
1015 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001016 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001017 nocrt: true,
1018 system_shared_libs: [],
1019 stl: "none",
1020 stubs: {
1021 versions: ["27", "28", "29"],
1022 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001023 apex_available: [
1024 "//apex_available:platform",
1025 "myapex"
1026 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027 }
1028
1029 cc_library {
1030 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001031 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 nocrt: true,
1033 system_shared_libs: [],
1034 stl: "none",
1035 stubs: {
1036 versions: ["27", "28", "29"],
1037 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001038 apex_available: [
1039 "//apex_available:platform",
1040 "myapex"
1041 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001042 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001043
1044 cc_library {
1045 name: "libBootstrap",
1046 srcs: ["mylib.cpp"],
1047 stl: "none",
1048 bootstrap: true,
1049 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001050 `)
1051
Sundong Ahnabb64432019-10-22 13:58:29 +09001052 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001053 copyCmds := apexRule.Args["copy_commands"]
1054
1055 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001056 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001057 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1058 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001059
1060 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001061 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001062
Colin Cross7113d202019-11-20 16:39:12 -08001063 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1064 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1065 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001066
1067 // For dependency to libc
1068 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001069 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001070 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001071 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001072 // ... Cflags from stub is correctly exported to mylib
1073 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1074 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1075
1076 // For dependency to libm
1077 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001078 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001079 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001080 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001081 // ... and is not compiling with the stub
1082 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1083 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1084
1085 // For dependency to libdl
1086 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001087 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001088 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001089 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1090 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001091 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001092 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001093 // ... Cflags from stub is correctly exported to mylib
1094 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1095 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001096
1097 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001098 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1099 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1100 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1101 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001102}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001103
1104func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001105 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001106 apex {
1107 name: "myapex",
1108 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001109 native_shared_libs: ["mylib"],
1110 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001111 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001112 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001113 }
1114
1115 apex_key {
1116 name: "myapex.key",
1117 public_key: "testkey.avbpubkey",
1118 private_key: "testkey.pem",
1119 }
1120
1121 prebuilt_etc {
1122 name: "myetc",
1123 src: "myprebuilt",
1124 sub_dir: "foo/bar",
1125 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001126
1127 cc_library {
1128 name: "mylib",
1129 srcs: ["mylib.cpp"],
1130 relative_install_path: "foo/bar",
1131 system_shared_libs: [],
1132 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001133 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001134 }
1135
1136 cc_binary {
1137 name: "mybin",
1138 srcs: ["mylib.cpp"],
1139 relative_install_path: "foo/bar",
1140 system_shared_libs: [],
1141 static_executable: true,
1142 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001143 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001144 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001145 `)
1146
Sundong Ahnabb64432019-10-22 13:58:29 +09001147 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001148 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1149
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001150 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001151 ensureListContains(t, dirs, "etc")
1152 ensureListContains(t, dirs, "etc/foo")
1153 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001154 ensureListContains(t, dirs, "lib64")
1155 ensureListContains(t, dirs, "lib64/foo")
1156 ensureListContains(t, dirs, "lib64/foo/bar")
1157 ensureListContains(t, dirs, "lib")
1158 ensureListContains(t, dirs, "lib/foo")
1159 ensureListContains(t, dirs, "lib/foo/bar")
1160
Jiyong Parkbd13e442019-03-15 18:10:35 +09001161 ensureListContains(t, dirs, "bin")
1162 ensureListContains(t, dirs, "bin/foo")
1163 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001164}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001165
1166func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001167 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001168 apex {
1169 name: "myapex",
1170 key: "myapex.key",
1171 native_shared_libs: ["mylib"],
1172 use_vendor: true,
1173 }
1174
1175 apex_key {
1176 name: "myapex.key",
1177 public_key: "testkey.avbpubkey",
1178 private_key: "testkey.pem",
1179 }
1180
1181 cc_library {
1182 name: "mylib",
1183 srcs: ["mylib.cpp"],
1184 shared_libs: ["mylib2"],
1185 system_shared_libs: [],
1186 vendor_available: true,
1187 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001188 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001189 }
1190
1191 cc_library {
1192 name: "mylib2",
1193 srcs: ["mylib.cpp"],
1194 system_shared_libs: [],
1195 vendor_available: true,
1196 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001197 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001198 }
Jooyung Handc782442019-11-01 03:14:38 +09001199 `, func(fs map[string][]byte, config android.Config) {
1200 setUseVendorWhitelistForTest(config, []string{"myapex"})
1201 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001202
1203 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001204 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001205 for _, implicit := range i.Implicits {
1206 inputsList = append(inputsList, implicit.String())
1207 }
1208 }
1209 inputsString := strings.Join(inputsList, " ")
1210
1211 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001212 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1213 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001214
1215 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001216 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1217 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001218}
Jiyong Park16e91a02018-12-20 18:18:08 +09001219
Jooyung Handc782442019-11-01 03:14:38 +09001220func TestUseVendorRestriction(t *testing.T) {
1221 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1222 apex {
1223 name: "myapex",
1224 key: "myapex.key",
1225 use_vendor: true,
1226 }
1227 apex_key {
1228 name: "myapex.key",
1229 public_key: "testkey.avbpubkey",
1230 private_key: "testkey.pem",
1231 }
1232 `, func(fs map[string][]byte, config android.Config) {
1233 setUseVendorWhitelistForTest(config, []string{""})
1234 })
1235 // no error with whitelist
1236 testApex(t, `
1237 apex {
1238 name: "myapex",
1239 key: "myapex.key",
1240 use_vendor: true,
1241 }
1242 apex_key {
1243 name: "myapex.key",
1244 public_key: "testkey.avbpubkey",
1245 private_key: "testkey.pem",
1246 }
1247 `, func(fs map[string][]byte, config android.Config) {
1248 setUseVendorWhitelistForTest(config, []string{"myapex"})
1249 })
1250}
1251
Jooyung Han5c998b92019-06-27 11:30:33 +09001252func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1253 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1254 apex {
1255 name: "myapex",
1256 key: "myapex.key",
1257 native_shared_libs: ["mylib"],
1258 use_vendor: true,
1259 }
1260
1261 apex_key {
1262 name: "myapex.key",
1263 public_key: "testkey.avbpubkey",
1264 private_key: "testkey.pem",
1265 }
1266
1267 cc_library {
1268 name: "mylib",
1269 srcs: ["mylib.cpp"],
1270 system_shared_libs: [],
1271 stl: "none",
1272 }
1273 `)
1274}
1275
Jiyong Park16e91a02018-12-20 18:18:08 +09001276func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001277 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001278 apex {
1279 name: "myapex",
1280 key: "myapex.key",
1281 native_shared_libs: ["mylib"],
1282 }
1283
1284 apex_key {
1285 name: "myapex.key",
1286 public_key: "testkey.avbpubkey",
1287 private_key: "testkey.pem",
1288 }
1289
1290 cc_library {
1291 name: "mylib",
1292 srcs: ["mylib.cpp"],
1293 system_shared_libs: [],
1294 stl: "none",
1295 stubs: {
1296 versions: ["1", "2", "3"],
1297 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001298 apex_available: [
1299 "//apex_available:platform",
1300 "myapex",
1301 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001302 }
1303
1304 cc_binary {
1305 name: "not_in_apex",
1306 srcs: ["mylib.cpp"],
1307 static_libs: ["mylib"],
1308 static_executable: true,
1309 system_shared_libs: [],
1310 stl: "none",
1311 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001312 `)
1313
Colin Cross7113d202019-11-20 16:39:12 -08001314 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001315
1316 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001317 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001318}
Jiyong Park9335a262018-12-24 11:31:58 +09001319
1320func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001321 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001322 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001323 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001324 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001325 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001326 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001327 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001328 }
1329
1330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 system_shared_libs: [],
1334 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001335 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001336 }
1337
1338 apex_key {
1339 name: "myapex.key",
1340 public_key: "testkey.avbpubkey",
1341 private_key: "testkey.pem",
1342 }
1343
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001344 android_app_certificate {
1345 name: "myapex.certificate",
1346 certificate: "testkey",
1347 }
1348
1349 android_app_certificate {
1350 name: "myapex.certificate.override",
1351 certificate: "testkey.override",
1352 }
1353
Jiyong Park9335a262018-12-24 11:31:58 +09001354 `)
1355
1356 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001357 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001358
1359 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1360 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1361 "vendor/foo/devkeys/testkey.avbpubkey")
1362 }
1363 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1364 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1365 "vendor/foo/devkeys/testkey.pem")
1366 }
1367
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001368 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001369 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001370 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001371 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001372 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001373 }
1374}
Jiyong Park58e364a2019-01-19 19:24:06 +09001375
Jooyung Hanf121a652019-12-17 14:30:11 +09001376func TestCertificate(t *testing.T) {
1377 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1378 ctx, _ := testApex(t, `
1379 apex {
1380 name: "myapex",
1381 key: "myapex.key",
1382 }
1383 apex_key {
1384 name: "myapex.key",
1385 public_key: "testkey.avbpubkey",
1386 private_key: "testkey.pem",
1387 }`)
1388 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1389 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1390 if actual := rule.Args["certificates"]; actual != expected {
1391 t.Errorf("certificates should be %q, not %q", expected, actual)
1392 }
1393 })
1394 t.Run("override when unspecified", func(t *testing.T) {
1395 ctx, _ := testApex(t, `
1396 apex {
1397 name: "myapex_keytest",
1398 key: "myapex.key",
1399 file_contexts: ":myapex-file_contexts",
1400 }
1401 apex_key {
1402 name: "myapex.key",
1403 public_key: "testkey.avbpubkey",
1404 private_key: "testkey.pem",
1405 }
1406 android_app_certificate {
1407 name: "myapex.certificate.override",
1408 certificate: "testkey.override",
1409 }`)
1410 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1411 expected := "testkey.override.x509.pem testkey.override.pk8"
1412 if actual := rule.Args["certificates"]; actual != expected {
1413 t.Errorf("certificates should be %q, not %q", expected, actual)
1414 }
1415 })
1416 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1417 ctx, _ := testApex(t, `
1418 apex {
1419 name: "myapex",
1420 key: "myapex.key",
1421 certificate: ":myapex.certificate",
1422 }
1423 apex_key {
1424 name: "myapex.key",
1425 public_key: "testkey.avbpubkey",
1426 private_key: "testkey.pem",
1427 }
1428 android_app_certificate {
1429 name: "myapex.certificate",
1430 certificate: "testkey",
1431 }`)
1432 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1433 expected := "testkey.x509.pem testkey.pk8"
1434 if actual := rule.Args["certificates"]; actual != expected {
1435 t.Errorf("certificates should be %q, not %q", expected, actual)
1436 }
1437 })
1438 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1439 ctx, _ := testApex(t, `
1440 apex {
1441 name: "myapex_keytest",
1442 key: "myapex.key",
1443 file_contexts: ":myapex-file_contexts",
1444 certificate: ":myapex.certificate",
1445 }
1446 apex_key {
1447 name: "myapex.key",
1448 public_key: "testkey.avbpubkey",
1449 private_key: "testkey.pem",
1450 }
1451 android_app_certificate {
1452 name: "myapex.certificate.override",
1453 certificate: "testkey.override",
1454 }`)
1455 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1456 expected := "testkey.override.x509.pem testkey.override.pk8"
1457 if actual := rule.Args["certificates"]; actual != expected {
1458 t.Errorf("certificates should be %q, not %q", expected, actual)
1459 }
1460 })
1461 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1462 ctx, _ := testApex(t, `
1463 apex {
1464 name: "myapex",
1465 key: "myapex.key",
1466 certificate: "testkey",
1467 }
1468 apex_key {
1469 name: "myapex.key",
1470 public_key: "testkey.avbpubkey",
1471 private_key: "testkey.pem",
1472 }`)
1473 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1474 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1475 if actual := rule.Args["certificates"]; actual != expected {
1476 t.Errorf("certificates should be %q, not %q", expected, actual)
1477 }
1478 })
1479 t.Run("override when specified as <name>", func(t *testing.T) {
1480 ctx, _ := testApex(t, `
1481 apex {
1482 name: "myapex_keytest",
1483 key: "myapex.key",
1484 file_contexts: ":myapex-file_contexts",
1485 certificate: "testkey",
1486 }
1487 apex_key {
1488 name: "myapex.key",
1489 public_key: "testkey.avbpubkey",
1490 private_key: "testkey.pem",
1491 }
1492 android_app_certificate {
1493 name: "myapex.certificate.override",
1494 certificate: "testkey.override",
1495 }`)
1496 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1497 expected := "testkey.override.x509.pem testkey.override.pk8"
1498 if actual := rule.Args["certificates"]; actual != expected {
1499 t.Errorf("certificates should be %q, not %q", expected, actual)
1500 }
1501 })
1502}
1503
Jiyong Park58e364a2019-01-19 19:24:06 +09001504func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001505 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001506 apex {
1507 name: "myapex",
1508 key: "myapex.key",
1509 native_shared_libs: ["mylib"],
1510 }
1511
1512 apex {
1513 name: "otherapex",
1514 key: "myapex.key",
1515 native_shared_libs: ["mylib"],
1516 }
1517
1518 apex_key {
1519 name: "myapex.key",
1520 public_key: "testkey.avbpubkey",
1521 private_key: "testkey.pem",
1522 }
1523
1524 cc_library {
1525 name: "mylib",
1526 srcs: ["mylib.cpp"],
1527 system_shared_libs: [],
1528 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001529 // TODO: remove //apex_available:platform
1530 apex_available: [
1531 "//apex_available:platform",
1532 "myapex",
1533 "otherapex",
1534 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001535 }
1536 `)
1537
Jooyung Han6b8459b2019-10-30 08:29:25 +09001538 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001539 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001540 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001541 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1542 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001543
Jooyung Han6b8459b2019-10-30 08:29:25 +09001544 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001545 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001546 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001547 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1548 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001549
Jooyung Han6b8459b2019-10-30 08:29:25 +09001550 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001551 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001552 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001553 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1554 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001555}
Jiyong Park7e636d02019-01-28 16:16:54 +09001556
1557func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001558 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001559 apex {
1560 name: "myapex",
1561 key: "myapex.key",
1562 native_shared_libs: ["mylib"],
1563 }
1564
1565 apex_key {
1566 name: "myapex.key",
1567 public_key: "testkey.avbpubkey",
1568 private_key: "testkey.pem",
1569 }
1570
1571 cc_library_headers {
1572 name: "mylib_headers",
1573 export_include_dirs: ["my_include"],
1574 system_shared_libs: [],
1575 stl: "none",
1576 }
1577
1578 cc_library {
1579 name: "mylib",
1580 srcs: ["mylib.cpp"],
1581 system_shared_libs: [],
1582 stl: "none",
1583 header_libs: ["mylib_headers"],
1584 export_header_lib_headers: ["mylib_headers"],
1585 stubs: {
1586 versions: ["1", "2", "3"],
1587 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001588 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001589 }
1590
1591 cc_library {
1592 name: "otherlib",
1593 srcs: ["mylib.cpp"],
1594 system_shared_libs: [],
1595 stl: "none",
1596 shared_libs: ["mylib"],
1597 }
1598 `)
1599
Colin Cross7113d202019-11-20 16:39:12 -08001600 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001601
1602 // Ensure that the include path of the header lib is exported to 'otherlib'
1603 ensureContains(t, cFlags, "-Imy_include")
1604}
Alex Light9670d332019-01-29 18:07:33 -08001605
Jiyong Park7cd10e32020-01-14 09:22:18 +09001606type fileInApex struct {
1607 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001608 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001609 isLink bool
1610}
1611
Jooyung Hana57af4a2020-01-23 05:36:59 +00001612func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001613 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001614 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001615 copyCmds := apexRule.Args["copy_commands"]
1616 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001617 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001618 for _, cmd := range strings.Split(copyCmds, "&&") {
1619 cmd = strings.TrimSpace(cmd)
1620 if cmd == "" {
1621 continue
1622 }
1623 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001624 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001625 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001626 switch terms[0] {
1627 case "mkdir":
1628 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001629 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001630 t.Fatal("copyCmds contains invalid cp command", cmd)
1631 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001632 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001633 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001634 isLink = false
1635 case "ln":
1636 if len(terms) != 3 && len(terms) != 4 {
1637 // ln LINK TARGET or ln -s LINK TARGET
1638 t.Fatal("copyCmds contains invalid ln command", cmd)
1639 }
1640 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001641 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001642 isLink = true
1643 default:
1644 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1645 }
1646 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001647 index := strings.Index(dst, imageApexDir)
1648 if index == -1 {
1649 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1650 }
1651 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001652 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001653 }
1654 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001655 return ret
1656}
1657
Jooyung Hana57af4a2020-01-23 05:36:59 +00001658func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1659 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001660 var failed bool
1661 var surplus []string
1662 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001663 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001664 for _, expected := range files {
1665 if matched, _ := path.Match(expected, file.path); matched {
1666 filesMatched[expected] = true
1667 return
1668 }
1669 }
1670 surplus = append(surplus, file.path)
1671 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001672
Jooyung Han31c470b2019-10-18 16:26:59 +09001673 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001674 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001675 t.Log("surplus files", surplus)
1676 failed = true
1677 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001678
1679 if len(files) > len(filesMatched) {
1680 var missing []string
1681 for _, expected := range files {
1682 if !filesMatched[expected] {
1683 missing = append(missing, expected)
1684 }
1685 }
1686 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001687 t.Log("missing files", missing)
1688 failed = true
1689 }
1690 if failed {
1691 t.Fail()
1692 }
1693}
1694
Jooyung Han344d5432019-08-23 11:17:39 +09001695func TestVndkApexCurrent(t *testing.T) {
1696 ctx, _ := testApex(t, `
1697 apex_vndk {
1698 name: "myapex",
1699 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001700 }
1701
1702 apex_key {
1703 name: "myapex.key",
1704 public_key: "testkey.avbpubkey",
1705 private_key: "testkey.pem",
1706 }
1707
1708 cc_library {
1709 name: "libvndk",
1710 srcs: ["mylib.cpp"],
1711 vendor_available: true,
1712 vndk: {
1713 enabled: true,
1714 },
1715 system_shared_libs: [],
1716 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001717 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001718 }
1719
1720 cc_library {
1721 name: "libvndksp",
1722 srcs: ["mylib.cpp"],
1723 vendor_available: true,
1724 vndk: {
1725 enabled: true,
1726 support_system_process: true,
1727 },
1728 system_shared_libs: [],
1729 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001730 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001731 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001732 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001733
Jooyung Hana57af4a2020-01-23 05:36:59 +00001734 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001735 "lib/libvndk.so",
1736 "lib/libvndksp.so",
1737 "lib64/libvndk.so",
1738 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001739 "etc/llndk.libraries.VER.txt",
1740 "etc/vndkcore.libraries.VER.txt",
1741 "etc/vndksp.libraries.VER.txt",
1742 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001743 })
Jooyung Han344d5432019-08-23 11:17:39 +09001744}
1745
1746func TestVndkApexWithPrebuilt(t *testing.T) {
1747 ctx, _ := testApex(t, `
1748 apex_vndk {
1749 name: "myapex",
1750 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001751 }
1752
1753 apex_key {
1754 name: "myapex.key",
1755 public_key: "testkey.avbpubkey",
1756 private_key: "testkey.pem",
1757 }
1758
1759 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001760 name: "libvndk",
1761 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001762 vendor_available: true,
1763 vndk: {
1764 enabled: true,
1765 },
1766 system_shared_libs: [],
1767 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001768 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001769 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001770
1771 cc_prebuilt_library_shared {
1772 name: "libvndk.arm",
1773 srcs: ["libvndk.arm.so"],
1774 vendor_available: true,
1775 vndk: {
1776 enabled: true,
1777 },
1778 enabled: false,
1779 arch: {
1780 arm: {
1781 enabled: true,
1782 },
1783 },
1784 system_shared_libs: [],
1785 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001786 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001787 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001788 `+vndkLibrariesTxtFiles("current"),
1789 withFiles(map[string][]byte{
1790 "libvndk.so": nil,
1791 "libvndk.arm.so": nil,
1792 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001793
Jooyung Hana57af4a2020-01-23 05:36:59 +00001794 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001795 "lib/libvndk.so",
1796 "lib/libvndk.arm.so",
1797 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001798 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001799 })
Jooyung Han344d5432019-08-23 11:17:39 +09001800}
1801
Jooyung Han39edb6c2019-11-06 16:53:07 +09001802func vndkLibrariesTxtFiles(vers ...string) (result string) {
1803 for _, v := range vers {
1804 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001805 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001806 result += `
1807 vndk_libraries_txt {
1808 name: "` + txt + `.libraries.txt",
1809 }
1810 `
1811 }
1812 } else {
1813 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1814 result += `
1815 prebuilt_etc {
1816 name: "` + txt + `.libraries.` + v + `.txt",
1817 src: "dummy.txt",
1818 }
1819 `
1820 }
1821 }
1822 }
1823 return
1824}
1825
Jooyung Han344d5432019-08-23 11:17:39 +09001826func TestVndkApexVersion(t *testing.T) {
1827 ctx, _ := testApex(t, `
1828 apex_vndk {
1829 name: "myapex_v27",
1830 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001831 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001832 vndk_version: "27",
1833 }
1834
1835 apex_key {
1836 name: "myapex.key",
1837 public_key: "testkey.avbpubkey",
1838 private_key: "testkey.pem",
1839 }
1840
Jooyung Han31c470b2019-10-18 16:26:59 +09001841 vndk_prebuilt_shared {
1842 name: "libvndk27",
1843 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001844 vendor_available: true,
1845 vndk: {
1846 enabled: true,
1847 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001848 target_arch: "arm64",
1849 arch: {
1850 arm: {
1851 srcs: ["libvndk27_arm.so"],
1852 },
1853 arm64: {
1854 srcs: ["libvndk27_arm64.so"],
1855 },
1856 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001857 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001858 }
1859
1860 vndk_prebuilt_shared {
1861 name: "libvndk27",
1862 version: "27",
1863 vendor_available: true,
1864 vndk: {
1865 enabled: true,
1866 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001867 target_arch: "x86_64",
1868 arch: {
1869 x86: {
1870 srcs: ["libvndk27_x86.so"],
1871 },
1872 x86_64: {
1873 srcs: ["libvndk27_x86_64.so"],
1874 },
1875 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001876 }
1877 `+vndkLibrariesTxtFiles("27"),
1878 withFiles(map[string][]byte{
1879 "libvndk27_arm.so": nil,
1880 "libvndk27_arm64.so": nil,
1881 "libvndk27_x86.so": nil,
1882 "libvndk27_x86_64.so": nil,
1883 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001884
Jooyung Hana57af4a2020-01-23 05:36:59 +00001885 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001886 "lib/libvndk27_arm.so",
1887 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001888 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001889 })
Jooyung Han344d5432019-08-23 11:17:39 +09001890}
1891
1892func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1893 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1894 apex_vndk {
1895 name: "myapex_v27",
1896 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001897 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001898 vndk_version: "27",
1899 }
1900 apex_vndk {
1901 name: "myapex_v27_other",
1902 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001903 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001904 vndk_version: "27",
1905 }
1906
1907 apex_key {
1908 name: "myapex.key",
1909 public_key: "testkey.avbpubkey",
1910 private_key: "testkey.pem",
1911 }
1912
1913 cc_library {
1914 name: "libvndk",
1915 srcs: ["mylib.cpp"],
1916 vendor_available: true,
1917 vndk: {
1918 enabled: true,
1919 },
1920 system_shared_libs: [],
1921 stl: "none",
1922 }
1923
1924 vndk_prebuilt_shared {
1925 name: "libvndk",
1926 version: "27",
1927 vendor_available: true,
1928 vndk: {
1929 enabled: true,
1930 },
1931 srcs: ["libvndk.so"],
1932 }
1933 `, withFiles(map[string][]byte{
1934 "libvndk.so": nil,
1935 }))
1936}
1937
Jooyung Han90eee022019-10-01 20:02:42 +09001938func TestVndkApexNameRule(t *testing.T) {
1939 ctx, _ := testApex(t, `
1940 apex_vndk {
1941 name: "myapex",
1942 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001943 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001944 }
1945 apex_vndk {
1946 name: "myapex_v28",
1947 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001948 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001949 vndk_version: "28",
1950 }
1951 apex_key {
1952 name: "myapex.key",
1953 public_key: "testkey.avbpubkey",
1954 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001955 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001956
1957 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001958 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001959 actual := proptools.String(bundle.properties.Apex_name)
1960 if !reflect.DeepEqual(actual, expected) {
1961 t.Errorf("Got '%v', expected '%v'", actual, expected)
1962 }
1963 }
1964
1965 assertApexName("com.android.vndk.vVER", "myapex")
1966 assertApexName("com.android.vndk.v28", "myapex_v28")
1967}
1968
Jooyung Han344d5432019-08-23 11:17:39 +09001969func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1970 ctx, _ := testApex(t, `
1971 apex_vndk {
1972 name: "myapex",
1973 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001974 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001975 }
1976
1977 apex_key {
1978 name: "myapex.key",
1979 public_key: "testkey.avbpubkey",
1980 private_key: "testkey.pem",
1981 }
1982
1983 cc_library {
1984 name: "libvndk",
1985 srcs: ["mylib.cpp"],
1986 vendor_available: true,
1987 native_bridge_supported: true,
1988 host_supported: true,
1989 vndk: {
1990 enabled: true,
1991 },
1992 system_shared_libs: [],
1993 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001994 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001995 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001996 `+vndkLibrariesTxtFiles("current"),
1997 withTargets(map[android.OsType][]android.Target{
1998 android.Android: []android.Target{
1999 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2000 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2001 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2002 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2003 },
2004 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002005
Jooyung Hana57af4a2020-01-23 05:36:59 +00002006 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002007 "lib/libvndk.so",
2008 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002009 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002010 })
Jooyung Han344d5432019-08-23 11:17:39 +09002011}
2012
2013func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2014 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2015 apex_vndk {
2016 name: "myapex",
2017 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002018 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002019 native_bridge_supported: true,
2020 }
2021
2022 apex_key {
2023 name: "myapex.key",
2024 public_key: "testkey.avbpubkey",
2025 private_key: "testkey.pem",
2026 }
2027
2028 cc_library {
2029 name: "libvndk",
2030 srcs: ["mylib.cpp"],
2031 vendor_available: true,
2032 native_bridge_supported: true,
2033 host_supported: true,
2034 vndk: {
2035 enabled: true,
2036 },
2037 system_shared_libs: [],
2038 stl: "none",
2039 }
2040 `)
2041}
2042
Jooyung Han31c470b2019-10-18 16:26:59 +09002043func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002044 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002045 apex_vndk {
2046 name: "myapex_v27",
2047 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002048 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002049 vndk_version: "27",
2050 }
2051
2052 apex_key {
2053 name: "myapex.key",
2054 public_key: "testkey.avbpubkey",
2055 private_key: "testkey.pem",
2056 }
2057
2058 vndk_prebuilt_shared {
2059 name: "libvndk27",
2060 version: "27",
2061 target_arch: "arm",
2062 vendor_available: true,
2063 vndk: {
2064 enabled: true,
2065 },
2066 arch: {
2067 arm: {
2068 srcs: ["libvndk27.so"],
2069 }
2070 },
2071 }
2072
2073 vndk_prebuilt_shared {
2074 name: "libvndk27",
2075 version: "27",
2076 target_arch: "arm",
2077 binder32bit: true,
2078 vendor_available: true,
2079 vndk: {
2080 enabled: true,
2081 },
2082 arch: {
2083 arm: {
2084 srcs: ["libvndk27binder32.so"],
2085 }
2086 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002087 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002088 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002089 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002090 withFiles(map[string][]byte{
2091 "libvndk27.so": nil,
2092 "libvndk27binder32.so": nil,
2093 }),
2094 withBinder32bit,
2095 withTargets(map[android.OsType][]android.Target{
2096 android.Android: []android.Target{
2097 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2098 },
2099 }),
2100 )
2101
Jooyung Hana57af4a2020-01-23 05:36:59 +00002102 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002103 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002104 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002105 })
2106}
2107
Jooyung Hane1633032019-08-01 17:41:43 +09002108func TestDependenciesInApexManifest(t *testing.T) {
2109 ctx, _ := testApex(t, `
2110 apex {
2111 name: "myapex_nodep",
2112 key: "myapex.key",
2113 native_shared_libs: ["lib_nodep"],
2114 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002115 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002116 }
2117
2118 apex {
2119 name: "myapex_dep",
2120 key: "myapex.key",
2121 native_shared_libs: ["lib_dep"],
2122 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002123 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002124 }
2125
2126 apex {
2127 name: "myapex_provider",
2128 key: "myapex.key",
2129 native_shared_libs: ["libfoo"],
2130 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002131 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002132 }
2133
2134 apex {
2135 name: "myapex_selfcontained",
2136 key: "myapex.key",
2137 native_shared_libs: ["lib_dep", "libfoo"],
2138 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002139 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002140 }
2141
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147
2148 cc_library {
2149 name: "lib_nodep",
2150 srcs: ["mylib.cpp"],
2151 system_shared_libs: [],
2152 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002153 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002154 }
2155
2156 cc_library {
2157 name: "lib_dep",
2158 srcs: ["mylib.cpp"],
2159 shared_libs: ["libfoo"],
2160 system_shared_libs: [],
2161 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002162 apex_available: [
2163 "myapex_dep",
2164 "myapex_provider",
2165 "myapex_selfcontained",
2166 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002167 }
2168
2169 cc_library {
2170 name: "libfoo",
2171 srcs: ["mytest.cpp"],
2172 stubs: {
2173 versions: ["1"],
2174 },
2175 system_shared_libs: [],
2176 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002177 apex_available: [
2178 "myapex_provider",
2179 "myapex_selfcontained",
2180 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002181 }
2182 `)
2183
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002184 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002185 var provideNativeLibs, requireNativeLibs []string
2186
Sundong Ahnabb64432019-10-22 13:58:29 +09002187 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002188 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2189 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002190 ensureListEmpty(t, provideNativeLibs)
2191 ensureListEmpty(t, requireNativeLibs)
2192
Sundong Ahnabb64432019-10-22 13:58:29 +09002193 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002194 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2195 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002196 ensureListEmpty(t, provideNativeLibs)
2197 ensureListContains(t, requireNativeLibs, "libfoo.so")
2198
Sundong Ahnabb64432019-10-22 13:58:29 +09002199 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002200 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2201 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002202 ensureListContains(t, provideNativeLibs, "libfoo.so")
2203 ensureListEmpty(t, requireNativeLibs)
2204
Sundong Ahnabb64432019-10-22 13:58:29 +09002205 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002206 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2207 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002208 ensureListContains(t, provideNativeLibs, "libfoo.so")
2209 ensureListEmpty(t, requireNativeLibs)
2210}
2211
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002212func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002213 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002214 apex {
2215 name: "myapex",
2216 key: "myapex.key",
2217 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002218 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002219 }
2220
2221 apex_key {
2222 name: "myapex.key",
2223 public_key: "testkey.avbpubkey",
2224 private_key: "testkey.pem",
2225 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002226
2227 cc_library {
2228 name: "mylib",
2229 srcs: ["mylib.cpp"],
2230 system_shared_libs: [],
2231 stl: "none",
2232 apex_available: [
2233 "//apex_available:platform",
2234 "myapex",
2235 ],
2236 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002237 `)
2238
Sundong Ahnabb64432019-10-22 13:58:29 +09002239 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002240 apexManifestRule := module.Rule("apexManifestRule")
2241 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2242 apexRule := module.Rule("apexRule")
2243 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002244
2245 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2246 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2247 name := apexBundle.BaseModuleName()
2248 prefix := "TARGET_"
2249 var builder strings.Builder
2250 data.Custom(&builder, name, prefix, "", data)
2251 androidMk := builder.String()
2252 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2253 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002254}
2255
Alex Light0851b882019-02-07 13:20:53 -08002256func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002257 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002258 apex {
2259 name: "myapex",
2260 key: "myapex.key",
2261 native_shared_libs: ["mylib_common"],
2262 }
2263
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269
2270 cc_library {
2271 name: "mylib_common",
2272 srcs: ["mylib.cpp"],
2273 system_shared_libs: [],
2274 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002275 apex_available: [
2276 "//apex_available:platform",
2277 "myapex",
2278 ],
Alex Light0851b882019-02-07 13:20:53 -08002279 }
2280 `)
2281
Sundong Ahnabb64432019-10-22 13:58:29 +09002282 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002283 apexRule := module.Rule("apexRule")
2284 copyCmds := apexRule.Args["copy_commands"]
2285
2286 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2287 t.Log("Apex was a test apex!")
2288 t.Fail()
2289 }
2290 // Ensure that main rule creates an output
2291 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2292
2293 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002294 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002295
2296 // Ensure that both direct and indirect deps are copied into apex
2297 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2298
Colin Cross7113d202019-11-20 16:39:12 -08002299 // Ensure that the platform variant ends with _shared
2300 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002301
2302 if !android.InAnyApex("mylib_common") {
2303 t.Log("Found mylib_common not in any apex!")
2304 t.Fail()
2305 }
2306}
2307
2308func TestTestApex(t *testing.T) {
2309 if android.InAnyApex("mylib_common_test") {
2310 t.Fatal("mylib_common_test must not be used in any other tests since this checks that global state is not updated in an illegal way!")
2311 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002312 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002313 apex_test {
2314 name: "myapex",
2315 key: "myapex.key",
2316 native_shared_libs: ["mylib_common_test"],
2317 }
2318
2319 apex_key {
2320 name: "myapex.key",
2321 public_key: "testkey.avbpubkey",
2322 private_key: "testkey.pem",
2323 }
2324
2325 cc_library {
2326 name: "mylib_common_test",
2327 srcs: ["mylib.cpp"],
2328 system_shared_libs: [],
2329 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002330 // TODO: remove //apex_available:platform
2331 apex_available: [
2332 "//apex_available:platform",
2333 "myapex",
2334 ],
Alex Light0851b882019-02-07 13:20:53 -08002335 }
2336 `)
2337
Sundong Ahnabb64432019-10-22 13:58:29 +09002338 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002339 apexRule := module.Rule("apexRule")
2340 copyCmds := apexRule.Args["copy_commands"]
2341
2342 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2343 t.Log("Apex was not a test apex!")
2344 t.Fail()
2345 }
2346 // Ensure that main rule creates an output
2347 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2348
2349 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002350 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002351
2352 // Ensure that both direct and indirect deps are copied into apex
2353 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2354
Colin Cross7113d202019-11-20 16:39:12 -08002355 // Ensure that the platform variant ends with _shared
2356 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002357
2358 if android.InAnyApex("mylib_common_test") {
2359 t.Log("Found mylib_common_test in some apex!")
2360 t.Fail()
2361 }
2362}
2363
Alex Light9670d332019-01-29 18:07:33 -08002364func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002365 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002366 apex {
2367 name: "myapex",
2368 key: "myapex.key",
2369 multilib: {
2370 first: {
2371 native_shared_libs: ["mylib_common"],
2372 }
2373 },
2374 target: {
2375 android: {
2376 multilib: {
2377 first: {
2378 native_shared_libs: ["mylib"],
2379 }
2380 }
2381 },
2382 host: {
2383 multilib: {
2384 first: {
2385 native_shared_libs: ["mylib2"],
2386 }
2387 }
2388 }
2389 }
2390 }
2391
2392 apex_key {
2393 name: "myapex.key",
2394 public_key: "testkey.avbpubkey",
2395 private_key: "testkey.pem",
2396 }
2397
2398 cc_library {
2399 name: "mylib",
2400 srcs: ["mylib.cpp"],
2401 system_shared_libs: [],
2402 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002403 // TODO: remove //apex_available:platform
2404 apex_available: [
2405 "//apex_available:platform",
2406 "myapex",
2407 ],
Alex Light9670d332019-01-29 18:07:33 -08002408 }
2409
2410 cc_library {
2411 name: "mylib_common",
2412 srcs: ["mylib.cpp"],
2413 system_shared_libs: [],
2414 stl: "none",
2415 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002416 // TODO: remove //apex_available:platform
2417 apex_available: [
2418 "//apex_available:platform",
2419 "myapex",
2420 ],
Alex Light9670d332019-01-29 18:07:33 -08002421 }
2422
2423 cc_library {
2424 name: "mylib2",
2425 srcs: ["mylib.cpp"],
2426 system_shared_libs: [],
2427 stl: "none",
2428 compile_multilib: "first",
2429 }
2430 `)
2431
Sundong Ahnabb64432019-10-22 13:58:29 +09002432 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002433 copyCmds := apexRule.Args["copy_commands"]
2434
2435 // Ensure that main rule creates an output
2436 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2437
2438 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002439 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2440 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2441 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002442
2443 // Ensure that both direct and indirect deps are copied into apex
2444 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2445 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2446 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2447
Colin Cross7113d202019-11-20 16:39:12 -08002448 // Ensure that the platform variant ends with _shared
2449 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2450 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2451 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002452}
Jiyong Park04480cf2019-02-06 00:16:29 +09002453
2454func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002455 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002456 apex {
2457 name: "myapex",
2458 key: "myapex.key",
2459 binaries: ["myscript"],
2460 }
2461
2462 apex_key {
2463 name: "myapex.key",
2464 public_key: "testkey.avbpubkey",
2465 private_key: "testkey.pem",
2466 }
2467
2468 sh_binary {
2469 name: "myscript",
2470 src: "mylib.cpp",
2471 filename: "myscript.sh",
2472 sub_dir: "script",
2473 }
2474 `)
2475
Sundong Ahnabb64432019-10-22 13:58:29 +09002476 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002477 copyCmds := apexRule.Args["copy_commands"]
2478
2479 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2480}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002481
Jooyung Han91df2082019-11-20 01:49:42 +09002482func TestApexInVariousPartition(t *testing.T) {
2483 testcases := []struct {
2484 propName, parition, flattenedPartition string
2485 }{
2486 {"", "system", "system_ext"},
2487 {"product_specific: true", "product", "product"},
2488 {"soc_specific: true", "vendor", "vendor"},
2489 {"proprietary: true", "vendor", "vendor"},
2490 {"vendor: true", "vendor", "vendor"},
2491 {"system_ext_specific: true", "system_ext", "system_ext"},
2492 }
2493 for _, tc := range testcases {
2494 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2495 ctx, _ := testApex(t, `
2496 apex {
2497 name: "myapex",
2498 key: "myapex.key",
2499 `+tc.propName+`
2500 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002501
Jooyung Han91df2082019-11-20 01:49:42 +09002502 apex_key {
2503 name: "myapex.key",
2504 public_key: "testkey.avbpubkey",
2505 private_key: "testkey.pem",
2506 }
2507 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002508
Jooyung Han91df2082019-11-20 01:49:42 +09002509 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2510 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2511 actual := apex.installDir.String()
2512 if actual != expected {
2513 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2514 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002515
Jooyung Han91df2082019-11-20 01:49:42 +09002516 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2517 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2518 actual = flattened.installDir.String()
2519 if actual != expected {
2520 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2521 }
2522 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002523 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002524}
Jiyong Park67882562019-03-21 01:11:21 +09002525
Jooyung Han54aca7b2019-11-20 02:26:02 +09002526func TestFileContexts(t *testing.T) {
2527 ctx, _ := testApex(t, `
2528 apex {
2529 name: "myapex",
2530 key: "myapex.key",
2531 }
2532
2533 apex_key {
2534 name: "myapex.key",
2535 public_key: "testkey.avbpubkey",
2536 private_key: "testkey.pem",
2537 }
2538 `)
2539 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2540 apexRule := module.Rule("apexRule")
2541 actual := apexRule.Args["file_contexts"]
2542 expected := "system/sepolicy/apex/myapex-file_contexts"
2543 if actual != expected {
2544 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2545 }
2546
2547 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2548 apex {
2549 name: "myapex",
2550 key: "myapex.key",
2551 file_contexts: "my_own_file_contexts",
2552 }
2553
2554 apex_key {
2555 name: "myapex.key",
2556 public_key: "testkey.avbpubkey",
2557 private_key: "testkey.pem",
2558 }
2559 `, withFiles(map[string][]byte{
2560 "my_own_file_contexts": nil,
2561 }))
2562
2563 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2564 apex {
2565 name: "myapex",
2566 key: "myapex.key",
2567 product_specific: true,
2568 file_contexts: "product_specific_file_contexts",
2569 }
2570
2571 apex_key {
2572 name: "myapex.key",
2573 public_key: "testkey.avbpubkey",
2574 private_key: "testkey.pem",
2575 }
2576 `)
2577
2578 ctx, _ = testApex(t, `
2579 apex {
2580 name: "myapex",
2581 key: "myapex.key",
2582 product_specific: true,
2583 file_contexts: "product_specific_file_contexts",
2584 }
2585
2586 apex_key {
2587 name: "myapex.key",
2588 public_key: "testkey.avbpubkey",
2589 private_key: "testkey.pem",
2590 }
2591 `, withFiles(map[string][]byte{
2592 "product_specific_file_contexts": nil,
2593 }))
2594 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2595 apexRule = module.Rule("apexRule")
2596 actual = apexRule.Args["file_contexts"]
2597 expected = "product_specific_file_contexts"
2598 if actual != expected {
2599 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2600 }
2601
2602 ctx, _ = testApex(t, `
2603 apex {
2604 name: "myapex",
2605 key: "myapex.key",
2606 product_specific: true,
2607 file_contexts: ":my-file-contexts",
2608 }
2609
2610 apex_key {
2611 name: "myapex.key",
2612 public_key: "testkey.avbpubkey",
2613 private_key: "testkey.pem",
2614 }
2615
2616 filegroup {
2617 name: "my-file-contexts",
2618 srcs: ["product_specific_file_contexts"],
2619 }
2620 `, withFiles(map[string][]byte{
2621 "product_specific_file_contexts": nil,
2622 }))
2623 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2624 apexRule = module.Rule("apexRule")
2625 actual = apexRule.Args["file_contexts"]
2626 expected = "product_specific_file_contexts"
2627 if actual != expected {
2628 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2629 }
2630}
2631
Jiyong Park67882562019-03-21 01:11:21 +09002632func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002633 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002634 apex_key {
2635 name: "myapex.key",
2636 public_key: ":my.avbpubkey",
2637 private_key: ":my.pem",
2638 product_specific: true,
2639 }
2640
2641 filegroup {
2642 name: "my.avbpubkey",
2643 srcs: ["testkey2.avbpubkey"],
2644 }
2645
2646 filegroup {
2647 name: "my.pem",
2648 srcs: ["testkey2.pem"],
2649 }
2650 `)
2651
2652 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2653 expected_pubkey := "testkey2.avbpubkey"
2654 actual_pubkey := apex_key.public_key_file.String()
2655 if actual_pubkey != expected_pubkey {
2656 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2657 }
2658 expected_privkey := "testkey2.pem"
2659 actual_privkey := apex_key.private_key_file.String()
2660 if actual_privkey != expected_privkey {
2661 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2662 }
2663}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002664
2665func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002666 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002667 prebuilt_apex {
2668 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002669 arch: {
2670 arm64: {
2671 src: "myapex-arm64.apex",
2672 },
2673 arm: {
2674 src: "myapex-arm.apex",
2675 },
2676 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002677 }
2678 `)
2679
2680 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2681
Jiyong Parkc95714e2019-03-29 14:23:10 +09002682 expectedInput := "myapex-arm64.apex"
2683 if prebuilt.inputApex.String() != expectedInput {
2684 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2685 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002686}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002687
2688func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002689 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002690 prebuilt_apex {
2691 name: "myapex",
2692 src: "myapex-arm.apex",
2693 filename: "notmyapex.apex",
2694 }
2695 `)
2696
2697 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2698
2699 expected := "notmyapex.apex"
2700 if p.installFilename != expected {
2701 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2702 }
2703}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002704
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002705func TestPrebuiltOverrides(t *testing.T) {
2706 ctx, config := testApex(t, `
2707 prebuilt_apex {
2708 name: "myapex.prebuilt",
2709 src: "myapex-arm.apex",
2710 overrides: [
2711 "myapex",
2712 ],
2713 }
2714 `)
2715
2716 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2717
2718 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002719 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002720 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002721 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002722 }
2723}
2724
Roland Levillain630846d2019-06-26 12:48:34 +01002725func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002726 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002727 apex_test {
2728 name: "myapex",
2729 key: "myapex.key",
2730 tests: [
2731 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002732 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002733 ],
2734 }
2735
2736 apex_key {
2737 name: "myapex.key",
2738 public_key: "testkey.avbpubkey",
2739 private_key: "testkey.pem",
2740 }
2741
2742 cc_test {
2743 name: "mytest",
2744 gtest: false,
2745 srcs: ["mytest.cpp"],
2746 relative_install_path: "test",
2747 system_shared_libs: [],
2748 static_executable: true,
2749 stl: "none",
2750 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002751
2752 cc_test {
2753 name: "mytests",
2754 gtest: false,
2755 srcs: [
2756 "mytest1.cpp",
2757 "mytest2.cpp",
2758 "mytest3.cpp",
2759 ],
2760 test_per_src: true,
2761 relative_install_path: "test",
2762 system_shared_libs: [],
2763 static_executable: true,
2764 stl: "none",
2765 }
Roland Levillain630846d2019-06-26 12:48:34 +01002766 `)
2767
Sundong Ahnabb64432019-10-22 13:58:29 +09002768 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002769 copyCmds := apexRule.Args["copy_commands"]
2770
2771 // Ensure that test dep is copied into apex.
2772 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002773
2774 // Ensure that test deps built with `test_per_src` are copied into apex.
2775 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2776 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2777 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002778
2779 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002780 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002781 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2782 name := apexBundle.BaseModuleName()
2783 prefix := "TARGET_"
2784 var builder strings.Builder
2785 data.Custom(&builder, name, prefix, "", data)
2786 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002787 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2788 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2789 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2790 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002791 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002792 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002793 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002794}
2795
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002796func TestInstallExtraFlattenedApexes(t *testing.T) {
2797 ctx, config := testApex(t, `
2798 apex {
2799 name: "myapex",
2800 key: "myapex.key",
2801 }
2802 apex_key {
2803 name: "myapex.key",
2804 public_key: "testkey.avbpubkey",
2805 private_key: "testkey.pem",
2806 }
2807 `, func(fs map[string][]byte, config android.Config) {
2808 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2809 })
2810 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002811 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002812 mk := android.AndroidMkDataForTest(t, config, "", ab)
2813 var builder strings.Builder
2814 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2815 androidMk := builder.String()
2816 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2817}
2818
Jooyung Han5c998b92019-06-27 11:30:33 +09002819func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002820 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002821 apex {
2822 name: "myapex",
2823 key: "myapex.key",
2824 native_shared_libs: ["mylib"],
2825 uses: ["commonapex"],
2826 }
2827
2828 apex {
2829 name: "commonapex",
2830 key: "myapex.key",
2831 native_shared_libs: ["libcommon"],
2832 provide_cpp_shared_libs: true,
2833 }
2834
2835 apex_key {
2836 name: "myapex.key",
2837 public_key: "testkey.avbpubkey",
2838 private_key: "testkey.pem",
2839 }
2840
2841 cc_library {
2842 name: "mylib",
2843 srcs: ["mylib.cpp"],
2844 shared_libs: ["libcommon"],
2845 system_shared_libs: [],
2846 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002847 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002848 }
2849
2850 cc_library {
2851 name: "libcommon",
2852 srcs: ["mylib_common.cpp"],
2853 system_shared_libs: [],
2854 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002855 // TODO: remove //apex_available:platform
2856 apex_available: [
2857 "//apex_available:platform",
2858 "commonapex",
2859 "myapex",
2860 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002861 }
2862 `)
2863
Sundong Ahnabb64432019-10-22 13:58:29 +09002864 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002865 apexRule1 := module1.Rule("apexRule")
2866 copyCmds1 := apexRule1.Args["copy_commands"]
2867
Sundong Ahnabb64432019-10-22 13:58:29 +09002868 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002869 apexRule2 := module2.Rule("apexRule")
2870 copyCmds2 := apexRule2.Args["copy_commands"]
2871
Colin Cross7113d202019-11-20 16:39:12 -08002872 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2873 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002874 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2875 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2876 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2877}
2878
2879func TestApexUsesFailsIfNotProvided(t *testing.T) {
2880 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2881 apex {
2882 name: "myapex",
2883 key: "myapex.key",
2884 uses: ["commonapex"],
2885 }
2886
2887 apex {
2888 name: "commonapex",
2889 key: "myapex.key",
2890 }
2891
2892 apex_key {
2893 name: "myapex.key",
2894 public_key: "testkey.avbpubkey",
2895 private_key: "testkey.pem",
2896 }
2897 `)
2898 testApexError(t, `uses: "commonapex" is not a provider`, `
2899 apex {
2900 name: "myapex",
2901 key: "myapex.key",
2902 uses: ["commonapex"],
2903 }
2904
2905 cc_library {
2906 name: "commonapex",
2907 system_shared_libs: [],
2908 stl: "none",
2909 }
2910
2911 apex_key {
2912 name: "myapex.key",
2913 public_key: "testkey.avbpubkey",
2914 private_key: "testkey.pem",
2915 }
2916 `)
2917}
2918
2919func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2920 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2921 apex {
2922 name: "myapex",
2923 key: "myapex.key",
2924 use_vendor: true,
2925 uses: ["commonapex"],
2926 }
2927
2928 apex {
2929 name: "commonapex",
2930 key: "myapex.key",
2931 provide_cpp_shared_libs: true,
2932 }
2933
2934 apex_key {
2935 name: "myapex.key",
2936 public_key: "testkey.avbpubkey",
2937 private_key: "testkey.pem",
2938 }
Jooyung Handc782442019-11-01 03:14:38 +09002939 `, func(fs map[string][]byte, config android.Config) {
2940 setUseVendorWhitelistForTest(config, []string{"myapex"})
2941 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002942}
2943
Jooyung Hand48f3c32019-08-23 11:18:57 +09002944func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2945 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2946 apex {
2947 name: "myapex",
2948 key: "myapex.key",
2949 native_shared_libs: ["libfoo"],
2950 }
2951
2952 apex_key {
2953 name: "myapex.key",
2954 public_key: "testkey.avbpubkey",
2955 private_key: "testkey.pem",
2956 }
2957
2958 cc_library {
2959 name: "libfoo",
2960 stl: "none",
2961 system_shared_libs: [],
2962 enabled: false,
2963 }
2964 `)
2965 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2966 apex {
2967 name: "myapex",
2968 key: "myapex.key",
2969 java_libs: ["myjar"],
2970 }
2971
2972 apex_key {
2973 name: "myapex.key",
2974 public_key: "testkey.avbpubkey",
2975 private_key: "testkey.pem",
2976 }
2977
2978 java_library {
2979 name: "myjar",
2980 srcs: ["foo/bar/MyClass.java"],
2981 sdk_version: "none",
2982 system_modules: "none",
2983 compile_dex: true,
2984 enabled: false,
2985 }
2986 `)
2987}
2988
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002989func TestApexWithApps(t *testing.T) {
2990 ctx, _ := testApex(t, `
2991 apex {
2992 name: "myapex",
2993 key: "myapex.key",
2994 apps: [
2995 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002996 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002997 ],
2998 }
2999
3000 apex_key {
3001 name: "myapex.key",
3002 public_key: "testkey.avbpubkey",
3003 private_key: "testkey.pem",
3004 }
3005
3006 android_app {
3007 name: "AppFoo",
3008 srcs: ["foo/bar/MyClass.java"],
3009 sdk_version: "none",
3010 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003011 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003012 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003013 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003014
3015 android_app {
3016 name: "AppFooPriv",
3017 srcs: ["foo/bar/MyClass.java"],
3018 sdk_version: "none",
3019 system_modules: "none",
3020 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003021 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003022 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003023
3024 cc_library_shared {
3025 name: "libjni",
3026 srcs: ["mylib.cpp"],
3027 stl: "none",
3028 system_shared_libs: [],
3029 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003030 `)
3031
Sundong Ahnabb64432019-10-22 13:58:29 +09003032 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003033 apexRule := module.Rule("apexRule")
3034 copyCmds := apexRule.Args["copy_commands"]
3035
3036 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003037 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003038
3039 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003040 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003041 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003042 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3043 // ... uncompressed
3044 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3045 t.Errorf("jni lib is not uncompressed for AppFoo")
3046 }
3047 // ... and not directly inside the APEX
3048 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003049}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003050
Dario Frenicde2a032019-10-27 00:29:22 +01003051func TestApexWithAppImports(t *testing.T) {
3052 ctx, _ := testApex(t, `
3053 apex {
3054 name: "myapex",
3055 key: "myapex.key",
3056 apps: [
3057 "AppFooPrebuilt",
3058 "AppFooPrivPrebuilt",
3059 ],
3060 }
3061
3062 apex_key {
3063 name: "myapex.key",
3064 public_key: "testkey.avbpubkey",
3065 private_key: "testkey.pem",
3066 }
3067
3068 android_app_import {
3069 name: "AppFooPrebuilt",
3070 apk: "PrebuiltAppFoo.apk",
3071 presigned: true,
3072 dex_preopt: {
3073 enabled: false,
3074 },
3075 }
3076
3077 android_app_import {
3078 name: "AppFooPrivPrebuilt",
3079 apk: "PrebuiltAppFooPriv.apk",
3080 privileged: true,
3081 presigned: true,
3082 dex_preopt: {
3083 enabled: false,
3084 },
3085 }
3086 `)
3087
Sundong Ahnabb64432019-10-22 13:58:29 +09003088 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003089 apexRule := module.Rule("apexRule")
3090 copyCmds := apexRule.Args["copy_commands"]
3091
3092 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3093 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003094}
3095
Dario Freni6f3937c2019-12-20 22:58:03 +00003096func TestApexWithTestHelperApp(t *testing.T) {
3097 ctx, _ := testApex(t, `
3098 apex {
3099 name: "myapex",
3100 key: "myapex.key",
3101 apps: [
3102 "TesterHelpAppFoo",
3103 ],
3104 }
3105
3106 apex_key {
3107 name: "myapex.key",
3108 public_key: "testkey.avbpubkey",
3109 private_key: "testkey.pem",
3110 }
3111
3112 android_test_helper_app {
3113 name: "TesterHelpAppFoo",
3114 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003115 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003116 }
3117
3118 `)
3119
3120 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3121 apexRule := module.Rule("apexRule")
3122 copyCmds := apexRule.Args["copy_commands"]
3123
3124 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3125}
3126
Jooyung Han18020ea2019-11-13 10:50:48 +09003127func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3128 // libfoo's apex_available comes from cc_defaults
3129 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3130 apex {
3131 name: "myapex",
3132 key: "myapex.key",
3133 native_shared_libs: ["libfoo"],
3134 }
3135
3136 apex_key {
3137 name: "myapex.key",
3138 public_key: "testkey.avbpubkey",
3139 private_key: "testkey.pem",
3140 }
3141
3142 apex {
3143 name: "otherapex",
3144 key: "myapex.key",
3145 native_shared_libs: ["libfoo"],
3146 }
3147
3148 cc_defaults {
3149 name: "libfoo-defaults",
3150 apex_available: ["otherapex"],
3151 }
3152
3153 cc_library {
3154 name: "libfoo",
3155 defaults: ["libfoo-defaults"],
3156 stl: "none",
3157 system_shared_libs: [],
3158 }`)
3159}
3160
Jiyong Park127b40b2019-09-30 16:04:35 +09003161func TestApexAvailable(t *testing.T) {
3162 // libfoo is not available to myapex, but only to otherapex
3163 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3164 apex {
3165 name: "myapex",
3166 key: "myapex.key",
3167 native_shared_libs: ["libfoo"],
3168 }
3169
3170 apex_key {
3171 name: "myapex.key",
3172 public_key: "testkey.avbpubkey",
3173 private_key: "testkey.pem",
3174 }
3175
3176 apex {
3177 name: "otherapex",
3178 key: "otherapex.key",
3179 native_shared_libs: ["libfoo"],
3180 }
3181
3182 apex_key {
3183 name: "otherapex.key",
3184 public_key: "testkey.avbpubkey",
3185 private_key: "testkey.pem",
3186 }
3187
3188 cc_library {
3189 name: "libfoo",
3190 stl: "none",
3191 system_shared_libs: [],
3192 apex_available: ["otherapex"],
3193 }`)
3194
3195 // libbar is an indirect dep
3196 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3197 apex {
3198 name: "myapex",
3199 key: "myapex.key",
3200 native_shared_libs: ["libfoo"],
3201 }
3202
3203 apex_key {
3204 name: "myapex.key",
3205 public_key: "testkey.avbpubkey",
3206 private_key: "testkey.pem",
3207 }
3208
3209 apex {
3210 name: "otherapex",
3211 key: "otherapex.key",
3212 native_shared_libs: ["libfoo"],
3213 }
3214
3215 apex_key {
3216 name: "otherapex.key",
3217 public_key: "testkey.avbpubkey",
3218 private_key: "testkey.pem",
3219 }
3220
3221 cc_library {
3222 name: "libfoo",
3223 stl: "none",
3224 shared_libs: ["libbar"],
3225 system_shared_libs: [],
3226 apex_available: ["myapex", "otherapex"],
3227 }
3228
3229 cc_library {
3230 name: "libbar",
3231 stl: "none",
3232 system_shared_libs: [],
3233 apex_available: ["otherapex"],
3234 }`)
3235
3236 testApexError(t, "\"otherapex\" is not a valid module name", `
3237 apex {
3238 name: "myapex",
3239 key: "myapex.key",
3240 native_shared_libs: ["libfoo"],
3241 }
3242
3243 apex_key {
3244 name: "myapex.key",
3245 public_key: "testkey.avbpubkey",
3246 private_key: "testkey.pem",
3247 }
3248
3249 cc_library {
3250 name: "libfoo",
3251 stl: "none",
3252 system_shared_libs: [],
3253 apex_available: ["otherapex"],
3254 }`)
3255
3256 ctx, _ := testApex(t, `
3257 apex {
3258 name: "myapex",
3259 key: "myapex.key",
3260 native_shared_libs: ["libfoo", "libbar"],
3261 }
3262
3263 apex_key {
3264 name: "myapex.key",
3265 public_key: "testkey.avbpubkey",
3266 private_key: "testkey.pem",
3267 }
3268
3269 cc_library {
3270 name: "libfoo",
3271 stl: "none",
3272 system_shared_libs: [],
3273 apex_available: ["myapex"],
3274 }
3275
3276 cc_library {
3277 name: "libbar",
3278 stl: "none",
3279 system_shared_libs: [],
3280 apex_available: ["//apex_available:anyapex"],
3281 }`)
3282
3283 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003284 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3285 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3286 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3287 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003288
3289 ctx, _ = testApex(t, `
3290 apex {
3291 name: "myapex",
3292 key: "myapex.key",
3293 }
3294
3295 apex_key {
3296 name: "myapex.key",
3297 public_key: "testkey.avbpubkey",
3298 private_key: "testkey.pem",
3299 }
3300
3301 cc_library {
3302 name: "libfoo",
3303 stl: "none",
3304 system_shared_libs: [],
3305 apex_available: ["//apex_available:platform"],
3306 }`)
3307
3308 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003309 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3310 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003311
3312 ctx, _ = testApex(t, `
3313 apex {
3314 name: "myapex",
3315 key: "myapex.key",
3316 native_shared_libs: ["libfoo"],
3317 }
3318
3319 apex_key {
3320 name: "myapex.key",
3321 public_key: "testkey.avbpubkey",
3322 private_key: "testkey.pem",
3323 }
3324
3325 cc_library {
3326 name: "libfoo",
3327 stl: "none",
3328 system_shared_libs: [],
3329 apex_available: ["myapex"],
3330 static: {
3331 apex_available: ["//apex_available:platform"],
3332 },
3333 }`)
3334
3335 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003336 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3337 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003338 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003339 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3340 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003341}
3342
Jiyong Park5d790c32019-11-15 18:40:32 +09003343func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003344 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003345 apex {
3346 name: "myapex",
3347 key: "myapex.key",
3348 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003349 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003350 }
3351
3352 override_apex {
3353 name: "override_myapex",
3354 base: "myapex",
3355 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003356 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003357 }
3358
3359 apex_key {
3360 name: "myapex.key",
3361 public_key: "testkey.avbpubkey",
3362 private_key: "testkey.pem",
3363 }
3364
3365 android_app {
3366 name: "app",
3367 srcs: ["foo/bar/MyClass.java"],
3368 package_name: "foo",
3369 sdk_version: "none",
3370 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003371 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003372 }
3373
3374 override_android_app {
3375 name: "override_app",
3376 base: "app",
3377 package_name: "bar",
3378 }
3379 `)
3380
Jiyong Park317645e2019-12-05 13:20:58 +09003381 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3382 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3383 if originalVariant.GetOverriddenBy() != "" {
3384 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3385 }
3386 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3387 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3388 }
3389
Jiyong Park5d790c32019-11-15 18:40:32 +09003390 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3391 apexRule := module.Rule("apexRule")
3392 copyCmds := apexRule.Args["copy_commands"]
3393
3394 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3395 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003396
3397 apexBundle := module.Module().(*apexBundle)
3398 name := apexBundle.Name()
3399 if name != "override_myapex" {
3400 t.Errorf("name should be \"override_myapex\", but was %q", name)
3401 }
3402
3403 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3404 var builder strings.Builder
3405 data.Custom(&builder, name, "TARGET_", "", data)
3406 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003407 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003408 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3409 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003410 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003411 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003412 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003413 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3414 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003415}
3416
Jooyung Han214bf372019-11-12 13:03:50 +09003417func TestLegacyAndroid10Support(t *testing.T) {
3418 ctx, _ := testApex(t, `
3419 apex {
3420 name: "myapex",
3421 key: "myapex.key",
3422 legacy_android10_support: true,
3423 }
3424
3425 apex_key {
3426 name: "myapex.key",
3427 public_key: "testkey.avbpubkey",
3428 private_key: "testkey.pem",
3429 }
3430 `)
3431
3432 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3433 args := module.Rule("apexRule").Args
3434 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003435 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003436}
3437
Jooyung Han58f26ab2019-12-18 15:34:32 +09003438func TestJavaSDKLibrary(t *testing.T) {
3439 ctx, _ := testApex(t, `
3440 apex {
3441 name: "myapex",
3442 key: "myapex.key",
3443 java_libs: ["foo"],
3444 }
3445
3446 apex_key {
3447 name: "myapex.key",
3448 public_key: "testkey.avbpubkey",
3449 private_key: "testkey.pem",
3450 }
3451
3452 java_sdk_library {
3453 name: "foo",
3454 srcs: ["a.java"],
3455 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003456 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003457 }
3458 `, withFiles(map[string][]byte{
3459 "api/current.txt": nil,
3460 "api/removed.txt": nil,
3461 "api/system-current.txt": nil,
3462 "api/system-removed.txt": nil,
3463 "api/test-current.txt": nil,
3464 "api/test-removed.txt": nil,
3465 }))
3466
3467 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003468 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003469 "javalib/foo.jar",
3470 "etc/permissions/foo.xml",
3471 })
3472 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003473 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3474 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003475}
3476
atrost6e126252020-01-27 17:01:16 +00003477func TestCompatConfig(t *testing.T) {
3478 ctx, _ := testApex(t, `
3479 apex {
3480 name: "myapex",
3481 key: "myapex.key",
3482 prebuilts: ["myjar-platform-compat-config"],
3483 java_libs: ["myjar"],
3484 }
3485
3486 apex_key {
3487 name: "myapex.key",
3488 public_key: "testkey.avbpubkey",
3489 private_key: "testkey.pem",
3490 }
3491
3492 platform_compat_config {
3493 name: "myjar-platform-compat-config",
3494 src: ":myjar",
3495 }
3496
3497 java_library {
3498 name: "myjar",
3499 srcs: ["foo/bar/MyClass.java"],
3500 sdk_version: "none",
3501 system_modules: "none",
3502 compile_dex: true,
3503 apex_available: [ "myapex" ],
3504 }
3505 `)
3506 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3507 "etc/compatconfig/myjar-platform-compat-config.xml",
3508 "javalib/myjar.jar",
3509 })
3510}
3511
Jiyong Park479321d2019-12-16 11:47:12 +09003512func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3513 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3514 apex {
3515 name: "myapex",
3516 key: "myapex.key",
3517 java_libs: ["myjar"],
3518 }
3519
3520 apex_key {
3521 name: "myapex.key",
3522 public_key: "testkey.avbpubkey",
3523 private_key: "testkey.pem",
3524 }
3525
3526 java_library {
3527 name: "myjar",
3528 srcs: ["foo/bar/MyClass.java"],
3529 sdk_version: "none",
3530 system_modules: "none",
3531 }
3532 `)
3533}
3534
Jiyong Park7afd1072019-12-30 16:56:33 +09003535func TestCarryRequiredModuleNames(t *testing.T) {
3536 ctx, config := testApex(t, `
3537 apex {
3538 name: "myapex",
3539 key: "myapex.key",
3540 native_shared_libs: ["mylib"],
3541 }
3542
3543 apex_key {
3544 name: "myapex.key",
3545 public_key: "testkey.avbpubkey",
3546 private_key: "testkey.pem",
3547 }
3548
3549 cc_library {
3550 name: "mylib",
3551 srcs: ["mylib.cpp"],
3552 system_shared_libs: [],
3553 stl: "none",
3554 required: ["a", "b"],
3555 host_required: ["c", "d"],
3556 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003557 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003558 }
3559 `)
3560
3561 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3562 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3563 name := apexBundle.BaseModuleName()
3564 prefix := "TARGET_"
3565 var builder strings.Builder
3566 data.Custom(&builder, name, prefix, "", data)
3567 androidMk := builder.String()
3568 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3569 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3570 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3571}
3572
Jiyong Park7cd10e32020-01-14 09:22:18 +09003573func TestSymlinksFromApexToSystem(t *testing.T) {
3574 bp := `
3575 apex {
3576 name: "myapex",
3577 key: "myapex.key",
3578 native_shared_libs: ["mylib"],
3579 java_libs: ["myjar"],
3580 }
3581
3582 apex_key {
3583 name: "myapex.key",
3584 public_key: "testkey.avbpubkey",
3585 private_key: "testkey.pem",
3586 }
3587
3588 cc_library {
3589 name: "mylib",
3590 srcs: ["mylib.cpp"],
3591 shared_libs: ["myotherlib"],
3592 system_shared_libs: [],
3593 stl: "none",
3594 apex_available: [
3595 "myapex",
3596 "//apex_available:platform",
3597 ],
3598 }
3599
3600 cc_library {
3601 name: "myotherlib",
3602 srcs: ["mylib.cpp"],
3603 system_shared_libs: [],
3604 stl: "none",
3605 apex_available: [
3606 "myapex",
3607 "//apex_available:platform",
3608 ],
3609 }
3610
3611 java_library {
3612 name: "myjar",
3613 srcs: ["foo/bar/MyClass.java"],
3614 sdk_version: "none",
3615 system_modules: "none",
3616 libs: ["myotherjar"],
3617 compile_dex: true,
3618 apex_available: [
3619 "myapex",
3620 "//apex_available:platform",
3621 ],
3622 }
3623
3624 java_library {
3625 name: "myotherjar",
3626 srcs: ["foo/bar/MyClass.java"],
3627 sdk_version: "none",
3628 system_modules: "none",
3629 apex_available: [
3630 "myapex",
3631 "//apex_available:platform",
3632 ],
3633 }
3634 `
3635
3636 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3637 for _, f := range files {
3638 if f.path == file {
3639 if f.isLink {
3640 t.Errorf("%q is not a real file", file)
3641 }
3642 return
3643 }
3644 }
3645 t.Errorf("%q is not found", file)
3646 }
3647
3648 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3649 for _, f := range files {
3650 if f.path == file {
3651 if !f.isLink {
3652 t.Errorf("%q is not a symlink", file)
3653 }
3654 return
3655 }
3656 }
3657 t.Errorf("%q is not found", file)
3658 }
3659
3660 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003661 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003662 ensureRealfileExists(t, files, "javalib/myjar.jar")
3663 ensureRealfileExists(t, files, "lib64/mylib.so")
3664 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3665
3666 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003667 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003668 ensureRealfileExists(t, files, "javalib/myjar.jar")
3669 ensureRealfileExists(t, files, "lib64/mylib.so")
3670 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
3671}
3672
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003673func TestMain(m *testing.M) {
3674 run := func() int {
3675 setUp()
3676 defer tearDown()
3677
3678 return m.Run()
3679 }
3680
3681 os.Exit(run())
3682}