blob: 207ca05d0e61119424d77f2cb53d68eb71527f6d [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jiyong Park7cd10e32020-01-14 09:22:18 +090094func withUnbundledBuild(fs map[string][]byte, config android.Config) {
95 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
96}
97
Jooyung Han344d5432019-08-23 11:17:39 +090098func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090099 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100
101 bp = bp + `
102 toolchain_library {
103 name: "libcompiler_rt-extras",
104 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
109 toolchain_library {
110 name: "libatomic",
111 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 vendor_available: true,
113 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900114 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900115 }
116
117 toolchain_library {
118 name: "libgcc",
119 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900120 vendor_available: true,
121 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900122 }
123
124 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700125 name: "libgcc_stripped",
126 src: "",
127 vendor_available: true,
128 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900129 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700130 }
131
132 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900133 name: "libclang_rt.builtins-aarch64-android",
134 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900135 vendor_available: true,
136 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900137 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138 }
139
140 toolchain_library {
141 name: "libclang_rt.builtins-arm-android",
142 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 vendor_available: true,
144 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900145 native_bridge_supported: true,
146 }
147
148 toolchain_library {
149 name: "libclang_rt.builtins-x86_64-android",
150 src: "",
151 vendor_available: true,
152 recovery_available: true,
153 native_bridge_supported: true,
154 }
155
156 toolchain_library {
157 name: "libclang_rt.builtins-i686-android",
158 src: "",
159 vendor_available: true,
160 recovery_available: true,
161 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900162 }
163
164 cc_object {
165 name: "crtbegin_so",
166 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900167 vendor_available: true,
168 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900169 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900170 }
171
172 cc_object {
173 name: "crtend_so",
174 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900175 vendor_available: true,
176 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900177 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900178 }
179
Alex Light3d673592019-01-18 14:37:31 -0800180 cc_object {
181 name: "crtbegin_static",
182 stl: "none",
183 }
184
185 cc_object {
186 name: "crtend_android",
187 stl: "none",
188 }
189
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 llndk_library {
191 name: "libc",
192 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900193 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900194 }
195
196 llndk_library {
197 name: "libm",
198 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900199 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900200 }
201
202 llndk_library {
203 name: "libdl",
204 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900205 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900206 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900207
208 filegroup {
209 name: "myapex-file_contexts",
210 srcs: [
211 "system/sepolicy/apex/myapex-file_contexts",
212 ],
213 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800215
Dario Frenicde2a032019-10-27 00:29:22 +0100216 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217
Jooyung Han344d5432019-08-23 11:17:39 +0900218 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900219 "a.java": nil,
220 "PrebuiltAppFoo.apk": nil,
221 "PrebuiltAppFooPriv.apk": nil,
222 "build/make/target/product/security": nil,
223 "apex_manifest.json": nil,
224 "AndroidManifest.xml": nil,
225 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900226 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900227 "system/sepolicy/apex/otherapex-file_contexts": nil,
228 "system/sepolicy/apex/commonapex-file_contexts": nil,
229 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800230 "mylib.cpp": nil,
231 "mylib_common.cpp": nil,
232 "mytest.cpp": nil,
233 "mytest1.cpp": nil,
234 "mytest2.cpp": nil,
235 "mytest3.cpp": nil,
236 "myprebuilt": nil,
237 "my_include": nil,
238 "foo/bar/MyClass.java": nil,
239 "prebuilt.jar": nil,
240 "vendor/foo/devkeys/test.x509.pem": nil,
241 "vendor/foo/devkeys/test.pk8": nil,
242 "testkey.x509.pem": nil,
243 "testkey.pk8": nil,
244 "testkey.override.x509.pem": nil,
245 "testkey.override.pk8": nil,
246 "vendor/foo/devkeys/testkey.avbpubkey": nil,
247 "vendor/foo/devkeys/testkey.pem": nil,
248 "NOTICE": nil,
249 "custom_notice": nil,
250 "testkey2.avbpubkey": nil,
251 "testkey2.pem": nil,
252 "myapex-arm64.apex": nil,
253 "myapex-arm.apex": nil,
254 "frameworks/base/api/current.txt": nil,
255 "framework/aidl/a.aidl": nil,
256 "build/make/core/proguard.flags": nil,
257 "build/make/core/proguard_basic_keeps.flags": nil,
258 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900259 }
260
261 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800262 // The fs now needs to be populated before creating the config, call handlers twice
263 // for now, once to get any fs changes, and later after the config was created to
264 // set product variables or targets.
265 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
266 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900267 }
268
Colin Cross98be1bb2019-12-13 20:41:13 -0800269 config := android.TestArchConfig(buildDir, nil, bp, fs)
270 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
271 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
272 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
273 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
274 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
275 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
276
277 for _, handler := range handlers {
278 // The fs now needs to be populated before creating the config, call handlers twice
279 // for now, earlier to get any fs changes, and now after the config was created to
280 // set product variables or targets.
281 tempFS := map[string][]byte{}
282 handler(tempFS, config)
283 }
284
285 ctx := android.NewTestArchContext()
286 ctx.RegisterModuleType("apex", BundleFactory)
287 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
288 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
289 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
290 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
291 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
292 ctx.RegisterModuleType("override_apex", overrideApexFactory)
293
Jooyung Hana57af4a2020-01-23 05:36:59 +0000294 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
295 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
296
Paul Duffin77980a82019-12-19 16:01:36 +0000297 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800298 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800299 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
300 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000302 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800304 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000305 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000306 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000307 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900308 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800309
Colin Cross98be1bb2019-12-13 20:41:13 -0800310 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800311 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800312
313 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314
Jooyung Han5c998b92019-06-27 11:30:33 +0900315 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316}
317
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700318func setUp() {
319 var err error
320 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700322 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324}
325
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700326func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 os.RemoveAll(buildDir)
328}
329
330// ensure that 'result' contains 'expected'
331func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900332 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 if !strings.Contains(result, expected) {
334 t.Errorf("%q is not found in %q", expected, result)
335 }
336}
337
338// ensures that 'result' does not contain 'notExpected'
339func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900340 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 if strings.Contains(result, notExpected) {
342 t.Errorf("%q is found in %q", notExpected, result)
343 }
344}
345
346func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900347 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 if !android.InList(expected, result) {
349 t.Errorf("%q is not found in %v", expected, result)
350 }
351}
352
353func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900354 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 if android.InList(notExpected, result) {
356 t.Errorf("%q is found in %v", notExpected, result)
357 }
358}
359
Jooyung Hane1633032019-08-01 17:41:43 +0900360func ensureListEmpty(t *testing.T, result []string) {
361 t.Helper()
362 if len(result) > 0 {
363 t.Errorf("%q is expected to be empty", result)
364 }
365}
366
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367// Minimal test
368func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700369 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900370 apex_defaults {
371 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900372 manifest: ":myapex.manifest",
373 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900374 key: "myapex.key",
375 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800376 multilib: {
377 both: {
378 binaries: ["foo",],
379 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900381 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900382 }
383
Jiyong Park30ca9372019-02-07 16:27:23 +0900384 apex {
385 name: "myapex",
386 defaults: ["myapex-defaults"],
387 }
388
Jiyong Park25fc6a92018-11-18 18:02:45 +0900389 apex_key {
390 name: "myapex.key",
391 public_key: "testkey.avbpubkey",
392 private_key: "testkey.pem",
393 }
394
Jiyong Park809bb722019-02-13 21:33:49 +0900395 filegroup {
396 name: "myapex.manifest",
397 srcs: ["apex_manifest.json"],
398 }
399
400 filegroup {
401 name: "myapex.androidmanifest",
402 srcs: ["AndroidManifest.xml"],
403 }
404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 cc_library {
406 name: "mylib",
407 srcs: ["mylib.cpp"],
408 shared_libs: ["mylib2"],
409 system_shared_libs: [],
410 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000411 // TODO: remove //apex_available:platform
412 apex_available: [
413 "//apex_available:platform",
414 "myapex",
415 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900416 }
417
Alex Light3d673592019-01-18 14:37:31 -0800418 cc_binary {
419 name: "foo",
420 srcs: ["mylib.cpp"],
421 compile_multilib: "both",
422 multilib: {
423 lib32: {
424 suffix: "32",
425 },
426 lib64: {
427 suffix: "64",
428 },
429 },
430 symlinks: ["foo_link_"],
431 symlink_preferred_arch: true,
432 system_shared_libs: [],
433 static_executable: true,
434 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000435 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800436 }
437
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 cc_library {
439 name: "mylib2",
440 srcs: ["mylib.cpp"],
441 system_shared_libs: [],
442 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900443 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000444 // TODO: remove //apex_available:platform
445 apex_available: [
446 "//apex_available:platform",
447 "myapex",
448 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900450
451 java_library {
452 name: "myjar",
453 srcs: ["foo/bar/MyClass.java"],
454 sdk_version: "none",
455 system_modules: "none",
456 compile_dex: true,
457 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900458 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900464 }
465
466 java_library {
467 name: "myotherjar",
468 srcs: ["foo/bar/MyClass.java"],
469 sdk_version: "none",
470 system_modules: "none",
471 compile_dex: true,
Jiyong Park0f80c182020-01-31 02:49:53 +0900472 // TODO: remove //apex_available:platform
473 apex_available: [
474 "//apex_available:platform",
475 "myapex",
476 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900478
479 java_library {
480 name: "mysharedjar",
481 srcs: ["foo/bar/MyClass.java"],
482 sdk_version: "none",
483 system_modules: "none",
484 compile_dex: true,
485 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900486 `)
487
Sundong Ahnabb64432019-10-22 13:58:29 +0900488 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900489
490 optFlags := apexRule.Args["opt_flags"]
491 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700492 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900493 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900494
Jiyong Park25fc6a92018-11-18 18:02:45 +0900495 copyCmds := apexRule.Args["copy_commands"]
496
497 // Ensure that main rule creates an output
498 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
499
500 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800501 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900503
504 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800505 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900507
508 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800509 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
510 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900511 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
512 // .. but not for java libs
513 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900514 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800515
Colin Cross7113d202019-11-20 16:39:12 -0800516 // Ensure that the platform variant ends with _shared or _common
517 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
518 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900519 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
520 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900521 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
522
523 // Ensure that dynamic dependency to java libs are not included
524 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800525
526 // Ensure that all symlinks are present.
527 found_foo_link_64 := false
528 found_foo := false
529 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900530 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800531 if strings.HasSuffix(cmd, "bin/foo") {
532 found_foo = true
533 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
534 found_foo_link_64 = true
535 }
536 }
537 }
538 good := found_foo && found_foo_link_64
539 if !good {
540 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
541 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900542
Sundong Ahnabb64432019-10-22 13:58:29 +0900543 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700544 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700545 if len(noticeInputs) != 2 {
546 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900547 }
548 ensureListContains(t, noticeInputs, "NOTICE")
549 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900550
551 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 +0900552 ensureListContains(t, depsInfo, "myjar <- myapex")
553 ensureListContains(t, depsInfo, "mylib <- myapex")
554 ensureListContains(t, depsInfo, "mylib2 <- mylib")
555 ensureListContains(t, depsInfo, "myotherjar <- myjar")
556 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800557}
558
Jooyung Hanf21c7972019-12-16 22:32:06 +0900559func TestDefaults(t *testing.T) {
560 ctx, _ := testApex(t, `
561 apex_defaults {
562 name: "myapex-defaults",
563 key: "myapex.key",
564 prebuilts: ["myetc"],
565 native_shared_libs: ["mylib"],
566 java_libs: ["myjar"],
567 apps: ["AppFoo"],
568 }
569
570 prebuilt_etc {
571 name: "myetc",
572 src: "myprebuilt",
573 }
574
575 apex {
576 name: "myapex",
577 defaults: ["myapex-defaults"],
578 }
579
580 apex_key {
581 name: "myapex.key",
582 public_key: "testkey.avbpubkey",
583 private_key: "testkey.pem",
584 }
585
586 cc_library {
587 name: "mylib",
588 system_shared_libs: [],
589 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000590 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900591 }
592
593 java_library {
594 name: "myjar",
595 srcs: ["foo/bar/MyClass.java"],
596 sdk_version: "none",
597 system_modules: "none",
598 compile_dex: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000599 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900600 }
601
602 android_app {
603 name: "AppFoo",
604 srcs: ["foo/bar/MyClass.java"],
605 sdk_version: "none",
606 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000607 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900608 }
609 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000610 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900611 "etc/myetc",
612 "javalib/myjar.jar",
613 "lib64/mylib.so",
614 "app/AppFoo/AppFoo.apk",
615 })
616}
617
Jooyung Han01a3ee22019-11-02 02:52:25 +0900618func TestApexManifest(t *testing.T) {
619 ctx, _ := testApex(t, `
620 apex {
621 name: "myapex",
622 key: "myapex.key",
623 }
624
625 apex_key {
626 name: "myapex.key",
627 public_key: "testkey.avbpubkey",
628 private_key: "testkey.pem",
629 }
630 `)
631
632 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900633 args := module.Rule("apexRule").Args
634 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
635 t.Error("manifest should be apex_manifest.pb, but " + manifest)
636 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900637}
638
Alex Light5098a612018-11-29 17:12:15 -0800639func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700640 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800641 apex {
642 name: "myapex",
643 key: "myapex.key",
644 payload_type: "zip",
645 native_shared_libs: ["mylib"],
646 }
647
648 apex_key {
649 name: "myapex.key",
650 public_key: "testkey.avbpubkey",
651 private_key: "testkey.pem",
652 }
653
654 cc_library {
655 name: "mylib",
656 srcs: ["mylib.cpp"],
657 shared_libs: ["mylib2"],
658 system_shared_libs: [],
659 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800661 }
662
663 cc_library {
664 name: "mylib2",
665 srcs: ["mylib.cpp"],
666 system_shared_libs: [],
667 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000668 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800669 }
670 `)
671
Sundong Ahnabb64432019-10-22 13:58:29 +0900672 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800673 copyCmds := zipApexRule.Args["copy_commands"]
674
675 // Ensure that main rule creates an output
676 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
677
678 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800679 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800680
681 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800682 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800683
684 // Ensure that both direct and indirect deps are copied into apex
685 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
686 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900687}
688
689func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700690 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900691 apex {
692 name: "myapex",
693 key: "myapex.key",
694 native_shared_libs: ["mylib", "mylib3"],
695 }
696
697 apex_key {
698 name: "myapex.key",
699 public_key: "testkey.avbpubkey",
700 private_key: "testkey.pem",
701 }
702
703 cc_library {
704 name: "mylib",
705 srcs: ["mylib.cpp"],
706 shared_libs: ["mylib2", "mylib3"],
707 system_shared_libs: [],
708 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000709 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710 }
711
712 cc_library {
713 name: "mylib2",
714 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900715 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900716 system_shared_libs: [],
717 stl: "none",
718 stubs: {
719 versions: ["1", "2", "3"],
720 },
721 }
722
723 cc_library {
724 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900725 srcs: ["mylib.cpp"],
726 shared_libs: ["mylib4"],
727 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900728 stl: "none",
729 stubs: {
730 versions: ["10", "11", "12"],
731 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000732 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900733 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900734
735 cc_library {
736 name: "mylib4",
737 srcs: ["mylib.cpp"],
738 system_shared_libs: [],
739 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000740 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900741 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742 `)
743
Sundong Ahnabb64432019-10-22 13:58:29 +0900744 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745 copyCmds := apexRule.Args["copy_commands"]
746
747 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800748 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900749
750 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800751 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900752
753 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800754 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755
Colin Cross7113d202019-11-20 16:39:12 -0800756 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900757
758 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900759 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900760 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900761 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900762
763 // 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 -0800764 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900765 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800766 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900767
768 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900769 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900770 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900771
772 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900773 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900774
Jooyung Hana57af4a2020-01-23 05:36:59 +0000775 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900776 "lib64/mylib.so",
777 "lib64/mylib3.so",
778 "lib64/mylib4.so",
779 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900780}
781
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900782func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700783 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900784 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900785 name: "myapex2",
786 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900787 native_shared_libs: ["mylib"],
788 }
789
790 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900791 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900792 public_key: "testkey.avbpubkey",
793 private_key: "testkey.pem",
794 }
795
796 cc_library {
797 name: "mylib",
798 srcs: ["mylib.cpp"],
799 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900800 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900801 system_shared_libs: [],
802 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000803 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900804 }
805
806 cc_library {
807 name: "libfoo",
808 srcs: ["mylib.cpp"],
809 shared_libs: ["libbar"],
810 system_shared_libs: [],
811 stl: "none",
812 stubs: {
813 versions: ["10", "20", "30"],
814 },
815 }
816
817 cc_library {
818 name: "libbar",
819 srcs: ["mylib.cpp"],
820 system_shared_libs: [],
821 stl: "none",
822 }
823
Jiyong Park678c8812020-02-07 17:25:49 +0900824 cc_library_static {
825 name: "libbaz",
826 srcs: ["mylib.cpp"],
827 system_shared_libs: [],
828 stl: "none",
829 apex_available: [ "myapex2" ],
830 }
831
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900832 `)
833
Jiyong Park83dc74b2020-01-14 18:38:44 +0900834 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900835 copyCmds := apexRule.Args["copy_commands"]
836
837 // Ensure that direct non-stubs dep is always included
838 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
839
840 // Ensure that indirect stubs dep is not included
841 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
842
843 // Ensure that dependency of stubs is not included
844 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
845
Jiyong Park83dc74b2020-01-14 18:38:44 +0900846 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900847
848 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900849 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900850 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900851 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900852
Jiyong Park3ff16992019-12-27 14:11:47 +0900853 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900854
855 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
856 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900857
858 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 +0900859
860 ensureListContains(t, depsInfo, "mylib <- myapex2")
861 ensureListContains(t, depsInfo, "libbaz <- mylib")
862 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900863}
864
Jooyung Hand3639552019-08-09 12:57:43 +0900865func TestApexWithRuntimeLibsDependency(t *testing.T) {
866 /*
867 myapex
868 |
869 v (runtime_libs)
870 mylib ------+------> libfoo [provides stub]
871 |
872 `------> libbar
873 */
874 ctx, _ := testApex(t, `
875 apex {
876 name: "myapex",
877 key: "myapex.key",
878 native_shared_libs: ["mylib"],
879 }
880
881 apex_key {
882 name: "myapex.key",
883 public_key: "testkey.avbpubkey",
884 private_key: "testkey.pem",
885 }
886
887 cc_library {
888 name: "mylib",
889 srcs: ["mylib.cpp"],
890 runtime_libs: ["libfoo", "libbar"],
891 system_shared_libs: [],
892 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000893 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900894 }
895
896 cc_library {
897 name: "libfoo",
898 srcs: ["mylib.cpp"],
899 system_shared_libs: [],
900 stl: "none",
901 stubs: {
902 versions: ["10", "20", "30"],
903 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900904 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900905 }
906
907 cc_library {
908 name: "libbar",
909 srcs: ["mylib.cpp"],
910 system_shared_libs: [],
911 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000912 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900913 }
914
915 `)
916
Sundong Ahnabb64432019-10-22 13:58:29 +0900917 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900918 copyCmds := apexRule.Args["copy_commands"]
919
920 // Ensure that direct non-stubs dep is always included
921 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
922
923 // Ensure that indirect stubs dep is not included
924 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
925
926 // Ensure that runtime_libs dep in included
927 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
928
Sundong Ahnabb64432019-10-22 13:58:29 +0900929 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900930 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
931 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900932
933}
934
Jooyung Han9c80bae2019-08-20 17:30:57 +0900935func TestApexDependencyToLLNDK(t *testing.T) {
936 ctx, _ := testApex(t, `
937 apex {
938 name: "myapex",
939 key: "myapex.key",
940 use_vendor: true,
941 native_shared_libs: ["mylib"],
942 }
943
944 apex_key {
945 name: "myapex.key",
946 public_key: "testkey.avbpubkey",
947 private_key: "testkey.pem",
948 }
949
950 cc_library {
951 name: "mylib",
952 srcs: ["mylib.cpp"],
953 vendor_available: true,
954 shared_libs: ["libbar"],
955 system_shared_libs: [],
956 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000957 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900958 }
959
960 cc_library {
961 name: "libbar",
962 srcs: ["mylib.cpp"],
963 system_shared_libs: [],
964 stl: "none",
965 }
966
967 llndk_library {
968 name: "libbar",
969 symbol_file: "",
970 }
Jooyung Handc782442019-11-01 03:14:38 +0900971 `, func(fs map[string][]byte, config android.Config) {
972 setUseVendorWhitelistForTest(config, []string{"myapex"})
973 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900974
Sundong Ahnabb64432019-10-22 13:58:29 +0900975 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900976 copyCmds := apexRule.Args["copy_commands"]
977
978 // Ensure that LLNDK dep is not included
979 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
980
Sundong Ahnabb64432019-10-22 13:58:29 +0900981 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900982 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900983
984 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900985 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900986
987}
988
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700990 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900991 apex {
992 name: "myapex",
993 key: "myapex.key",
994 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
995 }
996
997 apex_key {
998 name: "myapex.key",
999 public_key: "testkey.avbpubkey",
1000 private_key: "testkey.pem",
1001 }
1002
1003 cc_library {
1004 name: "mylib",
1005 srcs: ["mylib.cpp"],
1006 shared_libs: ["libdl#27"],
1007 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001008 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001009 }
1010
1011 cc_library_shared {
1012 name: "mylib_shared",
1013 srcs: ["mylib.cpp"],
1014 shared_libs: ["libdl#27"],
1015 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001016 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001017 }
1018
1019 cc_library {
1020 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001021 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001022 nocrt: true,
1023 system_shared_libs: [],
1024 stl: "none",
1025 stubs: {
1026 versions: ["27", "28", "29"],
1027 },
1028 }
1029
1030 cc_library {
1031 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001032 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 nocrt: true,
1034 system_shared_libs: [],
1035 stl: "none",
1036 stubs: {
1037 versions: ["27", "28", "29"],
1038 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001039 apex_available: [
1040 "//apex_available:platform",
1041 "myapex"
1042 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001043 }
1044
1045 cc_library {
1046 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001047 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001048 nocrt: true,
1049 system_shared_libs: [],
1050 stl: "none",
1051 stubs: {
1052 versions: ["27", "28", "29"],
1053 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001054 apex_available: [
1055 "//apex_available:platform",
1056 "myapex"
1057 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001058 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001059
1060 cc_library {
1061 name: "libBootstrap",
1062 srcs: ["mylib.cpp"],
1063 stl: "none",
1064 bootstrap: true,
1065 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001066 `)
1067
Sundong Ahnabb64432019-10-22 13:58:29 +09001068 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001069 copyCmds := apexRule.Args["copy_commands"]
1070
1071 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001072 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001073 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1074 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001075
1076 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001077 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001078
Colin Cross7113d202019-11-20 16:39:12 -08001079 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1080 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1081 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001082
1083 // For dependency to libc
1084 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001085 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001086 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001087 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001088 // ... Cflags from stub is correctly exported to mylib
1089 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1090 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1091
1092 // For dependency to libm
1093 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001094 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001095 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001096 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001097 // ... and is not compiling with the stub
1098 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1099 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1100
1101 // For dependency to libdl
1102 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001103 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001104 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001105 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1106 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001107 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001108 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001109 // ... Cflags from stub is correctly exported to mylib
1110 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1111 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001112
1113 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001114 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1115 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1116 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1117 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001118}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001119
1120func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001121 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001122 apex {
1123 name: "myapex",
1124 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001125 native_shared_libs: ["mylib"],
1126 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001127 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001128 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001129 }
1130
1131 apex_key {
1132 name: "myapex.key",
1133 public_key: "testkey.avbpubkey",
1134 private_key: "testkey.pem",
1135 }
1136
1137 prebuilt_etc {
1138 name: "myetc",
1139 src: "myprebuilt",
1140 sub_dir: "foo/bar",
1141 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001142
1143 cc_library {
1144 name: "mylib",
1145 srcs: ["mylib.cpp"],
1146 relative_install_path: "foo/bar",
1147 system_shared_libs: [],
1148 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001149 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001150 }
1151
1152 cc_binary {
1153 name: "mybin",
1154 srcs: ["mylib.cpp"],
1155 relative_install_path: "foo/bar",
1156 system_shared_libs: [],
1157 static_executable: true,
1158 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001159 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001160 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001161 `)
1162
Sundong Ahnabb64432019-10-22 13:58:29 +09001163 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001164 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1165
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001166 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001167 ensureListContains(t, dirs, "etc")
1168 ensureListContains(t, dirs, "etc/foo")
1169 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001170 ensureListContains(t, dirs, "lib64")
1171 ensureListContains(t, dirs, "lib64/foo")
1172 ensureListContains(t, dirs, "lib64/foo/bar")
1173 ensureListContains(t, dirs, "lib")
1174 ensureListContains(t, dirs, "lib/foo")
1175 ensureListContains(t, dirs, "lib/foo/bar")
1176
Jiyong Parkbd13e442019-03-15 18:10:35 +09001177 ensureListContains(t, dirs, "bin")
1178 ensureListContains(t, dirs, "bin/foo")
1179 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001180}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001181
1182func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001183 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001184 apex {
1185 name: "myapex",
1186 key: "myapex.key",
1187 native_shared_libs: ["mylib"],
1188 use_vendor: true,
1189 }
1190
1191 apex_key {
1192 name: "myapex.key",
1193 public_key: "testkey.avbpubkey",
1194 private_key: "testkey.pem",
1195 }
1196
1197 cc_library {
1198 name: "mylib",
1199 srcs: ["mylib.cpp"],
1200 shared_libs: ["mylib2"],
1201 system_shared_libs: [],
1202 vendor_available: true,
1203 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001204 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001205 }
1206
1207 cc_library {
1208 name: "mylib2",
1209 srcs: ["mylib.cpp"],
1210 system_shared_libs: [],
1211 vendor_available: true,
1212 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001213 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001214 }
Jooyung Handc782442019-11-01 03:14:38 +09001215 `, func(fs map[string][]byte, config android.Config) {
1216 setUseVendorWhitelistForTest(config, []string{"myapex"})
1217 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001218
1219 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001220 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001221 for _, implicit := range i.Implicits {
1222 inputsList = append(inputsList, implicit.String())
1223 }
1224 }
1225 inputsString := strings.Join(inputsList, " ")
1226
1227 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001228 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1229 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001230
1231 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001232 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1233 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001234}
Jiyong Park16e91a02018-12-20 18:18:08 +09001235
Jooyung Handc782442019-11-01 03:14:38 +09001236func TestUseVendorRestriction(t *testing.T) {
1237 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1238 apex {
1239 name: "myapex",
1240 key: "myapex.key",
1241 use_vendor: true,
1242 }
1243 apex_key {
1244 name: "myapex.key",
1245 public_key: "testkey.avbpubkey",
1246 private_key: "testkey.pem",
1247 }
1248 `, func(fs map[string][]byte, config android.Config) {
1249 setUseVendorWhitelistForTest(config, []string{""})
1250 })
1251 // no error with whitelist
1252 testApex(t, `
1253 apex {
1254 name: "myapex",
1255 key: "myapex.key",
1256 use_vendor: true,
1257 }
1258 apex_key {
1259 name: "myapex.key",
1260 public_key: "testkey.avbpubkey",
1261 private_key: "testkey.pem",
1262 }
1263 `, func(fs map[string][]byte, config android.Config) {
1264 setUseVendorWhitelistForTest(config, []string{"myapex"})
1265 })
1266}
1267
Jooyung Han5c998b92019-06-27 11:30:33 +09001268func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1269 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1270 apex {
1271 name: "myapex",
1272 key: "myapex.key",
1273 native_shared_libs: ["mylib"],
1274 use_vendor: true,
1275 }
1276
1277 apex_key {
1278 name: "myapex.key",
1279 public_key: "testkey.avbpubkey",
1280 private_key: "testkey.pem",
1281 }
1282
1283 cc_library {
1284 name: "mylib",
1285 srcs: ["mylib.cpp"],
1286 system_shared_libs: [],
1287 stl: "none",
1288 }
1289 `)
1290}
1291
Jiyong Park16e91a02018-12-20 18:18:08 +09001292func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001293 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001294 apex {
1295 name: "myapex",
1296 key: "myapex.key",
1297 native_shared_libs: ["mylib"],
1298 }
1299
1300 apex_key {
1301 name: "myapex.key",
1302 public_key: "testkey.avbpubkey",
1303 private_key: "testkey.pem",
1304 }
1305
1306 cc_library {
1307 name: "mylib",
1308 srcs: ["mylib.cpp"],
1309 system_shared_libs: [],
1310 stl: "none",
1311 stubs: {
1312 versions: ["1", "2", "3"],
1313 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001314 apex_available: [
1315 "//apex_available:platform",
1316 "myapex",
1317 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001318 }
1319
1320 cc_binary {
1321 name: "not_in_apex",
1322 srcs: ["mylib.cpp"],
1323 static_libs: ["mylib"],
1324 static_executable: true,
1325 system_shared_libs: [],
1326 stl: "none",
1327 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001328 `)
1329
Colin Cross7113d202019-11-20 16:39:12 -08001330 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001331
1332 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001333 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001334}
Jiyong Park9335a262018-12-24 11:31:58 +09001335
1336func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001337 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001338 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001339 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001340 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001341 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001342 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001343 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001344 }
1345
1346 cc_library {
1347 name: "mylib",
1348 srcs: ["mylib.cpp"],
1349 system_shared_libs: [],
1350 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001351 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001352 }
1353
1354 apex_key {
1355 name: "myapex.key",
1356 public_key: "testkey.avbpubkey",
1357 private_key: "testkey.pem",
1358 }
1359
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001360 android_app_certificate {
1361 name: "myapex.certificate",
1362 certificate: "testkey",
1363 }
1364
1365 android_app_certificate {
1366 name: "myapex.certificate.override",
1367 certificate: "testkey.override",
1368 }
1369
Jiyong Park9335a262018-12-24 11:31:58 +09001370 `)
1371
1372 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001373 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001374
1375 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1376 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1377 "vendor/foo/devkeys/testkey.avbpubkey")
1378 }
1379 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1380 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1381 "vendor/foo/devkeys/testkey.pem")
1382 }
1383
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001384 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001385 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001386 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001387 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001388 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001389 }
1390}
Jiyong Park58e364a2019-01-19 19:24:06 +09001391
Jooyung Hanf121a652019-12-17 14:30:11 +09001392func TestCertificate(t *testing.T) {
1393 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1394 ctx, _ := testApex(t, `
1395 apex {
1396 name: "myapex",
1397 key: "myapex.key",
1398 }
1399 apex_key {
1400 name: "myapex.key",
1401 public_key: "testkey.avbpubkey",
1402 private_key: "testkey.pem",
1403 }`)
1404 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1405 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1406 if actual := rule.Args["certificates"]; actual != expected {
1407 t.Errorf("certificates should be %q, not %q", expected, actual)
1408 }
1409 })
1410 t.Run("override when unspecified", func(t *testing.T) {
1411 ctx, _ := testApex(t, `
1412 apex {
1413 name: "myapex_keytest",
1414 key: "myapex.key",
1415 file_contexts: ":myapex-file_contexts",
1416 }
1417 apex_key {
1418 name: "myapex.key",
1419 public_key: "testkey.avbpubkey",
1420 private_key: "testkey.pem",
1421 }
1422 android_app_certificate {
1423 name: "myapex.certificate.override",
1424 certificate: "testkey.override",
1425 }`)
1426 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1427 expected := "testkey.override.x509.pem testkey.override.pk8"
1428 if actual := rule.Args["certificates"]; actual != expected {
1429 t.Errorf("certificates should be %q, not %q", expected, actual)
1430 }
1431 })
1432 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1433 ctx, _ := testApex(t, `
1434 apex {
1435 name: "myapex",
1436 key: "myapex.key",
1437 certificate: ":myapex.certificate",
1438 }
1439 apex_key {
1440 name: "myapex.key",
1441 public_key: "testkey.avbpubkey",
1442 private_key: "testkey.pem",
1443 }
1444 android_app_certificate {
1445 name: "myapex.certificate",
1446 certificate: "testkey",
1447 }`)
1448 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1449 expected := "testkey.x509.pem testkey.pk8"
1450 if actual := rule.Args["certificates"]; actual != expected {
1451 t.Errorf("certificates should be %q, not %q", expected, actual)
1452 }
1453 })
1454 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1455 ctx, _ := testApex(t, `
1456 apex {
1457 name: "myapex_keytest",
1458 key: "myapex.key",
1459 file_contexts: ":myapex-file_contexts",
1460 certificate: ":myapex.certificate",
1461 }
1462 apex_key {
1463 name: "myapex.key",
1464 public_key: "testkey.avbpubkey",
1465 private_key: "testkey.pem",
1466 }
1467 android_app_certificate {
1468 name: "myapex.certificate.override",
1469 certificate: "testkey.override",
1470 }`)
1471 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1472 expected := "testkey.override.x509.pem testkey.override.pk8"
1473 if actual := rule.Args["certificates"]; actual != expected {
1474 t.Errorf("certificates should be %q, not %q", expected, actual)
1475 }
1476 })
1477 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1478 ctx, _ := testApex(t, `
1479 apex {
1480 name: "myapex",
1481 key: "myapex.key",
1482 certificate: "testkey",
1483 }
1484 apex_key {
1485 name: "myapex.key",
1486 public_key: "testkey.avbpubkey",
1487 private_key: "testkey.pem",
1488 }`)
1489 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1490 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1491 if actual := rule.Args["certificates"]; actual != expected {
1492 t.Errorf("certificates should be %q, not %q", expected, actual)
1493 }
1494 })
1495 t.Run("override when specified as <name>", func(t *testing.T) {
1496 ctx, _ := testApex(t, `
1497 apex {
1498 name: "myapex_keytest",
1499 key: "myapex.key",
1500 file_contexts: ":myapex-file_contexts",
1501 certificate: "testkey",
1502 }
1503 apex_key {
1504 name: "myapex.key",
1505 public_key: "testkey.avbpubkey",
1506 private_key: "testkey.pem",
1507 }
1508 android_app_certificate {
1509 name: "myapex.certificate.override",
1510 certificate: "testkey.override",
1511 }`)
1512 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1513 expected := "testkey.override.x509.pem testkey.override.pk8"
1514 if actual := rule.Args["certificates"]; actual != expected {
1515 t.Errorf("certificates should be %q, not %q", expected, actual)
1516 }
1517 })
1518}
1519
Jiyong Park58e364a2019-01-19 19:24:06 +09001520func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001521 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001522 apex {
1523 name: "myapex",
1524 key: "myapex.key",
1525 native_shared_libs: ["mylib"],
1526 }
1527
1528 apex {
1529 name: "otherapex",
1530 key: "myapex.key",
1531 native_shared_libs: ["mylib"],
1532 }
1533
1534 apex_key {
1535 name: "myapex.key",
1536 public_key: "testkey.avbpubkey",
1537 private_key: "testkey.pem",
1538 }
1539
1540 cc_library {
1541 name: "mylib",
1542 srcs: ["mylib.cpp"],
1543 system_shared_libs: [],
1544 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001545 // TODO: remove //apex_available:platform
1546 apex_available: [
1547 "//apex_available:platform",
1548 "myapex",
1549 "otherapex",
1550 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001551 }
1552 `)
1553
Jooyung Han6b8459b2019-10-30 08:29:25 +09001554 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001555 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001556 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001557 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1558 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001559
Jooyung Han6b8459b2019-10-30 08:29:25 +09001560 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001561 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001562 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001563 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1564 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001565
Jooyung Han6b8459b2019-10-30 08:29:25 +09001566 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001567 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001568 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001569 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1570 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001571}
Jiyong Park7e636d02019-01-28 16:16:54 +09001572
1573func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001574 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001575 apex {
1576 name: "myapex",
1577 key: "myapex.key",
1578 native_shared_libs: ["mylib"],
1579 }
1580
1581 apex_key {
1582 name: "myapex.key",
1583 public_key: "testkey.avbpubkey",
1584 private_key: "testkey.pem",
1585 }
1586
1587 cc_library_headers {
1588 name: "mylib_headers",
1589 export_include_dirs: ["my_include"],
1590 system_shared_libs: [],
1591 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001592 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001593 }
1594
1595 cc_library {
1596 name: "mylib",
1597 srcs: ["mylib.cpp"],
1598 system_shared_libs: [],
1599 stl: "none",
1600 header_libs: ["mylib_headers"],
1601 export_header_lib_headers: ["mylib_headers"],
1602 stubs: {
1603 versions: ["1", "2", "3"],
1604 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001605 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001606 }
1607
1608 cc_library {
1609 name: "otherlib",
1610 srcs: ["mylib.cpp"],
1611 system_shared_libs: [],
1612 stl: "none",
1613 shared_libs: ["mylib"],
1614 }
1615 `)
1616
Colin Cross7113d202019-11-20 16:39:12 -08001617 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001618
1619 // Ensure that the include path of the header lib is exported to 'otherlib'
1620 ensureContains(t, cFlags, "-Imy_include")
1621}
Alex Light9670d332019-01-29 18:07:33 -08001622
Jiyong Park7cd10e32020-01-14 09:22:18 +09001623type fileInApex struct {
1624 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001625 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001626 isLink bool
1627}
1628
Jooyung Hana57af4a2020-01-23 05:36:59 +00001629func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001630 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001631 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001632 copyCmds := apexRule.Args["copy_commands"]
1633 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001634 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001635 for _, cmd := range strings.Split(copyCmds, "&&") {
1636 cmd = strings.TrimSpace(cmd)
1637 if cmd == "" {
1638 continue
1639 }
1640 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001641 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001642 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001643 switch terms[0] {
1644 case "mkdir":
1645 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001646 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001647 t.Fatal("copyCmds contains invalid cp command", cmd)
1648 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001649 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001650 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001651 isLink = false
1652 case "ln":
1653 if len(terms) != 3 && len(terms) != 4 {
1654 // ln LINK TARGET or ln -s LINK TARGET
1655 t.Fatal("copyCmds contains invalid ln command", cmd)
1656 }
1657 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001658 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001659 isLink = true
1660 default:
1661 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1662 }
1663 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001664 index := strings.Index(dst, imageApexDir)
1665 if index == -1 {
1666 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1667 }
1668 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001669 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001670 }
1671 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001672 return ret
1673}
1674
Jooyung Hana57af4a2020-01-23 05:36:59 +00001675func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1676 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001677 var failed bool
1678 var surplus []string
1679 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001680 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001681 for _, expected := range files {
1682 if matched, _ := path.Match(expected, file.path); matched {
1683 filesMatched[expected] = true
1684 return
1685 }
1686 }
1687 surplus = append(surplus, file.path)
1688 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001689
Jooyung Han31c470b2019-10-18 16:26:59 +09001690 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001691 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001692 t.Log("surplus files", surplus)
1693 failed = true
1694 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001695
1696 if len(files) > len(filesMatched) {
1697 var missing []string
1698 for _, expected := range files {
1699 if !filesMatched[expected] {
1700 missing = append(missing, expected)
1701 }
1702 }
1703 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001704 t.Log("missing files", missing)
1705 failed = true
1706 }
1707 if failed {
1708 t.Fail()
1709 }
1710}
1711
Jooyung Han344d5432019-08-23 11:17:39 +09001712func TestVndkApexCurrent(t *testing.T) {
1713 ctx, _ := testApex(t, `
1714 apex_vndk {
1715 name: "myapex",
1716 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001717 }
1718
1719 apex_key {
1720 name: "myapex.key",
1721 public_key: "testkey.avbpubkey",
1722 private_key: "testkey.pem",
1723 }
1724
1725 cc_library {
1726 name: "libvndk",
1727 srcs: ["mylib.cpp"],
1728 vendor_available: true,
1729 vndk: {
1730 enabled: true,
1731 },
1732 system_shared_libs: [],
1733 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001734 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001735 }
1736
1737 cc_library {
1738 name: "libvndksp",
1739 srcs: ["mylib.cpp"],
1740 vendor_available: true,
1741 vndk: {
1742 enabled: true,
1743 support_system_process: true,
1744 },
1745 system_shared_libs: [],
1746 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001747 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001748 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001749 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001750
Jooyung Hana57af4a2020-01-23 05:36:59 +00001751 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001752 "lib/libvndk.so",
1753 "lib/libvndksp.so",
1754 "lib64/libvndk.so",
1755 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001756 "etc/llndk.libraries.VER.txt",
1757 "etc/vndkcore.libraries.VER.txt",
1758 "etc/vndksp.libraries.VER.txt",
1759 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001760 })
Jooyung Han344d5432019-08-23 11:17:39 +09001761}
1762
1763func TestVndkApexWithPrebuilt(t *testing.T) {
1764 ctx, _ := testApex(t, `
1765 apex_vndk {
1766 name: "myapex",
1767 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001768 }
1769
1770 apex_key {
1771 name: "myapex.key",
1772 public_key: "testkey.avbpubkey",
1773 private_key: "testkey.pem",
1774 }
1775
1776 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001777 name: "libvndk",
1778 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001779 vendor_available: true,
1780 vndk: {
1781 enabled: true,
1782 },
1783 system_shared_libs: [],
1784 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001785 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001786 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001787
1788 cc_prebuilt_library_shared {
1789 name: "libvndk.arm",
1790 srcs: ["libvndk.arm.so"],
1791 vendor_available: true,
1792 vndk: {
1793 enabled: true,
1794 },
1795 enabled: false,
1796 arch: {
1797 arm: {
1798 enabled: true,
1799 },
1800 },
1801 system_shared_libs: [],
1802 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001803 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001804 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001805 `+vndkLibrariesTxtFiles("current"),
1806 withFiles(map[string][]byte{
1807 "libvndk.so": nil,
1808 "libvndk.arm.so": nil,
1809 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001810
Jooyung Hana57af4a2020-01-23 05:36:59 +00001811 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001812 "lib/libvndk.so",
1813 "lib/libvndk.arm.so",
1814 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001815 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001816 })
Jooyung Han344d5432019-08-23 11:17:39 +09001817}
1818
Jooyung Han39edb6c2019-11-06 16:53:07 +09001819func vndkLibrariesTxtFiles(vers ...string) (result string) {
1820 for _, v := range vers {
1821 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001822 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001823 result += `
1824 vndk_libraries_txt {
1825 name: "` + txt + `.libraries.txt",
1826 }
1827 `
1828 }
1829 } else {
1830 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1831 result += `
1832 prebuilt_etc {
1833 name: "` + txt + `.libraries.` + v + `.txt",
1834 src: "dummy.txt",
1835 }
1836 `
1837 }
1838 }
1839 }
1840 return
1841}
1842
Jooyung Han344d5432019-08-23 11:17:39 +09001843func TestVndkApexVersion(t *testing.T) {
1844 ctx, _ := testApex(t, `
1845 apex_vndk {
1846 name: "myapex_v27",
1847 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001848 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001849 vndk_version: "27",
1850 }
1851
1852 apex_key {
1853 name: "myapex.key",
1854 public_key: "testkey.avbpubkey",
1855 private_key: "testkey.pem",
1856 }
1857
Jooyung Han31c470b2019-10-18 16:26:59 +09001858 vndk_prebuilt_shared {
1859 name: "libvndk27",
1860 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001861 vendor_available: true,
1862 vndk: {
1863 enabled: true,
1864 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001865 target_arch: "arm64",
1866 arch: {
1867 arm: {
1868 srcs: ["libvndk27_arm.so"],
1869 },
1870 arm64: {
1871 srcs: ["libvndk27_arm64.so"],
1872 },
1873 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001874 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001875 }
1876
1877 vndk_prebuilt_shared {
1878 name: "libvndk27",
1879 version: "27",
1880 vendor_available: true,
1881 vndk: {
1882 enabled: true,
1883 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001884 target_arch: "x86_64",
1885 arch: {
1886 x86: {
1887 srcs: ["libvndk27_x86.so"],
1888 },
1889 x86_64: {
1890 srcs: ["libvndk27_x86_64.so"],
1891 },
1892 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001893 }
1894 `+vndkLibrariesTxtFiles("27"),
1895 withFiles(map[string][]byte{
1896 "libvndk27_arm.so": nil,
1897 "libvndk27_arm64.so": nil,
1898 "libvndk27_x86.so": nil,
1899 "libvndk27_x86_64.so": nil,
1900 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001901
Jooyung Hana57af4a2020-01-23 05:36:59 +00001902 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001903 "lib/libvndk27_arm.so",
1904 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001905 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001906 })
Jooyung Han344d5432019-08-23 11:17:39 +09001907}
1908
1909func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1910 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1911 apex_vndk {
1912 name: "myapex_v27",
1913 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001914 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001915 vndk_version: "27",
1916 }
1917 apex_vndk {
1918 name: "myapex_v27_other",
1919 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001920 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001921 vndk_version: "27",
1922 }
1923
1924 apex_key {
1925 name: "myapex.key",
1926 public_key: "testkey.avbpubkey",
1927 private_key: "testkey.pem",
1928 }
1929
1930 cc_library {
1931 name: "libvndk",
1932 srcs: ["mylib.cpp"],
1933 vendor_available: true,
1934 vndk: {
1935 enabled: true,
1936 },
1937 system_shared_libs: [],
1938 stl: "none",
1939 }
1940
1941 vndk_prebuilt_shared {
1942 name: "libvndk",
1943 version: "27",
1944 vendor_available: true,
1945 vndk: {
1946 enabled: true,
1947 },
1948 srcs: ["libvndk.so"],
1949 }
1950 `, withFiles(map[string][]byte{
1951 "libvndk.so": nil,
1952 }))
1953}
1954
Jooyung Han90eee022019-10-01 20:02:42 +09001955func TestVndkApexNameRule(t *testing.T) {
1956 ctx, _ := testApex(t, `
1957 apex_vndk {
1958 name: "myapex",
1959 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001960 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001961 }
1962 apex_vndk {
1963 name: "myapex_v28",
1964 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001965 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001966 vndk_version: "28",
1967 }
1968 apex_key {
1969 name: "myapex.key",
1970 public_key: "testkey.avbpubkey",
1971 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001972 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001973
1974 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001975 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001976 actual := proptools.String(bundle.properties.Apex_name)
1977 if !reflect.DeepEqual(actual, expected) {
1978 t.Errorf("Got '%v', expected '%v'", actual, expected)
1979 }
1980 }
1981
1982 assertApexName("com.android.vndk.vVER", "myapex")
1983 assertApexName("com.android.vndk.v28", "myapex_v28")
1984}
1985
Jooyung Han344d5432019-08-23 11:17:39 +09001986func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1987 ctx, _ := testApex(t, `
1988 apex_vndk {
1989 name: "myapex",
1990 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001991 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001992 }
1993
1994 apex_key {
1995 name: "myapex.key",
1996 public_key: "testkey.avbpubkey",
1997 private_key: "testkey.pem",
1998 }
1999
2000 cc_library {
2001 name: "libvndk",
2002 srcs: ["mylib.cpp"],
2003 vendor_available: true,
2004 native_bridge_supported: true,
2005 host_supported: true,
2006 vndk: {
2007 enabled: true,
2008 },
2009 system_shared_libs: [],
2010 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002011 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002012 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002013 `+vndkLibrariesTxtFiles("current"),
2014 withTargets(map[android.OsType][]android.Target{
2015 android.Android: []android.Target{
2016 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2017 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2018 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2019 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2020 },
2021 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002022
Jooyung Hana57af4a2020-01-23 05:36:59 +00002023 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002024 "lib/libvndk.so",
2025 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002026 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002027 })
Jooyung Han344d5432019-08-23 11:17:39 +09002028}
2029
2030func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2031 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2032 apex_vndk {
2033 name: "myapex",
2034 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002035 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002036 native_bridge_supported: true,
2037 }
2038
2039 apex_key {
2040 name: "myapex.key",
2041 public_key: "testkey.avbpubkey",
2042 private_key: "testkey.pem",
2043 }
2044
2045 cc_library {
2046 name: "libvndk",
2047 srcs: ["mylib.cpp"],
2048 vendor_available: true,
2049 native_bridge_supported: true,
2050 host_supported: true,
2051 vndk: {
2052 enabled: true,
2053 },
2054 system_shared_libs: [],
2055 stl: "none",
2056 }
2057 `)
2058}
2059
Jooyung Han31c470b2019-10-18 16:26:59 +09002060func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002061 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002062 apex_vndk {
2063 name: "myapex_v27",
2064 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002065 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002066 vndk_version: "27",
2067 }
2068
2069 apex_key {
2070 name: "myapex.key",
2071 public_key: "testkey.avbpubkey",
2072 private_key: "testkey.pem",
2073 }
2074
2075 vndk_prebuilt_shared {
2076 name: "libvndk27",
2077 version: "27",
2078 target_arch: "arm",
2079 vendor_available: true,
2080 vndk: {
2081 enabled: true,
2082 },
2083 arch: {
2084 arm: {
2085 srcs: ["libvndk27.so"],
2086 }
2087 },
2088 }
2089
2090 vndk_prebuilt_shared {
2091 name: "libvndk27",
2092 version: "27",
2093 target_arch: "arm",
2094 binder32bit: true,
2095 vendor_available: true,
2096 vndk: {
2097 enabled: true,
2098 },
2099 arch: {
2100 arm: {
2101 srcs: ["libvndk27binder32.so"],
2102 }
2103 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002104 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002105 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002106 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002107 withFiles(map[string][]byte{
2108 "libvndk27.so": nil,
2109 "libvndk27binder32.so": nil,
2110 }),
2111 withBinder32bit,
2112 withTargets(map[android.OsType][]android.Target{
2113 android.Android: []android.Target{
2114 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2115 },
2116 }),
2117 )
2118
Jooyung Hana57af4a2020-01-23 05:36:59 +00002119 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002120 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002121 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002122 })
2123}
2124
Jooyung Hane1633032019-08-01 17:41:43 +09002125func TestDependenciesInApexManifest(t *testing.T) {
2126 ctx, _ := testApex(t, `
2127 apex {
2128 name: "myapex_nodep",
2129 key: "myapex.key",
2130 native_shared_libs: ["lib_nodep"],
2131 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002132 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002133 }
2134
2135 apex {
2136 name: "myapex_dep",
2137 key: "myapex.key",
2138 native_shared_libs: ["lib_dep"],
2139 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002140 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002141 }
2142
2143 apex {
2144 name: "myapex_provider",
2145 key: "myapex.key",
2146 native_shared_libs: ["libfoo"],
2147 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002148 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002149 }
2150
2151 apex {
2152 name: "myapex_selfcontained",
2153 key: "myapex.key",
2154 native_shared_libs: ["lib_dep", "libfoo"],
2155 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002156 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002157 }
2158
2159 apex_key {
2160 name: "myapex.key",
2161 public_key: "testkey.avbpubkey",
2162 private_key: "testkey.pem",
2163 }
2164
2165 cc_library {
2166 name: "lib_nodep",
2167 srcs: ["mylib.cpp"],
2168 system_shared_libs: [],
2169 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002170 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002171 }
2172
2173 cc_library {
2174 name: "lib_dep",
2175 srcs: ["mylib.cpp"],
2176 shared_libs: ["libfoo"],
2177 system_shared_libs: [],
2178 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002179 apex_available: [
2180 "myapex_dep",
2181 "myapex_provider",
2182 "myapex_selfcontained",
2183 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002184 }
2185
2186 cc_library {
2187 name: "libfoo",
2188 srcs: ["mytest.cpp"],
2189 stubs: {
2190 versions: ["1"],
2191 },
2192 system_shared_libs: [],
2193 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002194 apex_available: [
2195 "myapex_provider",
2196 "myapex_selfcontained",
2197 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002198 }
2199 `)
2200
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002201 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002202 var provideNativeLibs, requireNativeLibs []string
2203
Sundong Ahnabb64432019-10-22 13:58:29 +09002204 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002205 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2206 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002207 ensureListEmpty(t, provideNativeLibs)
2208 ensureListEmpty(t, requireNativeLibs)
2209
Sundong Ahnabb64432019-10-22 13:58:29 +09002210 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002211 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2212 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002213 ensureListEmpty(t, provideNativeLibs)
2214 ensureListContains(t, requireNativeLibs, "libfoo.so")
2215
Sundong Ahnabb64432019-10-22 13:58:29 +09002216 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002217 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2218 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002219 ensureListContains(t, provideNativeLibs, "libfoo.so")
2220 ensureListEmpty(t, requireNativeLibs)
2221
Sundong Ahnabb64432019-10-22 13:58:29 +09002222 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002223 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2224 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002225 ensureListContains(t, provideNativeLibs, "libfoo.so")
2226 ensureListEmpty(t, requireNativeLibs)
2227}
2228
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002229func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002230 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002231 apex {
2232 name: "myapex",
2233 key: "myapex.key",
2234 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002235 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002236 }
2237
2238 apex_key {
2239 name: "myapex.key",
2240 public_key: "testkey.avbpubkey",
2241 private_key: "testkey.pem",
2242 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002243
2244 cc_library {
2245 name: "mylib",
2246 srcs: ["mylib.cpp"],
2247 system_shared_libs: [],
2248 stl: "none",
2249 apex_available: [
2250 "//apex_available:platform",
2251 "myapex",
2252 ],
2253 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002254 `)
2255
Sundong Ahnabb64432019-10-22 13:58:29 +09002256 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002257 apexManifestRule := module.Rule("apexManifestRule")
2258 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2259 apexRule := module.Rule("apexRule")
2260 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002261
2262 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2263 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2264 name := apexBundle.BaseModuleName()
2265 prefix := "TARGET_"
2266 var builder strings.Builder
2267 data.Custom(&builder, name, prefix, "", data)
2268 androidMk := builder.String()
2269 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2270 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002271}
2272
Alex Light0851b882019-02-07 13:20:53 -08002273func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002274 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002275 apex {
2276 name: "myapex",
2277 key: "myapex.key",
2278 native_shared_libs: ["mylib_common"],
2279 }
2280
2281 apex_key {
2282 name: "myapex.key",
2283 public_key: "testkey.avbpubkey",
2284 private_key: "testkey.pem",
2285 }
2286
2287 cc_library {
2288 name: "mylib_common",
2289 srcs: ["mylib.cpp"],
2290 system_shared_libs: [],
2291 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002292 apex_available: [
2293 "//apex_available:platform",
2294 "myapex",
2295 ],
Alex Light0851b882019-02-07 13:20:53 -08002296 }
2297 `)
2298
Sundong Ahnabb64432019-10-22 13:58:29 +09002299 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002300 apexRule := module.Rule("apexRule")
2301 copyCmds := apexRule.Args["copy_commands"]
2302
2303 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2304 t.Log("Apex was a test apex!")
2305 t.Fail()
2306 }
2307 // Ensure that main rule creates an output
2308 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2309
2310 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002311 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002312
2313 // Ensure that both direct and indirect deps are copied into apex
2314 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2315
Colin Cross7113d202019-11-20 16:39:12 -08002316 // Ensure that the platform variant ends with _shared
2317 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002318
2319 if !android.InAnyApex("mylib_common") {
2320 t.Log("Found mylib_common not in any apex!")
2321 t.Fail()
2322 }
2323}
2324
2325func TestTestApex(t *testing.T) {
2326 if android.InAnyApex("mylib_common_test") {
2327 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!")
2328 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002329 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002330 apex_test {
2331 name: "myapex",
2332 key: "myapex.key",
2333 native_shared_libs: ["mylib_common_test"],
2334 }
2335
2336 apex_key {
2337 name: "myapex.key",
2338 public_key: "testkey.avbpubkey",
2339 private_key: "testkey.pem",
2340 }
2341
2342 cc_library {
2343 name: "mylib_common_test",
2344 srcs: ["mylib.cpp"],
2345 system_shared_libs: [],
2346 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002347 // TODO: remove //apex_available:platform
2348 apex_available: [
2349 "//apex_available:platform",
2350 "myapex",
2351 ],
Alex Light0851b882019-02-07 13:20:53 -08002352 }
2353 `)
2354
Sundong Ahnabb64432019-10-22 13:58:29 +09002355 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002356 apexRule := module.Rule("apexRule")
2357 copyCmds := apexRule.Args["copy_commands"]
2358
2359 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2360 t.Log("Apex was not a test apex!")
2361 t.Fail()
2362 }
2363 // Ensure that main rule creates an output
2364 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2365
2366 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002367 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002368
2369 // Ensure that both direct and indirect deps are copied into apex
2370 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2371
Colin Cross7113d202019-11-20 16:39:12 -08002372 // Ensure that the platform variant ends with _shared
2373 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002374
2375 if android.InAnyApex("mylib_common_test") {
2376 t.Log("Found mylib_common_test in some apex!")
2377 t.Fail()
2378 }
2379}
2380
Alex Light9670d332019-01-29 18:07:33 -08002381func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002382 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002383 apex {
2384 name: "myapex",
2385 key: "myapex.key",
2386 multilib: {
2387 first: {
2388 native_shared_libs: ["mylib_common"],
2389 }
2390 },
2391 target: {
2392 android: {
2393 multilib: {
2394 first: {
2395 native_shared_libs: ["mylib"],
2396 }
2397 }
2398 },
2399 host: {
2400 multilib: {
2401 first: {
2402 native_shared_libs: ["mylib2"],
2403 }
2404 }
2405 }
2406 }
2407 }
2408
2409 apex_key {
2410 name: "myapex.key",
2411 public_key: "testkey.avbpubkey",
2412 private_key: "testkey.pem",
2413 }
2414
2415 cc_library {
2416 name: "mylib",
2417 srcs: ["mylib.cpp"],
2418 system_shared_libs: [],
2419 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002420 // TODO: remove //apex_available:platform
2421 apex_available: [
2422 "//apex_available:platform",
2423 "myapex",
2424 ],
Alex Light9670d332019-01-29 18:07:33 -08002425 }
2426
2427 cc_library {
2428 name: "mylib_common",
2429 srcs: ["mylib.cpp"],
2430 system_shared_libs: [],
2431 stl: "none",
2432 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002433 // TODO: remove //apex_available:platform
2434 apex_available: [
2435 "//apex_available:platform",
2436 "myapex",
2437 ],
Alex Light9670d332019-01-29 18:07:33 -08002438 }
2439
2440 cc_library {
2441 name: "mylib2",
2442 srcs: ["mylib.cpp"],
2443 system_shared_libs: [],
2444 stl: "none",
2445 compile_multilib: "first",
2446 }
2447 `)
2448
Sundong Ahnabb64432019-10-22 13:58:29 +09002449 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002450 copyCmds := apexRule.Args["copy_commands"]
2451
2452 // Ensure that main rule creates an output
2453 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2454
2455 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002456 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2457 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2458 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002459
2460 // Ensure that both direct and indirect deps are copied into apex
2461 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2462 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2463 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2464
Colin Cross7113d202019-11-20 16:39:12 -08002465 // Ensure that the platform variant ends with _shared
2466 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2467 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2468 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002469}
Jiyong Park04480cf2019-02-06 00:16:29 +09002470
2471func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002472 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002473 apex {
2474 name: "myapex",
2475 key: "myapex.key",
2476 binaries: ["myscript"],
2477 }
2478
2479 apex_key {
2480 name: "myapex.key",
2481 public_key: "testkey.avbpubkey",
2482 private_key: "testkey.pem",
2483 }
2484
2485 sh_binary {
2486 name: "myscript",
2487 src: "mylib.cpp",
2488 filename: "myscript.sh",
2489 sub_dir: "script",
2490 }
2491 `)
2492
Sundong Ahnabb64432019-10-22 13:58:29 +09002493 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002494 copyCmds := apexRule.Args["copy_commands"]
2495
2496 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2497}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002498
Jooyung Han91df2082019-11-20 01:49:42 +09002499func TestApexInVariousPartition(t *testing.T) {
2500 testcases := []struct {
2501 propName, parition, flattenedPartition string
2502 }{
2503 {"", "system", "system_ext"},
2504 {"product_specific: true", "product", "product"},
2505 {"soc_specific: true", "vendor", "vendor"},
2506 {"proprietary: true", "vendor", "vendor"},
2507 {"vendor: true", "vendor", "vendor"},
2508 {"system_ext_specific: true", "system_ext", "system_ext"},
2509 }
2510 for _, tc := range testcases {
2511 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2512 ctx, _ := testApex(t, `
2513 apex {
2514 name: "myapex",
2515 key: "myapex.key",
2516 `+tc.propName+`
2517 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002518
Jooyung Han91df2082019-11-20 01:49:42 +09002519 apex_key {
2520 name: "myapex.key",
2521 public_key: "testkey.avbpubkey",
2522 private_key: "testkey.pem",
2523 }
2524 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002525
Jooyung Han91df2082019-11-20 01:49:42 +09002526 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2527 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2528 actual := apex.installDir.String()
2529 if actual != expected {
2530 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2531 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002532
Jooyung Han91df2082019-11-20 01:49:42 +09002533 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2534 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2535 actual = flattened.installDir.String()
2536 if actual != expected {
2537 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2538 }
2539 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002540 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002541}
Jiyong Park67882562019-03-21 01:11:21 +09002542
Jooyung Han54aca7b2019-11-20 02:26:02 +09002543func TestFileContexts(t *testing.T) {
2544 ctx, _ := testApex(t, `
2545 apex {
2546 name: "myapex",
2547 key: "myapex.key",
2548 }
2549
2550 apex_key {
2551 name: "myapex.key",
2552 public_key: "testkey.avbpubkey",
2553 private_key: "testkey.pem",
2554 }
2555 `)
2556 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2557 apexRule := module.Rule("apexRule")
2558 actual := apexRule.Args["file_contexts"]
2559 expected := "system/sepolicy/apex/myapex-file_contexts"
2560 if actual != expected {
2561 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2562 }
2563
2564 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2565 apex {
2566 name: "myapex",
2567 key: "myapex.key",
2568 file_contexts: "my_own_file_contexts",
2569 }
2570
2571 apex_key {
2572 name: "myapex.key",
2573 public_key: "testkey.avbpubkey",
2574 private_key: "testkey.pem",
2575 }
2576 `, withFiles(map[string][]byte{
2577 "my_own_file_contexts": nil,
2578 }))
2579
2580 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2581 apex {
2582 name: "myapex",
2583 key: "myapex.key",
2584 product_specific: true,
2585 file_contexts: "product_specific_file_contexts",
2586 }
2587
2588 apex_key {
2589 name: "myapex.key",
2590 public_key: "testkey.avbpubkey",
2591 private_key: "testkey.pem",
2592 }
2593 `)
2594
2595 ctx, _ = testApex(t, `
2596 apex {
2597 name: "myapex",
2598 key: "myapex.key",
2599 product_specific: true,
2600 file_contexts: "product_specific_file_contexts",
2601 }
2602
2603 apex_key {
2604 name: "myapex.key",
2605 public_key: "testkey.avbpubkey",
2606 private_key: "testkey.pem",
2607 }
2608 `, withFiles(map[string][]byte{
2609 "product_specific_file_contexts": nil,
2610 }))
2611 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2612 apexRule = module.Rule("apexRule")
2613 actual = apexRule.Args["file_contexts"]
2614 expected = "product_specific_file_contexts"
2615 if actual != expected {
2616 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2617 }
2618
2619 ctx, _ = testApex(t, `
2620 apex {
2621 name: "myapex",
2622 key: "myapex.key",
2623 product_specific: true,
2624 file_contexts: ":my-file-contexts",
2625 }
2626
2627 apex_key {
2628 name: "myapex.key",
2629 public_key: "testkey.avbpubkey",
2630 private_key: "testkey.pem",
2631 }
2632
2633 filegroup {
2634 name: "my-file-contexts",
2635 srcs: ["product_specific_file_contexts"],
2636 }
2637 `, withFiles(map[string][]byte{
2638 "product_specific_file_contexts": nil,
2639 }))
2640 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2641 apexRule = module.Rule("apexRule")
2642 actual = apexRule.Args["file_contexts"]
2643 expected = "product_specific_file_contexts"
2644 if actual != expected {
2645 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2646 }
2647}
2648
Jiyong Park67882562019-03-21 01:11:21 +09002649func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002650 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002651 apex_key {
2652 name: "myapex.key",
2653 public_key: ":my.avbpubkey",
2654 private_key: ":my.pem",
2655 product_specific: true,
2656 }
2657
2658 filegroup {
2659 name: "my.avbpubkey",
2660 srcs: ["testkey2.avbpubkey"],
2661 }
2662
2663 filegroup {
2664 name: "my.pem",
2665 srcs: ["testkey2.pem"],
2666 }
2667 `)
2668
2669 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2670 expected_pubkey := "testkey2.avbpubkey"
2671 actual_pubkey := apex_key.public_key_file.String()
2672 if actual_pubkey != expected_pubkey {
2673 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2674 }
2675 expected_privkey := "testkey2.pem"
2676 actual_privkey := apex_key.private_key_file.String()
2677 if actual_privkey != expected_privkey {
2678 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2679 }
2680}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002681
2682func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002683 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002684 prebuilt_apex {
2685 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002686 arch: {
2687 arm64: {
2688 src: "myapex-arm64.apex",
2689 },
2690 arm: {
2691 src: "myapex-arm.apex",
2692 },
2693 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002694 }
2695 `)
2696
2697 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2698
Jiyong Parkc95714e2019-03-29 14:23:10 +09002699 expectedInput := "myapex-arm64.apex"
2700 if prebuilt.inputApex.String() != expectedInput {
2701 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2702 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002703}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002704
2705func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002706 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002707 prebuilt_apex {
2708 name: "myapex",
2709 src: "myapex-arm.apex",
2710 filename: "notmyapex.apex",
2711 }
2712 `)
2713
2714 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2715
2716 expected := "notmyapex.apex"
2717 if p.installFilename != expected {
2718 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2719 }
2720}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002721
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002722func TestPrebuiltOverrides(t *testing.T) {
2723 ctx, config := testApex(t, `
2724 prebuilt_apex {
2725 name: "myapex.prebuilt",
2726 src: "myapex-arm.apex",
2727 overrides: [
2728 "myapex",
2729 ],
2730 }
2731 `)
2732
2733 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2734
2735 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002736 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002737 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002738 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002739 }
2740}
2741
Roland Levillain630846d2019-06-26 12:48:34 +01002742func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002743 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002744 apex_test {
2745 name: "myapex",
2746 key: "myapex.key",
2747 tests: [
2748 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002749 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002750 ],
2751 }
2752
2753 apex_key {
2754 name: "myapex.key",
2755 public_key: "testkey.avbpubkey",
2756 private_key: "testkey.pem",
2757 }
2758
2759 cc_test {
2760 name: "mytest",
2761 gtest: false,
2762 srcs: ["mytest.cpp"],
2763 relative_install_path: "test",
2764 system_shared_libs: [],
2765 static_executable: true,
2766 stl: "none",
2767 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002768
2769 cc_test {
2770 name: "mytests",
2771 gtest: false,
2772 srcs: [
2773 "mytest1.cpp",
2774 "mytest2.cpp",
2775 "mytest3.cpp",
2776 ],
2777 test_per_src: true,
2778 relative_install_path: "test",
2779 system_shared_libs: [],
2780 static_executable: true,
2781 stl: "none",
2782 }
Roland Levillain630846d2019-06-26 12:48:34 +01002783 `)
2784
Sundong Ahnabb64432019-10-22 13:58:29 +09002785 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002786 copyCmds := apexRule.Args["copy_commands"]
2787
2788 // Ensure that test dep is copied into apex.
2789 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002790
2791 // Ensure that test deps built with `test_per_src` are copied into apex.
2792 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2793 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2794 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002795
2796 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002797 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002798 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2799 name := apexBundle.BaseModuleName()
2800 prefix := "TARGET_"
2801 var builder strings.Builder
2802 data.Custom(&builder, name, prefix, "", data)
2803 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002804 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2805 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2806 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2807 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002808 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002809 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002810 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002811}
2812
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002813func TestInstallExtraFlattenedApexes(t *testing.T) {
2814 ctx, config := testApex(t, `
2815 apex {
2816 name: "myapex",
2817 key: "myapex.key",
2818 }
2819 apex_key {
2820 name: "myapex.key",
2821 public_key: "testkey.avbpubkey",
2822 private_key: "testkey.pem",
2823 }
2824 `, func(fs map[string][]byte, config android.Config) {
2825 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2826 })
2827 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002828 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002829 mk := android.AndroidMkDataForTest(t, config, "", ab)
2830 var builder strings.Builder
2831 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2832 androidMk := builder.String()
2833 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2834}
2835
Jooyung Han5c998b92019-06-27 11:30:33 +09002836func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002837 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002838 apex {
2839 name: "myapex",
2840 key: "myapex.key",
2841 native_shared_libs: ["mylib"],
2842 uses: ["commonapex"],
2843 }
2844
2845 apex {
2846 name: "commonapex",
2847 key: "myapex.key",
2848 native_shared_libs: ["libcommon"],
2849 provide_cpp_shared_libs: true,
2850 }
2851
2852 apex_key {
2853 name: "myapex.key",
2854 public_key: "testkey.avbpubkey",
2855 private_key: "testkey.pem",
2856 }
2857
2858 cc_library {
2859 name: "mylib",
2860 srcs: ["mylib.cpp"],
2861 shared_libs: ["libcommon"],
2862 system_shared_libs: [],
2863 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002864 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002865 }
2866
2867 cc_library {
2868 name: "libcommon",
2869 srcs: ["mylib_common.cpp"],
2870 system_shared_libs: [],
2871 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002872 // TODO: remove //apex_available:platform
2873 apex_available: [
2874 "//apex_available:platform",
2875 "commonapex",
2876 "myapex",
2877 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002878 }
2879 `)
2880
Sundong Ahnabb64432019-10-22 13:58:29 +09002881 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002882 apexRule1 := module1.Rule("apexRule")
2883 copyCmds1 := apexRule1.Args["copy_commands"]
2884
Sundong Ahnabb64432019-10-22 13:58:29 +09002885 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002886 apexRule2 := module2.Rule("apexRule")
2887 copyCmds2 := apexRule2.Args["copy_commands"]
2888
Colin Cross7113d202019-11-20 16:39:12 -08002889 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2890 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002891 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2892 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2893 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2894}
2895
2896func TestApexUsesFailsIfNotProvided(t *testing.T) {
2897 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2898 apex {
2899 name: "myapex",
2900 key: "myapex.key",
2901 uses: ["commonapex"],
2902 }
2903
2904 apex {
2905 name: "commonapex",
2906 key: "myapex.key",
2907 }
2908
2909 apex_key {
2910 name: "myapex.key",
2911 public_key: "testkey.avbpubkey",
2912 private_key: "testkey.pem",
2913 }
2914 `)
2915 testApexError(t, `uses: "commonapex" is not a provider`, `
2916 apex {
2917 name: "myapex",
2918 key: "myapex.key",
2919 uses: ["commonapex"],
2920 }
2921
2922 cc_library {
2923 name: "commonapex",
2924 system_shared_libs: [],
2925 stl: "none",
2926 }
2927
2928 apex_key {
2929 name: "myapex.key",
2930 public_key: "testkey.avbpubkey",
2931 private_key: "testkey.pem",
2932 }
2933 `)
2934}
2935
2936func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2937 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
2941 use_vendor: true,
2942 uses: ["commonapex"],
2943 }
2944
2945 apex {
2946 name: "commonapex",
2947 key: "myapex.key",
2948 provide_cpp_shared_libs: true,
2949 }
2950
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
Jooyung Handc782442019-11-01 03:14:38 +09002956 `, func(fs map[string][]byte, config android.Config) {
2957 setUseVendorWhitelistForTest(config, []string{"myapex"})
2958 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002959}
2960
Jooyung Hand48f3c32019-08-23 11:18:57 +09002961func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2962 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2963 apex {
2964 name: "myapex",
2965 key: "myapex.key",
2966 native_shared_libs: ["libfoo"],
2967 }
2968
2969 apex_key {
2970 name: "myapex.key",
2971 public_key: "testkey.avbpubkey",
2972 private_key: "testkey.pem",
2973 }
2974
2975 cc_library {
2976 name: "libfoo",
2977 stl: "none",
2978 system_shared_libs: [],
2979 enabled: false,
2980 }
2981 `)
2982 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2983 apex {
2984 name: "myapex",
2985 key: "myapex.key",
2986 java_libs: ["myjar"],
2987 }
2988
2989 apex_key {
2990 name: "myapex.key",
2991 public_key: "testkey.avbpubkey",
2992 private_key: "testkey.pem",
2993 }
2994
2995 java_library {
2996 name: "myjar",
2997 srcs: ["foo/bar/MyClass.java"],
2998 sdk_version: "none",
2999 system_modules: "none",
3000 compile_dex: true,
3001 enabled: false,
3002 }
3003 `)
3004}
3005
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003006func TestApexWithApps(t *testing.T) {
3007 ctx, _ := testApex(t, `
3008 apex {
3009 name: "myapex",
3010 key: "myapex.key",
3011 apps: [
3012 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003013 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003014 ],
3015 }
3016
3017 apex_key {
3018 name: "myapex.key",
3019 public_key: "testkey.avbpubkey",
3020 private_key: "testkey.pem",
3021 }
3022
3023 android_app {
3024 name: "AppFoo",
3025 srcs: ["foo/bar/MyClass.java"],
3026 sdk_version: "none",
3027 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003028 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003029 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003030 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003031
3032 android_app {
3033 name: "AppFooPriv",
3034 srcs: ["foo/bar/MyClass.java"],
3035 sdk_version: "none",
3036 system_modules: "none",
3037 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003038 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003039 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003040
3041 cc_library_shared {
3042 name: "libjni",
3043 srcs: ["mylib.cpp"],
3044 stl: "none",
3045 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003046 apex_available: [ "myapex" ],
Jiyong Park8be103b2019-11-08 15:53:48 +09003047 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003048 `)
3049
Sundong Ahnabb64432019-10-22 13:58:29 +09003050 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003051 apexRule := module.Rule("apexRule")
3052 copyCmds := apexRule.Args["copy_commands"]
3053
3054 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003055 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003056
3057 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003058 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003059 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003060 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3061 // ... uncompressed
3062 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3063 t.Errorf("jni lib is not uncompressed for AppFoo")
3064 }
3065 // ... and not directly inside the APEX
3066 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003067}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003068
Dario Frenicde2a032019-10-27 00:29:22 +01003069func TestApexWithAppImports(t *testing.T) {
3070 ctx, _ := testApex(t, `
3071 apex {
3072 name: "myapex",
3073 key: "myapex.key",
3074 apps: [
3075 "AppFooPrebuilt",
3076 "AppFooPrivPrebuilt",
3077 ],
3078 }
3079
3080 apex_key {
3081 name: "myapex.key",
3082 public_key: "testkey.avbpubkey",
3083 private_key: "testkey.pem",
3084 }
3085
3086 android_app_import {
3087 name: "AppFooPrebuilt",
3088 apk: "PrebuiltAppFoo.apk",
3089 presigned: true,
3090 dex_preopt: {
3091 enabled: false,
3092 },
3093 }
3094
3095 android_app_import {
3096 name: "AppFooPrivPrebuilt",
3097 apk: "PrebuiltAppFooPriv.apk",
3098 privileged: true,
3099 presigned: true,
3100 dex_preopt: {
3101 enabled: false,
3102 },
3103 }
3104 `)
3105
Sundong Ahnabb64432019-10-22 13:58:29 +09003106 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003107 apexRule := module.Rule("apexRule")
3108 copyCmds := apexRule.Args["copy_commands"]
3109
3110 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3111 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003112}
3113
Dario Freni6f3937c2019-12-20 22:58:03 +00003114func TestApexWithTestHelperApp(t *testing.T) {
3115 ctx, _ := testApex(t, `
3116 apex {
3117 name: "myapex",
3118 key: "myapex.key",
3119 apps: [
3120 "TesterHelpAppFoo",
3121 ],
3122 }
3123
3124 apex_key {
3125 name: "myapex.key",
3126 public_key: "testkey.avbpubkey",
3127 private_key: "testkey.pem",
3128 }
3129
3130 android_test_helper_app {
3131 name: "TesterHelpAppFoo",
3132 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003133 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003134 }
3135
3136 `)
3137
3138 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3139 apexRule := module.Rule("apexRule")
3140 copyCmds := apexRule.Args["copy_commands"]
3141
3142 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3143}
3144
Jooyung Han18020ea2019-11-13 10:50:48 +09003145func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3146 // libfoo's apex_available comes from cc_defaults
3147 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3148 apex {
3149 name: "myapex",
3150 key: "myapex.key",
3151 native_shared_libs: ["libfoo"],
3152 }
3153
3154 apex_key {
3155 name: "myapex.key",
3156 public_key: "testkey.avbpubkey",
3157 private_key: "testkey.pem",
3158 }
3159
3160 apex {
3161 name: "otherapex",
3162 key: "myapex.key",
3163 native_shared_libs: ["libfoo"],
3164 }
3165
3166 cc_defaults {
3167 name: "libfoo-defaults",
3168 apex_available: ["otherapex"],
3169 }
3170
3171 cc_library {
3172 name: "libfoo",
3173 defaults: ["libfoo-defaults"],
3174 stl: "none",
3175 system_shared_libs: [],
3176 }`)
3177}
3178
Jiyong Park127b40b2019-09-30 16:04:35 +09003179func TestApexAvailable(t *testing.T) {
3180 // libfoo is not available to myapex, but only to otherapex
3181 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3182 apex {
3183 name: "myapex",
3184 key: "myapex.key",
3185 native_shared_libs: ["libfoo"],
3186 }
3187
3188 apex_key {
3189 name: "myapex.key",
3190 public_key: "testkey.avbpubkey",
3191 private_key: "testkey.pem",
3192 }
3193
3194 apex {
3195 name: "otherapex",
3196 key: "otherapex.key",
3197 native_shared_libs: ["libfoo"],
3198 }
3199
3200 apex_key {
3201 name: "otherapex.key",
3202 public_key: "testkey.avbpubkey",
3203 private_key: "testkey.pem",
3204 }
3205
3206 cc_library {
3207 name: "libfoo",
3208 stl: "none",
3209 system_shared_libs: [],
3210 apex_available: ["otherapex"],
3211 }`)
3212
3213 // libbar is an indirect dep
3214 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3215 apex {
3216 name: "myapex",
3217 key: "myapex.key",
3218 native_shared_libs: ["libfoo"],
3219 }
3220
3221 apex_key {
3222 name: "myapex.key",
3223 public_key: "testkey.avbpubkey",
3224 private_key: "testkey.pem",
3225 }
3226
3227 apex {
3228 name: "otherapex",
3229 key: "otherapex.key",
3230 native_shared_libs: ["libfoo"],
3231 }
3232
3233 apex_key {
3234 name: "otherapex.key",
3235 public_key: "testkey.avbpubkey",
3236 private_key: "testkey.pem",
3237 }
3238
3239 cc_library {
3240 name: "libfoo",
3241 stl: "none",
3242 shared_libs: ["libbar"],
3243 system_shared_libs: [],
3244 apex_available: ["myapex", "otherapex"],
3245 }
3246
3247 cc_library {
3248 name: "libbar",
3249 stl: "none",
3250 system_shared_libs: [],
3251 apex_available: ["otherapex"],
3252 }`)
3253
3254 testApexError(t, "\"otherapex\" is not a valid module name", `
3255 apex {
3256 name: "myapex",
3257 key: "myapex.key",
3258 native_shared_libs: ["libfoo"],
3259 }
3260
3261 apex_key {
3262 name: "myapex.key",
3263 public_key: "testkey.avbpubkey",
3264 private_key: "testkey.pem",
3265 }
3266
3267 cc_library {
3268 name: "libfoo",
3269 stl: "none",
3270 system_shared_libs: [],
3271 apex_available: ["otherapex"],
3272 }`)
3273
3274 ctx, _ := testApex(t, `
3275 apex {
3276 name: "myapex",
3277 key: "myapex.key",
3278 native_shared_libs: ["libfoo", "libbar"],
3279 }
3280
3281 apex_key {
3282 name: "myapex.key",
3283 public_key: "testkey.avbpubkey",
3284 private_key: "testkey.pem",
3285 }
3286
3287 cc_library {
3288 name: "libfoo",
3289 stl: "none",
3290 system_shared_libs: [],
3291 apex_available: ["myapex"],
3292 }
3293
3294 cc_library {
3295 name: "libbar",
3296 stl: "none",
3297 system_shared_libs: [],
3298 apex_available: ["//apex_available:anyapex"],
3299 }`)
3300
3301 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003302 // TODO(jiyong) the checks for the platform variant are removed because we now create
3303 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3304 // the platform variants are not used from other platform modules. When that is done,
3305 // these checks will be replaced by expecting a specific error message that will be
3306 // emitted when the platform variant is used.
3307 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3308 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3309 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3310 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003311
3312 ctx, _ = testApex(t, `
3313 apex {
3314 name: "myapex",
3315 key: "myapex.key",
3316 }
3317
3318 apex_key {
3319 name: "myapex.key",
3320 public_key: "testkey.avbpubkey",
3321 private_key: "testkey.pem",
3322 }
3323
3324 cc_library {
3325 name: "libfoo",
3326 stl: "none",
3327 system_shared_libs: [],
3328 apex_available: ["//apex_available:platform"],
3329 }`)
3330
3331 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003332 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3333 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003334
3335 ctx, _ = testApex(t, `
3336 apex {
3337 name: "myapex",
3338 key: "myapex.key",
3339 native_shared_libs: ["libfoo"],
3340 }
3341
3342 apex_key {
3343 name: "myapex.key",
3344 public_key: "testkey.avbpubkey",
3345 private_key: "testkey.pem",
3346 }
3347
3348 cc_library {
3349 name: "libfoo",
3350 stl: "none",
3351 system_shared_libs: [],
3352 apex_available: ["myapex"],
3353 static: {
3354 apex_available: ["//apex_available:platform"],
3355 },
3356 }`)
3357
3358 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003359 // TODO(jiyong) the checks for the platform variant are removed because we now create
3360 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3361 // the platform variants are not used from other platform modules. When that is done,
3362 // these checks will be replaced by expecting a specific error message that will be
3363 // emitted when the platform variant is used.
3364 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3365 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3366 // // but the static variant is available to both myapex and the platform
3367 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3368 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003369}
3370
Jiyong Park5d790c32019-11-15 18:40:32 +09003371func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003372 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003373 apex {
3374 name: "myapex",
3375 key: "myapex.key",
3376 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003377 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003378 }
3379
3380 override_apex {
3381 name: "override_myapex",
3382 base: "myapex",
3383 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003384 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003385 }
3386
3387 apex_key {
3388 name: "myapex.key",
3389 public_key: "testkey.avbpubkey",
3390 private_key: "testkey.pem",
3391 }
3392
3393 android_app {
3394 name: "app",
3395 srcs: ["foo/bar/MyClass.java"],
3396 package_name: "foo",
3397 sdk_version: "none",
3398 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003399 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003400 }
3401
3402 override_android_app {
3403 name: "override_app",
3404 base: "app",
3405 package_name: "bar",
3406 }
3407 `)
3408
Jiyong Park317645e2019-12-05 13:20:58 +09003409 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3410 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3411 if originalVariant.GetOverriddenBy() != "" {
3412 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3413 }
3414 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3415 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3416 }
3417
Jiyong Park5d790c32019-11-15 18:40:32 +09003418 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3419 apexRule := module.Rule("apexRule")
3420 copyCmds := apexRule.Args["copy_commands"]
3421
3422 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3423 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003424
3425 apexBundle := module.Module().(*apexBundle)
3426 name := apexBundle.Name()
3427 if name != "override_myapex" {
3428 t.Errorf("name should be \"override_myapex\", but was %q", name)
3429 }
3430
3431 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3432 var builder strings.Builder
3433 data.Custom(&builder, name, "TARGET_", "", data)
3434 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003435 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003436 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3437 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003438 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003439 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003440 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003441 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3442 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003443}
3444
Jooyung Han214bf372019-11-12 13:03:50 +09003445func TestLegacyAndroid10Support(t *testing.T) {
3446 ctx, _ := testApex(t, `
3447 apex {
3448 name: "myapex",
3449 key: "myapex.key",
3450 legacy_android10_support: true,
3451 }
3452
3453 apex_key {
3454 name: "myapex.key",
3455 public_key: "testkey.avbpubkey",
3456 private_key: "testkey.pem",
3457 }
3458 `)
3459
3460 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3461 args := module.Rule("apexRule").Args
3462 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003463 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003464}
3465
Jooyung Han58f26ab2019-12-18 15:34:32 +09003466func TestJavaSDKLibrary(t *testing.T) {
3467 ctx, _ := testApex(t, `
3468 apex {
3469 name: "myapex",
3470 key: "myapex.key",
3471 java_libs: ["foo"],
3472 }
3473
3474 apex_key {
3475 name: "myapex.key",
3476 public_key: "testkey.avbpubkey",
3477 private_key: "testkey.pem",
3478 }
3479
3480 java_sdk_library {
3481 name: "foo",
3482 srcs: ["a.java"],
3483 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003484 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003485 }
3486 `, withFiles(map[string][]byte{
3487 "api/current.txt": nil,
3488 "api/removed.txt": nil,
3489 "api/system-current.txt": nil,
3490 "api/system-removed.txt": nil,
3491 "api/test-current.txt": nil,
3492 "api/test-removed.txt": nil,
3493 }))
3494
3495 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003496 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003497 "javalib/foo.jar",
3498 "etc/permissions/foo.xml",
3499 })
3500 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003501 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3502 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003503}
3504
atrost6e126252020-01-27 17:01:16 +00003505func TestCompatConfig(t *testing.T) {
3506 ctx, _ := testApex(t, `
3507 apex {
3508 name: "myapex",
3509 key: "myapex.key",
3510 prebuilts: ["myjar-platform-compat-config"],
3511 java_libs: ["myjar"],
3512 }
3513
3514 apex_key {
3515 name: "myapex.key",
3516 public_key: "testkey.avbpubkey",
3517 private_key: "testkey.pem",
3518 }
3519
3520 platform_compat_config {
3521 name: "myjar-platform-compat-config",
3522 src: ":myjar",
3523 }
3524
3525 java_library {
3526 name: "myjar",
3527 srcs: ["foo/bar/MyClass.java"],
3528 sdk_version: "none",
3529 system_modules: "none",
3530 compile_dex: true,
3531 apex_available: [ "myapex" ],
3532 }
3533 `)
3534 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3535 "etc/compatconfig/myjar-platform-compat-config.xml",
3536 "javalib/myjar.jar",
3537 })
3538}
3539
Jiyong Park479321d2019-12-16 11:47:12 +09003540func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3541 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3542 apex {
3543 name: "myapex",
3544 key: "myapex.key",
3545 java_libs: ["myjar"],
3546 }
3547
3548 apex_key {
3549 name: "myapex.key",
3550 public_key: "testkey.avbpubkey",
3551 private_key: "testkey.pem",
3552 }
3553
3554 java_library {
3555 name: "myjar",
3556 srcs: ["foo/bar/MyClass.java"],
3557 sdk_version: "none",
3558 system_modules: "none",
3559 }
3560 `)
3561}
3562
Jiyong Park7afd1072019-12-30 16:56:33 +09003563func TestCarryRequiredModuleNames(t *testing.T) {
3564 ctx, config := testApex(t, `
3565 apex {
3566 name: "myapex",
3567 key: "myapex.key",
3568 native_shared_libs: ["mylib"],
3569 }
3570
3571 apex_key {
3572 name: "myapex.key",
3573 public_key: "testkey.avbpubkey",
3574 private_key: "testkey.pem",
3575 }
3576
3577 cc_library {
3578 name: "mylib",
3579 srcs: ["mylib.cpp"],
3580 system_shared_libs: [],
3581 stl: "none",
3582 required: ["a", "b"],
3583 host_required: ["c", "d"],
3584 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003585 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003586 }
3587 `)
3588
3589 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3590 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3591 name := apexBundle.BaseModuleName()
3592 prefix := "TARGET_"
3593 var builder strings.Builder
3594 data.Custom(&builder, name, prefix, "", data)
3595 androidMk := builder.String()
3596 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3597 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3598 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3599}
3600
Jiyong Park7cd10e32020-01-14 09:22:18 +09003601func TestSymlinksFromApexToSystem(t *testing.T) {
3602 bp := `
3603 apex {
3604 name: "myapex",
3605 key: "myapex.key",
3606 native_shared_libs: ["mylib"],
3607 java_libs: ["myjar"],
3608 }
3609
3610 apex_key {
3611 name: "myapex.key",
3612 public_key: "testkey.avbpubkey",
3613 private_key: "testkey.pem",
3614 }
3615
3616 cc_library {
3617 name: "mylib",
3618 srcs: ["mylib.cpp"],
3619 shared_libs: ["myotherlib"],
3620 system_shared_libs: [],
3621 stl: "none",
3622 apex_available: [
3623 "myapex",
3624 "//apex_available:platform",
3625 ],
3626 }
3627
3628 cc_library {
3629 name: "myotherlib",
3630 srcs: ["mylib.cpp"],
3631 system_shared_libs: [],
3632 stl: "none",
3633 apex_available: [
3634 "myapex",
3635 "//apex_available:platform",
3636 ],
3637 }
3638
3639 java_library {
3640 name: "myjar",
3641 srcs: ["foo/bar/MyClass.java"],
3642 sdk_version: "none",
3643 system_modules: "none",
3644 libs: ["myotherjar"],
3645 compile_dex: true,
3646 apex_available: [
3647 "myapex",
3648 "//apex_available:platform",
3649 ],
3650 }
3651
3652 java_library {
3653 name: "myotherjar",
3654 srcs: ["foo/bar/MyClass.java"],
3655 sdk_version: "none",
3656 system_modules: "none",
3657 apex_available: [
3658 "myapex",
3659 "//apex_available:platform",
3660 ],
3661 }
3662 `
3663
3664 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3665 for _, f := range files {
3666 if f.path == file {
3667 if f.isLink {
3668 t.Errorf("%q is not a real file", file)
3669 }
3670 return
3671 }
3672 }
3673 t.Errorf("%q is not found", file)
3674 }
3675
3676 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3677 for _, f := range files {
3678 if f.path == file {
3679 if !f.isLink {
3680 t.Errorf("%q is not a symlink", file)
3681 }
3682 return
3683 }
3684 }
3685 t.Errorf("%q is not found", file)
3686 }
3687
3688 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003689 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003690 ensureRealfileExists(t, files, "javalib/myjar.jar")
3691 ensureRealfileExists(t, files, "lib64/mylib.so")
3692 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3693
3694 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003695 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003696 ensureRealfileExists(t, files, "javalib/myjar.jar")
3697 ensureRealfileExists(t, files, "lib64/mylib.so")
3698 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
3699}
3700
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003701func TestMain(m *testing.M) {
3702 run := func() int {
3703 setUp()
3704 defer tearDown()
3705
3706 return m.Run()
3707 }
3708
3709 os.Exit(run())
3710}