blob: c5b89e6de87582df31745945d7ecc495a486246c [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")
552 ensureListContains(t, depsInfo, "internal myjar")
553 ensureListContains(t, depsInfo, "internal mylib")
554 ensureListContains(t, depsInfo, "internal mylib2")
555 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800556}
557
Jooyung Hanf21c7972019-12-16 22:32:06 +0900558func TestDefaults(t *testing.T) {
559 ctx, _ := testApex(t, `
560 apex_defaults {
561 name: "myapex-defaults",
562 key: "myapex.key",
563 prebuilts: ["myetc"],
564 native_shared_libs: ["mylib"],
565 java_libs: ["myjar"],
566 apps: ["AppFoo"],
567 }
568
569 prebuilt_etc {
570 name: "myetc",
571 src: "myprebuilt",
572 }
573
574 apex {
575 name: "myapex",
576 defaults: ["myapex-defaults"],
577 }
578
579 apex_key {
580 name: "myapex.key",
581 public_key: "testkey.avbpubkey",
582 private_key: "testkey.pem",
583 }
584
585 cc_library {
586 name: "mylib",
587 system_shared_libs: [],
588 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000589 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900590 }
591
592 java_library {
593 name: "myjar",
594 srcs: ["foo/bar/MyClass.java"],
595 sdk_version: "none",
596 system_modules: "none",
597 compile_dex: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000598 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900599 }
600
601 android_app {
602 name: "AppFoo",
603 srcs: ["foo/bar/MyClass.java"],
604 sdk_version: "none",
605 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000606 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900607 }
608 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000609 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610 "etc/myetc",
611 "javalib/myjar.jar",
612 "lib64/mylib.so",
613 "app/AppFoo/AppFoo.apk",
614 })
615}
616
Jooyung Han01a3ee22019-11-02 02:52:25 +0900617func TestApexManifest(t *testing.T) {
618 ctx, _ := testApex(t, `
619 apex {
620 name: "myapex",
621 key: "myapex.key",
622 }
623
624 apex_key {
625 name: "myapex.key",
626 public_key: "testkey.avbpubkey",
627 private_key: "testkey.pem",
628 }
629 `)
630
631 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900632 args := module.Rule("apexRule").Args
633 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
634 t.Error("manifest should be apex_manifest.pb, but " + manifest)
635 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900636}
637
Alex Light5098a612018-11-29 17:12:15 -0800638func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700639 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800640 apex {
641 name: "myapex",
642 key: "myapex.key",
643 payload_type: "zip",
644 native_shared_libs: ["mylib"],
645 }
646
647 apex_key {
648 name: "myapex.key",
649 public_key: "testkey.avbpubkey",
650 private_key: "testkey.pem",
651 }
652
653 cc_library {
654 name: "mylib",
655 srcs: ["mylib.cpp"],
656 shared_libs: ["mylib2"],
657 system_shared_libs: [],
658 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000659 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800660 }
661
662 cc_library {
663 name: "mylib2",
664 srcs: ["mylib.cpp"],
665 system_shared_libs: [],
666 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000667 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800668 }
669 `)
670
Sundong Ahnabb64432019-10-22 13:58:29 +0900671 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800672 copyCmds := zipApexRule.Args["copy_commands"]
673
674 // Ensure that main rule creates an output
675 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
676
677 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800678 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800679
680 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800681 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800682
683 // Ensure that both direct and indirect deps are copied into apex
684 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
685 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686}
687
688func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700689 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900690 apex {
691 name: "myapex",
692 key: "myapex.key",
693 native_shared_libs: ["mylib", "mylib3"],
694 }
695
696 apex_key {
697 name: "myapex.key",
698 public_key: "testkey.avbpubkey",
699 private_key: "testkey.pem",
700 }
701
702 cc_library {
703 name: "mylib",
704 srcs: ["mylib.cpp"],
705 shared_libs: ["mylib2", "mylib3"],
706 system_shared_libs: [],
707 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000708 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709 }
710
711 cc_library {
712 name: "mylib2",
713 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900714 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900715 system_shared_libs: [],
716 stl: "none",
717 stubs: {
718 versions: ["1", "2", "3"],
719 },
720 }
721
722 cc_library {
723 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900724 srcs: ["mylib.cpp"],
725 shared_libs: ["mylib4"],
726 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900727 stl: "none",
728 stubs: {
729 versions: ["10", "11", "12"],
730 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000731 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900732 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900733
734 cc_library {
735 name: "mylib4",
736 srcs: ["mylib.cpp"],
737 system_shared_libs: [],
738 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000739 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900740 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900741 `)
742
Sundong Ahnabb64432019-10-22 13:58:29 +0900743 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900744 copyCmds := apexRule.Args["copy_commands"]
745
746 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800747 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748
749 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800750 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900751
752 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800753 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754
Colin Cross7113d202019-11-20 16:39:12 -0800755 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900756
757 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900758 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900759 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900760 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900761
762 // 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 -0800763 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900764 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800765 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900766
767 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900768 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900769 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900770
771 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900772 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900773
Jooyung Hana57af4a2020-01-23 05:36:59 +0000774 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900775 "lib64/mylib.so",
776 "lib64/mylib3.so",
777 "lib64/mylib4.so",
778 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900779}
780
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700782 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900783 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900784 name: "myapex2",
785 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900786 native_shared_libs: ["mylib"],
787 }
788
789 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900790 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900791 public_key: "testkey.avbpubkey",
792 private_key: "testkey.pem",
793 }
794
795 cc_library {
796 name: "mylib",
797 srcs: ["mylib.cpp"],
798 shared_libs: ["libfoo#10"],
799 system_shared_libs: [],
800 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000801 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802 }
803
804 cc_library {
805 name: "libfoo",
806 srcs: ["mylib.cpp"],
807 shared_libs: ["libbar"],
808 system_shared_libs: [],
809 stl: "none",
810 stubs: {
811 versions: ["10", "20", "30"],
812 },
813 }
814
815 cc_library {
816 name: "libbar",
817 srcs: ["mylib.cpp"],
818 system_shared_libs: [],
819 stl: "none",
820 }
821
822 `)
823
Jiyong Park83dc74b2020-01-14 18:38:44 +0900824 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900825 copyCmds := apexRule.Args["copy_commands"]
826
827 // Ensure that direct non-stubs dep is always included
828 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
829
830 // Ensure that indirect stubs dep is not included
831 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
832
833 // Ensure that dependency of stubs is not included
834 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
835
Jiyong Park83dc74b2020-01-14 18:38:44 +0900836 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900837
838 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900839 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900840 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900841 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900842
Jiyong Park3ff16992019-12-27 14:11:47 +0900843 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900844
845 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
846 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900847
848 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
849 ensureListContains(t, depsInfo, "internal mylib")
850 ensureListContains(t, depsInfo, "external libfoo")
851 ensureListNotContains(t, depsInfo, "internal libfoo")
852 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900853}
854
Jooyung Hand3639552019-08-09 12:57:43 +0900855func TestApexWithRuntimeLibsDependency(t *testing.T) {
856 /*
857 myapex
858 |
859 v (runtime_libs)
860 mylib ------+------> libfoo [provides stub]
861 |
862 `------> libbar
863 */
864 ctx, _ := testApex(t, `
865 apex {
866 name: "myapex",
867 key: "myapex.key",
868 native_shared_libs: ["mylib"],
869 }
870
871 apex_key {
872 name: "myapex.key",
873 public_key: "testkey.avbpubkey",
874 private_key: "testkey.pem",
875 }
876
877 cc_library {
878 name: "mylib",
879 srcs: ["mylib.cpp"],
880 runtime_libs: ["libfoo", "libbar"],
881 system_shared_libs: [],
882 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000883 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900884 }
885
886 cc_library {
887 name: "libfoo",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
891 stubs: {
892 versions: ["10", "20", "30"],
893 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900894 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900895 }
896
897 cc_library {
898 name: "libbar",
899 srcs: ["mylib.cpp"],
900 system_shared_libs: [],
901 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000902 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900903 }
904
905 `)
906
Sundong Ahnabb64432019-10-22 13:58:29 +0900907 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900908 copyCmds := apexRule.Args["copy_commands"]
909
910 // Ensure that direct non-stubs dep is always included
911 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
912
913 // Ensure that indirect stubs dep is not included
914 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
915
916 // Ensure that runtime_libs dep in included
917 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
918
Sundong Ahnabb64432019-10-22 13:58:29 +0900919 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900920 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
921 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900922
923}
924
Jooyung Han9c80bae2019-08-20 17:30:57 +0900925func TestApexDependencyToLLNDK(t *testing.T) {
926 ctx, _ := testApex(t, `
927 apex {
928 name: "myapex",
929 key: "myapex.key",
930 use_vendor: true,
931 native_shared_libs: ["mylib"],
932 }
933
934 apex_key {
935 name: "myapex.key",
936 public_key: "testkey.avbpubkey",
937 private_key: "testkey.pem",
938 }
939
940 cc_library {
941 name: "mylib",
942 srcs: ["mylib.cpp"],
943 vendor_available: true,
944 shared_libs: ["libbar"],
945 system_shared_libs: [],
946 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000947 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900948 }
949
950 cc_library {
951 name: "libbar",
952 srcs: ["mylib.cpp"],
953 system_shared_libs: [],
954 stl: "none",
955 }
956
957 llndk_library {
958 name: "libbar",
959 symbol_file: "",
960 }
Jooyung Handc782442019-11-01 03:14:38 +0900961 `, func(fs map[string][]byte, config android.Config) {
962 setUseVendorWhitelistForTest(config, []string{"myapex"})
963 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900964
Sundong Ahnabb64432019-10-22 13:58:29 +0900965 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900966 copyCmds := apexRule.Args["copy_commands"]
967
968 // Ensure that LLNDK dep is not included
969 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
970
Sundong Ahnabb64432019-10-22 13:58:29 +0900971 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900972 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900973
974 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900975 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900976
977}
978
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700980 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900981 apex {
982 name: "myapex",
983 key: "myapex.key",
984 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
985 }
986
987 apex_key {
988 name: "myapex.key",
989 public_key: "testkey.avbpubkey",
990 private_key: "testkey.pem",
991 }
992
993 cc_library {
994 name: "mylib",
995 srcs: ["mylib.cpp"],
996 shared_libs: ["libdl#27"],
997 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000998 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900999 }
1000
1001 cc_library_shared {
1002 name: "mylib_shared",
1003 srcs: ["mylib.cpp"],
1004 shared_libs: ["libdl#27"],
1005 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001006 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001007 }
1008
1009 cc_library {
1010 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001011 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001012 nocrt: true,
1013 system_shared_libs: [],
1014 stl: "none",
1015 stubs: {
1016 versions: ["27", "28", "29"],
1017 },
1018 }
1019
1020 cc_library {
1021 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001022 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 nocrt: true,
1024 system_shared_libs: [],
1025 stl: "none",
1026 stubs: {
1027 versions: ["27", "28", "29"],
1028 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001029 apex_available: [
1030 "//apex_available:platform",
1031 "myapex"
1032 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 }
1034
1035 cc_library {
1036 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001037 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001038 nocrt: true,
1039 system_shared_libs: [],
1040 stl: "none",
1041 stubs: {
1042 versions: ["27", "28", "29"],
1043 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001044 apex_available: [
1045 "//apex_available:platform",
1046 "myapex"
1047 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001048 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001049
1050 cc_library {
1051 name: "libBootstrap",
1052 srcs: ["mylib.cpp"],
1053 stl: "none",
1054 bootstrap: true,
1055 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001056 `)
1057
Sundong Ahnabb64432019-10-22 13:58:29 +09001058 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001059 copyCmds := apexRule.Args["copy_commands"]
1060
1061 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001062 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001063 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1064 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001065
1066 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001067 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001068
Colin Cross7113d202019-11-20 16:39:12 -08001069 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1070 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1071 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001072
1073 // For dependency to libc
1074 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001075 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001076 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001077 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001078 // ... Cflags from stub is correctly exported to mylib
1079 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1080 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1081
1082 // For dependency to libm
1083 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001084 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001085 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001086 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001087 // ... and is not compiling with the stub
1088 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1089 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1090
1091 // For dependency to libdl
1092 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001093 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001094 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001095 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1096 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001097 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001098 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001099 // ... Cflags from stub is correctly exported to mylib
1100 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1101 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001102
1103 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001104 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1105 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1106 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1107 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001108}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001109
1110func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001111 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001112 apex {
1113 name: "myapex",
1114 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001115 native_shared_libs: ["mylib"],
1116 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001117 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001118 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001119 }
1120
1121 apex_key {
1122 name: "myapex.key",
1123 public_key: "testkey.avbpubkey",
1124 private_key: "testkey.pem",
1125 }
1126
1127 prebuilt_etc {
1128 name: "myetc",
1129 src: "myprebuilt",
1130 sub_dir: "foo/bar",
1131 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001132
1133 cc_library {
1134 name: "mylib",
1135 srcs: ["mylib.cpp"],
1136 relative_install_path: "foo/bar",
1137 system_shared_libs: [],
1138 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001139 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001140 }
1141
1142 cc_binary {
1143 name: "mybin",
1144 srcs: ["mylib.cpp"],
1145 relative_install_path: "foo/bar",
1146 system_shared_libs: [],
1147 static_executable: true,
1148 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001149 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001150 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001151 `)
1152
Sundong Ahnabb64432019-10-22 13:58:29 +09001153 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001154 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1155
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001156 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001157 ensureListContains(t, dirs, "etc")
1158 ensureListContains(t, dirs, "etc/foo")
1159 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001160 ensureListContains(t, dirs, "lib64")
1161 ensureListContains(t, dirs, "lib64/foo")
1162 ensureListContains(t, dirs, "lib64/foo/bar")
1163 ensureListContains(t, dirs, "lib")
1164 ensureListContains(t, dirs, "lib/foo")
1165 ensureListContains(t, dirs, "lib/foo/bar")
1166
Jiyong Parkbd13e442019-03-15 18:10:35 +09001167 ensureListContains(t, dirs, "bin")
1168 ensureListContains(t, dirs, "bin/foo")
1169 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001170}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001171
1172func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001173 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001174 apex {
1175 name: "myapex",
1176 key: "myapex.key",
1177 native_shared_libs: ["mylib"],
1178 use_vendor: true,
1179 }
1180
1181 apex_key {
1182 name: "myapex.key",
1183 public_key: "testkey.avbpubkey",
1184 private_key: "testkey.pem",
1185 }
1186
1187 cc_library {
1188 name: "mylib",
1189 srcs: ["mylib.cpp"],
1190 shared_libs: ["mylib2"],
1191 system_shared_libs: [],
1192 vendor_available: true,
1193 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001194 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001195 }
1196
1197 cc_library {
1198 name: "mylib2",
1199 srcs: ["mylib.cpp"],
1200 system_shared_libs: [],
1201 vendor_available: true,
1202 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001203 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001204 }
Jooyung Handc782442019-11-01 03:14:38 +09001205 `, func(fs map[string][]byte, config android.Config) {
1206 setUseVendorWhitelistForTest(config, []string{"myapex"})
1207 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001208
1209 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001210 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001211 for _, implicit := range i.Implicits {
1212 inputsList = append(inputsList, implicit.String())
1213 }
1214 }
1215 inputsString := strings.Join(inputsList, " ")
1216
1217 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001218 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1219 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001220
1221 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001222 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1223 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001224}
Jiyong Park16e91a02018-12-20 18:18:08 +09001225
Jooyung Handc782442019-11-01 03:14:38 +09001226func TestUseVendorRestriction(t *testing.T) {
1227 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1228 apex {
1229 name: "myapex",
1230 key: "myapex.key",
1231 use_vendor: true,
1232 }
1233 apex_key {
1234 name: "myapex.key",
1235 public_key: "testkey.avbpubkey",
1236 private_key: "testkey.pem",
1237 }
1238 `, func(fs map[string][]byte, config android.Config) {
1239 setUseVendorWhitelistForTest(config, []string{""})
1240 })
1241 // no error with whitelist
1242 testApex(t, `
1243 apex {
1244 name: "myapex",
1245 key: "myapex.key",
1246 use_vendor: true,
1247 }
1248 apex_key {
1249 name: "myapex.key",
1250 public_key: "testkey.avbpubkey",
1251 private_key: "testkey.pem",
1252 }
1253 `, func(fs map[string][]byte, config android.Config) {
1254 setUseVendorWhitelistForTest(config, []string{"myapex"})
1255 })
1256}
1257
Jooyung Han5c998b92019-06-27 11:30:33 +09001258func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1259 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1260 apex {
1261 name: "myapex",
1262 key: "myapex.key",
1263 native_shared_libs: ["mylib"],
1264 use_vendor: true,
1265 }
1266
1267 apex_key {
1268 name: "myapex.key",
1269 public_key: "testkey.avbpubkey",
1270 private_key: "testkey.pem",
1271 }
1272
1273 cc_library {
1274 name: "mylib",
1275 srcs: ["mylib.cpp"],
1276 system_shared_libs: [],
1277 stl: "none",
1278 }
1279 `)
1280}
1281
Jiyong Park16e91a02018-12-20 18:18:08 +09001282func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001283 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001284 apex {
1285 name: "myapex",
1286 key: "myapex.key",
1287 native_shared_libs: ["mylib"],
1288 }
1289
1290 apex_key {
1291 name: "myapex.key",
1292 public_key: "testkey.avbpubkey",
1293 private_key: "testkey.pem",
1294 }
1295
1296 cc_library {
1297 name: "mylib",
1298 srcs: ["mylib.cpp"],
1299 system_shared_libs: [],
1300 stl: "none",
1301 stubs: {
1302 versions: ["1", "2", "3"],
1303 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001304 apex_available: [
1305 "//apex_available:platform",
1306 "myapex",
1307 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001308 }
1309
1310 cc_binary {
1311 name: "not_in_apex",
1312 srcs: ["mylib.cpp"],
1313 static_libs: ["mylib"],
1314 static_executable: true,
1315 system_shared_libs: [],
1316 stl: "none",
1317 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001318 `)
1319
Colin Cross7113d202019-11-20 16:39:12 -08001320 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001321
1322 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001323 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001324}
Jiyong Park9335a262018-12-24 11:31:58 +09001325
1326func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001327 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001328 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001329 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001330 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001331 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001332 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001333 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001334 }
1335
1336 cc_library {
1337 name: "mylib",
1338 srcs: ["mylib.cpp"],
1339 system_shared_libs: [],
1340 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001341 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001342 }
1343
1344 apex_key {
1345 name: "myapex.key",
1346 public_key: "testkey.avbpubkey",
1347 private_key: "testkey.pem",
1348 }
1349
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001350 android_app_certificate {
1351 name: "myapex.certificate",
1352 certificate: "testkey",
1353 }
1354
1355 android_app_certificate {
1356 name: "myapex.certificate.override",
1357 certificate: "testkey.override",
1358 }
1359
Jiyong Park9335a262018-12-24 11:31:58 +09001360 `)
1361
1362 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001363 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001364
1365 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1366 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1367 "vendor/foo/devkeys/testkey.avbpubkey")
1368 }
1369 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1370 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1371 "vendor/foo/devkeys/testkey.pem")
1372 }
1373
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001374 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001375 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001376 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001377 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001378 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001379 }
1380}
Jiyong Park58e364a2019-01-19 19:24:06 +09001381
Jooyung Hanf121a652019-12-17 14:30:11 +09001382func TestCertificate(t *testing.T) {
1383 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1384 ctx, _ := testApex(t, `
1385 apex {
1386 name: "myapex",
1387 key: "myapex.key",
1388 }
1389 apex_key {
1390 name: "myapex.key",
1391 public_key: "testkey.avbpubkey",
1392 private_key: "testkey.pem",
1393 }`)
1394 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1395 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1396 if actual := rule.Args["certificates"]; actual != expected {
1397 t.Errorf("certificates should be %q, not %q", expected, actual)
1398 }
1399 })
1400 t.Run("override when unspecified", func(t *testing.T) {
1401 ctx, _ := testApex(t, `
1402 apex {
1403 name: "myapex_keytest",
1404 key: "myapex.key",
1405 file_contexts: ":myapex-file_contexts",
1406 }
1407 apex_key {
1408 name: "myapex.key",
1409 public_key: "testkey.avbpubkey",
1410 private_key: "testkey.pem",
1411 }
1412 android_app_certificate {
1413 name: "myapex.certificate.override",
1414 certificate: "testkey.override",
1415 }`)
1416 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1417 expected := "testkey.override.x509.pem testkey.override.pk8"
1418 if actual := rule.Args["certificates"]; actual != expected {
1419 t.Errorf("certificates should be %q, not %q", expected, actual)
1420 }
1421 })
1422 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1423 ctx, _ := testApex(t, `
1424 apex {
1425 name: "myapex",
1426 key: "myapex.key",
1427 certificate: ":myapex.certificate",
1428 }
1429 apex_key {
1430 name: "myapex.key",
1431 public_key: "testkey.avbpubkey",
1432 private_key: "testkey.pem",
1433 }
1434 android_app_certificate {
1435 name: "myapex.certificate",
1436 certificate: "testkey",
1437 }`)
1438 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1439 expected := "testkey.x509.pem testkey.pk8"
1440 if actual := rule.Args["certificates"]; actual != expected {
1441 t.Errorf("certificates should be %q, not %q", expected, actual)
1442 }
1443 })
1444 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1445 ctx, _ := testApex(t, `
1446 apex {
1447 name: "myapex_keytest",
1448 key: "myapex.key",
1449 file_contexts: ":myapex-file_contexts",
1450 certificate: ":myapex.certificate",
1451 }
1452 apex_key {
1453 name: "myapex.key",
1454 public_key: "testkey.avbpubkey",
1455 private_key: "testkey.pem",
1456 }
1457 android_app_certificate {
1458 name: "myapex.certificate.override",
1459 certificate: "testkey.override",
1460 }`)
1461 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1462 expected := "testkey.override.x509.pem testkey.override.pk8"
1463 if actual := rule.Args["certificates"]; actual != expected {
1464 t.Errorf("certificates should be %q, not %q", expected, actual)
1465 }
1466 })
1467 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1468 ctx, _ := testApex(t, `
1469 apex {
1470 name: "myapex",
1471 key: "myapex.key",
1472 certificate: "testkey",
1473 }
1474 apex_key {
1475 name: "myapex.key",
1476 public_key: "testkey.avbpubkey",
1477 private_key: "testkey.pem",
1478 }`)
1479 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1480 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1481 if actual := rule.Args["certificates"]; actual != expected {
1482 t.Errorf("certificates should be %q, not %q", expected, actual)
1483 }
1484 })
1485 t.Run("override when specified as <name>", func(t *testing.T) {
1486 ctx, _ := testApex(t, `
1487 apex {
1488 name: "myapex_keytest",
1489 key: "myapex.key",
1490 file_contexts: ":myapex-file_contexts",
1491 certificate: "testkey",
1492 }
1493 apex_key {
1494 name: "myapex.key",
1495 public_key: "testkey.avbpubkey",
1496 private_key: "testkey.pem",
1497 }
1498 android_app_certificate {
1499 name: "myapex.certificate.override",
1500 certificate: "testkey.override",
1501 }`)
1502 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1503 expected := "testkey.override.x509.pem testkey.override.pk8"
1504 if actual := rule.Args["certificates"]; actual != expected {
1505 t.Errorf("certificates should be %q, not %q", expected, actual)
1506 }
1507 })
1508}
1509
Jiyong Park58e364a2019-01-19 19:24:06 +09001510func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001511 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001512 apex {
1513 name: "myapex",
1514 key: "myapex.key",
1515 native_shared_libs: ["mylib"],
1516 }
1517
1518 apex {
1519 name: "otherapex",
1520 key: "myapex.key",
1521 native_shared_libs: ["mylib"],
1522 }
1523
1524 apex_key {
1525 name: "myapex.key",
1526 public_key: "testkey.avbpubkey",
1527 private_key: "testkey.pem",
1528 }
1529
1530 cc_library {
1531 name: "mylib",
1532 srcs: ["mylib.cpp"],
1533 system_shared_libs: [],
1534 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001535 // TODO: remove //apex_available:platform
1536 apex_available: [
1537 "//apex_available:platform",
1538 "myapex",
1539 "otherapex",
1540 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001541 }
1542 `)
1543
Jooyung Han6b8459b2019-10-30 08:29:25 +09001544 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001545 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001546 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001547 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1548 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001549
Jooyung Han6b8459b2019-10-30 08:29:25 +09001550 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001551 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001552 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001553 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1554 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001555
Jooyung Han6b8459b2019-10-30 08:29:25 +09001556 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001557 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001558 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001559 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1560 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001561}
Jiyong Park7e636d02019-01-28 16:16:54 +09001562
1563func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001564 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001565 apex {
1566 name: "myapex",
1567 key: "myapex.key",
1568 native_shared_libs: ["mylib"],
1569 }
1570
1571 apex_key {
1572 name: "myapex.key",
1573 public_key: "testkey.avbpubkey",
1574 private_key: "testkey.pem",
1575 }
1576
1577 cc_library_headers {
1578 name: "mylib_headers",
1579 export_include_dirs: ["my_include"],
1580 system_shared_libs: [],
1581 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001582 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001583 }
1584
1585 cc_library {
1586 name: "mylib",
1587 srcs: ["mylib.cpp"],
1588 system_shared_libs: [],
1589 stl: "none",
1590 header_libs: ["mylib_headers"],
1591 export_header_lib_headers: ["mylib_headers"],
1592 stubs: {
1593 versions: ["1", "2", "3"],
1594 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001595 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001596 }
1597
1598 cc_library {
1599 name: "otherlib",
1600 srcs: ["mylib.cpp"],
1601 system_shared_libs: [],
1602 stl: "none",
1603 shared_libs: ["mylib"],
1604 }
1605 `)
1606
Colin Cross7113d202019-11-20 16:39:12 -08001607 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001608
1609 // Ensure that the include path of the header lib is exported to 'otherlib'
1610 ensureContains(t, cFlags, "-Imy_include")
1611}
Alex Light9670d332019-01-29 18:07:33 -08001612
Jiyong Park7cd10e32020-01-14 09:22:18 +09001613type fileInApex struct {
1614 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001615 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001616 isLink bool
1617}
1618
Jooyung Hana57af4a2020-01-23 05:36:59 +00001619func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001620 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001621 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001622 copyCmds := apexRule.Args["copy_commands"]
1623 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001624 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001625 for _, cmd := range strings.Split(copyCmds, "&&") {
1626 cmd = strings.TrimSpace(cmd)
1627 if cmd == "" {
1628 continue
1629 }
1630 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001631 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001632 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001633 switch terms[0] {
1634 case "mkdir":
1635 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001636 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001637 t.Fatal("copyCmds contains invalid cp command", cmd)
1638 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001639 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001640 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001641 isLink = false
1642 case "ln":
1643 if len(terms) != 3 && len(terms) != 4 {
1644 // ln LINK TARGET or ln -s LINK TARGET
1645 t.Fatal("copyCmds contains invalid ln command", cmd)
1646 }
1647 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001648 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001649 isLink = true
1650 default:
1651 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1652 }
1653 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001654 index := strings.Index(dst, imageApexDir)
1655 if index == -1 {
1656 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1657 }
1658 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001659 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001660 }
1661 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001662 return ret
1663}
1664
Jooyung Hana57af4a2020-01-23 05:36:59 +00001665func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1666 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001667 var failed bool
1668 var surplus []string
1669 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001670 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001671 for _, expected := range files {
1672 if matched, _ := path.Match(expected, file.path); matched {
1673 filesMatched[expected] = true
1674 return
1675 }
1676 }
1677 surplus = append(surplus, file.path)
1678 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001679
Jooyung Han31c470b2019-10-18 16:26:59 +09001680 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001681 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001682 t.Log("surplus files", surplus)
1683 failed = true
1684 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001685
1686 if len(files) > len(filesMatched) {
1687 var missing []string
1688 for _, expected := range files {
1689 if !filesMatched[expected] {
1690 missing = append(missing, expected)
1691 }
1692 }
1693 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001694 t.Log("missing files", missing)
1695 failed = true
1696 }
1697 if failed {
1698 t.Fail()
1699 }
1700}
1701
Jooyung Han344d5432019-08-23 11:17:39 +09001702func TestVndkApexCurrent(t *testing.T) {
1703 ctx, _ := testApex(t, `
1704 apex_vndk {
1705 name: "myapex",
1706 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001707 }
1708
1709 apex_key {
1710 name: "myapex.key",
1711 public_key: "testkey.avbpubkey",
1712 private_key: "testkey.pem",
1713 }
1714
1715 cc_library {
1716 name: "libvndk",
1717 srcs: ["mylib.cpp"],
1718 vendor_available: true,
1719 vndk: {
1720 enabled: true,
1721 },
1722 system_shared_libs: [],
1723 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001724 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001725 }
1726
1727 cc_library {
1728 name: "libvndksp",
1729 srcs: ["mylib.cpp"],
1730 vendor_available: true,
1731 vndk: {
1732 enabled: true,
1733 support_system_process: true,
1734 },
1735 system_shared_libs: [],
1736 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001737 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001738 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001739 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001740
Jooyung Hana57af4a2020-01-23 05:36:59 +00001741 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001742 "lib/libvndk.so",
1743 "lib/libvndksp.so",
1744 "lib64/libvndk.so",
1745 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001746 "etc/llndk.libraries.VER.txt",
1747 "etc/vndkcore.libraries.VER.txt",
1748 "etc/vndksp.libraries.VER.txt",
1749 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001750 })
Jooyung Han344d5432019-08-23 11:17:39 +09001751}
1752
1753func TestVndkApexWithPrebuilt(t *testing.T) {
1754 ctx, _ := testApex(t, `
1755 apex_vndk {
1756 name: "myapex",
1757 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001758 }
1759
1760 apex_key {
1761 name: "myapex.key",
1762 public_key: "testkey.avbpubkey",
1763 private_key: "testkey.pem",
1764 }
1765
1766 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001767 name: "libvndk",
1768 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001769 vendor_available: true,
1770 vndk: {
1771 enabled: true,
1772 },
1773 system_shared_libs: [],
1774 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001775 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001776 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001777
1778 cc_prebuilt_library_shared {
1779 name: "libvndk.arm",
1780 srcs: ["libvndk.arm.so"],
1781 vendor_available: true,
1782 vndk: {
1783 enabled: true,
1784 },
1785 enabled: false,
1786 arch: {
1787 arm: {
1788 enabled: true,
1789 },
1790 },
1791 system_shared_libs: [],
1792 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001793 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001794 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001795 `+vndkLibrariesTxtFiles("current"),
1796 withFiles(map[string][]byte{
1797 "libvndk.so": nil,
1798 "libvndk.arm.so": nil,
1799 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001800
Jooyung Hana57af4a2020-01-23 05:36:59 +00001801 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001802 "lib/libvndk.so",
1803 "lib/libvndk.arm.so",
1804 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001805 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001806 })
Jooyung Han344d5432019-08-23 11:17:39 +09001807}
1808
Jooyung Han39edb6c2019-11-06 16:53:07 +09001809func vndkLibrariesTxtFiles(vers ...string) (result string) {
1810 for _, v := range vers {
1811 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001812 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001813 result += `
1814 vndk_libraries_txt {
1815 name: "` + txt + `.libraries.txt",
1816 }
1817 `
1818 }
1819 } else {
1820 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1821 result += `
1822 prebuilt_etc {
1823 name: "` + txt + `.libraries.` + v + `.txt",
1824 src: "dummy.txt",
1825 }
1826 `
1827 }
1828 }
1829 }
1830 return
1831}
1832
Jooyung Han344d5432019-08-23 11:17:39 +09001833func TestVndkApexVersion(t *testing.T) {
1834 ctx, _ := testApex(t, `
1835 apex_vndk {
1836 name: "myapex_v27",
1837 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001838 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001839 vndk_version: "27",
1840 }
1841
1842 apex_key {
1843 name: "myapex.key",
1844 public_key: "testkey.avbpubkey",
1845 private_key: "testkey.pem",
1846 }
1847
Jooyung Han31c470b2019-10-18 16:26:59 +09001848 vndk_prebuilt_shared {
1849 name: "libvndk27",
1850 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001851 vendor_available: true,
1852 vndk: {
1853 enabled: true,
1854 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001855 target_arch: "arm64",
1856 arch: {
1857 arm: {
1858 srcs: ["libvndk27_arm.so"],
1859 },
1860 arm64: {
1861 srcs: ["libvndk27_arm64.so"],
1862 },
1863 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001864 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001865 }
1866
1867 vndk_prebuilt_shared {
1868 name: "libvndk27",
1869 version: "27",
1870 vendor_available: true,
1871 vndk: {
1872 enabled: true,
1873 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001874 target_arch: "x86_64",
1875 arch: {
1876 x86: {
1877 srcs: ["libvndk27_x86.so"],
1878 },
1879 x86_64: {
1880 srcs: ["libvndk27_x86_64.so"],
1881 },
1882 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001883 }
1884 `+vndkLibrariesTxtFiles("27"),
1885 withFiles(map[string][]byte{
1886 "libvndk27_arm.so": nil,
1887 "libvndk27_arm64.so": nil,
1888 "libvndk27_x86.so": nil,
1889 "libvndk27_x86_64.so": nil,
1890 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001891
Jooyung Hana57af4a2020-01-23 05:36:59 +00001892 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001893 "lib/libvndk27_arm.so",
1894 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001895 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001896 })
Jooyung Han344d5432019-08-23 11:17:39 +09001897}
1898
1899func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1900 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1901 apex_vndk {
1902 name: "myapex_v27",
1903 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001904 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001905 vndk_version: "27",
1906 }
1907 apex_vndk {
1908 name: "myapex_v27_other",
1909 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001910 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001911 vndk_version: "27",
1912 }
1913
1914 apex_key {
1915 name: "myapex.key",
1916 public_key: "testkey.avbpubkey",
1917 private_key: "testkey.pem",
1918 }
1919
1920 cc_library {
1921 name: "libvndk",
1922 srcs: ["mylib.cpp"],
1923 vendor_available: true,
1924 vndk: {
1925 enabled: true,
1926 },
1927 system_shared_libs: [],
1928 stl: "none",
1929 }
1930
1931 vndk_prebuilt_shared {
1932 name: "libvndk",
1933 version: "27",
1934 vendor_available: true,
1935 vndk: {
1936 enabled: true,
1937 },
1938 srcs: ["libvndk.so"],
1939 }
1940 `, withFiles(map[string][]byte{
1941 "libvndk.so": nil,
1942 }))
1943}
1944
Jooyung Han90eee022019-10-01 20:02:42 +09001945func TestVndkApexNameRule(t *testing.T) {
1946 ctx, _ := testApex(t, `
1947 apex_vndk {
1948 name: "myapex",
1949 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001950 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001951 }
1952 apex_vndk {
1953 name: "myapex_v28",
1954 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001955 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001956 vndk_version: "28",
1957 }
1958 apex_key {
1959 name: "myapex.key",
1960 public_key: "testkey.avbpubkey",
1961 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001962 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001963
1964 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001965 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001966 actual := proptools.String(bundle.properties.Apex_name)
1967 if !reflect.DeepEqual(actual, expected) {
1968 t.Errorf("Got '%v', expected '%v'", actual, expected)
1969 }
1970 }
1971
1972 assertApexName("com.android.vndk.vVER", "myapex")
1973 assertApexName("com.android.vndk.v28", "myapex_v28")
1974}
1975
Jooyung Han344d5432019-08-23 11:17:39 +09001976func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1977 ctx, _ := testApex(t, `
1978 apex_vndk {
1979 name: "myapex",
1980 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001981 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001982 }
1983
1984 apex_key {
1985 name: "myapex.key",
1986 public_key: "testkey.avbpubkey",
1987 private_key: "testkey.pem",
1988 }
1989
1990 cc_library {
1991 name: "libvndk",
1992 srcs: ["mylib.cpp"],
1993 vendor_available: true,
1994 native_bridge_supported: true,
1995 host_supported: true,
1996 vndk: {
1997 enabled: true,
1998 },
1999 system_shared_libs: [],
2000 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002001 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002002 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002003 `+vndkLibrariesTxtFiles("current"),
2004 withTargets(map[android.OsType][]android.Target{
2005 android.Android: []android.Target{
2006 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2007 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2008 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2009 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2010 },
2011 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002012
Jooyung Hana57af4a2020-01-23 05:36:59 +00002013 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002014 "lib/libvndk.so",
2015 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002016 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002017 })
Jooyung Han344d5432019-08-23 11:17:39 +09002018}
2019
2020func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2021 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2022 apex_vndk {
2023 name: "myapex",
2024 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002025 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002026 native_bridge_supported: true,
2027 }
2028
2029 apex_key {
2030 name: "myapex.key",
2031 public_key: "testkey.avbpubkey",
2032 private_key: "testkey.pem",
2033 }
2034
2035 cc_library {
2036 name: "libvndk",
2037 srcs: ["mylib.cpp"],
2038 vendor_available: true,
2039 native_bridge_supported: true,
2040 host_supported: true,
2041 vndk: {
2042 enabled: true,
2043 },
2044 system_shared_libs: [],
2045 stl: "none",
2046 }
2047 `)
2048}
2049
Jooyung Han31c470b2019-10-18 16:26:59 +09002050func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002051 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002052 apex_vndk {
2053 name: "myapex_v27",
2054 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002055 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002056 vndk_version: "27",
2057 }
2058
2059 apex_key {
2060 name: "myapex.key",
2061 public_key: "testkey.avbpubkey",
2062 private_key: "testkey.pem",
2063 }
2064
2065 vndk_prebuilt_shared {
2066 name: "libvndk27",
2067 version: "27",
2068 target_arch: "arm",
2069 vendor_available: true,
2070 vndk: {
2071 enabled: true,
2072 },
2073 arch: {
2074 arm: {
2075 srcs: ["libvndk27.so"],
2076 }
2077 },
2078 }
2079
2080 vndk_prebuilt_shared {
2081 name: "libvndk27",
2082 version: "27",
2083 target_arch: "arm",
2084 binder32bit: true,
2085 vendor_available: true,
2086 vndk: {
2087 enabled: true,
2088 },
2089 arch: {
2090 arm: {
2091 srcs: ["libvndk27binder32.so"],
2092 }
2093 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002094 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002095 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002096 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002097 withFiles(map[string][]byte{
2098 "libvndk27.so": nil,
2099 "libvndk27binder32.so": nil,
2100 }),
2101 withBinder32bit,
2102 withTargets(map[android.OsType][]android.Target{
2103 android.Android: []android.Target{
2104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2105 },
2106 }),
2107 )
2108
Jooyung Hana57af4a2020-01-23 05:36:59 +00002109 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002110 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002111 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002112 })
2113}
2114
Jooyung Hane1633032019-08-01 17:41:43 +09002115func TestDependenciesInApexManifest(t *testing.T) {
2116 ctx, _ := testApex(t, `
2117 apex {
2118 name: "myapex_nodep",
2119 key: "myapex.key",
2120 native_shared_libs: ["lib_nodep"],
2121 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002122 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002123 }
2124
2125 apex {
2126 name: "myapex_dep",
2127 key: "myapex.key",
2128 native_shared_libs: ["lib_dep"],
2129 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002130 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002131 }
2132
2133 apex {
2134 name: "myapex_provider",
2135 key: "myapex.key",
2136 native_shared_libs: ["libfoo"],
2137 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002138 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002139 }
2140
2141 apex {
2142 name: "myapex_selfcontained",
2143 key: "myapex.key",
2144 native_shared_libs: ["lib_dep", "libfoo"],
2145 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002146 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002147 }
2148
2149 apex_key {
2150 name: "myapex.key",
2151 public_key: "testkey.avbpubkey",
2152 private_key: "testkey.pem",
2153 }
2154
2155 cc_library {
2156 name: "lib_nodep",
2157 srcs: ["mylib.cpp"],
2158 system_shared_libs: [],
2159 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002160 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002161 }
2162
2163 cc_library {
2164 name: "lib_dep",
2165 srcs: ["mylib.cpp"],
2166 shared_libs: ["libfoo"],
2167 system_shared_libs: [],
2168 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002169 apex_available: [
2170 "myapex_dep",
2171 "myapex_provider",
2172 "myapex_selfcontained",
2173 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002174 }
2175
2176 cc_library {
2177 name: "libfoo",
2178 srcs: ["mytest.cpp"],
2179 stubs: {
2180 versions: ["1"],
2181 },
2182 system_shared_libs: [],
2183 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002184 apex_available: [
2185 "myapex_provider",
2186 "myapex_selfcontained",
2187 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002188 }
2189 `)
2190
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002191 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002192 var provideNativeLibs, requireNativeLibs []string
2193
Sundong Ahnabb64432019-10-22 13:58:29 +09002194 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002195 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2196 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002197 ensureListEmpty(t, provideNativeLibs)
2198 ensureListEmpty(t, requireNativeLibs)
2199
Sundong Ahnabb64432019-10-22 13:58:29 +09002200 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002201 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2202 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002203 ensureListEmpty(t, provideNativeLibs)
2204 ensureListContains(t, requireNativeLibs, "libfoo.so")
2205
Sundong Ahnabb64432019-10-22 13:58:29 +09002206 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002207 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2208 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002209 ensureListContains(t, provideNativeLibs, "libfoo.so")
2210 ensureListEmpty(t, requireNativeLibs)
2211
Sundong Ahnabb64432019-10-22 13:58:29 +09002212 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002213 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2214 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002215 ensureListContains(t, provideNativeLibs, "libfoo.so")
2216 ensureListEmpty(t, requireNativeLibs)
2217}
2218
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002219func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002220 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002221 apex {
2222 name: "myapex",
2223 key: "myapex.key",
2224 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002225 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002226 }
2227
2228 apex_key {
2229 name: "myapex.key",
2230 public_key: "testkey.avbpubkey",
2231 private_key: "testkey.pem",
2232 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002233
2234 cc_library {
2235 name: "mylib",
2236 srcs: ["mylib.cpp"],
2237 system_shared_libs: [],
2238 stl: "none",
2239 apex_available: [
2240 "//apex_available:platform",
2241 "myapex",
2242 ],
2243 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002244 `)
2245
Sundong Ahnabb64432019-10-22 13:58:29 +09002246 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002247 apexManifestRule := module.Rule("apexManifestRule")
2248 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2249 apexRule := module.Rule("apexRule")
2250 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002251
2252 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2253 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2254 name := apexBundle.BaseModuleName()
2255 prefix := "TARGET_"
2256 var builder strings.Builder
2257 data.Custom(&builder, name, prefix, "", data)
2258 androidMk := builder.String()
2259 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2260 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002261}
2262
Alex Light0851b882019-02-07 13:20:53 -08002263func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002264 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002265 apex {
2266 name: "myapex",
2267 key: "myapex.key",
2268 native_shared_libs: ["mylib_common"],
2269 }
2270
2271 apex_key {
2272 name: "myapex.key",
2273 public_key: "testkey.avbpubkey",
2274 private_key: "testkey.pem",
2275 }
2276
2277 cc_library {
2278 name: "mylib_common",
2279 srcs: ["mylib.cpp"],
2280 system_shared_libs: [],
2281 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002282 apex_available: [
2283 "//apex_available:platform",
2284 "myapex",
2285 ],
Alex Light0851b882019-02-07 13:20:53 -08002286 }
2287 `)
2288
Sundong Ahnabb64432019-10-22 13:58:29 +09002289 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002290 apexRule := module.Rule("apexRule")
2291 copyCmds := apexRule.Args["copy_commands"]
2292
2293 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2294 t.Log("Apex was a test apex!")
2295 t.Fail()
2296 }
2297 // Ensure that main rule creates an output
2298 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2299
2300 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002301 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002302
2303 // Ensure that both direct and indirect deps are copied into apex
2304 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2305
Colin Cross7113d202019-11-20 16:39:12 -08002306 // Ensure that the platform variant ends with _shared
2307 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002308
2309 if !android.InAnyApex("mylib_common") {
2310 t.Log("Found mylib_common not in any apex!")
2311 t.Fail()
2312 }
2313}
2314
2315func TestTestApex(t *testing.T) {
2316 if android.InAnyApex("mylib_common_test") {
2317 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!")
2318 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002319 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002320 apex_test {
2321 name: "myapex",
2322 key: "myapex.key",
2323 native_shared_libs: ["mylib_common_test"],
2324 }
2325
2326 apex_key {
2327 name: "myapex.key",
2328 public_key: "testkey.avbpubkey",
2329 private_key: "testkey.pem",
2330 }
2331
2332 cc_library {
2333 name: "mylib_common_test",
2334 srcs: ["mylib.cpp"],
2335 system_shared_libs: [],
2336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002337 // TODO: remove //apex_available:platform
2338 apex_available: [
2339 "//apex_available:platform",
2340 "myapex",
2341 ],
Alex Light0851b882019-02-07 13:20:53 -08002342 }
2343 `)
2344
Sundong Ahnabb64432019-10-22 13:58:29 +09002345 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002346 apexRule := module.Rule("apexRule")
2347 copyCmds := apexRule.Args["copy_commands"]
2348
2349 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2350 t.Log("Apex was not a test apex!")
2351 t.Fail()
2352 }
2353 // Ensure that main rule creates an output
2354 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2355
2356 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002357 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002358
2359 // Ensure that both direct and indirect deps are copied into apex
2360 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2361
Colin Cross7113d202019-11-20 16:39:12 -08002362 // Ensure that the platform variant ends with _shared
2363 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002364
2365 if android.InAnyApex("mylib_common_test") {
2366 t.Log("Found mylib_common_test in some apex!")
2367 t.Fail()
2368 }
2369}
2370
Alex Light9670d332019-01-29 18:07:33 -08002371func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002372 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002373 apex {
2374 name: "myapex",
2375 key: "myapex.key",
2376 multilib: {
2377 first: {
2378 native_shared_libs: ["mylib_common"],
2379 }
2380 },
2381 target: {
2382 android: {
2383 multilib: {
2384 first: {
2385 native_shared_libs: ["mylib"],
2386 }
2387 }
2388 },
2389 host: {
2390 multilib: {
2391 first: {
2392 native_shared_libs: ["mylib2"],
2393 }
2394 }
2395 }
2396 }
2397 }
2398
2399 apex_key {
2400 name: "myapex.key",
2401 public_key: "testkey.avbpubkey",
2402 private_key: "testkey.pem",
2403 }
2404
2405 cc_library {
2406 name: "mylib",
2407 srcs: ["mylib.cpp"],
2408 system_shared_libs: [],
2409 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002410 // TODO: remove //apex_available:platform
2411 apex_available: [
2412 "//apex_available:platform",
2413 "myapex",
2414 ],
Alex Light9670d332019-01-29 18:07:33 -08002415 }
2416
2417 cc_library {
2418 name: "mylib_common",
2419 srcs: ["mylib.cpp"],
2420 system_shared_libs: [],
2421 stl: "none",
2422 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002423 // TODO: remove //apex_available:platform
2424 apex_available: [
2425 "//apex_available:platform",
2426 "myapex",
2427 ],
Alex Light9670d332019-01-29 18:07:33 -08002428 }
2429
2430 cc_library {
2431 name: "mylib2",
2432 srcs: ["mylib.cpp"],
2433 system_shared_libs: [],
2434 stl: "none",
2435 compile_multilib: "first",
2436 }
2437 `)
2438
Sundong Ahnabb64432019-10-22 13:58:29 +09002439 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002440 copyCmds := apexRule.Args["copy_commands"]
2441
2442 // Ensure that main rule creates an output
2443 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2444
2445 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002446 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2447 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2448 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002449
2450 // Ensure that both direct and indirect deps are copied into apex
2451 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2452 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2453 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2454
Colin Cross7113d202019-11-20 16:39:12 -08002455 // Ensure that the platform variant ends with _shared
2456 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2457 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2458 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002459}
Jiyong Park04480cf2019-02-06 00:16:29 +09002460
2461func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002462 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002463 apex {
2464 name: "myapex",
2465 key: "myapex.key",
2466 binaries: ["myscript"],
2467 }
2468
2469 apex_key {
2470 name: "myapex.key",
2471 public_key: "testkey.avbpubkey",
2472 private_key: "testkey.pem",
2473 }
2474
2475 sh_binary {
2476 name: "myscript",
2477 src: "mylib.cpp",
2478 filename: "myscript.sh",
2479 sub_dir: "script",
2480 }
2481 `)
2482
Sundong Ahnabb64432019-10-22 13:58:29 +09002483 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002484 copyCmds := apexRule.Args["copy_commands"]
2485
2486 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2487}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002488
Jooyung Han91df2082019-11-20 01:49:42 +09002489func TestApexInVariousPartition(t *testing.T) {
2490 testcases := []struct {
2491 propName, parition, flattenedPartition string
2492 }{
2493 {"", "system", "system_ext"},
2494 {"product_specific: true", "product", "product"},
2495 {"soc_specific: true", "vendor", "vendor"},
2496 {"proprietary: true", "vendor", "vendor"},
2497 {"vendor: true", "vendor", "vendor"},
2498 {"system_ext_specific: true", "system_ext", "system_ext"},
2499 }
2500 for _, tc := range testcases {
2501 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2502 ctx, _ := testApex(t, `
2503 apex {
2504 name: "myapex",
2505 key: "myapex.key",
2506 `+tc.propName+`
2507 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002508
Jooyung Han91df2082019-11-20 01:49:42 +09002509 apex_key {
2510 name: "myapex.key",
2511 public_key: "testkey.avbpubkey",
2512 private_key: "testkey.pem",
2513 }
2514 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002515
Jooyung Han91df2082019-11-20 01:49:42 +09002516 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2517 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2518 actual := apex.installDir.String()
2519 if actual != expected {
2520 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2521 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002522
Jooyung Han91df2082019-11-20 01:49:42 +09002523 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2524 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2525 actual = flattened.installDir.String()
2526 if actual != expected {
2527 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2528 }
2529 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002530 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002531}
Jiyong Park67882562019-03-21 01:11:21 +09002532
Jooyung Han54aca7b2019-11-20 02:26:02 +09002533func TestFileContexts(t *testing.T) {
2534 ctx, _ := testApex(t, `
2535 apex {
2536 name: "myapex",
2537 key: "myapex.key",
2538 }
2539
2540 apex_key {
2541 name: "myapex.key",
2542 public_key: "testkey.avbpubkey",
2543 private_key: "testkey.pem",
2544 }
2545 `)
2546 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2547 apexRule := module.Rule("apexRule")
2548 actual := apexRule.Args["file_contexts"]
2549 expected := "system/sepolicy/apex/myapex-file_contexts"
2550 if actual != expected {
2551 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2552 }
2553
2554 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2555 apex {
2556 name: "myapex",
2557 key: "myapex.key",
2558 file_contexts: "my_own_file_contexts",
2559 }
2560
2561 apex_key {
2562 name: "myapex.key",
2563 public_key: "testkey.avbpubkey",
2564 private_key: "testkey.pem",
2565 }
2566 `, withFiles(map[string][]byte{
2567 "my_own_file_contexts": nil,
2568 }))
2569
2570 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2571 apex {
2572 name: "myapex",
2573 key: "myapex.key",
2574 product_specific: true,
2575 file_contexts: "product_specific_file_contexts",
2576 }
2577
2578 apex_key {
2579 name: "myapex.key",
2580 public_key: "testkey.avbpubkey",
2581 private_key: "testkey.pem",
2582 }
2583 `)
2584
2585 ctx, _ = testApex(t, `
2586 apex {
2587 name: "myapex",
2588 key: "myapex.key",
2589 product_specific: true,
2590 file_contexts: "product_specific_file_contexts",
2591 }
2592
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598 `, withFiles(map[string][]byte{
2599 "product_specific_file_contexts": nil,
2600 }))
2601 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2602 apexRule = module.Rule("apexRule")
2603 actual = apexRule.Args["file_contexts"]
2604 expected = "product_specific_file_contexts"
2605 if actual != expected {
2606 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2607 }
2608
2609 ctx, _ = testApex(t, `
2610 apex {
2611 name: "myapex",
2612 key: "myapex.key",
2613 product_specific: true,
2614 file_contexts: ":my-file-contexts",
2615 }
2616
2617 apex_key {
2618 name: "myapex.key",
2619 public_key: "testkey.avbpubkey",
2620 private_key: "testkey.pem",
2621 }
2622
2623 filegroup {
2624 name: "my-file-contexts",
2625 srcs: ["product_specific_file_contexts"],
2626 }
2627 `, withFiles(map[string][]byte{
2628 "product_specific_file_contexts": nil,
2629 }))
2630 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2631 apexRule = module.Rule("apexRule")
2632 actual = apexRule.Args["file_contexts"]
2633 expected = "product_specific_file_contexts"
2634 if actual != expected {
2635 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2636 }
2637}
2638
Jiyong Park67882562019-03-21 01:11:21 +09002639func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002640 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002641 apex_key {
2642 name: "myapex.key",
2643 public_key: ":my.avbpubkey",
2644 private_key: ":my.pem",
2645 product_specific: true,
2646 }
2647
2648 filegroup {
2649 name: "my.avbpubkey",
2650 srcs: ["testkey2.avbpubkey"],
2651 }
2652
2653 filegroup {
2654 name: "my.pem",
2655 srcs: ["testkey2.pem"],
2656 }
2657 `)
2658
2659 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2660 expected_pubkey := "testkey2.avbpubkey"
2661 actual_pubkey := apex_key.public_key_file.String()
2662 if actual_pubkey != expected_pubkey {
2663 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2664 }
2665 expected_privkey := "testkey2.pem"
2666 actual_privkey := apex_key.private_key_file.String()
2667 if actual_privkey != expected_privkey {
2668 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2669 }
2670}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002671
2672func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002673 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002674 prebuilt_apex {
2675 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002676 arch: {
2677 arm64: {
2678 src: "myapex-arm64.apex",
2679 },
2680 arm: {
2681 src: "myapex-arm.apex",
2682 },
2683 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002684 }
2685 `)
2686
2687 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2688
Jiyong Parkc95714e2019-03-29 14:23:10 +09002689 expectedInput := "myapex-arm64.apex"
2690 if prebuilt.inputApex.String() != expectedInput {
2691 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2692 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002693}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002694
2695func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002696 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002697 prebuilt_apex {
2698 name: "myapex",
2699 src: "myapex-arm.apex",
2700 filename: "notmyapex.apex",
2701 }
2702 `)
2703
2704 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2705
2706 expected := "notmyapex.apex"
2707 if p.installFilename != expected {
2708 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2709 }
2710}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002711
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002712func TestPrebuiltOverrides(t *testing.T) {
2713 ctx, config := testApex(t, `
2714 prebuilt_apex {
2715 name: "myapex.prebuilt",
2716 src: "myapex-arm.apex",
2717 overrides: [
2718 "myapex",
2719 ],
2720 }
2721 `)
2722
2723 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2724
2725 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002726 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002727 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002728 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002729 }
2730}
2731
Roland Levillain630846d2019-06-26 12:48:34 +01002732func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002733 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002734 apex_test {
2735 name: "myapex",
2736 key: "myapex.key",
2737 tests: [
2738 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002739 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002740 ],
2741 }
2742
2743 apex_key {
2744 name: "myapex.key",
2745 public_key: "testkey.avbpubkey",
2746 private_key: "testkey.pem",
2747 }
2748
2749 cc_test {
2750 name: "mytest",
2751 gtest: false,
2752 srcs: ["mytest.cpp"],
2753 relative_install_path: "test",
2754 system_shared_libs: [],
2755 static_executable: true,
2756 stl: "none",
2757 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002758
2759 cc_test {
2760 name: "mytests",
2761 gtest: false,
2762 srcs: [
2763 "mytest1.cpp",
2764 "mytest2.cpp",
2765 "mytest3.cpp",
2766 ],
2767 test_per_src: true,
2768 relative_install_path: "test",
2769 system_shared_libs: [],
2770 static_executable: true,
2771 stl: "none",
2772 }
Roland Levillain630846d2019-06-26 12:48:34 +01002773 `)
2774
Sundong Ahnabb64432019-10-22 13:58:29 +09002775 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002776 copyCmds := apexRule.Args["copy_commands"]
2777
2778 // Ensure that test dep is copied into apex.
2779 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002780
2781 // Ensure that test deps built with `test_per_src` are copied into apex.
2782 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2783 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2784 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002785
2786 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002787 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002788 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2789 name := apexBundle.BaseModuleName()
2790 prefix := "TARGET_"
2791 var builder strings.Builder
2792 data.Custom(&builder, name, prefix, "", data)
2793 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002794 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2795 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2796 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2797 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002798 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002799 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002800 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002801}
2802
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002803func TestInstallExtraFlattenedApexes(t *testing.T) {
2804 ctx, config := testApex(t, `
2805 apex {
2806 name: "myapex",
2807 key: "myapex.key",
2808 }
2809 apex_key {
2810 name: "myapex.key",
2811 public_key: "testkey.avbpubkey",
2812 private_key: "testkey.pem",
2813 }
2814 `, func(fs map[string][]byte, config android.Config) {
2815 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2816 })
2817 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002818 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002819 mk := android.AndroidMkDataForTest(t, config, "", ab)
2820 var builder strings.Builder
2821 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2822 androidMk := builder.String()
2823 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2824}
2825
Jooyung Han5c998b92019-06-27 11:30:33 +09002826func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002827 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002828 apex {
2829 name: "myapex",
2830 key: "myapex.key",
2831 native_shared_libs: ["mylib"],
2832 uses: ["commonapex"],
2833 }
2834
2835 apex {
2836 name: "commonapex",
2837 key: "myapex.key",
2838 native_shared_libs: ["libcommon"],
2839 provide_cpp_shared_libs: true,
2840 }
2841
2842 apex_key {
2843 name: "myapex.key",
2844 public_key: "testkey.avbpubkey",
2845 private_key: "testkey.pem",
2846 }
2847
2848 cc_library {
2849 name: "mylib",
2850 srcs: ["mylib.cpp"],
2851 shared_libs: ["libcommon"],
2852 system_shared_libs: [],
2853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002854 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002855 }
2856
2857 cc_library {
2858 name: "libcommon",
2859 srcs: ["mylib_common.cpp"],
2860 system_shared_libs: [],
2861 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002862 // TODO: remove //apex_available:platform
2863 apex_available: [
2864 "//apex_available:platform",
2865 "commonapex",
2866 "myapex",
2867 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002868 }
2869 `)
2870
Sundong Ahnabb64432019-10-22 13:58:29 +09002871 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002872 apexRule1 := module1.Rule("apexRule")
2873 copyCmds1 := apexRule1.Args["copy_commands"]
2874
Sundong Ahnabb64432019-10-22 13:58:29 +09002875 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002876 apexRule2 := module2.Rule("apexRule")
2877 copyCmds2 := apexRule2.Args["copy_commands"]
2878
Colin Cross7113d202019-11-20 16:39:12 -08002879 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2880 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002881 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2882 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2883 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2884}
2885
2886func TestApexUsesFailsIfNotProvided(t *testing.T) {
2887 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2888 apex {
2889 name: "myapex",
2890 key: "myapex.key",
2891 uses: ["commonapex"],
2892 }
2893
2894 apex {
2895 name: "commonapex",
2896 key: "myapex.key",
2897 }
2898
2899 apex_key {
2900 name: "myapex.key",
2901 public_key: "testkey.avbpubkey",
2902 private_key: "testkey.pem",
2903 }
2904 `)
2905 testApexError(t, `uses: "commonapex" is not a provider`, `
2906 apex {
2907 name: "myapex",
2908 key: "myapex.key",
2909 uses: ["commonapex"],
2910 }
2911
2912 cc_library {
2913 name: "commonapex",
2914 system_shared_libs: [],
2915 stl: "none",
2916 }
2917
2918 apex_key {
2919 name: "myapex.key",
2920 public_key: "testkey.avbpubkey",
2921 private_key: "testkey.pem",
2922 }
2923 `)
2924}
2925
2926func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2927 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2928 apex {
2929 name: "myapex",
2930 key: "myapex.key",
2931 use_vendor: true,
2932 uses: ["commonapex"],
2933 }
2934
2935 apex {
2936 name: "commonapex",
2937 key: "myapex.key",
2938 provide_cpp_shared_libs: true,
2939 }
2940
2941 apex_key {
2942 name: "myapex.key",
2943 public_key: "testkey.avbpubkey",
2944 private_key: "testkey.pem",
2945 }
Jooyung Handc782442019-11-01 03:14:38 +09002946 `, func(fs map[string][]byte, config android.Config) {
2947 setUseVendorWhitelistForTest(config, []string{"myapex"})
2948 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002949}
2950
Jooyung Hand48f3c32019-08-23 11:18:57 +09002951func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2952 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2953 apex {
2954 name: "myapex",
2955 key: "myapex.key",
2956 native_shared_libs: ["libfoo"],
2957 }
2958
2959 apex_key {
2960 name: "myapex.key",
2961 public_key: "testkey.avbpubkey",
2962 private_key: "testkey.pem",
2963 }
2964
2965 cc_library {
2966 name: "libfoo",
2967 stl: "none",
2968 system_shared_libs: [],
2969 enabled: false,
2970 }
2971 `)
2972 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2973 apex {
2974 name: "myapex",
2975 key: "myapex.key",
2976 java_libs: ["myjar"],
2977 }
2978
2979 apex_key {
2980 name: "myapex.key",
2981 public_key: "testkey.avbpubkey",
2982 private_key: "testkey.pem",
2983 }
2984
2985 java_library {
2986 name: "myjar",
2987 srcs: ["foo/bar/MyClass.java"],
2988 sdk_version: "none",
2989 system_modules: "none",
2990 compile_dex: true,
2991 enabled: false,
2992 }
2993 `)
2994}
2995
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002996func TestApexWithApps(t *testing.T) {
2997 ctx, _ := testApex(t, `
2998 apex {
2999 name: "myapex",
3000 key: "myapex.key",
3001 apps: [
3002 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003003 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003004 ],
3005 }
3006
3007 apex_key {
3008 name: "myapex.key",
3009 public_key: "testkey.avbpubkey",
3010 private_key: "testkey.pem",
3011 }
3012
3013 android_app {
3014 name: "AppFoo",
3015 srcs: ["foo/bar/MyClass.java"],
3016 sdk_version: "none",
3017 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003018 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003019 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003020 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003021
3022 android_app {
3023 name: "AppFooPriv",
3024 srcs: ["foo/bar/MyClass.java"],
3025 sdk_version: "none",
3026 system_modules: "none",
3027 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003028 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003029 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003030
3031 cc_library_shared {
3032 name: "libjni",
3033 srcs: ["mylib.cpp"],
3034 stl: "none",
3035 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003036 apex_available: [ "myapex" ],
Jiyong Park8be103b2019-11-08 15:53:48 +09003037 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003038 `)
3039
Sundong Ahnabb64432019-10-22 13:58:29 +09003040 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003041 apexRule := module.Rule("apexRule")
3042 copyCmds := apexRule.Args["copy_commands"]
3043
3044 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003045 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003046
3047 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003048 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003049 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003050 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3051 // ... uncompressed
3052 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3053 t.Errorf("jni lib is not uncompressed for AppFoo")
3054 }
3055 // ... and not directly inside the APEX
3056 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003057}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003058
Dario Frenicde2a032019-10-27 00:29:22 +01003059func TestApexWithAppImports(t *testing.T) {
3060 ctx, _ := testApex(t, `
3061 apex {
3062 name: "myapex",
3063 key: "myapex.key",
3064 apps: [
3065 "AppFooPrebuilt",
3066 "AppFooPrivPrebuilt",
3067 ],
3068 }
3069
3070 apex_key {
3071 name: "myapex.key",
3072 public_key: "testkey.avbpubkey",
3073 private_key: "testkey.pem",
3074 }
3075
3076 android_app_import {
3077 name: "AppFooPrebuilt",
3078 apk: "PrebuiltAppFoo.apk",
3079 presigned: true,
3080 dex_preopt: {
3081 enabled: false,
3082 },
3083 }
3084
3085 android_app_import {
3086 name: "AppFooPrivPrebuilt",
3087 apk: "PrebuiltAppFooPriv.apk",
3088 privileged: true,
3089 presigned: true,
3090 dex_preopt: {
3091 enabled: false,
3092 },
3093 }
3094 `)
3095
Sundong Ahnabb64432019-10-22 13:58:29 +09003096 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003097 apexRule := module.Rule("apexRule")
3098 copyCmds := apexRule.Args["copy_commands"]
3099
3100 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3101 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003102}
3103
Dario Freni6f3937c2019-12-20 22:58:03 +00003104func TestApexWithTestHelperApp(t *testing.T) {
3105 ctx, _ := testApex(t, `
3106 apex {
3107 name: "myapex",
3108 key: "myapex.key",
3109 apps: [
3110 "TesterHelpAppFoo",
3111 ],
3112 }
3113
3114 apex_key {
3115 name: "myapex.key",
3116 public_key: "testkey.avbpubkey",
3117 private_key: "testkey.pem",
3118 }
3119
3120 android_test_helper_app {
3121 name: "TesterHelpAppFoo",
3122 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003123 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003124 }
3125
3126 `)
3127
3128 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3129 apexRule := module.Rule("apexRule")
3130 copyCmds := apexRule.Args["copy_commands"]
3131
3132 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3133}
3134
Jooyung Han18020ea2019-11-13 10:50:48 +09003135func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3136 // libfoo's apex_available comes from cc_defaults
3137 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3138 apex {
3139 name: "myapex",
3140 key: "myapex.key",
3141 native_shared_libs: ["libfoo"],
3142 }
3143
3144 apex_key {
3145 name: "myapex.key",
3146 public_key: "testkey.avbpubkey",
3147 private_key: "testkey.pem",
3148 }
3149
3150 apex {
3151 name: "otherapex",
3152 key: "myapex.key",
3153 native_shared_libs: ["libfoo"],
3154 }
3155
3156 cc_defaults {
3157 name: "libfoo-defaults",
3158 apex_available: ["otherapex"],
3159 }
3160
3161 cc_library {
3162 name: "libfoo",
3163 defaults: ["libfoo-defaults"],
3164 stl: "none",
3165 system_shared_libs: [],
3166 }`)
3167}
3168
Jiyong Park127b40b2019-09-30 16:04:35 +09003169func TestApexAvailable(t *testing.T) {
3170 // libfoo is not available to myapex, but only to otherapex
3171 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3172 apex {
3173 name: "myapex",
3174 key: "myapex.key",
3175 native_shared_libs: ["libfoo"],
3176 }
3177
3178 apex_key {
3179 name: "myapex.key",
3180 public_key: "testkey.avbpubkey",
3181 private_key: "testkey.pem",
3182 }
3183
3184 apex {
3185 name: "otherapex",
3186 key: "otherapex.key",
3187 native_shared_libs: ["libfoo"],
3188 }
3189
3190 apex_key {
3191 name: "otherapex.key",
3192 public_key: "testkey.avbpubkey",
3193 private_key: "testkey.pem",
3194 }
3195
3196 cc_library {
3197 name: "libfoo",
3198 stl: "none",
3199 system_shared_libs: [],
3200 apex_available: ["otherapex"],
3201 }`)
3202
3203 // libbar is an indirect dep
3204 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3205 apex {
3206 name: "myapex",
3207 key: "myapex.key",
3208 native_shared_libs: ["libfoo"],
3209 }
3210
3211 apex_key {
3212 name: "myapex.key",
3213 public_key: "testkey.avbpubkey",
3214 private_key: "testkey.pem",
3215 }
3216
3217 apex {
3218 name: "otherapex",
3219 key: "otherapex.key",
3220 native_shared_libs: ["libfoo"],
3221 }
3222
3223 apex_key {
3224 name: "otherapex.key",
3225 public_key: "testkey.avbpubkey",
3226 private_key: "testkey.pem",
3227 }
3228
3229 cc_library {
3230 name: "libfoo",
3231 stl: "none",
3232 shared_libs: ["libbar"],
3233 system_shared_libs: [],
3234 apex_available: ["myapex", "otherapex"],
3235 }
3236
3237 cc_library {
3238 name: "libbar",
3239 stl: "none",
3240 system_shared_libs: [],
3241 apex_available: ["otherapex"],
3242 }`)
3243
3244 testApexError(t, "\"otherapex\" is not a valid module name", `
3245 apex {
3246 name: "myapex",
3247 key: "myapex.key",
3248 native_shared_libs: ["libfoo"],
3249 }
3250
3251 apex_key {
3252 name: "myapex.key",
3253 public_key: "testkey.avbpubkey",
3254 private_key: "testkey.pem",
3255 }
3256
3257 cc_library {
3258 name: "libfoo",
3259 stl: "none",
3260 system_shared_libs: [],
3261 apex_available: ["otherapex"],
3262 }`)
3263
3264 ctx, _ := testApex(t, `
3265 apex {
3266 name: "myapex",
3267 key: "myapex.key",
3268 native_shared_libs: ["libfoo", "libbar"],
3269 }
3270
3271 apex_key {
3272 name: "myapex.key",
3273 public_key: "testkey.avbpubkey",
3274 private_key: "testkey.pem",
3275 }
3276
3277 cc_library {
3278 name: "libfoo",
3279 stl: "none",
3280 system_shared_libs: [],
3281 apex_available: ["myapex"],
3282 }
3283
3284 cc_library {
3285 name: "libbar",
3286 stl: "none",
3287 system_shared_libs: [],
3288 apex_available: ["//apex_available:anyapex"],
3289 }`)
3290
3291 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003292 // TODO(jiyong) the checks for the platform variant are removed because we now create
3293 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3294 // the platform variants are not used from other platform modules. When that is done,
3295 // these checks will be replaced by expecting a specific error message that will be
3296 // emitted when the platform variant is used.
3297 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3298 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3299 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3300 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003301
3302 ctx, _ = testApex(t, `
3303 apex {
3304 name: "myapex",
3305 key: "myapex.key",
3306 }
3307
3308 apex_key {
3309 name: "myapex.key",
3310 public_key: "testkey.avbpubkey",
3311 private_key: "testkey.pem",
3312 }
3313
3314 cc_library {
3315 name: "libfoo",
3316 stl: "none",
3317 system_shared_libs: [],
3318 apex_available: ["//apex_available:platform"],
3319 }`)
3320
3321 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003322 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3323 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003324
3325 ctx, _ = testApex(t, `
3326 apex {
3327 name: "myapex",
3328 key: "myapex.key",
3329 native_shared_libs: ["libfoo"],
3330 }
3331
3332 apex_key {
3333 name: "myapex.key",
3334 public_key: "testkey.avbpubkey",
3335 private_key: "testkey.pem",
3336 }
3337
3338 cc_library {
3339 name: "libfoo",
3340 stl: "none",
3341 system_shared_libs: [],
3342 apex_available: ["myapex"],
3343 static: {
3344 apex_available: ["//apex_available:platform"],
3345 },
3346 }`)
3347
3348 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003349 // TODO(jiyong) the checks for the platform variant are removed because we now create
3350 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3351 // the platform variants are not used from other platform modules. When that is done,
3352 // these checks will be replaced by expecting a specific error message that will be
3353 // emitted when the platform variant is used.
3354 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3355 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3356 // // but the static variant is available to both myapex and the platform
3357 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3358 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003359}
3360
Jiyong Park5d790c32019-11-15 18:40:32 +09003361func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003362 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003363 apex {
3364 name: "myapex",
3365 key: "myapex.key",
3366 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003367 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003368 }
3369
3370 override_apex {
3371 name: "override_myapex",
3372 base: "myapex",
3373 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003374 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003375 }
3376
3377 apex_key {
3378 name: "myapex.key",
3379 public_key: "testkey.avbpubkey",
3380 private_key: "testkey.pem",
3381 }
3382
3383 android_app {
3384 name: "app",
3385 srcs: ["foo/bar/MyClass.java"],
3386 package_name: "foo",
3387 sdk_version: "none",
3388 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003389 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003390 }
3391
3392 override_android_app {
3393 name: "override_app",
3394 base: "app",
3395 package_name: "bar",
3396 }
3397 `)
3398
Jiyong Park317645e2019-12-05 13:20:58 +09003399 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3400 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3401 if originalVariant.GetOverriddenBy() != "" {
3402 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3403 }
3404 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3405 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3406 }
3407
Jiyong Park5d790c32019-11-15 18:40:32 +09003408 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3409 apexRule := module.Rule("apexRule")
3410 copyCmds := apexRule.Args["copy_commands"]
3411
3412 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3413 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003414
3415 apexBundle := module.Module().(*apexBundle)
3416 name := apexBundle.Name()
3417 if name != "override_myapex" {
3418 t.Errorf("name should be \"override_myapex\", but was %q", name)
3419 }
3420
3421 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3422 var builder strings.Builder
3423 data.Custom(&builder, name, "TARGET_", "", data)
3424 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003425 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003426 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3427 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003428 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003429 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003430 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003431 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3432 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003433}
3434
Jooyung Han214bf372019-11-12 13:03:50 +09003435func TestLegacyAndroid10Support(t *testing.T) {
3436 ctx, _ := testApex(t, `
3437 apex {
3438 name: "myapex",
3439 key: "myapex.key",
3440 legacy_android10_support: true,
3441 }
3442
3443 apex_key {
3444 name: "myapex.key",
3445 public_key: "testkey.avbpubkey",
3446 private_key: "testkey.pem",
3447 }
3448 `)
3449
3450 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3451 args := module.Rule("apexRule").Args
3452 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003453 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003454}
3455
Jooyung Han58f26ab2019-12-18 15:34:32 +09003456func TestJavaSDKLibrary(t *testing.T) {
3457 ctx, _ := testApex(t, `
3458 apex {
3459 name: "myapex",
3460 key: "myapex.key",
3461 java_libs: ["foo"],
3462 }
3463
3464 apex_key {
3465 name: "myapex.key",
3466 public_key: "testkey.avbpubkey",
3467 private_key: "testkey.pem",
3468 }
3469
3470 java_sdk_library {
3471 name: "foo",
3472 srcs: ["a.java"],
3473 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003474 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003475 }
3476 `, withFiles(map[string][]byte{
3477 "api/current.txt": nil,
3478 "api/removed.txt": nil,
3479 "api/system-current.txt": nil,
3480 "api/system-removed.txt": nil,
3481 "api/test-current.txt": nil,
3482 "api/test-removed.txt": nil,
3483 }))
3484
3485 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003486 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003487 "javalib/foo.jar",
3488 "etc/permissions/foo.xml",
3489 })
3490 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003491 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3492 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003493}
3494
atrost6e126252020-01-27 17:01:16 +00003495func TestCompatConfig(t *testing.T) {
3496 ctx, _ := testApex(t, `
3497 apex {
3498 name: "myapex",
3499 key: "myapex.key",
3500 prebuilts: ["myjar-platform-compat-config"],
3501 java_libs: ["myjar"],
3502 }
3503
3504 apex_key {
3505 name: "myapex.key",
3506 public_key: "testkey.avbpubkey",
3507 private_key: "testkey.pem",
3508 }
3509
3510 platform_compat_config {
3511 name: "myjar-platform-compat-config",
3512 src: ":myjar",
3513 }
3514
3515 java_library {
3516 name: "myjar",
3517 srcs: ["foo/bar/MyClass.java"],
3518 sdk_version: "none",
3519 system_modules: "none",
3520 compile_dex: true,
3521 apex_available: [ "myapex" ],
3522 }
3523 `)
3524 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3525 "etc/compatconfig/myjar-platform-compat-config.xml",
3526 "javalib/myjar.jar",
3527 })
3528}
3529
Jiyong Park479321d2019-12-16 11:47:12 +09003530func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3531 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3532 apex {
3533 name: "myapex",
3534 key: "myapex.key",
3535 java_libs: ["myjar"],
3536 }
3537
3538 apex_key {
3539 name: "myapex.key",
3540 public_key: "testkey.avbpubkey",
3541 private_key: "testkey.pem",
3542 }
3543
3544 java_library {
3545 name: "myjar",
3546 srcs: ["foo/bar/MyClass.java"],
3547 sdk_version: "none",
3548 system_modules: "none",
3549 }
3550 `)
3551}
3552
Jiyong Park7afd1072019-12-30 16:56:33 +09003553func TestCarryRequiredModuleNames(t *testing.T) {
3554 ctx, config := testApex(t, `
3555 apex {
3556 name: "myapex",
3557 key: "myapex.key",
3558 native_shared_libs: ["mylib"],
3559 }
3560
3561 apex_key {
3562 name: "myapex.key",
3563 public_key: "testkey.avbpubkey",
3564 private_key: "testkey.pem",
3565 }
3566
3567 cc_library {
3568 name: "mylib",
3569 srcs: ["mylib.cpp"],
3570 system_shared_libs: [],
3571 stl: "none",
3572 required: ["a", "b"],
3573 host_required: ["c", "d"],
3574 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003575 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003576 }
3577 `)
3578
3579 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3580 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3581 name := apexBundle.BaseModuleName()
3582 prefix := "TARGET_"
3583 var builder strings.Builder
3584 data.Custom(&builder, name, prefix, "", data)
3585 androidMk := builder.String()
3586 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3587 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3588 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3589}
3590
Jiyong Park7cd10e32020-01-14 09:22:18 +09003591func TestSymlinksFromApexToSystem(t *testing.T) {
3592 bp := `
3593 apex {
3594 name: "myapex",
3595 key: "myapex.key",
3596 native_shared_libs: ["mylib"],
3597 java_libs: ["myjar"],
3598 }
3599
3600 apex_key {
3601 name: "myapex.key",
3602 public_key: "testkey.avbpubkey",
3603 private_key: "testkey.pem",
3604 }
3605
3606 cc_library {
3607 name: "mylib",
3608 srcs: ["mylib.cpp"],
3609 shared_libs: ["myotherlib"],
3610 system_shared_libs: [],
3611 stl: "none",
3612 apex_available: [
3613 "myapex",
3614 "//apex_available:platform",
3615 ],
3616 }
3617
3618 cc_library {
3619 name: "myotherlib",
3620 srcs: ["mylib.cpp"],
3621 system_shared_libs: [],
3622 stl: "none",
3623 apex_available: [
3624 "myapex",
3625 "//apex_available:platform",
3626 ],
3627 }
3628
3629 java_library {
3630 name: "myjar",
3631 srcs: ["foo/bar/MyClass.java"],
3632 sdk_version: "none",
3633 system_modules: "none",
3634 libs: ["myotherjar"],
3635 compile_dex: true,
3636 apex_available: [
3637 "myapex",
3638 "//apex_available:platform",
3639 ],
3640 }
3641
3642 java_library {
3643 name: "myotherjar",
3644 srcs: ["foo/bar/MyClass.java"],
3645 sdk_version: "none",
3646 system_modules: "none",
3647 apex_available: [
3648 "myapex",
3649 "//apex_available:platform",
3650 ],
3651 }
3652 `
3653
3654 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3655 for _, f := range files {
3656 if f.path == file {
3657 if f.isLink {
3658 t.Errorf("%q is not a real file", file)
3659 }
3660 return
3661 }
3662 }
3663 t.Errorf("%q is not found", file)
3664 }
3665
3666 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3667 for _, f := range files {
3668 if f.path == file {
3669 if !f.isLink {
3670 t.Errorf("%q is not a symlink", file)
3671 }
3672 return
3673 }
3674 }
3675 t.Errorf("%q is not found", file)
3676 }
3677
3678 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003679 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003680 ensureRealfileExists(t, files, "javalib/myjar.jar")
3681 ensureRealfileExists(t, files, "lib64/mylib.so")
3682 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3683
3684 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003685 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003686 ensureRealfileExists(t, files, "javalib/myjar.jar")
3687 ensureRealfileExists(t, files, "lib64/mylib.so")
3688 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
3689}
3690
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003691func TestMain(m *testing.M) {
3692 run := func() int {
3693 setUp()
3694 defer tearDown()
3695
3696 return m.Run()
3697 }
3698
3699 os.Exit(run())
3700}