blob: 0420586de024c91c1a93282216fbf96ac5c808bd [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",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900456 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900457 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000458 // TODO: remove //apex_available:platform
459 apex_available: [
460 "//apex_available:platform",
461 "myapex",
462 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900463 }
464
465 java_library {
466 name: "myotherjar",
467 srcs: ["foo/bar/MyClass.java"],
468 sdk_version: "none",
469 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900470 // TODO: remove //apex_available:platform
471 apex_available: [
472 "//apex_available:platform",
473 "myapex",
474 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900476
477 java_library {
478 name: "mysharedjar",
479 srcs: ["foo/bar/MyClass.java"],
480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900482 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483 `)
484
Sundong Ahnabb64432019-10-22 13:58:29 +0900485 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900486
487 optFlags := apexRule.Args["opt_flags"]
488 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700489 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900490 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900491
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492 copyCmds := apexRule.Args["copy_commands"]
493
494 // Ensure that main rule creates an output
495 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
496
497 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800498 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900499 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900500
501 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800502 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900503 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900504
505 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800506 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
507 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900508 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
509 // .. but not for java libs
510 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900511 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800512
Colin Cross7113d202019-11-20 16:39:12 -0800513 // Ensure that the platform variant ends with _shared or _common
514 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
515 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900516 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
517 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900518 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
519
520 // Ensure that dynamic dependency to java libs are not included
521 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800522
523 // Ensure that all symlinks are present.
524 found_foo_link_64 := false
525 found_foo := false
526 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900527 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800528 if strings.HasSuffix(cmd, "bin/foo") {
529 found_foo = true
530 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
531 found_foo_link_64 = true
532 }
533 }
534 }
535 good := found_foo && found_foo_link_64
536 if !good {
537 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
538 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900539
Sundong Ahnabb64432019-10-22 13:58:29 +0900540 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700541 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700542 if len(noticeInputs) != 2 {
543 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900544 }
545 ensureListContains(t, noticeInputs, "NOTICE")
546 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900547
548 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900549 ensureListContains(t, depsInfo, "myjar <- myapex")
550 ensureListContains(t, depsInfo, "mylib <- myapex")
551 ensureListContains(t, depsInfo, "mylib2 <- mylib")
552 ensureListContains(t, depsInfo, "myotherjar <- myjar")
553 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800554}
555
Jooyung Hanf21c7972019-12-16 22:32:06 +0900556func TestDefaults(t *testing.T) {
557 ctx, _ := testApex(t, `
558 apex_defaults {
559 name: "myapex-defaults",
560 key: "myapex.key",
561 prebuilts: ["myetc"],
562 native_shared_libs: ["mylib"],
563 java_libs: ["myjar"],
564 apps: ["AppFoo"],
565 }
566
567 prebuilt_etc {
568 name: "myetc",
569 src: "myprebuilt",
570 }
571
572 apex {
573 name: "myapex",
574 defaults: ["myapex-defaults"],
575 }
576
577 apex_key {
578 name: "myapex.key",
579 public_key: "testkey.avbpubkey",
580 private_key: "testkey.pem",
581 }
582
583 cc_library {
584 name: "mylib",
585 system_shared_libs: [],
586 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000587 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900588 }
589
590 java_library {
591 name: "myjar",
592 srcs: ["foo/bar/MyClass.java"],
593 sdk_version: "none",
594 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000595 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900596 }
597
598 android_app {
599 name: "AppFoo",
600 srcs: ["foo/bar/MyClass.java"],
601 sdk_version: "none",
602 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000603 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900604 }
605 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000606 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900607 "etc/myetc",
608 "javalib/myjar.jar",
609 "lib64/mylib.so",
610 "app/AppFoo/AppFoo.apk",
611 })
612}
613
Jooyung Han01a3ee22019-11-02 02:52:25 +0900614func TestApexManifest(t *testing.T) {
615 ctx, _ := testApex(t, `
616 apex {
617 name: "myapex",
618 key: "myapex.key",
619 }
620
621 apex_key {
622 name: "myapex.key",
623 public_key: "testkey.avbpubkey",
624 private_key: "testkey.pem",
625 }
626 `)
627
628 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900629 args := module.Rule("apexRule").Args
630 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
631 t.Error("manifest should be apex_manifest.pb, but " + manifest)
632 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900633}
634
Alex Light5098a612018-11-29 17:12:15 -0800635func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700636 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800637 apex {
638 name: "myapex",
639 key: "myapex.key",
640 payload_type: "zip",
641 native_shared_libs: ["mylib"],
642 }
643
644 apex_key {
645 name: "myapex.key",
646 public_key: "testkey.avbpubkey",
647 private_key: "testkey.pem",
648 }
649
650 cc_library {
651 name: "mylib",
652 srcs: ["mylib.cpp"],
653 shared_libs: ["mylib2"],
654 system_shared_libs: [],
655 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800657 }
658
659 cc_library {
660 name: "mylib2",
661 srcs: ["mylib.cpp"],
662 system_shared_libs: [],
663 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800665 }
666 `)
667
Sundong Ahnabb64432019-10-22 13:58:29 +0900668 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800669 copyCmds := zipApexRule.Args["copy_commands"]
670
671 // Ensure that main rule creates an output
672 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
673
674 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800675 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800676
677 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800678 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800679
680 // Ensure that both direct and indirect deps are copied into apex
681 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
682 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683}
684
685func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700686 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900687 apex {
688 name: "myapex",
689 key: "myapex.key",
690 native_shared_libs: ["mylib", "mylib3"],
691 }
692
693 apex_key {
694 name: "myapex.key",
695 public_key: "testkey.avbpubkey",
696 private_key: "testkey.pem",
697 }
698
699 cc_library {
700 name: "mylib",
701 srcs: ["mylib.cpp"],
702 shared_libs: ["mylib2", "mylib3"],
703 system_shared_libs: [],
704 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000705 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706 }
707
708 cc_library {
709 name: "mylib2",
710 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900711 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900712 system_shared_libs: [],
713 stl: "none",
714 stubs: {
715 versions: ["1", "2", "3"],
716 },
717 }
718
719 cc_library {
720 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900721 srcs: ["mylib.cpp"],
722 shared_libs: ["mylib4"],
723 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724 stl: "none",
725 stubs: {
726 versions: ["10", "11", "12"],
727 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000728 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900729 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900730
731 cc_library {
732 name: "mylib4",
733 srcs: ["mylib.cpp"],
734 system_shared_libs: [],
735 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000736 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900737 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900738 `)
739
Sundong Ahnabb64432019-10-22 13:58:29 +0900740 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900741 copyCmds := apexRule.Args["copy_commands"]
742
743 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800744 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745
746 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800747 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748
749 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800750 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900751
Colin Cross7113d202019-11-20 16:39:12 -0800752 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900753
754 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900755 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900756 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900757 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900758
759 // 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 -0800760 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900761 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800762 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900763
764 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900765 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900766 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900767
768 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900769 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900770
Jooyung Hana57af4a2020-01-23 05:36:59 +0000771 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900772 "lib64/mylib.so",
773 "lib64/mylib3.so",
774 "lib64/mylib4.so",
775 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900776}
777
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900778func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700779 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900780 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900781 name: "myapex2",
782 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900783 native_shared_libs: ["mylib"],
784 }
785
786 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900787 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900788 public_key: "testkey.avbpubkey",
789 private_key: "testkey.pem",
790 }
791
792 cc_library {
793 name: "mylib",
794 srcs: ["mylib.cpp"],
795 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900796 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900797 system_shared_libs: [],
798 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000799 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900800 }
801
802 cc_library {
803 name: "libfoo",
804 srcs: ["mylib.cpp"],
805 shared_libs: ["libbar"],
806 system_shared_libs: [],
807 stl: "none",
808 stubs: {
809 versions: ["10", "20", "30"],
810 },
811 }
812
813 cc_library {
814 name: "libbar",
815 srcs: ["mylib.cpp"],
816 system_shared_libs: [],
817 stl: "none",
818 }
819
Jiyong Park678c8812020-02-07 17:25:49 +0900820 cc_library_static {
821 name: "libbaz",
822 srcs: ["mylib.cpp"],
823 system_shared_libs: [],
824 stl: "none",
825 apex_available: [ "myapex2" ],
826 }
827
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900828 `)
829
Jiyong Park83dc74b2020-01-14 18:38:44 +0900830 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900831 copyCmds := apexRule.Args["copy_commands"]
832
833 // Ensure that direct non-stubs dep is always included
834 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
835
836 // Ensure that indirect stubs dep is not included
837 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
838
839 // Ensure that dependency of stubs is not included
840 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
841
Jiyong Park83dc74b2020-01-14 18:38:44 +0900842 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900843
844 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900845 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900846 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900847 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900848
Jiyong Park3ff16992019-12-27 14:11:47 +0900849 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900850
851 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
852 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900853
854 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900855
856 ensureListContains(t, depsInfo, "mylib <- myapex2")
857 ensureListContains(t, depsInfo, "libbaz <- mylib")
858 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900859}
860
Jooyung Hand3639552019-08-09 12:57:43 +0900861func TestApexWithRuntimeLibsDependency(t *testing.T) {
862 /*
863 myapex
864 |
865 v (runtime_libs)
866 mylib ------+------> libfoo [provides stub]
867 |
868 `------> libbar
869 */
870 ctx, _ := testApex(t, `
871 apex {
872 name: "myapex",
873 key: "myapex.key",
874 native_shared_libs: ["mylib"],
875 }
876
877 apex_key {
878 name: "myapex.key",
879 public_key: "testkey.avbpubkey",
880 private_key: "testkey.pem",
881 }
882
883 cc_library {
884 name: "mylib",
885 srcs: ["mylib.cpp"],
886 runtime_libs: ["libfoo", "libbar"],
887 system_shared_libs: [],
888 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000889 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900890 }
891
892 cc_library {
893 name: "libfoo",
894 srcs: ["mylib.cpp"],
895 system_shared_libs: [],
896 stl: "none",
897 stubs: {
898 versions: ["10", "20", "30"],
899 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900900 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900901 }
902
903 cc_library {
904 name: "libbar",
905 srcs: ["mylib.cpp"],
906 system_shared_libs: [],
907 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000908 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900909 }
910
911 `)
912
Sundong Ahnabb64432019-10-22 13:58:29 +0900913 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900914 copyCmds := apexRule.Args["copy_commands"]
915
916 // Ensure that direct non-stubs dep is always included
917 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
918
919 // Ensure that indirect stubs dep is not included
920 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
921
922 // Ensure that runtime_libs dep in included
923 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
924
Sundong Ahnabb64432019-10-22 13:58:29 +0900925 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900926 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
927 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900928
929}
930
Jooyung Han9c80bae2019-08-20 17:30:57 +0900931func TestApexDependencyToLLNDK(t *testing.T) {
932 ctx, _ := testApex(t, `
933 apex {
934 name: "myapex",
935 key: "myapex.key",
936 use_vendor: true,
937 native_shared_libs: ["mylib"],
938 }
939
940 apex_key {
941 name: "myapex.key",
942 public_key: "testkey.avbpubkey",
943 private_key: "testkey.pem",
944 }
945
946 cc_library {
947 name: "mylib",
948 srcs: ["mylib.cpp"],
949 vendor_available: true,
950 shared_libs: ["libbar"],
951 system_shared_libs: [],
952 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000953 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900954 }
955
956 cc_library {
957 name: "libbar",
958 srcs: ["mylib.cpp"],
959 system_shared_libs: [],
960 stl: "none",
961 }
962
963 llndk_library {
964 name: "libbar",
965 symbol_file: "",
966 }
Jooyung Handc782442019-11-01 03:14:38 +0900967 `, func(fs map[string][]byte, config android.Config) {
968 setUseVendorWhitelistForTest(config, []string{"myapex"})
969 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900970
Sundong Ahnabb64432019-10-22 13:58:29 +0900971 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900972 copyCmds := apexRule.Args["copy_commands"]
973
974 // Ensure that LLNDK dep is not included
975 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
976
Sundong Ahnabb64432019-10-22 13:58:29 +0900977 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900978 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900979
980 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900981 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900982
983}
984
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700986 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 apex {
988 name: "myapex",
989 key: "myapex.key",
990 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
991 }
992
993 apex_key {
994 name: "myapex.key",
995 public_key: "testkey.avbpubkey",
996 private_key: "testkey.pem",
997 }
998
999 cc_library {
1000 name: "mylib",
1001 srcs: ["mylib.cpp"],
1002 shared_libs: ["libdl#27"],
1003 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001004 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005 }
1006
1007 cc_library_shared {
1008 name: "mylib_shared",
1009 srcs: ["mylib.cpp"],
1010 shared_libs: ["libdl#27"],
1011 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001012 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013 }
1014
1015 cc_library {
1016 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001017 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001018 nocrt: true,
1019 system_shared_libs: [],
1020 stl: "none",
1021 stubs: {
1022 versions: ["27", "28", "29"],
1023 },
1024 }
1025
1026 cc_library {
1027 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001028 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001029 nocrt: true,
1030 system_shared_libs: [],
1031 stl: "none",
1032 stubs: {
1033 versions: ["27", "28", "29"],
1034 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001035 apex_available: [
1036 "//apex_available:platform",
1037 "myapex"
1038 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001039 }
1040
1041 cc_library {
1042 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001043 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001044 nocrt: true,
1045 system_shared_libs: [],
1046 stl: "none",
1047 stubs: {
1048 versions: ["27", "28", "29"],
1049 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001050 apex_available: [
1051 "//apex_available:platform",
1052 "myapex"
1053 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001054 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001055
1056 cc_library {
1057 name: "libBootstrap",
1058 srcs: ["mylib.cpp"],
1059 stl: "none",
1060 bootstrap: true,
1061 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001062 `)
1063
Sundong Ahnabb64432019-10-22 13:58:29 +09001064 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001065 copyCmds := apexRule.Args["copy_commands"]
1066
1067 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001068 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001069 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1070 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001071
1072 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001073 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001074
Colin Cross7113d202019-11-20 16:39:12 -08001075 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1076 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1077 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001078
1079 // For dependency to libc
1080 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001081 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001082 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001083 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001084 // ... Cflags from stub is correctly exported to mylib
1085 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1086 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1087
1088 // For dependency to libm
1089 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001090 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001091 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001092 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001093 // ... and is not compiling with the stub
1094 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1095 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1096
1097 // For dependency to libdl
1098 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001099 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001100 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001101 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1102 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001103 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001104 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001105 // ... Cflags from stub is correctly exported to mylib
1106 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1107 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001108
1109 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001110 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1111 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1112 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1113 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001114}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001115
1116func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001117 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001118 apex {
1119 name: "myapex",
1120 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001121 native_shared_libs: ["mylib"],
1122 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001123 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001124 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001125 }
1126
1127 apex_key {
1128 name: "myapex.key",
1129 public_key: "testkey.avbpubkey",
1130 private_key: "testkey.pem",
1131 }
1132
1133 prebuilt_etc {
1134 name: "myetc",
1135 src: "myprebuilt",
1136 sub_dir: "foo/bar",
1137 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001138
1139 cc_library {
1140 name: "mylib",
1141 srcs: ["mylib.cpp"],
1142 relative_install_path: "foo/bar",
1143 system_shared_libs: [],
1144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001145 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001146 }
1147
1148 cc_binary {
1149 name: "mybin",
1150 srcs: ["mylib.cpp"],
1151 relative_install_path: "foo/bar",
1152 system_shared_libs: [],
1153 static_executable: true,
1154 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001155 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001156 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001157 `)
1158
Sundong Ahnabb64432019-10-22 13:58:29 +09001159 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001160 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1161
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001162 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001163 ensureListContains(t, dirs, "etc")
1164 ensureListContains(t, dirs, "etc/foo")
1165 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001166 ensureListContains(t, dirs, "lib64")
1167 ensureListContains(t, dirs, "lib64/foo")
1168 ensureListContains(t, dirs, "lib64/foo/bar")
1169 ensureListContains(t, dirs, "lib")
1170 ensureListContains(t, dirs, "lib/foo")
1171 ensureListContains(t, dirs, "lib/foo/bar")
1172
Jiyong Parkbd13e442019-03-15 18:10:35 +09001173 ensureListContains(t, dirs, "bin")
1174 ensureListContains(t, dirs, "bin/foo")
1175 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001176}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001177
1178func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001179 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001180 apex {
1181 name: "myapex",
1182 key: "myapex.key",
1183 native_shared_libs: ["mylib"],
1184 use_vendor: true,
1185 }
1186
1187 apex_key {
1188 name: "myapex.key",
1189 public_key: "testkey.avbpubkey",
1190 private_key: "testkey.pem",
1191 }
1192
1193 cc_library {
1194 name: "mylib",
1195 srcs: ["mylib.cpp"],
1196 shared_libs: ["mylib2"],
1197 system_shared_libs: [],
1198 vendor_available: true,
1199 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001200 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001201 }
1202
1203 cc_library {
1204 name: "mylib2",
1205 srcs: ["mylib.cpp"],
1206 system_shared_libs: [],
1207 vendor_available: true,
1208 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001209 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001210 }
Jooyung Handc782442019-11-01 03:14:38 +09001211 `, func(fs map[string][]byte, config android.Config) {
1212 setUseVendorWhitelistForTest(config, []string{"myapex"})
1213 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001214
1215 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001216 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001217 for _, implicit := range i.Implicits {
1218 inputsList = append(inputsList, implicit.String())
1219 }
1220 }
1221 inputsString := strings.Join(inputsList, " ")
1222
1223 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001224 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1225 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001226
1227 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001228 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1229 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001230}
Jiyong Park16e91a02018-12-20 18:18:08 +09001231
Jooyung Handc782442019-11-01 03:14:38 +09001232func TestUseVendorRestriction(t *testing.T) {
1233 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1234 apex {
1235 name: "myapex",
1236 key: "myapex.key",
1237 use_vendor: true,
1238 }
1239 apex_key {
1240 name: "myapex.key",
1241 public_key: "testkey.avbpubkey",
1242 private_key: "testkey.pem",
1243 }
1244 `, func(fs map[string][]byte, config android.Config) {
1245 setUseVendorWhitelistForTest(config, []string{""})
1246 })
1247 // no error with whitelist
1248 testApex(t, `
1249 apex {
1250 name: "myapex",
1251 key: "myapex.key",
1252 use_vendor: true,
1253 }
1254 apex_key {
1255 name: "myapex.key",
1256 public_key: "testkey.avbpubkey",
1257 private_key: "testkey.pem",
1258 }
1259 `, func(fs map[string][]byte, config android.Config) {
1260 setUseVendorWhitelistForTest(config, []string{"myapex"})
1261 })
1262}
1263
Jooyung Han5c998b92019-06-27 11:30:33 +09001264func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1265 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1266 apex {
1267 name: "myapex",
1268 key: "myapex.key",
1269 native_shared_libs: ["mylib"],
1270 use_vendor: true,
1271 }
1272
1273 apex_key {
1274 name: "myapex.key",
1275 public_key: "testkey.avbpubkey",
1276 private_key: "testkey.pem",
1277 }
1278
1279 cc_library {
1280 name: "mylib",
1281 srcs: ["mylib.cpp"],
1282 system_shared_libs: [],
1283 stl: "none",
1284 }
1285 `)
1286}
1287
Jiyong Park16e91a02018-12-20 18:18:08 +09001288func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001289 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001290 apex {
1291 name: "myapex",
1292 key: "myapex.key",
1293 native_shared_libs: ["mylib"],
1294 }
1295
1296 apex_key {
1297 name: "myapex.key",
1298 public_key: "testkey.avbpubkey",
1299 private_key: "testkey.pem",
1300 }
1301
1302 cc_library {
1303 name: "mylib",
1304 srcs: ["mylib.cpp"],
1305 system_shared_libs: [],
1306 stl: "none",
1307 stubs: {
1308 versions: ["1", "2", "3"],
1309 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001310 apex_available: [
1311 "//apex_available:platform",
1312 "myapex",
1313 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001314 }
1315
1316 cc_binary {
1317 name: "not_in_apex",
1318 srcs: ["mylib.cpp"],
1319 static_libs: ["mylib"],
1320 static_executable: true,
1321 system_shared_libs: [],
1322 stl: "none",
1323 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001324 `)
1325
Colin Cross7113d202019-11-20 16:39:12 -08001326 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001327
1328 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001329 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001330}
Jiyong Park9335a262018-12-24 11:31:58 +09001331
1332func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001333 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001334 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001335 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001336 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001337 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001338 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001339 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001340 }
1341
1342 cc_library {
1343 name: "mylib",
1344 srcs: ["mylib.cpp"],
1345 system_shared_libs: [],
1346 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001347 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001348 }
1349
1350 apex_key {
1351 name: "myapex.key",
1352 public_key: "testkey.avbpubkey",
1353 private_key: "testkey.pem",
1354 }
1355
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001356 android_app_certificate {
1357 name: "myapex.certificate",
1358 certificate: "testkey",
1359 }
1360
1361 android_app_certificate {
1362 name: "myapex.certificate.override",
1363 certificate: "testkey.override",
1364 }
1365
Jiyong Park9335a262018-12-24 11:31:58 +09001366 `)
1367
1368 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001369 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001370
1371 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1372 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1373 "vendor/foo/devkeys/testkey.avbpubkey")
1374 }
1375 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1376 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1377 "vendor/foo/devkeys/testkey.pem")
1378 }
1379
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001380 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001381 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001382 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001383 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001384 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001385 }
1386}
Jiyong Park58e364a2019-01-19 19:24:06 +09001387
Jooyung Hanf121a652019-12-17 14:30:11 +09001388func TestCertificate(t *testing.T) {
1389 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1390 ctx, _ := testApex(t, `
1391 apex {
1392 name: "myapex",
1393 key: "myapex.key",
1394 }
1395 apex_key {
1396 name: "myapex.key",
1397 public_key: "testkey.avbpubkey",
1398 private_key: "testkey.pem",
1399 }`)
1400 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1401 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1402 if actual := rule.Args["certificates"]; actual != expected {
1403 t.Errorf("certificates should be %q, not %q", expected, actual)
1404 }
1405 })
1406 t.Run("override when unspecified", func(t *testing.T) {
1407 ctx, _ := testApex(t, `
1408 apex {
1409 name: "myapex_keytest",
1410 key: "myapex.key",
1411 file_contexts: ":myapex-file_contexts",
1412 }
1413 apex_key {
1414 name: "myapex.key",
1415 public_key: "testkey.avbpubkey",
1416 private_key: "testkey.pem",
1417 }
1418 android_app_certificate {
1419 name: "myapex.certificate.override",
1420 certificate: "testkey.override",
1421 }`)
1422 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1423 expected := "testkey.override.x509.pem testkey.override.pk8"
1424 if actual := rule.Args["certificates"]; actual != expected {
1425 t.Errorf("certificates should be %q, not %q", expected, actual)
1426 }
1427 })
1428 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1429 ctx, _ := testApex(t, `
1430 apex {
1431 name: "myapex",
1432 key: "myapex.key",
1433 certificate: ":myapex.certificate",
1434 }
1435 apex_key {
1436 name: "myapex.key",
1437 public_key: "testkey.avbpubkey",
1438 private_key: "testkey.pem",
1439 }
1440 android_app_certificate {
1441 name: "myapex.certificate",
1442 certificate: "testkey",
1443 }`)
1444 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1445 expected := "testkey.x509.pem testkey.pk8"
1446 if actual := rule.Args["certificates"]; actual != expected {
1447 t.Errorf("certificates should be %q, not %q", expected, actual)
1448 }
1449 })
1450 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1451 ctx, _ := testApex(t, `
1452 apex {
1453 name: "myapex_keytest",
1454 key: "myapex.key",
1455 file_contexts: ":myapex-file_contexts",
1456 certificate: ":myapex.certificate",
1457 }
1458 apex_key {
1459 name: "myapex.key",
1460 public_key: "testkey.avbpubkey",
1461 private_key: "testkey.pem",
1462 }
1463 android_app_certificate {
1464 name: "myapex.certificate.override",
1465 certificate: "testkey.override",
1466 }`)
1467 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1468 expected := "testkey.override.x509.pem testkey.override.pk8"
1469 if actual := rule.Args["certificates"]; actual != expected {
1470 t.Errorf("certificates should be %q, not %q", expected, actual)
1471 }
1472 })
1473 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1474 ctx, _ := testApex(t, `
1475 apex {
1476 name: "myapex",
1477 key: "myapex.key",
1478 certificate: "testkey",
1479 }
1480 apex_key {
1481 name: "myapex.key",
1482 public_key: "testkey.avbpubkey",
1483 private_key: "testkey.pem",
1484 }`)
1485 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1486 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1487 if actual := rule.Args["certificates"]; actual != expected {
1488 t.Errorf("certificates should be %q, not %q", expected, actual)
1489 }
1490 })
1491 t.Run("override when specified as <name>", func(t *testing.T) {
1492 ctx, _ := testApex(t, `
1493 apex {
1494 name: "myapex_keytest",
1495 key: "myapex.key",
1496 file_contexts: ":myapex-file_contexts",
1497 certificate: "testkey",
1498 }
1499 apex_key {
1500 name: "myapex.key",
1501 public_key: "testkey.avbpubkey",
1502 private_key: "testkey.pem",
1503 }
1504 android_app_certificate {
1505 name: "myapex.certificate.override",
1506 certificate: "testkey.override",
1507 }`)
1508 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1509 expected := "testkey.override.x509.pem testkey.override.pk8"
1510 if actual := rule.Args["certificates"]; actual != expected {
1511 t.Errorf("certificates should be %q, not %q", expected, actual)
1512 }
1513 })
1514}
1515
Jiyong Park58e364a2019-01-19 19:24:06 +09001516func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001517 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001518 apex {
1519 name: "myapex",
1520 key: "myapex.key",
1521 native_shared_libs: ["mylib"],
1522 }
1523
1524 apex {
1525 name: "otherapex",
1526 key: "myapex.key",
1527 native_shared_libs: ["mylib"],
1528 }
1529
1530 apex_key {
1531 name: "myapex.key",
1532 public_key: "testkey.avbpubkey",
1533 private_key: "testkey.pem",
1534 }
1535
1536 cc_library {
1537 name: "mylib",
1538 srcs: ["mylib.cpp"],
1539 system_shared_libs: [],
1540 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001541 // TODO: remove //apex_available:platform
1542 apex_available: [
1543 "//apex_available:platform",
1544 "myapex",
1545 "otherapex",
1546 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001547 }
1548 `)
1549
Jooyung Han6b8459b2019-10-30 08:29:25 +09001550 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001551 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001552 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001553 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1554 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001555
Jooyung Han6b8459b2019-10-30 08:29:25 +09001556 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001557 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001558 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001559 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1560 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001561
Jooyung Han6b8459b2019-10-30 08:29:25 +09001562 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001563 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001564 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001565 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1566 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001567}
Jiyong Park7e636d02019-01-28 16:16:54 +09001568
1569func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001570 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001571 apex {
1572 name: "myapex",
1573 key: "myapex.key",
1574 native_shared_libs: ["mylib"],
1575 }
1576
1577 apex_key {
1578 name: "myapex.key",
1579 public_key: "testkey.avbpubkey",
1580 private_key: "testkey.pem",
1581 }
1582
1583 cc_library_headers {
1584 name: "mylib_headers",
1585 export_include_dirs: ["my_include"],
1586 system_shared_libs: [],
1587 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001588 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001589 }
1590
1591 cc_library {
1592 name: "mylib",
1593 srcs: ["mylib.cpp"],
1594 system_shared_libs: [],
1595 stl: "none",
1596 header_libs: ["mylib_headers"],
1597 export_header_lib_headers: ["mylib_headers"],
1598 stubs: {
1599 versions: ["1", "2", "3"],
1600 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001601 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001602 }
1603
1604 cc_library {
1605 name: "otherlib",
1606 srcs: ["mylib.cpp"],
1607 system_shared_libs: [],
1608 stl: "none",
1609 shared_libs: ["mylib"],
1610 }
1611 `)
1612
Colin Cross7113d202019-11-20 16:39:12 -08001613 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001614
1615 // Ensure that the include path of the header lib is exported to 'otherlib'
1616 ensureContains(t, cFlags, "-Imy_include")
1617}
Alex Light9670d332019-01-29 18:07:33 -08001618
Jiyong Park7cd10e32020-01-14 09:22:18 +09001619type fileInApex struct {
1620 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001621 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001622 isLink bool
1623}
1624
Jooyung Hana57af4a2020-01-23 05:36:59 +00001625func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001626 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001627 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001628 copyCmds := apexRule.Args["copy_commands"]
1629 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001630 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001631 for _, cmd := range strings.Split(copyCmds, "&&") {
1632 cmd = strings.TrimSpace(cmd)
1633 if cmd == "" {
1634 continue
1635 }
1636 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001637 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001638 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001639 switch terms[0] {
1640 case "mkdir":
1641 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001642 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001643 t.Fatal("copyCmds contains invalid cp command", cmd)
1644 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001645 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001646 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001647 isLink = false
1648 case "ln":
1649 if len(terms) != 3 && len(terms) != 4 {
1650 // ln LINK TARGET or ln -s LINK TARGET
1651 t.Fatal("copyCmds contains invalid ln command", cmd)
1652 }
1653 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001654 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001655 isLink = true
1656 default:
1657 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1658 }
1659 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001660 index := strings.Index(dst, imageApexDir)
1661 if index == -1 {
1662 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1663 }
1664 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001665 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001666 }
1667 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001668 return ret
1669}
1670
Jooyung Hana57af4a2020-01-23 05:36:59 +00001671func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1672 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001673 var failed bool
1674 var surplus []string
1675 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001676 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001677 for _, expected := range files {
1678 if matched, _ := path.Match(expected, file.path); matched {
1679 filesMatched[expected] = true
1680 return
1681 }
1682 }
1683 surplus = append(surplus, file.path)
1684 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001685
Jooyung Han31c470b2019-10-18 16:26:59 +09001686 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001687 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001688 t.Log("surplus files", surplus)
1689 failed = true
1690 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001691
1692 if len(files) > len(filesMatched) {
1693 var missing []string
1694 for _, expected := range files {
1695 if !filesMatched[expected] {
1696 missing = append(missing, expected)
1697 }
1698 }
1699 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001700 t.Log("missing files", missing)
1701 failed = true
1702 }
1703 if failed {
1704 t.Fail()
1705 }
1706}
1707
Jooyung Han344d5432019-08-23 11:17:39 +09001708func TestVndkApexCurrent(t *testing.T) {
1709 ctx, _ := testApex(t, `
1710 apex_vndk {
1711 name: "myapex",
1712 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001713 }
1714
1715 apex_key {
1716 name: "myapex.key",
1717 public_key: "testkey.avbpubkey",
1718 private_key: "testkey.pem",
1719 }
1720
1721 cc_library {
1722 name: "libvndk",
1723 srcs: ["mylib.cpp"],
1724 vendor_available: true,
1725 vndk: {
1726 enabled: 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 }
1732
1733 cc_library {
1734 name: "libvndksp",
1735 srcs: ["mylib.cpp"],
1736 vendor_available: true,
1737 vndk: {
1738 enabled: true,
1739 support_system_process: true,
1740 },
1741 system_shared_libs: [],
1742 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001743 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001744 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001745 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001746
Jooyung Hana57af4a2020-01-23 05:36:59 +00001747 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001748 "lib/libvndk.so",
1749 "lib/libvndksp.so",
1750 "lib64/libvndk.so",
1751 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001752 "etc/llndk.libraries.VER.txt",
1753 "etc/vndkcore.libraries.VER.txt",
1754 "etc/vndksp.libraries.VER.txt",
1755 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001756 })
Jooyung Han344d5432019-08-23 11:17:39 +09001757}
1758
1759func TestVndkApexWithPrebuilt(t *testing.T) {
1760 ctx, _ := testApex(t, `
1761 apex_vndk {
1762 name: "myapex",
1763 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001764 }
1765
1766 apex_key {
1767 name: "myapex.key",
1768 public_key: "testkey.avbpubkey",
1769 private_key: "testkey.pem",
1770 }
1771
1772 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001773 name: "libvndk",
1774 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001775 vendor_available: true,
1776 vndk: {
1777 enabled: true,
1778 },
1779 system_shared_libs: [],
1780 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001781 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001782 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001783
1784 cc_prebuilt_library_shared {
1785 name: "libvndk.arm",
1786 srcs: ["libvndk.arm.so"],
1787 vendor_available: true,
1788 vndk: {
1789 enabled: true,
1790 },
1791 enabled: false,
1792 arch: {
1793 arm: {
1794 enabled: true,
1795 },
1796 },
1797 system_shared_libs: [],
1798 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001799 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001800 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001801 `+vndkLibrariesTxtFiles("current"),
1802 withFiles(map[string][]byte{
1803 "libvndk.so": nil,
1804 "libvndk.arm.so": nil,
1805 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001806
Jooyung Hana57af4a2020-01-23 05:36:59 +00001807 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001808 "lib/libvndk.so",
1809 "lib/libvndk.arm.so",
1810 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001811 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001812 })
Jooyung Han344d5432019-08-23 11:17:39 +09001813}
1814
Jooyung Han39edb6c2019-11-06 16:53:07 +09001815func vndkLibrariesTxtFiles(vers ...string) (result string) {
1816 for _, v := range vers {
1817 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001818 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001819 result += `
1820 vndk_libraries_txt {
1821 name: "` + txt + `.libraries.txt",
1822 }
1823 `
1824 }
1825 } else {
1826 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1827 result += `
1828 prebuilt_etc {
1829 name: "` + txt + `.libraries.` + v + `.txt",
1830 src: "dummy.txt",
1831 }
1832 `
1833 }
1834 }
1835 }
1836 return
1837}
1838
Jooyung Han344d5432019-08-23 11:17:39 +09001839func TestVndkApexVersion(t *testing.T) {
1840 ctx, _ := testApex(t, `
1841 apex_vndk {
1842 name: "myapex_v27",
1843 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001844 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001845 vndk_version: "27",
1846 }
1847
1848 apex_key {
1849 name: "myapex.key",
1850 public_key: "testkey.avbpubkey",
1851 private_key: "testkey.pem",
1852 }
1853
Jooyung Han31c470b2019-10-18 16:26:59 +09001854 vndk_prebuilt_shared {
1855 name: "libvndk27",
1856 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001857 vendor_available: true,
1858 vndk: {
1859 enabled: true,
1860 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001861 target_arch: "arm64",
1862 arch: {
1863 arm: {
1864 srcs: ["libvndk27_arm.so"],
1865 },
1866 arm64: {
1867 srcs: ["libvndk27_arm64.so"],
1868 },
1869 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001870 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001871 }
1872
1873 vndk_prebuilt_shared {
1874 name: "libvndk27",
1875 version: "27",
1876 vendor_available: true,
1877 vndk: {
1878 enabled: true,
1879 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001880 target_arch: "x86_64",
1881 arch: {
1882 x86: {
1883 srcs: ["libvndk27_x86.so"],
1884 },
1885 x86_64: {
1886 srcs: ["libvndk27_x86_64.so"],
1887 },
1888 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001889 }
1890 `+vndkLibrariesTxtFiles("27"),
1891 withFiles(map[string][]byte{
1892 "libvndk27_arm.so": nil,
1893 "libvndk27_arm64.so": nil,
1894 "libvndk27_x86.so": nil,
1895 "libvndk27_x86_64.so": nil,
1896 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001897
Jooyung Hana57af4a2020-01-23 05:36:59 +00001898 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001899 "lib/libvndk27_arm.so",
1900 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001901 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001902 })
Jooyung Han344d5432019-08-23 11:17:39 +09001903}
1904
1905func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1906 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1907 apex_vndk {
1908 name: "myapex_v27",
1909 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001910 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001911 vndk_version: "27",
1912 }
1913 apex_vndk {
1914 name: "myapex_v27_other",
1915 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001916 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001917 vndk_version: "27",
1918 }
1919
1920 apex_key {
1921 name: "myapex.key",
1922 public_key: "testkey.avbpubkey",
1923 private_key: "testkey.pem",
1924 }
1925
1926 cc_library {
1927 name: "libvndk",
1928 srcs: ["mylib.cpp"],
1929 vendor_available: true,
1930 vndk: {
1931 enabled: true,
1932 },
1933 system_shared_libs: [],
1934 stl: "none",
1935 }
1936
1937 vndk_prebuilt_shared {
1938 name: "libvndk",
1939 version: "27",
1940 vendor_available: true,
1941 vndk: {
1942 enabled: true,
1943 },
1944 srcs: ["libvndk.so"],
1945 }
1946 `, withFiles(map[string][]byte{
1947 "libvndk.so": nil,
1948 }))
1949}
1950
Jooyung Han90eee022019-10-01 20:02:42 +09001951func TestVndkApexNameRule(t *testing.T) {
1952 ctx, _ := testApex(t, `
1953 apex_vndk {
1954 name: "myapex",
1955 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001956 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001957 }
1958 apex_vndk {
1959 name: "myapex_v28",
1960 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001961 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001962 vndk_version: "28",
1963 }
1964 apex_key {
1965 name: "myapex.key",
1966 public_key: "testkey.avbpubkey",
1967 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001968 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001969
1970 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001971 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001972 actual := proptools.String(bundle.properties.Apex_name)
1973 if !reflect.DeepEqual(actual, expected) {
1974 t.Errorf("Got '%v', expected '%v'", actual, expected)
1975 }
1976 }
1977
1978 assertApexName("com.android.vndk.vVER", "myapex")
1979 assertApexName("com.android.vndk.v28", "myapex_v28")
1980}
1981
Jooyung Han344d5432019-08-23 11:17:39 +09001982func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1983 ctx, _ := testApex(t, `
1984 apex_vndk {
1985 name: "myapex",
1986 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001987 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001988 }
1989
1990 apex_key {
1991 name: "myapex.key",
1992 public_key: "testkey.avbpubkey",
1993 private_key: "testkey.pem",
1994 }
1995
1996 cc_library {
1997 name: "libvndk",
1998 srcs: ["mylib.cpp"],
1999 vendor_available: true,
2000 native_bridge_supported: true,
2001 host_supported: true,
2002 vndk: {
2003 enabled: true,
2004 },
2005 system_shared_libs: [],
2006 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002007 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002008 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002009 `+vndkLibrariesTxtFiles("current"),
2010 withTargets(map[android.OsType][]android.Target{
2011 android.Android: []android.Target{
2012 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2013 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2014 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2015 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2016 },
2017 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002018
Jooyung Hana57af4a2020-01-23 05:36:59 +00002019 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002020 "lib/libvndk.so",
2021 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002022 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002023 })
Jooyung Han344d5432019-08-23 11:17:39 +09002024}
2025
2026func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2027 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2028 apex_vndk {
2029 name: "myapex",
2030 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002031 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002032 native_bridge_supported: true,
2033 }
2034
2035 apex_key {
2036 name: "myapex.key",
2037 public_key: "testkey.avbpubkey",
2038 private_key: "testkey.pem",
2039 }
2040
2041 cc_library {
2042 name: "libvndk",
2043 srcs: ["mylib.cpp"],
2044 vendor_available: true,
2045 native_bridge_supported: true,
2046 host_supported: true,
2047 vndk: {
2048 enabled: true,
2049 },
2050 system_shared_libs: [],
2051 stl: "none",
2052 }
2053 `)
2054}
2055
Jooyung Han31c470b2019-10-18 16:26:59 +09002056func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002057 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002058 apex_vndk {
2059 name: "myapex_v27",
2060 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002061 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002062 vndk_version: "27",
2063 }
2064
2065 apex_key {
2066 name: "myapex.key",
2067 public_key: "testkey.avbpubkey",
2068 private_key: "testkey.pem",
2069 }
2070
2071 vndk_prebuilt_shared {
2072 name: "libvndk27",
2073 version: "27",
2074 target_arch: "arm",
2075 vendor_available: true,
2076 vndk: {
2077 enabled: true,
2078 },
2079 arch: {
2080 arm: {
2081 srcs: ["libvndk27.so"],
2082 }
2083 },
2084 }
2085
2086 vndk_prebuilt_shared {
2087 name: "libvndk27",
2088 version: "27",
2089 target_arch: "arm",
2090 binder32bit: true,
2091 vendor_available: true,
2092 vndk: {
2093 enabled: true,
2094 },
2095 arch: {
2096 arm: {
2097 srcs: ["libvndk27binder32.so"],
2098 }
2099 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002100 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002101 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002102 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002103 withFiles(map[string][]byte{
2104 "libvndk27.so": nil,
2105 "libvndk27binder32.so": nil,
2106 }),
2107 withBinder32bit,
2108 withTargets(map[android.OsType][]android.Target{
2109 android.Android: []android.Target{
2110 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2111 },
2112 }),
2113 )
2114
Jooyung Hana57af4a2020-01-23 05:36:59 +00002115 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002116 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002117 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002118 })
2119}
2120
Jooyung Hane1633032019-08-01 17:41:43 +09002121func TestDependenciesInApexManifest(t *testing.T) {
2122 ctx, _ := testApex(t, `
2123 apex {
2124 name: "myapex_nodep",
2125 key: "myapex.key",
2126 native_shared_libs: ["lib_nodep"],
2127 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002128 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002129 }
2130
2131 apex {
2132 name: "myapex_dep",
2133 key: "myapex.key",
2134 native_shared_libs: ["lib_dep"],
2135 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002136 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002137 }
2138
2139 apex {
2140 name: "myapex_provider",
2141 key: "myapex.key",
2142 native_shared_libs: ["libfoo"],
2143 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002144 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002145 }
2146
2147 apex {
2148 name: "myapex_selfcontained",
2149 key: "myapex.key",
2150 native_shared_libs: ["lib_dep", "libfoo"],
2151 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002152 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002153 }
2154
2155 apex_key {
2156 name: "myapex.key",
2157 public_key: "testkey.avbpubkey",
2158 private_key: "testkey.pem",
2159 }
2160
2161 cc_library {
2162 name: "lib_nodep",
2163 srcs: ["mylib.cpp"],
2164 system_shared_libs: [],
2165 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002166 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002167 }
2168
2169 cc_library {
2170 name: "lib_dep",
2171 srcs: ["mylib.cpp"],
2172 shared_libs: ["libfoo"],
2173 system_shared_libs: [],
2174 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002175 apex_available: [
2176 "myapex_dep",
2177 "myapex_provider",
2178 "myapex_selfcontained",
2179 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002180 }
2181
2182 cc_library {
2183 name: "libfoo",
2184 srcs: ["mytest.cpp"],
2185 stubs: {
2186 versions: ["1"],
2187 },
2188 system_shared_libs: [],
2189 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002190 apex_available: [
2191 "myapex_provider",
2192 "myapex_selfcontained",
2193 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002194 }
2195 `)
2196
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002197 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002198 var provideNativeLibs, requireNativeLibs []string
2199
Sundong Ahnabb64432019-10-22 13:58:29 +09002200 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002201 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2202 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002203 ensureListEmpty(t, provideNativeLibs)
2204 ensureListEmpty(t, requireNativeLibs)
2205
Sundong Ahnabb64432019-10-22 13:58:29 +09002206 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002207 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2208 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002209 ensureListEmpty(t, provideNativeLibs)
2210 ensureListContains(t, requireNativeLibs, "libfoo.so")
2211
Sundong Ahnabb64432019-10-22 13:58:29 +09002212 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002213 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2214 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002215 ensureListContains(t, provideNativeLibs, "libfoo.so")
2216 ensureListEmpty(t, requireNativeLibs)
2217
Sundong Ahnabb64432019-10-22 13:58:29 +09002218 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002219 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2220 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002221 ensureListContains(t, provideNativeLibs, "libfoo.so")
2222 ensureListEmpty(t, requireNativeLibs)
2223}
2224
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002225func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002226 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002227 apex {
2228 name: "myapex",
2229 key: "myapex.key",
2230 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002231 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002232 }
2233
2234 apex_key {
2235 name: "myapex.key",
2236 public_key: "testkey.avbpubkey",
2237 private_key: "testkey.pem",
2238 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002239
2240 cc_library {
2241 name: "mylib",
2242 srcs: ["mylib.cpp"],
2243 system_shared_libs: [],
2244 stl: "none",
2245 apex_available: [
2246 "//apex_available:platform",
2247 "myapex",
2248 ],
2249 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002250 `)
2251
Sundong Ahnabb64432019-10-22 13:58:29 +09002252 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002253 apexManifestRule := module.Rule("apexManifestRule")
2254 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2255 apexRule := module.Rule("apexRule")
2256 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002257
2258 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2259 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2260 name := apexBundle.BaseModuleName()
2261 prefix := "TARGET_"
2262 var builder strings.Builder
2263 data.Custom(&builder, name, prefix, "", data)
2264 androidMk := builder.String()
2265 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2266 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002267}
2268
Alex Light0851b882019-02-07 13:20:53 -08002269func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002270 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002271 apex {
2272 name: "myapex",
2273 key: "myapex.key",
2274 native_shared_libs: ["mylib_common"],
2275 }
2276
2277 apex_key {
2278 name: "myapex.key",
2279 public_key: "testkey.avbpubkey",
2280 private_key: "testkey.pem",
2281 }
2282
2283 cc_library {
2284 name: "mylib_common",
2285 srcs: ["mylib.cpp"],
2286 system_shared_libs: [],
2287 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002288 apex_available: [
2289 "//apex_available:platform",
2290 "myapex",
2291 ],
Alex Light0851b882019-02-07 13:20:53 -08002292 }
2293 `)
2294
Sundong Ahnabb64432019-10-22 13:58:29 +09002295 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002296 apexRule := module.Rule("apexRule")
2297 copyCmds := apexRule.Args["copy_commands"]
2298
2299 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2300 t.Log("Apex was a test apex!")
2301 t.Fail()
2302 }
2303 // Ensure that main rule creates an output
2304 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2305
2306 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002307 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002308
2309 // Ensure that both direct and indirect deps are copied into apex
2310 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2311
Colin Cross7113d202019-11-20 16:39:12 -08002312 // Ensure that the platform variant ends with _shared
2313 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002314
2315 if !android.InAnyApex("mylib_common") {
2316 t.Log("Found mylib_common not in any apex!")
2317 t.Fail()
2318 }
2319}
2320
2321func TestTestApex(t *testing.T) {
2322 if android.InAnyApex("mylib_common_test") {
2323 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!")
2324 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002325 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002326 apex_test {
2327 name: "myapex",
2328 key: "myapex.key",
2329 native_shared_libs: ["mylib_common_test"],
2330 }
2331
2332 apex_key {
2333 name: "myapex.key",
2334 public_key: "testkey.avbpubkey",
2335 private_key: "testkey.pem",
2336 }
2337
2338 cc_library {
2339 name: "mylib_common_test",
2340 srcs: ["mylib.cpp"],
2341 system_shared_libs: [],
2342 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002343 // TODO: remove //apex_available:platform
2344 apex_available: [
2345 "//apex_available:platform",
2346 "myapex",
2347 ],
Alex Light0851b882019-02-07 13:20:53 -08002348 }
2349 `)
2350
Sundong Ahnabb64432019-10-22 13:58:29 +09002351 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002352 apexRule := module.Rule("apexRule")
2353 copyCmds := apexRule.Args["copy_commands"]
2354
2355 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2356 t.Log("Apex was not a test apex!")
2357 t.Fail()
2358 }
2359 // Ensure that main rule creates an output
2360 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2361
2362 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002363 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002364
2365 // Ensure that both direct and indirect deps are copied into apex
2366 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2367
Colin Cross7113d202019-11-20 16:39:12 -08002368 // Ensure that the platform variant ends with _shared
2369 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002370}
2371
Alex Light9670d332019-01-29 18:07:33 -08002372func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002373 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002374 apex {
2375 name: "myapex",
2376 key: "myapex.key",
2377 multilib: {
2378 first: {
2379 native_shared_libs: ["mylib_common"],
2380 }
2381 },
2382 target: {
2383 android: {
2384 multilib: {
2385 first: {
2386 native_shared_libs: ["mylib"],
2387 }
2388 }
2389 },
2390 host: {
2391 multilib: {
2392 first: {
2393 native_shared_libs: ["mylib2"],
2394 }
2395 }
2396 }
2397 }
2398 }
2399
2400 apex_key {
2401 name: "myapex.key",
2402 public_key: "testkey.avbpubkey",
2403 private_key: "testkey.pem",
2404 }
2405
2406 cc_library {
2407 name: "mylib",
2408 srcs: ["mylib.cpp"],
2409 system_shared_libs: [],
2410 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002411 // TODO: remove //apex_available:platform
2412 apex_available: [
2413 "//apex_available:platform",
2414 "myapex",
2415 ],
Alex Light9670d332019-01-29 18:07:33 -08002416 }
2417
2418 cc_library {
2419 name: "mylib_common",
2420 srcs: ["mylib.cpp"],
2421 system_shared_libs: [],
2422 stl: "none",
2423 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002424 // TODO: remove //apex_available:platform
2425 apex_available: [
2426 "//apex_available:platform",
2427 "myapex",
2428 ],
Alex Light9670d332019-01-29 18:07:33 -08002429 }
2430
2431 cc_library {
2432 name: "mylib2",
2433 srcs: ["mylib.cpp"],
2434 system_shared_libs: [],
2435 stl: "none",
2436 compile_multilib: "first",
2437 }
2438 `)
2439
Sundong Ahnabb64432019-10-22 13:58:29 +09002440 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002441 copyCmds := apexRule.Args["copy_commands"]
2442
2443 // Ensure that main rule creates an output
2444 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2445
2446 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002447 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2448 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2449 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002450
2451 // Ensure that both direct and indirect deps are copied into apex
2452 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2453 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2454 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2455
Colin Cross7113d202019-11-20 16:39:12 -08002456 // Ensure that the platform variant ends with _shared
2457 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2458 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2459 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002460}
Jiyong Park04480cf2019-02-06 00:16:29 +09002461
2462func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002463 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002464 apex {
2465 name: "myapex",
2466 key: "myapex.key",
2467 binaries: ["myscript"],
2468 }
2469
2470 apex_key {
2471 name: "myapex.key",
2472 public_key: "testkey.avbpubkey",
2473 private_key: "testkey.pem",
2474 }
2475
2476 sh_binary {
2477 name: "myscript",
2478 src: "mylib.cpp",
2479 filename: "myscript.sh",
2480 sub_dir: "script",
2481 }
2482 `)
2483
Sundong Ahnabb64432019-10-22 13:58:29 +09002484 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002485 copyCmds := apexRule.Args["copy_commands"]
2486
2487 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2488}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002489
Jooyung Han91df2082019-11-20 01:49:42 +09002490func TestApexInVariousPartition(t *testing.T) {
2491 testcases := []struct {
2492 propName, parition, flattenedPartition string
2493 }{
2494 {"", "system", "system_ext"},
2495 {"product_specific: true", "product", "product"},
2496 {"soc_specific: true", "vendor", "vendor"},
2497 {"proprietary: true", "vendor", "vendor"},
2498 {"vendor: true", "vendor", "vendor"},
2499 {"system_ext_specific: true", "system_ext", "system_ext"},
2500 }
2501 for _, tc := range testcases {
2502 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2503 ctx, _ := testApex(t, `
2504 apex {
2505 name: "myapex",
2506 key: "myapex.key",
2507 `+tc.propName+`
2508 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002509
Jooyung Han91df2082019-11-20 01:49:42 +09002510 apex_key {
2511 name: "myapex.key",
2512 public_key: "testkey.avbpubkey",
2513 private_key: "testkey.pem",
2514 }
2515 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002516
Jooyung Han91df2082019-11-20 01:49:42 +09002517 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2518 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2519 actual := apex.installDir.String()
2520 if actual != expected {
2521 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2522 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002523
Jooyung Han91df2082019-11-20 01:49:42 +09002524 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2525 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2526 actual = flattened.installDir.String()
2527 if actual != expected {
2528 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2529 }
2530 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002531 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002532}
Jiyong Park67882562019-03-21 01:11:21 +09002533
Jooyung Han54aca7b2019-11-20 02:26:02 +09002534func TestFileContexts(t *testing.T) {
2535 ctx, _ := testApex(t, `
2536 apex {
2537 name: "myapex",
2538 key: "myapex.key",
2539 }
2540
2541 apex_key {
2542 name: "myapex.key",
2543 public_key: "testkey.avbpubkey",
2544 private_key: "testkey.pem",
2545 }
2546 `)
2547 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2548 apexRule := module.Rule("apexRule")
2549 actual := apexRule.Args["file_contexts"]
2550 expected := "system/sepolicy/apex/myapex-file_contexts"
2551 if actual != expected {
2552 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2553 }
2554
2555 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2556 apex {
2557 name: "myapex",
2558 key: "myapex.key",
2559 file_contexts: "my_own_file_contexts",
2560 }
2561
2562 apex_key {
2563 name: "myapex.key",
2564 public_key: "testkey.avbpubkey",
2565 private_key: "testkey.pem",
2566 }
2567 `, withFiles(map[string][]byte{
2568 "my_own_file_contexts": nil,
2569 }))
2570
2571 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2572 apex {
2573 name: "myapex",
2574 key: "myapex.key",
2575 product_specific: true,
2576 file_contexts: "product_specific_file_contexts",
2577 }
2578
2579 apex_key {
2580 name: "myapex.key",
2581 public_key: "testkey.avbpubkey",
2582 private_key: "testkey.pem",
2583 }
2584 `)
2585
2586 ctx, _ = testApex(t, `
2587 apex {
2588 name: "myapex",
2589 key: "myapex.key",
2590 product_specific: true,
2591 file_contexts: "product_specific_file_contexts",
2592 }
2593
2594 apex_key {
2595 name: "myapex.key",
2596 public_key: "testkey.avbpubkey",
2597 private_key: "testkey.pem",
2598 }
2599 `, withFiles(map[string][]byte{
2600 "product_specific_file_contexts": nil,
2601 }))
2602 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2603 apexRule = module.Rule("apexRule")
2604 actual = apexRule.Args["file_contexts"]
2605 expected = "product_specific_file_contexts"
2606 if actual != expected {
2607 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2608 }
2609
2610 ctx, _ = testApex(t, `
2611 apex {
2612 name: "myapex",
2613 key: "myapex.key",
2614 product_specific: true,
2615 file_contexts: ":my-file-contexts",
2616 }
2617
2618 apex_key {
2619 name: "myapex.key",
2620 public_key: "testkey.avbpubkey",
2621 private_key: "testkey.pem",
2622 }
2623
2624 filegroup {
2625 name: "my-file-contexts",
2626 srcs: ["product_specific_file_contexts"],
2627 }
2628 `, withFiles(map[string][]byte{
2629 "product_specific_file_contexts": nil,
2630 }))
2631 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2632 apexRule = module.Rule("apexRule")
2633 actual = apexRule.Args["file_contexts"]
2634 expected = "product_specific_file_contexts"
2635 if actual != expected {
2636 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2637 }
2638}
2639
Jiyong Park67882562019-03-21 01:11:21 +09002640func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002641 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002642 apex_key {
2643 name: "myapex.key",
2644 public_key: ":my.avbpubkey",
2645 private_key: ":my.pem",
2646 product_specific: true,
2647 }
2648
2649 filegroup {
2650 name: "my.avbpubkey",
2651 srcs: ["testkey2.avbpubkey"],
2652 }
2653
2654 filegroup {
2655 name: "my.pem",
2656 srcs: ["testkey2.pem"],
2657 }
2658 `)
2659
2660 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2661 expected_pubkey := "testkey2.avbpubkey"
2662 actual_pubkey := apex_key.public_key_file.String()
2663 if actual_pubkey != expected_pubkey {
2664 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2665 }
2666 expected_privkey := "testkey2.pem"
2667 actual_privkey := apex_key.private_key_file.String()
2668 if actual_privkey != expected_privkey {
2669 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2670 }
2671}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002672
2673func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002674 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002675 prebuilt_apex {
2676 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002677 arch: {
2678 arm64: {
2679 src: "myapex-arm64.apex",
2680 },
2681 arm: {
2682 src: "myapex-arm.apex",
2683 },
2684 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002685 }
2686 `)
2687
2688 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2689
Jiyong Parkc95714e2019-03-29 14:23:10 +09002690 expectedInput := "myapex-arm64.apex"
2691 if prebuilt.inputApex.String() != expectedInput {
2692 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2693 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002694}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002695
2696func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002697 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002698 prebuilt_apex {
2699 name: "myapex",
2700 src: "myapex-arm.apex",
2701 filename: "notmyapex.apex",
2702 }
2703 `)
2704
2705 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2706
2707 expected := "notmyapex.apex"
2708 if p.installFilename != expected {
2709 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2710 }
2711}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002712
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002713func TestPrebuiltOverrides(t *testing.T) {
2714 ctx, config := testApex(t, `
2715 prebuilt_apex {
2716 name: "myapex.prebuilt",
2717 src: "myapex-arm.apex",
2718 overrides: [
2719 "myapex",
2720 ],
2721 }
2722 `)
2723
2724 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2725
2726 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002727 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002728 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002729 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002730 }
2731}
2732
Roland Levillain630846d2019-06-26 12:48:34 +01002733func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002734 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002735 apex_test {
2736 name: "myapex",
2737 key: "myapex.key",
2738 tests: [
2739 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002740 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002741 ],
2742 }
2743
2744 apex_key {
2745 name: "myapex.key",
2746 public_key: "testkey.avbpubkey",
2747 private_key: "testkey.pem",
2748 }
2749
2750 cc_test {
2751 name: "mytest",
2752 gtest: false,
2753 srcs: ["mytest.cpp"],
2754 relative_install_path: "test",
2755 system_shared_libs: [],
2756 static_executable: true,
2757 stl: "none",
2758 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002759
2760 cc_test {
2761 name: "mytests",
2762 gtest: false,
2763 srcs: [
2764 "mytest1.cpp",
2765 "mytest2.cpp",
2766 "mytest3.cpp",
2767 ],
2768 test_per_src: true,
2769 relative_install_path: "test",
2770 system_shared_libs: [],
2771 static_executable: true,
2772 stl: "none",
2773 }
Roland Levillain630846d2019-06-26 12:48:34 +01002774 `)
2775
Sundong Ahnabb64432019-10-22 13:58:29 +09002776 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002777 copyCmds := apexRule.Args["copy_commands"]
2778
2779 // Ensure that test dep is copied into apex.
2780 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002781
2782 // Ensure that test deps built with `test_per_src` are copied into apex.
2783 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2784 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2785 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002786
2787 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002788 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002789 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2790 name := apexBundle.BaseModuleName()
2791 prefix := "TARGET_"
2792 var builder strings.Builder
2793 data.Custom(&builder, name, prefix, "", data)
2794 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002795 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2796 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2797 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2798 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002799 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002800 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002801 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002802}
2803
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002804func TestInstallExtraFlattenedApexes(t *testing.T) {
2805 ctx, config := testApex(t, `
2806 apex {
2807 name: "myapex",
2808 key: "myapex.key",
2809 }
2810 apex_key {
2811 name: "myapex.key",
2812 public_key: "testkey.avbpubkey",
2813 private_key: "testkey.pem",
2814 }
2815 `, func(fs map[string][]byte, config android.Config) {
2816 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2817 })
2818 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002819 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002820 mk := android.AndroidMkDataForTest(t, config, "", ab)
2821 var builder strings.Builder
2822 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2823 androidMk := builder.String()
2824 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2825}
2826
Jooyung Han5c998b92019-06-27 11:30:33 +09002827func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002828 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002829 apex {
2830 name: "myapex",
2831 key: "myapex.key",
2832 native_shared_libs: ["mylib"],
2833 uses: ["commonapex"],
2834 }
2835
2836 apex {
2837 name: "commonapex",
2838 key: "myapex.key",
2839 native_shared_libs: ["libcommon"],
2840 provide_cpp_shared_libs: true,
2841 }
2842
2843 apex_key {
2844 name: "myapex.key",
2845 public_key: "testkey.avbpubkey",
2846 private_key: "testkey.pem",
2847 }
2848
2849 cc_library {
2850 name: "mylib",
2851 srcs: ["mylib.cpp"],
2852 shared_libs: ["libcommon"],
2853 system_shared_libs: [],
2854 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002855 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002856 }
2857
2858 cc_library {
2859 name: "libcommon",
2860 srcs: ["mylib_common.cpp"],
2861 system_shared_libs: [],
2862 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002863 // TODO: remove //apex_available:platform
2864 apex_available: [
2865 "//apex_available:platform",
2866 "commonapex",
2867 "myapex",
2868 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002869 }
2870 `)
2871
Sundong Ahnabb64432019-10-22 13:58:29 +09002872 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002873 apexRule1 := module1.Rule("apexRule")
2874 copyCmds1 := apexRule1.Args["copy_commands"]
2875
Sundong Ahnabb64432019-10-22 13:58:29 +09002876 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002877 apexRule2 := module2.Rule("apexRule")
2878 copyCmds2 := apexRule2.Args["copy_commands"]
2879
Colin Cross7113d202019-11-20 16:39:12 -08002880 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2881 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002882 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2883 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2884 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2885}
2886
2887func TestApexUsesFailsIfNotProvided(t *testing.T) {
2888 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2889 apex {
2890 name: "myapex",
2891 key: "myapex.key",
2892 uses: ["commonapex"],
2893 }
2894
2895 apex {
2896 name: "commonapex",
2897 key: "myapex.key",
2898 }
2899
2900 apex_key {
2901 name: "myapex.key",
2902 public_key: "testkey.avbpubkey",
2903 private_key: "testkey.pem",
2904 }
2905 `)
2906 testApexError(t, `uses: "commonapex" is not a provider`, `
2907 apex {
2908 name: "myapex",
2909 key: "myapex.key",
2910 uses: ["commonapex"],
2911 }
2912
2913 cc_library {
2914 name: "commonapex",
2915 system_shared_libs: [],
2916 stl: "none",
2917 }
2918
2919 apex_key {
2920 name: "myapex.key",
2921 public_key: "testkey.avbpubkey",
2922 private_key: "testkey.pem",
2923 }
2924 `)
2925}
2926
2927func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2928 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2929 apex {
2930 name: "myapex",
2931 key: "myapex.key",
2932 use_vendor: true,
2933 uses: ["commonapex"],
2934 }
2935
2936 apex {
2937 name: "commonapex",
2938 key: "myapex.key",
2939 provide_cpp_shared_libs: true,
2940 }
2941
2942 apex_key {
2943 name: "myapex.key",
2944 public_key: "testkey.avbpubkey",
2945 private_key: "testkey.pem",
2946 }
Jooyung Handc782442019-11-01 03:14:38 +09002947 `, func(fs map[string][]byte, config android.Config) {
2948 setUseVendorWhitelistForTest(config, []string{"myapex"})
2949 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002950}
2951
Jooyung Hand48f3c32019-08-23 11:18:57 +09002952func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2953 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2954 apex {
2955 name: "myapex",
2956 key: "myapex.key",
2957 native_shared_libs: ["libfoo"],
2958 }
2959
2960 apex_key {
2961 name: "myapex.key",
2962 public_key: "testkey.avbpubkey",
2963 private_key: "testkey.pem",
2964 }
2965
2966 cc_library {
2967 name: "libfoo",
2968 stl: "none",
2969 system_shared_libs: [],
2970 enabled: false,
2971 }
2972 `)
2973 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2974 apex {
2975 name: "myapex",
2976 key: "myapex.key",
2977 java_libs: ["myjar"],
2978 }
2979
2980 apex_key {
2981 name: "myapex.key",
2982 public_key: "testkey.avbpubkey",
2983 private_key: "testkey.pem",
2984 }
2985
2986 java_library {
2987 name: "myjar",
2988 srcs: ["foo/bar/MyClass.java"],
2989 sdk_version: "none",
2990 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002991 enabled: false,
2992 }
2993 `)
2994}
2995
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002996func TestApexWithApps(t *testing.T) {
2997 ctx, _ := testApex(t, `
2998 apex {
2999 name: "myapex",
3000 key: "myapex.key",
3001 apps: [
3002 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003003 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003004 ],
3005 }
3006
3007 apex_key {
3008 name: "myapex.key",
3009 public_key: "testkey.avbpubkey",
3010 private_key: "testkey.pem",
3011 }
3012
3013 android_app {
3014 name: "AppFoo",
3015 srcs: ["foo/bar/MyClass.java"],
3016 sdk_version: "none",
3017 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003018 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003019 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003020 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003021
3022 android_app {
3023 name: "AppFooPriv",
3024 srcs: ["foo/bar/MyClass.java"],
3025 sdk_version: "none",
3026 system_modules: "none",
3027 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003028 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003029 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003030
3031 cc_library_shared {
3032 name: "libjni",
3033 srcs: ["mylib.cpp"],
3034 stl: "none",
3035 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003036 apex_available: [ "myapex" ],
Jiyong Park8be103b2019-11-08 15:53:48 +09003037 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003038 `)
3039
Sundong Ahnabb64432019-10-22 13:58:29 +09003040 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003041 apexRule := module.Rule("apexRule")
3042 copyCmds := apexRule.Args["copy_commands"]
3043
3044 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003045 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003046
3047 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003048 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003049 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003050 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3051 // ... uncompressed
3052 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3053 t.Errorf("jni lib is not uncompressed for AppFoo")
3054 }
3055 // ... and not directly inside the APEX
3056 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003057}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003058
Dario Frenicde2a032019-10-27 00:29:22 +01003059func TestApexWithAppImports(t *testing.T) {
3060 ctx, _ := testApex(t, `
3061 apex {
3062 name: "myapex",
3063 key: "myapex.key",
3064 apps: [
3065 "AppFooPrebuilt",
3066 "AppFooPrivPrebuilt",
3067 ],
3068 }
3069
3070 apex_key {
3071 name: "myapex.key",
3072 public_key: "testkey.avbpubkey",
3073 private_key: "testkey.pem",
3074 }
3075
3076 android_app_import {
3077 name: "AppFooPrebuilt",
3078 apk: "PrebuiltAppFoo.apk",
3079 presigned: true,
3080 dex_preopt: {
3081 enabled: false,
3082 },
3083 }
3084
3085 android_app_import {
3086 name: "AppFooPrivPrebuilt",
3087 apk: "PrebuiltAppFooPriv.apk",
3088 privileged: true,
3089 presigned: true,
3090 dex_preopt: {
3091 enabled: false,
3092 },
3093 }
3094 `)
3095
Sundong Ahnabb64432019-10-22 13:58:29 +09003096 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003097 apexRule := module.Rule("apexRule")
3098 copyCmds := apexRule.Args["copy_commands"]
3099
3100 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3101 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003102}
3103
Dario Freni6f3937c2019-12-20 22:58:03 +00003104func TestApexWithTestHelperApp(t *testing.T) {
3105 ctx, _ := testApex(t, `
3106 apex {
3107 name: "myapex",
3108 key: "myapex.key",
3109 apps: [
3110 "TesterHelpAppFoo",
3111 ],
3112 }
3113
3114 apex_key {
3115 name: "myapex.key",
3116 public_key: "testkey.avbpubkey",
3117 private_key: "testkey.pem",
3118 }
3119
3120 android_test_helper_app {
3121 name: "TesterHelpAppFoo",
3122 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003123 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003124 }
3125
3126 `)
3127
3128 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3129 apexRule := module.Rule("apexRule")
3130 copyCmds := apexRule.Args["copy_commands"]
3131
3132 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3133}
3134
Jooyung Han18020ea2019-11-13 10:50:48 +09003135func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3136 // libfoo's apex_available comes from cc_defaults
3137 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3138 apex {
3139 name: "myapex",
3140 key: "myapex.key",
3141 native_shared_libs: ["libfoo"],
3142 }
3143
3144 apex_key {
3145 name: "myapex.key",
3146 public_key: "testkey.avbpubkey",
3147 private_key: "testkey.pem",
3148 }
3149
3150 apex {
3151 name: "otherapex",
3152 key: "myapex.key",
3153 native_shared_libs: ["libfoo"],
3154 }
3155
3156 cc_defaults {
3157 name: "libfoo-defaults",
3158 apex_available: ["otherapex"],
3159 }
3160
3161 cc_library {
3162 name: "libfoo",
3163 defaults: ["libfoo-defaults"],
3164 stl: "none",
3165 system_shared_libs: [],
3166 }`)
3167}
3168
Jiyong Park127b40b2019-09-30 16:04:35 +09003169func TestApexAvailable(t *testing.T) {
3170 // libfoo is not available to myapex, but only to otherapex
3171 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3172 apex {
3173 name: "myapex",
3174 key: "myapex.key",
3175 native_shared_libs: ["libfoo"],
3176 }
3177
3178 apex_key {
3179 name: "myapex.key",
3180 public_key: "testkey.avbpubkey",
3181 private_key: "testkey.pem",
3182 }
3183
3184 apex {
3185 name: "otherapex",
3186 key: "otherapex.key",
3187 native_shared_libs: ["libfoo"],
3188 }
3189
3190 apex_key {
3191 name: "otherapex.key",
3192 public_key: "testkey.avbpubkey",
3193 private_key: "testkey.pem",
3194 }
3195
3196 cc_library {
3197 name: "libfoo",
3198 stl: "none",
3199 system_shared_libs: [],
3200 apex_available: ["otherapex"],
3201 }`)
3202
3203 // libbar is an indirect dep
3204 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3205 apex {
3206 name: "myapex",
3207 key: "myapex.key",
3208 native_shared_libs: ["libfoo"],
3209 }
3210
3211 apex_key {
3212 name: "myapex.key",
3213 public_key: "testkey.avbpubkey",
3214 private_key: "testkey.pem",
3215 }
3216
3217 apex {
3218 name: "otherapex",
3219 key: "otherapex.key",
3220 native_shared_libs: ["libfoo"],
3221 }
3222
3223 apex_key {
3224 name: "otherapex.key",
3225 public_key: "testkey.avbpubkey",
3226 private_key: "testkey.pem",
3227 }
3228
3229 cc_library {
3230 name: "libfoo",
3231 stl: "none",
3232 shared_libs: ["libbar"],
3233 system_shared_libs: [],
3234 apex_available: ["myapex", "otherapex"],
3235 }
3236
3237 cc_library {
3238 name: "libbar",
3239 stl: "none",
3240 system_shared_libs: [],
3241 apex_available: ["otherapex"],
3242 }`)
3243
3244 testApexError(t, "\"otherapex\" is not a valid module name", `
3245 apex {
3246 name: "myapex",
3247 key: "myapex.key",
3248 native_shared_libs: ["libfoo"],
3249 }
3250
3251 apex_key {
3252 name: "myapex.key",
3253 public_key: "testkey.avbpubkey",
3254 private_key: "testkey.pem",
3255 }
3256
3257 cc_library {
3258 name: "libfoo",
3259 stl: "none",
3260 system_shared_libs: [],
3261 apex_available: ["otherapex"],
3262 }`)
3263
3264 ctx, _ := testApex(t, `
3265 apex {
3266 name: "myapex",
3267 key: "myapex.key",
3268 native_shared_libs: ["libfoo", "libbar"],
3269 }
3270
3271 apex_key {
3272 name: "myapex.key",
3273 public_key: "testkey.avbpubkey",
3274 private_key: "testkey.pem",
3275 }
3276
3277 cc_library {
3278 name: "libfoo",
3279 stl: "none",
3280 system_shared_libs: [],
3281 apex_available: ["myapex"],
3282 }
3283
3284 cc_library {
3285 name: "libbar",
3286 stl: "none",
3287 system_shared_libs: [],
3288 apex_available: ["//apex_available:anyapex"],
3289 }`)
3290
3291 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003292 // TODO(jiyong) the checks for the platform variant are removed because we now create
3293 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3294 // the platform variants are not used from other platform modules. When that is done,
3295 // these checks will be replaced by expecting a specific error message that will be
3296 // emitted when the platform variant is used.
3297 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3298 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3299 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3300 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003301
3302 ctx, _ = testApex(t, `
3303 apex {
3304 name: "myapex",
3305 key: "myapex.key",
3306 }
3307
3308 apex_key {
3309 name: "myapex.key",
3310 public_key: "testkey.avbpubkey",
3311 private_key: "testkey.pem",
3312 }
3313
3314 cc_library {
3315 name: "libfoo",
3316 stl: "none",
3317 system_shared_libs: [],
3318 apex_available: ["//apex_available:platform"],
3319 }`)
3320
3321 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003322 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3323 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003324
3325 ctx, _ = testApex(t, `
3326 apex {
3327 name: "myapex",
3328 key: "myapex.key",
3329 native_shared_libs: ["libfoo"],
3330 }
3331
3332 apex_key {
3333 name: "myapex.key",
3334 public_key: "testkey.avbpubkey",
3335 private_key: "testkey.pem",
3336 }
3337
3338 cc_library {
3339 name: "libfoo",
3340 stl: "none",
3341 system_shared_libs: [],
3342 apex_available: ["myapex"],
3343 static: {
3344 apex_available: ["//apex_available:platform"],
3345 },
3346 }`)
3347
3348 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003349 // TODO(jiyong) the checks for the platform variant are removed because we now create
3350 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3351 // the platform variants are not used from other platform modules. When that is done,
3352 // these checks will be replaced by expecting a specific error message that will be
3353 // emitted when the platform variant is used.
3354 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3355 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3356 // // but the static variant is available to both myapex and the platform
3357 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3358 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003359}
3360
Jiyong Park5d790c32019-11-15 18:40:32 +09003361func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003362 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003363 apex {
3364 name: "myapex",
3365 key: "myapex.key",
3366 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003367 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003368 }
3369
3370 override_apex {
3371 name: "override_myapex",
3372 base: "myapex",
3373 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003374 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003375 }
3376
3377 apex_key {
3378 name: "myapex.key",
3379 public_key: "testkey.avbpubkey",
3380 private_key: "testkey.pem",
3381 }
3382
3383 android_app {
3384 name: "app",
3385 srcs: ["foo/bar/MyClass.java"],
3386 package_name: "foo",
3387 sdk_version: "none",
3388 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003389 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003390 }
3391
3392 override_android_app {
3393 name: "override_app",
3394 base: "app",
3395 package_name: "bar",
3396 }
3397 `)
3398
Jiyong Park317645e2019-12-05 13:20:58 +09003399 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3400 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3401 if originalVariant.GetOverriddenBy() != "" {
3402 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3403 }
3404 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3405 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3406 }
3407
Jiyong Park5d790c32019-11-15 18:40:32 +09003408 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3409 apexRule := module.Rule("apexRule")
3410 copyCmds := apexRule.Args["copy_commands"]
3411
3412 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3413 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003414
3415 apexBundle := module.Module().(*apexBundle)
3416 name := apexBundle.Name()
3417 if name != "override_myapex" {
3418 t.Errorf("name should be \"override_myapex\", but was %q", name)
3419 }
3420
3421 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3422 var builder strings.Builder
3423 data.Custom(&builder, name, "TARGET_", "", data)
3424 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003425 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003426 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3427 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003428 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003429 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003430 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003431 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3432 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003433}
3434
Jooyung Han214bf372019-11-12 13:03:50 +09003435func TestLegacyAndroid10Support(t *testing.T) {
3436 ctx, _ := testApex(t, `
3437 apex {
3438 name: "myapex",
3439 key: "myapex.key",
3440 legacy_android10_support: true,
3441 }
3442
3443 apex_key {
3444 name: "myapex.key",
3445 public_key: "testkey.avbpubkey",
3446 private_key: "testkey.pem",
3447 }
3448 `)
3449
3450 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3451 args := module.Rule("apexRule").Args
3452 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003453 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003454}
3455
Jooyung Han58f26ab2019-12-18 15:34:32 +09003456func TestJavaSDKLibrary(t *testing.T) {
3457 ctx, _ := testApex(t, `
3458 apex {
3459 name: "myapex",
3460 key: "myapex.key",
3461 java_libs: ["foo"],
3462 }
3463
3464 apex_key {
3465 name: "myapex.key",
3466 public_key: "testkey.avbpubkey",
3467 private_key: "testkey.pem",
3468 }
3469
3470 java_sdk_library {
3471 name: "foo",
3472 srcs: ["a.java"],
3473 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003474 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003475 }
3476 `, withFiles(map[string][]byte{
3477 "api/current.txt": nil,
3478 "api/removed.txt": nil,
3479 "api/system-current.txt": nil,
3480 "api/system-removed.txt": nil,
3481 "api/test-current.txt": nil,
3482 "api/test-removed.txt": nil,
3483 }))
3484
3485 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003486 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003487 "javalib/foo.jar",
3488 "etc/permissions/foo.xml",
3489 })
3490 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffine74ac732020-02-06 13:51:46 +00003491 sdkLibrary := ctx.ModuleForTests("foo", "android_common_myapex").Module().(*java.SdkLibrary)
3492 xml := sdkLibrary.XmlPermissionsFileContent()
3493 ensureContains(t, xml, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003494}
3495
atrost6e126252020-01-27 17:01:16 +00003496func TestCompatConfig(t *testing.T) {
3497 ctx, _ := testApex(t, `
3498 apex {
3499 name: "myapex",
3500 key: "myapex.key",
3501 prebuilts: ["myjar-platform-compat-config"],
3502 java_libs: ["myjar"],
3503 }
3504
3505 apex_key {
3506 name: "myapex.key",
3507 public_key: "testkey.avbpubkey",
3508 private_key: "testkey.pem",
3509 }
3510
3511 platform_compat_config {
3512 name: "myjar-platform-compat-config",
3513 src: ":myjar",
3514 }
3515
3516 java_library {
3517 name: "myjar",
3518 srcs: ["foo/bar/MyClass.java"],
3519 sdk_version: "none",
3520 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003521 apex_available: [ "myapex" ],
3522 }
3523 `)
3524 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3525 "etc/compatconfig/myjar-platform-compat-config.xml",
3526 "javalib/myjar.jar",
3527 })
3528}
3529
Jiyong Park479321d2019-12-16 11:47:12 +09003530func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3531 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3532 apex {
3533 name: "myapex",
3534 key: "myapex.key",
3535 java_libs: ["myjar"],
3536 }
3537
3538 apex_key {
3539 name: "myapex.key",
3540 public_key: "testkey.avbpubkey",
3541 private_key: "testkey.pem",
3542 }
3543
3544 java_library {
3545 name: "myjar",
3546 srcs: ["foo/bar/MyClass.java"],
3547 sdk_version: "none",
3548 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003549 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003550 }
3551 `)
3552}
3553
Jiyong Park7afd1072019-12-30 16:56:33 +09003554func TestCarryRequiredModuleNames(t *testing.T) {
3555 ctx, config := testApex(t, `
3556 apex {
3557 name: "myapex",
3558 key: "myapex.key",
3559 native_shared_libs: ["mylib"],
3560 }
3561
3562 apex_key {
3563 name: "myapex.key",
3564 public_key: "testkey.avbpubkey",
3565 private_key: "testkey.pem",
3566 }
3567
3568 cc_library {
3569 name: "mylib",
3570 srcs: ["mylib.cpp"],
3571 system_shared_libs: [],
3572 stl: "none",
3573 required: ["a", "b"],
3574 host_required: ["c", "d"],
3575 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003576 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003577 }
3578 `)
3579
3580 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3581 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3582 name := apexBundle.BaseModuleName()
3583 prefix := "TARGET_"
3584 var builder strings.Builder
3585 data.Custom(&builder, name, prefix, "", data)
3586 androidMk := builder.String()
3587 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3588 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3589 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3590}
3591
Jiyong Park7cd10e32020-01-14 09:22:18 +09003592func TestSymlinksFromApexToSystem(t *testing.T) {
3593 bp := `
3594 apex {
3595 name: "myapex",
3596 key: "myapex.key",
3597 native_shared_libs: ["mylib"],
3598 java_libs: ["myjar"],
3599 }
3600
3601 apex_key {
3602 name: "myapex.key",
3603 public_key: "testkey.avbpubkey",
3604 private_key: "testkey.pem",
3605 }
3606
3607 cc_library {
3608 name: "mylib",
3609 srcs: ["mylib.cpp"],
3610 shared_libs: ["myotherlib"],
3611 system_shared_libs: [],
3612 stl: "none",
3613 apex_available: [
3614 "myapex",
3615 "//apex_available:platform",
3616 ],
3617 }
3618
3619 cc_library {
3620 name: "myotherlib",
3621 srcs: ["mylib.cpp"],
3622 system_shared_libs: [],
3623 stl: "none",
3624 apex_available: [
3625 "myapex",
3626 "//apex_available:platform",
3627 ],
3628 }
3629
3630 java_library {
3631 name: "myjar",
3632 srcs: ["foo/bar/MyClass.java"],
3633 sdk_version: "none",
3634 system_modules: "none",
3635 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003636 apex_available: [
3637 "myapex",
3638 "//apex_available:platform",
3639 ],
3640 }
3641
3642 java_library {
3643 name: "myotherjar",
3644 srcs: ["foo/bar/MyClass.java"],
3645 sdk_version: "none",
3646 system_modules: "none",
3647 apex_available: [
3648 "myapex",
3649 "//apex_available:platform",
3650 ],
3651 }
3652 `
3653
3654 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3655 for _, f := range files {
3656 if f.path == file {
3657 if f.isLink {
3658 t.Errorf("%q is not a real file", file)
3659 }
3660 return
3661 }
3662 }
3663 t.Errorf("%q is not found", file)
3664 }
3665
3666 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3667 for _, f := range files {
3668 if f.path == file {
3669 if !f.isLink {
3670 t.Errorf("%q is not a symlink", file)
3671 }
3672 return
3673 }
3674 }
3675 t.Errorf("%q is not found", file)
3676 }
3677
3678 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003679 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003680 ensureRealfileExists(t, files, "javalib/myjar.jar")
3681 ensureRealfileExists(t, files, "lib64/mylib.so")
3682 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3683
3684 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003685 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003686 ensureRealfileExists(t, files, "javalib/myjar.jar")
3687 ensureRealfileExists(t, files, "lib64/mylib.so")
3688 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
3689}
3690
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003691func TestMain(m *testing.M) {
3692 run := func() int {
3693 setUp()
3694 defer tearDown()
3695
3696 return m.Run()
3697 }
3698
3699 os.Exit(run())
3700}