blob: 9152fddd97798422e9c81fbc61a5c21d634157a4 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jiyong Park7cd10e32020-01-14 09:22:18 +090094func withUnbundledBuild(fs map[string][]byte, config android.Config) {
95 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
96}
97
Jooyung Han344d5432019-08-23 11:17:39 +090098func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090099 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100
101 bp = bp + `
102 toolchain_library {
103 name: "libcompiler_rt-extras",
104 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
109 toolchain_library {
110 name: "libatomic",
111 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 vendor_available: true,
113 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900114 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900115 }
116
117 toolchain_library {
118 name: "libgcc",
119 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900120 vendor_available: true,
121 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900122 }
123
124 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700125 name: "libgcc_stripped",
126 src: "",
127 vendor_available: true,
128 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900129 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700130 }
131
132 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900133 name: "libclang_rt.builtins-aarch64-android",
134 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900135 vendor_available: true,
136 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900137 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900138 }
139
140 toolchain_library {
141 name: "libclang_rt.builtins-arm-android",
142 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 vendor_available: true,
144 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900145 native_bridge_supported: true,
146 }
147
148 toolchain_library {
149 name: "libclang_rt.builtins-x86_64-android",
150 src: "",
151 vendor_available: true,
152 recovery_available: true,
153 native_bridge_supported: true,
154 }
155
156 toolchain_library {
157 name: "libclang_rt.builtins-i686-android",
158 src: "",
159 vendor_available: true,
160 recovery_available: true,
161 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900162 }
163
164 cc_object {
165 name: "crtbegin_so",
166 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900167 vendor_available: true,
168 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900169 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900170 }
171
172 cc_object {
173 name: "crtend_so",
174 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900175 vendor_available: true,
176 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900177 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900178 }
179
Alex Light3d673592019-01-18 14:37:31 -0800180 cc_object {
181 name: "crtbegin_static",
182 stl: "none",
183 }
184
185 cc_object {
186 name: "crtend_android",
187 stl: "none",
188 }
189
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 llndk_library {
191 name: "libc",
192 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900193 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900194 }
195
196 llndk_library {
197 name: "libm",
198 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900199 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900200 }
201
202 llndk_library {
203 name: "libdl",
204 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900205 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900206 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900207
208 filegroup {
209 name: "myapex-file_contexts",
210 srcs: [
211 "system/sepolicy/apex/myapex-file_contexts",
212 ],
213 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800215
Dario Frenicde2a032019-10-27 00:29:22 +0100216 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217
Jooyung Han344d5432019-08-23 11:17:39 +0900218 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900219 "a.java": nil,
220 "PrebuiltAppFoo.apk": nil,
221 "PrebuiltAppFooPriv.apk": nil,
222 "build/make/target/product/security": nil,
223 "apex_manifest.json": nil,
224 "AndroidManifest.xml": nil,
225 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900226 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900227 "system/sepolicy/apex/otherapex-file_contexts": nil,
228 "system/sepolicy/apex/commonapex-file_contexts": nil,
229 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800230 "mylib.cpp": nil,
231 "mylib_common.cpp": nil,
232 "mytest.cpp": nil,
233 "mytest1.cpp": nil,
234 "mytest2.cpp": nil,
235 "mytest3.cpp": nil,
236 "myprebuilt": nil,
237 "my_include": nil,
238 "foo/bar/MyClass.java": nil,
239 "prebuilt.jar": nil,
240 "vendor/foo/devkeys/test.x509.pem": nil,
241 "vendor/foo/devkeys/test.pk8": nil,
242 "testkey.x509.pem": nil,
243 "testkey.pk8": nil,
244 "testkey.override.x509.pem": nil,
245 "testkey.override.pk8": nil,
246 "vendor/foo/devkeys/testkey.avbpubkey": nil,
247 "vendor/foo/devkeys/testkey.pem": nil,
248 "NOTICE": nil,
249 "custom_notice": nil,
250 "testkey2.avbpubkey": nil,
251 "testkey2.pem": nil,
252 "myapex-arm64.apex": nil,
253 "myapex-arm.apex": nil,
254 "frameworks/base/api/current.txt": nil,
255 "framework/aidl/a.aidl": nil,
256 "build/make/core/proguard.flags": nil,
257 "build/make/core/proguard_basic_keeps.flags": nil,
258 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900259 }
260
261 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800262 // The fs now needs to be populated before creating the config, call handlers twice
263 // for now, once to get any fs changes, and later after the config was created to
264 // set product variables or targets.
265 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
266 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900267 }
268
Colin Cross98be1bb2019-12-13 20:41:13 -0800269 config := android.TestArchConfig(buildDir, nil, bp, fs)
270 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
271 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
272 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
273 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
274 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
275 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
276
277 for _, handler := range handlers {
278 // The fs now needs to be populated before creating the config, call handlers twice
279 // for now, earlier to get any fs changes, and now after the config was created to
280 // set product variables or targets.
281 tempFS := map[string][]byte{}
282 handler(tempFS, config)
283 }
284
285 ctx := android.NewTestArchContext()
286 ctx.RegisterModuleType("apex", BundleFactory)
287 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
288 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
289 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
290 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
291 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
292 ctx.RegisterModuleType("override_apex", overrideApexFactory)
293
Jooyung Hana57af4a2020-01-23 05:36:59 +0000294 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
295 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
296
Paul Duffin77980a82019-12-19 16:01:36 +0000297 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800298 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800299 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
300 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000302 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800304 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000305 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000306 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000307 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900308 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800309
Colin Cross98be1bb2019-12-13 20:41:13 -0800310 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800311 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800312
313 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314
Jooyung Han5c998b92019-06-27 11:30:33 +0900315 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316}
317
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700318func setUp() {
319 var err error
320 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700322 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324}
325
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700326func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 os.RemoveAll(buildDir)
328}
329
330// ensure that 'result' contains 'expected'
331func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900332 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 if !strings.Contains(result, expected) {
334 t.Errorf("%q is not found in %q", expected, result)
335 }
336}
337
338// ensures that 'result' does not contain 'notExpected'
339func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900340 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 if strings.Contains(result, notExpected) {
342 t.Errorf("%q is found in %q", notExpected, result)
343 }
344}
345
346func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900347 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 if !android.InList(expected, result) {
349 t.Errorf("%q is not found in %v", expected, result)
350 }
351}
352
353func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900354 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 if android.InList(notExpected, result) {
356 t.Errorf("%q is found in %v", notExpected, result)
357 }
358}
359
Jooyung Hane1633032019-08-01 17:41:43 +0900360func ensureListEmpty(t *testing.T, result []string) {
361 t.Helper()
362 if len(result) > 0 {
363 t.Errorf("%q is expected to be empty", result)
364 }
365}
366
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367// Minimal test
368func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700369 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900370 apex_defaults {
371 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900372 manifest: ":myapex.manifest",
373 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900374 key: "myapex.key",
375 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800376 multilib: {
377 both: {
378 binaries: ["foo",],
379 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900380 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900381 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900382 }
383
Jiyong Park30ca9372019-02-07 16:27:23 +0900384 apex {
385 name: "myapex",
386 defaults: ["myapex-defaults"],
387 }
388
Jiyong Park25fc6a92018-11-18 18:02:45 +0900389 apex_key {
390 name: "myapex.key",
391 public_key: "testkey.avbpubkey",
392 private_key: "testkey.pem",
393 }
394
Jiyong Park809bb722019-02-13 21:33:49 +0900395 filegroup {
396 name: "myapex.manifest",
397 srcs: ["apex_manifest.json"],
398 }
399
400 filegroup {
401 name: "myapex.androidmanifest",
402 srcs: ["AndroidManifest.xml"],
403 }
404
Jiyong Park25fc6a92018-11-18 18:02:45 +0900405 cc_library {
406 name: "mylib",
407 srcs: ["mylib.cpp"],
408 shared_libs: ["mylib2"],
409 system_shared_libs: [],
410 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000411 // TODO: remove //apex_available:platform
412 apex_available: [
413 "//apex_available:platform",
414 "myapex",
415 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900416 }
417
Alex Light3d673592019-01-18 14:37:31 -0800418 cc_binary {
419 name: "foo",
420 srcs: ["mylib.cpp"],
421 compile_multilib: "both",
422 multilib: {
423 lib32: {
424 suffix: "32",
425 },
426 lib64: {
427 suffix: "64",
428 },
429 },
430 symlinks: ["foo_link_"],
431 symlink_preferred_arch: true,
432 system_shared_libs: [],
433 static_executable: true,
434 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000435 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800436 }
437
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 cc_library {
439 name: "mylib2",
440 srcs: ["mylib.cpp"],
441 system_shared_libs: [],
442 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900443 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000444 // TODO: remove //apex_available:platform
445 apex_available: [
446 "//apex_available:platform",
447 "myapex",
448 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900450
451 java_library {
452 name: "myjar",
453 srcs: ["foo/bar/MyClass.java"],
454 sdk_version: "none",
455 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900456 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900457 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000458 // TODO: remove //apex_available:platform
459 apex_available: [
460 "//apex_available:platform",
461 "myapex",
462 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900463 }
464
465 java_library {
466 name: "myotherjar",
467 srcs: ["foo/bar/MyClass.java"],
468 sdk_version: "none",
469 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900470 // TODO: remove //apex_available:platform
471 apex_available: [
472 "//apex_available:platform",
473 "myapex",
474 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900476
477 java_library {
478 name: "mysharedjar",
479 srcs: ["foo/bar/MyClass.java"],
480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900482 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483 `)
484
Sundong Ahnabb64432019-10-22 13:58:29 +0900485 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900486
487 optFlags := apexRule.Args["opt_flags"]
488 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700489 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900490 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900491
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492 copyCmds := apexRule.Args["copy_commands"]
493
494 // Ensure that main rule creates an output
495 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
496
497 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800498 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900499 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900500
501 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800502 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900503 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900504
505 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800506 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
507 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900508 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
509 // .. but not for java libs
510 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900511 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800512
Colin Cross7113d202019-11-20 16:39:12 -0800513 // Ensure that the platform variant ends with _shared or _common
514 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
515 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900516 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
517 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900518 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
519
520 // Ensure that dynamic dependency to java libs are not included
521 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800522
523 // Ensure that all symlinks are present.
524 found_foo_link_64 := false
525 found_foo := false
526 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900527 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800528 if strings.HasSuffix(cmd, "bin/foo") {
529 found_foo = true
530 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
531 found_foo_link_64 = true
532 }
533 }
534 }
535 good := found_foo && found_foo_link_64
536 if !good {
537 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
538 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900539
Sundong Ahnabb64432019-10-22 13:58:29 +0900540 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700541 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700542 if len(noticeInputs) != 2 {
543 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900544 }
545 ensureListContains(t, noticeInputs, "NOTICE")
546 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900547
548 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
549 ensureListContains(t, depsInfo, "internal myjar")
550 ensureListContains(t, depsInfo, "internal mylib")
551 ensureListContains(t, depsInfo, "internal mylib2")
552 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800553}
554
Jooyung Hanf21c7972019-12-16 22:32:06 +0900555func TestDefaults(t *testing.T) {
556 ctx, _ := testApex(t, `
557 apex_defaults {
558 name: "myapex-defaults",
559 key: "myapex.key",
560 prebuilts: ["myetc"],
561 native_shared_libs: ["mylib"],
562 java_libs: ["myjar"],
563 apps: ["AppFoo"],
564 }
565
566 prebuilt_etc {
567 name: "myetc",
568 src: "myprebuilt",
569 }
570
571 apex {
572 name: "myapex",
573 defaults: ["myapex-defaults"],
574 }
575
576 apex_key {
577 name: "myapex.key",
578 public_key: "testkey.avbpubkey",
579 private_key: "testkey.pem",
580 }
581
582 cc_library {
583 name: "mylib",
584 system_shared_libs: [],
585 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000586 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900587 }
588
589 java_library {
590 name: "myjar",
591 srcs: ["foo/bar/MyClass.java"],
592 sdk_version: "none",
593 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000594 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900595 }
596
597 android_app {
598 name: "AppFoo",
599 srcs: ["foo/bar/MyClass.java"],
600 sdk_version: "none",
601 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000602 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900603 }
604 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000605 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900606 "etc/myetc",
607 "javalib/myjar.jar",
608 "lib64/mylib.so",
609 "app/AppFoo/AppFoo.apk",
610 })
611}
612
Jooyung Han01a3ee22019-11-02 02:52:25 +0900613func TestApexManifest(t *testing.T) {
614 ctx, _ := testApex(t, `
615 apex {
616 name: "myapex",
617 key: "myapex.key",
618 }
619
620 apex_key {
621 name: "myapex.key",
622 public_key: "testkey.avbpubkey",
623 private_key: "testkey.pem",
624 }
625 `)
626
627 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900628 args := module.Rule("apexRule").Args
629 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
630 t.Error("manifest should be apex_manifest.pb, but " + manifest)
631 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900632}
633
Alex Light5098a612018-11-29 17:12:15 -0800634func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700635 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800636 apex {
637 name: "myapex",
638 key: "myapex.key",
639 payload_type: "zip",
640 native_shared_libs: ["mylib"],
641 }
642
643 apex_key {
644 name: "myapex.key",
645 public_key: "testkey.avbpubkey",
646 private_key: "testkey.pem",
647 }
648
649 cc_library {
650 name: "mylib",
651 srcs: ["mylib.cpp"],
652 shared_libs: ["mylib2"],
653 system_shared_libs: [],
654 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000655 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800656 }
657
658 cc_library {
659 name: "mylib2",
660 srcs: ["mylib.cpp"],
661 system_shared_libs: [],
662 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000663 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800664 }
665 `)
666
Sundong Ahnabb64432019-10-22 13:58:29 +0900667 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800668 copyCmds := zipApexRule.Args["copy_commands"]
669
670 // Ensure that main rule creates an output
671 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
672
673 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800674 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800675
676 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800677 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800678
679 // Ensure that both direct and indirect deps are copied into apex
680 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
681 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900682}
683
684func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700685 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686 apex {
687 name: "myapex",
688 key: "myapex.key",
689 native_shared_libs: ["mylib", "mylib3"],
690 }
691
692 apex_key {
693 name: "myapex.key",
694 public_key: "testkey.avbpubkey",
695 private_key: "testkey.pem",
696 }
697
698 cc_library {
699 name: "mylib",
700 srcs: ["mylib.cpp"],
701 shared_libs: ["mylib2", "mylib3"],
702 system_shared_libs: [],
703 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000704 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900705 }
706
707 cc_library {
708 name: "mylib2",
709 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900710 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900711 system_shared_libs: [],
712 stl: "none",
713 stubs: {
714 versions: ["1", "2", "3"],
715 },
716 }
717
718 cc_library {
719 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900720 srcs: ["mylib.cpp"],
721 shared_libs: ["mylib4"],
722 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723 stl: "none",
724 stubs: {
725 versions: ["10", "11", "12"],
726 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000727 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900728 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900729
730 cc_library {
731 name: "mylib4",
732 srcs: ["mylib.cpp"],
733 system_shared_libs: [],
734 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000735 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900736 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737 `)
738
Sundong Ahnabb64432019-10-22 13:58:29 +0900739 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900740 copyCmds := apexRule.Args["copy_commands"]
741
742 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800743 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900744
745 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800746 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900747
748 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800749 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750
Colin Cross7113d202019-11-20 16:39:12 -0800751 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900752
753 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900754 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900756 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900757
758 // 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 -0800759 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900760 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800761 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900762
763 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900764 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900765 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900766
767 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900768 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900769
Jooyung Hana57af4a2020-01-23 05:36:59 +0000770 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900771 "lib64/mylib.so",
772 "lib64/mylib3.so",
773 "lib64/mylib4.so",
774 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900775}
776
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900777func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700778 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900780 name: "myapex2",
781 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900782 native_shared_libs: ["mylib"],
783 }
784
785 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900786 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900787 public_key: "testkey.avbpubkey",
788 private_key: "testkey.pem",
789 }
790
791 cc_library {
792 name: "mylib",
793 srcs: ["mylib.cpp"],
794 shared_libs: ["libfoo#10"],
795 system_shared_libs: [],
796 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000797 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900798 }
799
800 cc_library {
801 name: "libfoo",
802 srcs: ["mylib.cpp"],
803 shared_libs: ["libbar"],
804 system_shared_libs: [],
805 stl: "none",
806 stubs: {
807 versions: ["10", "20", "30"],
808 },
809 }
810
811 cc_library {
812 name: "libbar",
813 srcs: ["mylib.cpp"],
814 system_shared_libs: [],
815 stl: "none",
816 }
817
818 `)
819
Jiyong Park83dc74b2020-01-14 18:38:44 +0900820 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900821 copyCmds := apexRule.Args["copy_commands"]
822
823 // Ensure that direct non-stubs dep is always included
824 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
825
826 // Ensure that indirect stubs dep is not included
827 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
828
829 // Ensure that dependency of stubs is not included
830 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
831
Jiyong Park83dc74b2020-01-14 18:38:44 +0900832 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900833
834 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900835 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900836 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900837 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900838
Jiyong Park3ff16992019-12-27 14:11:47 +0900839 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900840
841 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
842 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900843
844 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
845 ensureListContains(t, depsInfo, "internal mylib")
846 ensureListContains(t, depsInfo, "external libfoo")
847 ensureListNotContains(t, depsInfo, "internal libfoo")
848 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900849}
850
Jooyung Hand3639552019-08-09 12:57:43 +0900851func TestApexWithRuntimeLibsDependency(t *testing.T) {
852 /*
853 myapex
854 |
855 v (runtime_libs)
856 mylib ------+------> libfoo [provides stub]
857 |
858 `------> libbar
859 */
860 ctx, _ := testApex(t, `
861 apex {
862 name: "myapex",
863 key: "myapex.key",
864 native_shared_libs: ["mylib"],
865 }
866
867 apex_key {
868 name: "myapex.key",
869 public_key: "testkey.avbpubkey",
870 private_key: "testkey.pem",
871 }
872
873 cc_library {
874 name: "mylib",
875 srcs: ["mylib.cpp"],
876 runtime_libs: ["libfoo", "libbar"],
877 system_shared_libs: [],
878 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000879 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900880 }
881
882 cc_library {
883 name: "libfoo",
884 srcs: ["mylib.cpp"],
885 system_shared_libs: [],
886 stl: "none",
887 stubs: {
888 versions: ["10", "20", "30"],
889 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900890 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900891 }
892
893 cc_library {
894 name: "libbar",
895 srcs: ["mylib.cpp"],
896 system_shared_libs: [],
897 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000898 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900899 }
900
901 `)
902
Sundong Ahnabb64432019-10-22 13:58:29 +0900903 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900904 copyCmds := apexRule.Args["copy_commands"]
905
906 // Ensure that direct non-stubs dep is always included
907 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
908
909 // Ensure that indirect stubs dep is not included
910 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
911
912 // Ensure that runtime_libs dep in included
913 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
914
Sundong Ahnabb64432019-10-22 13:58:29 +0900915 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900916 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
917 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900918
919}
920
Jooyung Han9c80bae2019-08-20 17:30:57 +0900921func TestApexDependencyToLLNDK(t *testing.T) {
922 ctx, _ := testApex(t, `
923 apex {
924 name: "myapex",
925 key: "myapex.key",
926 use_vendor: true,
927 native_shared_libs: ["mylib"],
928 }
929
930 apex_key {
931 name: "myapex.key",
932 public_key: "testkey.avbpubkey",
933 private_key: "testkey.pem",
934 }
935
936 cc_library {
937 name: "mylib",
938 srcs: ["mylib.cpp"],
939 vendor_available: true,
940 shared_libs: ["libbar"],
941 system_shared_libs: [],
942 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000943 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900944 }
945
946 cc_library {
947 name: "libbar",
948 srcs: ["mylib.cpp"],
949 system_shared_libs: [],
950 stl: "none",
951 }
952
953 llndk_library {
954 name: "libbar",
955 symbol_file: "",
956 }
Jooyung Handc782442019-11-01 03:14:38 +0900957 `, func(fs map[string][]byte, config android.Config) {
958 setUseVendorWhitelistForTest(config, []string{"myapex"})
959 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900960
Sundong Ahnabb64432019-10-22 13:58:29 +0900961 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900962 copyCmds := apexRule.Args["copy_commands"]
963
964 // Ensure that LLNDK dep is not included
965 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
966
Sundong Ahnabb64432019-10-22 13:58:29 +0900967 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900968 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900969
970 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900971 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900972
973}
974
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700976 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977 apex {
978 name: "myapex",
979 key: "myapex.key",
980 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
981 }
982
983 apex_key {
984 name: "myapex.key",
985 public_key: "testkey.avbpubkey",
986 private_key: "testkey.pem",
987 }
988
989 cc_library {
990 name: "mylib",
991 srcs: ["mylib.cpp"],
992 shared_libs: ["libdl#27"],
993 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000994 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 }
996
997 cc_library_shared {
998 name: "mylib_shared",
999 srcs: ["mylib.cpp"],
1000 shared_libs: ["libdl#27"],
1001 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001002 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003 }
1004
1005 cc_library {
1006 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -07001007 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001008 nocrt: true,
1009 system_shared_libs: [],
1010 stl: "none",
1011 stubs: {
1012 versions: ["27", "28", "29"],
1013 },
1014 }
1015
1016 cc_library {
1017 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001018 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001019 nocrt: true,
1020 system_shared_libs: [],
1021 stl: "none",
1022 stubs: {
1023 versions: ["27", "28", "29"],
1024 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001025 apex_available: [
1026 "//apex_available:platform",
1027 "myapex"
1028 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001029 }
1030
1031 cc_library {
1032 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001033 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001034 nocrt: true,
1035 system_shared_libs: [],
1036 stl: "none",
1037 stubs: {
1038 versions: ["27", "28", "29"],
1039 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001040 apex_available: [
1041 "//apex_available:platform",
1042 "myapex"
1043 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001044 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001045
1046 cc_library {
1047 name: "libBootstrap",
1048 srcs: ["mylib.cpp"],
1049 stl: "none",
1050 bootstrap: true,
1051 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001052 `)
1053
Sundong Ahnabb64432019-10-22 13:58:29 +09001054 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001055 copyCmds := apexRule.Args["copy_commands"]
1056
1057 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001058 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001059 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1060 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001061
1062 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001063 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001064
Colin Cross7113d202019-11-20 16:39:12 -08001065 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1066 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1067 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001068
1069 // For dependency to libc
1070 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001071 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001072 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001073 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001074 // ... Cflags from stub is correctly exported to mylib
1075 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1076 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1077
1078 // For dependency to libm
1079 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001080 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001081 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001082 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001083 // ... and is not compiling with the stub
1084 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1085 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1086
1087 // For dependency to libdl
1088 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001089 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001090 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001091 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1092 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001093 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001094 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001095 // ... Cflags from stub is correctly exported to mylib
1096 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1097 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001098
1099 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001100 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1101 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1102 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1103 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001104}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001105
1106func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001107 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001108 apex {
1109 name: "myapex",
1110 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001111 native_shared_libs: ["mylib"],
1112 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001113 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001114 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001115 }
1116
1117 apex_key {
1118 name: "myapex.key",
1119 public_key: "testkey.avbpubkey",
1120 private_key: "testkey.pem",
1121 }
1122
1123 prebuilt_etc {
1124 name: "myetc",
1125 src: "myprebuilt",
1126 sub_dir: "foo/bar",
1127 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001128
1129 cc_library {
1130 name: "mylib",
1131 srcs: ["mylib.cpp"],
1132 relative_install_path: "foo/bar",
1133 system_shared_libs: [],
1134 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001135 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001136 }
1137
1138 cc_binary {
1139 name: "mybin",
1140 srcs: ["mylib.cpp"],
1141 relative_install_path: "foo/bar",
1142 system_shared_libs: [],
1143 static_executable: true,
1144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001145 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001146 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001147 `)
1148
Sundong Ahnabb64432019-10-22 13:58:29 +09001149 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001150 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1151
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001152 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001153 ensureListContains(t, dirs, "etc")
1154 ensureListContains(t, dirs, "etc/foo")
1155 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001156 ensureListContains(t, dirs, "lib64")
1157 ensureListContains(t, dirs, "lib64/foo")
1158 ensureListContains(t, dirs, "lib64/foo/bar")
1159 ensureListContains(t, dirs, "lib")
1160 ensureListContains(t, dirs, "lib/foo")
1161 ensureListContains(t, dirs, "lib/foo/bar")
1162
Jiyong Parkbd13e442019-03-15 18:10:35 +09001163 ensureListContains(t, dirs, "bin")
1164 ensureListContains(t, dirs, "bin/foo")
1165 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001166}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001167
1168func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001169 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001170 apex {
1171 name: "myapex",
1172 key: "myapex.key",
1173 native_shared_libs: ["mylib"],
1174 use_vendor: true,
1175 }
1176
1177 apex_key {
1178 name: "myapex.key",
1179 public_key: "testkey.avbpubkey",
1180 private_key: "testkey.pem",
1181 }
1182
1183 cc_library {
1184 name: "mylib",
1185 srcs: ["mylib.cpp"],
1186 shared_libs: ["mylib2"],
1187 system_shared_libs: [],
1188 vendor_available: true,
1189 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001190 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001191 }
1192
1193 cc_library {
1194 name: "mylib2",
1195 srcs: ["mylib.cpp"],
1196 system_shared_libs: [],
1197 vendor_available: true,
1198 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001199 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001200 }
Jooyung Handc782442019-11-01 03:14:38 +09001201 `, func(fs map[string][]byte, config android.Config) {
1202 setUseVendorWhitelistForTest(config, []string{"myapex"})
1203 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001204
1205 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001206 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001207 for _, implicit := range i.Implicits {
1208 inputsList = append(inputsList, implicit.String())
1209 }
1210 }
1211 inputsString := strings.Join(inputsList, " ")
1212
1213 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001214 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1215 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001216
1217 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001218 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1219 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001220}
Jiyong Park16e91a02018-12-20 18:18:08 +09001221
Jooyung Handc782442019-11-01 03:14:38 +09001222func TestUseVendorRestriction(t *testing.T) {
1223 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1224 apex {
1225 name: "myapex",
1226 key: "myapex.key",
1227 use_vendor: true,
1228 }
1229 apex_key {
1230 name: "myapex.key",
1231 public_key: "testkey.avbpubkey",
1232 private_key: "testkey.pem",
1233 }
1234 `, func(fs map[string][]byte, config android.Config) {
1235 setUseVendorWhitelistForTest(config, []string{""})
1236 })
1237 // no error with whitelist
1238 testApex(t, `
1239 apex {
1240 name: "myapex",
1241 key: "myapex.key",
1242 use_vendor: true,
1243 }
1244 apex_key {
1245 name: "myapex.key",
1246 public_key: "testkey.avbpubkey",
1247 private_key: "testkey.pem",
1248 }
1249 `, func(fs map[string][]byte, config android.Config) {
1250 setUseVendorWhitelistForTest(config, []string{"myapex"})
1251 })
1252}
1253
Jooyung Han5c998b92019-06-27 11:30:33 +09001254func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1255 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1256 apex {
1257 name: "myapex",
1258 key: "myapex.key",
1259 native_shared_libs: ["mylib"],
1260 use_vendor: true,
1261 }
1262
1263 apex_key {
1264 name: "myapex.key",
1265 public_key: "testkey.avbpubkey",
1266 private_key: "testkey.pem",
1267 }
1268
1269 cc_library {
1270 name: "mylib",
1271 srcs: ["mylib.cpp"],
1272 system_shared_libs: [],
1273 stl: "none",
1274 }
1275 `)
1276}
1277
Jiyong Park16e91a02018-12-20 18:18:08 +09001278func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001279 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001280 apex {
1281 name: "myapex",
1282 key: "myapex.key",
1283 native_shared_libs: ["mylib"],
1284 }
1285
1286 apex_key {
1287 name: "myapex.key",
1288 public_key: "testkey.avbpubkey",
1289 private_key: "testkey.pem",
1290 }
1291
1292 cc_library {
1293 name: "mylib",
1294 srcs: ["mylib.cpp"],
1295 system_shared_libs: [],
1296 stl: "none",
1297 stubs: {
1298 versions: ["1", "2", "3"],
1299 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001300 apex_available: [
1301 "//apex_available:platform",
1302 "myapex",
1303 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001304 }
1305
1306 cc_binary {
1307 name: "not_in_apex",
1308 srcs: ["mylib.cpp"],
1309 static_libs: ["mylib"],
1310 static_executable: true,
1311 system_shared_libs: [],
1312 stl: "none",
1313 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001314 `)
1315
Colin Cross7113d202019-11-20 16:39:12 -08001316 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001317
1318 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001319 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001320}
Jiyong Park9335a262018-12-24 11:31:58 +09001321
1322func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001323 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001324 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001325 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001326 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001327 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001328 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001329 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001330 }
1331
1332 cc_library {
1333 name: "mylib",
1334 srcs: ["mylib.cpp"],
1335 system_shared_libs: [],
1336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001337 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001338 }
1339
1340 apex_key {
1341 name: "myapex.key",
1342 public_key: "testkey.avbpubkey",
1343 private_key: "testkey.pem",
1344 }
1345
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001346 android_app_certificate {
1347 name: "myapex.certificate",
1348 certificate: "testkey",
1349 }
1350
1351 android_app_certificate {
1352 name: "myapex.certificate.override",
1353 certificate: "testkey.override",
1354 }
1355
Jiyong Park9335a262018-12-24 11:31:58 +09001356 `)
1357
1358 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001359 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001360
1361 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1362 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1363 "vendor/foo/devkeys/testkey.avbpubkey")
1364 }
1365 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1366 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1367 "vendor/foo/devkeys/testkey.pem")
1368 }
1369
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001370 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001371 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001372 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001373 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001374 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001375 }
1376}
Jiyong Park58e364a2019-01-19 19:24:06 +09001377
Jooyung Hanf121a652019-12-17 14:30:11 +09001378func TestCertificate(t *testing.T) {
1379 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1380 ctx, _ := testApex(t, `
1381 apex {
1382 name: "myapex",
1383 key: "myapex.key",
1384 }
1385 apex_key {
1386 name: "myapex.key",
1387 public_key: "testkey.avbpubkey",
1388 private_key: "testkey.pem",
1389 }`)
1390 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1391 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1392 if actual := rule.Args["certificates"]; actual != expected {
1393 t.Errorf("certificates should be %q, not %q", expected, actual)
1394 }
1395 })
1396 t.Run("override when unspecified", func(t *testing.T) {
1397 ctx, _ := testApex(t, `
1398 apex {
1399 name: "myapex_keytest",
1400 key: "myapex.key",
1401 file_contexts: ":myapex-file_contexts",
1402 }
1403 apex_key {
1404 name: "myapex.key",
1405 public_key: "testkey.avbpubkey",
1406 private_key: "testkey.pem",
1407 }
1408 android_app_certificate {
1409 name: "myapex.certificate.override",
1410 certificate: "testkey.override",
1411 }`)
1412 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1413 expected := "testkey.override.x509.pem testkey.override.pk8"
1414 if actual := rule.Args["certificates"]; actual != expected {
1415 t.Errorf("certificates should be %q, not %q", expected, actual)
1416 }
1417 })
1418 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1419 ctx, _ := testApex(t, `
1420 apex {
1421 name: "myapex",
1422 key: "myapex.key",
1423 certificate: ":myapex.certificate",
1424 }
1425 apex_key {
1426 name: "myapex.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
1430 android_app_certificate {
1431 name: "myapex.certificate",
1432 certificate: "testkey",
1433 }`)
1434 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1435 expected := "testkey.x509.pem testkey.pk8"
1436 if actual := rule.Args["certificates"]; actual != expected {
1437 t.Errorf("certificates should be %q, not %q", expected, actual)
1438 }
1439 })
1440 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1441 ctx, _ := testApex(t, `
1442 apex {
1443 name: "myapex_keytest",
1444 key: "myapex.key",
1445 file_contexts: ":myapex-file_contexts",
1446 certificate: ":myapex.certificate",
1447 }
1448 apex_key {
1449 name: "myapex.key",
1450 public_key: "testkey.avbpubkey",
1451 private_key: "testkey.pem",
1452 }
1453 android_app_certificate {
1454 name: "myapex.certificate.override",
1455 certificate: "testkey.override",
1456 }`)
1457 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1458 expected := "testkey.override.x509.pem testkey.override.pk8"
1459 if actual := rule.Args["certificates"]; actual != expected {
1460 t.Errorf("certificates should be %q, not %q", expected, actual)
1461 }
1462 })
1463 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1464 ctx, _ := testApex(t, `
1465 apex {
1466 name: "myapex",
1467 key: "myapex.key",
1468 certificate: "testkey",
1469 }
1470 apex_key {
1471 name: "myapex.key",
1472 public_key: "testkey.avbpubkey",
1473 private_key: "testkey.pem",
1474 }`)
1475 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1476 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1477 if actual := rule.Args["certificates"]; actual != expected {
1478 t.Errorf("certificates should be %q, not %q", expected, actual)
1479 }
1480 })
1481 t.Run("override when specified as <name>", func(t *testing.T) {
1482 ctx, _ := testApex(t, `
1483 apex {
1484 name: "myapex_keytest",
1485 key: "myapex.key",
1486 file_contexts: ":myapex-file_contexts",
1487 certificate: "testkey",
1488 }
1489 apex_key {
1490 name: "myapex.key",
1491 public_key: "testkey.avbpubkey",
1492 private_key: "testkey.pem",
1493 }
1494 android_app_certificate {
1495 name: "myapex.certificate.override",
1496 certificate: "testkey.override",
1497 }`)
1498 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1499 expected := "testkey.override.x509.pem testkey.override.pk8"
1500 if actual := rule.Args["certificates"]; actual != expected {
1501 t.Errorf("certificates should be %q, not %q", expected, actual)
1502 }
1503 })
1504}
1505
Jiyong Park58e364a2019-01-19 19:24:06 +09001506func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001507 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001508 apex {
1509 name: "myapex",
1510 key: "myapex.key",
1511 native_shared_libs: ["mylib"],
1512 }
1513
1514 apex {
1515 name: "otherapex",
1516 key: "myapex.key",
1517 native_shared_libs: ["mylib"],
1518 }
1519
1520 apex_key {
1521 name: "myapex.key",
1522 public_key: "testkey.avbpubkey",
1523 private_key: "testkey.pem",
1524 }
1525
1526 cc_library {
1527 name: "mylib",
1528 srcs: ["mylib.cpp"],
1529 system_shared_libs: [],
1530 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001531 // TODO: remove //apex_available:platform
1532 apex_available: [
1533 "//apex_available:platform",
1534 "myapex",
1535 "otherapex",
1536 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001537 }
1538 `)
1539
Jooyung Han6b8459b2019-10-30 08:29:25 +09001540 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001541 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001542 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001543 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1544 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001545
Jooyung Han6b8459b2019-10-30 08:29:25 +09001546 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001547 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001548 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001549 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1550 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001551
Jooyung Han6b8459b2019-10-30 08:29:25 +09001552 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001553 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001554 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001555 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1556 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001557}
Jiyong Park7e636d02019-01-28 16:16:54 +09001558
1559func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001560 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001561 apex {
1562 name: "myapex",
1563 key: "myapex.key",
1564 native_shared_libs: ["mylib"],
1565 }
1566
1567 apex_key {
1568 name: "myapex.key",
1569 public_key: "testkey.avbpubkey",
1570 private_key: "testkey.pem",
1571 }
1572
1573 cc_library_headers {
1574 name: "mylib_headers",
1575 export_include_dirs: ["my_include"],
1576 system_shared_libs: [],
1577 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001578 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001579 }
1580
1581 cc_library {
1582 name: "mylib",
1583 srcs: ["mylib.cpp"],
1584 system_shared_libs: [],
1585 stl: "none",
1586 header_libs: ["mylib_headers"],
1587 export_header_lib_headers: ["mylib_headers"],
1588 stubs: {
1589 versions: ["1", "2", "3"],
1590 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001591 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001592 }
1593
1594 cc_library {
1595 name: "otherlib",
1596 srcs: ["mylib.cpp"],
1597 system_shared_libs: [],
1598 stl: "none",
1599 shared_libs: ["mylib"],
1600 }
1601 `)
1602
Colin Cross7113d202019-11-20 16:39:12 -08001603 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001604
1605 // Ensure that the include path of the header lib is exported to 'otherlib'
1606 ensureContains(t, cFlags, "-Imy_include")
1607}
Alex Light9670d332019-01-29 18:07:33 -08001608
Jiyong Park7cd10e32020-01-14 09:22:18 +09001609type fileInApex struct {
1610 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001611 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001612 isLink bool
1613}
1614
Jooyung Hana57af4a2020-01-23 05:36:59 +00001615func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001616 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001617 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001618 copyCmds := apexRule.Args["copy_commands"]
1619 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001620 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001621 for _, cmd := range strings.Split(copyCmds, "&&") {
1622 cmd = strings.TrimSpace(cmd)
1623 if cmd == "" {
1624 continue
1625 }
1626 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001627 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001628 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001629 switch terms[0] {
1630 case "mkdir":
1631 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001632 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001633 t.Fatal("copyCmds contains invalid cp command", cmd)
1634 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001635 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001636 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001637 isLink = false
1638 case "ln":
1639 if len(terms) != 3 && len(terms) != 4 {
1640 // ln LINK TARGET or ln -s LINK TARGET
1641 t.Fatal("copyCmds contains invalid ln command", cmd)
1642 }
1643 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001644 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001645 isLink = true
1646 default:
1647 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1648 }
1649 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001650 index := strings.Index(dst, imageApexDir)
1651 if index == -1 {
1652 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1653 }
1654 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001655 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001656 }
1657 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001658 return ret
1659}
1660
Jooyung Hana57af4a2020-01-23 05:36:59 +00001661func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1662 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001663 var failed bool
1664 var surplus []string
1665 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001666 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001667 for _, expected := range files {
1668 if matched, _ := path.Match(expected, file.path); matched {
1669 filesMatched[expected] = true
1670 return
1671 }
1672 }
1673 surplus = append(surplus, file.path)
1674 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001675
Jooyung Han31c470b2019-10-18 16:26:59 +09001676 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001677 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001678 t.Log("surplus files", surplus)
1679 failed = true
1680 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001681
1682 if len(files) > len(filesMatched) {
1683 var missing []string
1684 for _, expected := range files {
1685 if !filesMatched[expected] {
1686 missing = append(missing, expected)
1687 }
1688 }
1689 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001690 t.Log("missing files", missing)
1691 failed = true
1692 }
1693 if failed {
1694 t.Fail()
1695 }
1696}
1697
Jooyung Han344d5432019-08-23 11:17:39 +09001698func TestVndkApexCurrent(t *testing.T) {
1699 ctx, _ := testApex(t, `
1700 apex_vndk {
1701 name: "myapex",
1702 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001703 }
1704
1705 apex_key {
1706 name: "myapex.key",
1707 public_key: "testkey.avbpubkey",
1708 private_key: "testkey.pem",
1709 }
1710
1711 cc_library {
1712 name: "libvndk",
1713 srcs: ["mylib.cpp"],
1714 vendor_available: true,
1715 vndk: {
1716 enabled: true,
1717 },
1718 system_shared_libs: [],
1719 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001720 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001721 }
1722
1723 cc_library {
1724 name: "libvndksp",
1725 srcs: ["mylib.cpp"],
1726 vendor_available: true,
1727 vndk: {
1728 enabled: true,
1729 support_system_process: true,
1730 },
1731 system_shared_libs: [],
1732 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001733 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001734 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001735 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001736
Jooyung Hana57af4a2020-01-23 05:36:59 +00001737 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001738 "lib/libvndk.so",
1739 "lib/libvndksp.so",
1740 "lib64/libvndk.so",
1741 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001742 "etc/llndk.libraries.VER.txt",
1743 "etc/vndkcore.libraries.VER.txt",
1744 "etc/vndksp.libraries.VER.txt",
1745 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001746 })
Jooyung Han344d5432019-08-23 11:17:39 +09001747}
1748
1749func TestVndkApexWithPrebuilt(t *testing.T) {
1750 ctx, _ := testApex(t, `
1751 apex_vndk {
1752 name: "myapex",
1753 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001754 }
1755
1756 apex_key {
1757 name: "myapex.key",
1758 public_key: "testkey.avbpubkey",
1759 private_key: "testkey.pem",
1760 }
1761
1762 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001763 name: "libvndk",
1764 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001765 vendor_available: true,
1766 vndk: {
1767 enabled: true,
1768 },
1769 system_shared_libs: [],
1770 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001771 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001772 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001773
1774 cc_prebuilt_library_shared {
1775 name: "libvndk.arm",
1776 srcs: ["libvndk.arm.so"],
1777 vendor_available: true,
1778 vndk: {
1779 enabled: true,
1780 },
1781 enabled: false,
1782 arch: {
1783 arm: {
1784 enabled: true,
1785 },
1786 },
1787 system_shared_libs: [],
1788 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001789 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001790 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001791 `+vndkLibrariesTxtFiles("current"),
1792 withFiles(map[string][]byte{
1793 "libvndk.so": nil,
1794 "libvndk.arm.so": nil,
1795 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001796
Jooyung Hana57af4a2020-01-23 05:36:59 +00001797 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001798 "lib/libvndk.so",
1799 "lib/libvndk.arm.so",
1800 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001801 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001802 })
Jooyung Han344d5432019-08-23 11:17:39 +09001803}
1804
Jooyung Han39edb6c2019-11-06 16:53:07 +09001805func vndkLibrariesTxtFiles(vers ...string) (result string) {
1806 for _, v := range vers {
1807 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001808 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001809 result += `
1810 vndk_libraries_txt {
1811 name: "` + txt + `.libraries.txt",
1812 }
1813 `
1814 }
1815 } else {
1816 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1817 result += `
1818 prebuilt_etc {
1819 name: "` + txt + `.libraries.` + v + `.txt",
1820 src: "dummy.txt",
1821 }
1822 `
1823 }
1824 }
1825 }
1826 return
1827}
1828
Jooyung Han344d5432019-08-23 11:17:39 +09001829func TestVndkApexVersion(t *testing.T) {
1830 ctx, _ := testApex(t, `
1831 apex_vndk {
1832 name: "myapex_v27",
1833 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001834 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001835 vndk_version: "27",
1836 }
1837
1838 apex_key {
1839 name: "myapex.key",
1840 public_key: "testkey.avbpubkey",
1841 private_key: "testkey.pem",
1842 }
1843
Jooyung Han31c470b2019-10-18 16:26:59 +09001844 vndk_prebuilt_shared {
1845 name: "libvndk27",
1846 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001847 vendor_available: true,
1848 vndk: {
1849 enabled: true,
1850 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001851 target_arch: "arm64",
1852 arch: {
1853 arm: {
1854 srcs: ["libvndk27_arm.so"],
1855 },
1856 arm64: {
1857 srcs: ["libvndk27_arm64.so"],
1858 },
1859 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001860 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001861 }
1862
1863 vndk_prebuilt_shared {
1864 name: "libvndk27",
1865 version: "27",
1866 vendor_available: true,
1867 vndk: {
1868 enabled: true,
1869 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001870 target_arch: "x86_64",
1871 arch: {
1872 x86: {
1873 srcs: ["libvndk27_x86.so"],
1874 },
1875 x86_64: {
1876 srcs: ["libvndk27_x86_64.so"],
1877 },
1878 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001879 }
1880 `+vndkLibrariesTxtFiles("27"),
1881 withFiles(map[string][]byte{
1882 "libvndk27_arm.so": nil,
1883 "libvndk27_arm64.so": nil,
1884 "libvndk27_x86.so": nil,
1885 "libvndk27_x86_64.so": nil,
1886 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001887
Jooyung Hana57af4a2020-01-23 05:36:59 +00001888 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001889 "lib/libvndk27_arm.so",
1890 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001891 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001892 })
Jooyung Han344d5432019-08-23 11:17:39 +09001893}
1894
1895func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1896 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1897 apex_vndk {
1898 name: "myapex_v27",
1899 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001900 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001901 vndk_version: "27",
1902 }
1903 apex_vndk {
1904 name: "myapex_v27_other",
1905 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001906 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001907 vndk_version: "27",
1908 }
1909
1910 apex_key {
1911 name: "myapex.key",
1912 public_key: "testkey.avbpubkey",
1913 private_key: "testkey.pem",
1914 }
1915
1916 cc_library {
1917 name: "libvndk",
1918 srcs: ["mylib.cpp"],
1919 vendor_available: true,
1920 vndk: {
1921 enabled: true,
1922 },
1923 system_shared_libs: [],
1924 stl: "none",
1925 }
1926
1927 vndk_prebuilt_shared {
1928 name: "libvndk",
1929 version: "27",
1930 vendor_available: true,
1931 vndk: {
1932 enabled: true,
1933 },
1934 srcs: ["libvndk.so"],
1935 }
1936 `, withFiles(map[string][]byte{
1937 "libvndk.so": nil,
1938 }))
1939}
1940
Jooyung Han90eee022019-10-01 20:02:42 +09001941func TestVndkApexNameRule(t *testing.T) {
1942 ctx, _ := testApex(t, `
1943 apex_vndk {
1944 name: "myapex",
1945 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001946 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001947 }
1948 apex_vndk {
1949 name: "myapex_v28",
1950 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001951 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001952 vndk_version: "28",
1953 }
1954 apex_key {
1955 name: "myapex.key",
1956 public_key: "testkey.avbpubkey",
1957 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001958 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001959
1960 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001961 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001962 actual := proptools.String(bundle.properties.Apex_name)
1963 if !reflect.DeepEqual(actual, expected) {
1964 t.Errorf("Got '%v', expected '%v'", actual, expected)
1965 }
1966 }
1967
1968 assertApexName("com.android.vndk.vVER", "myapex")
1969 assertApexName("com.android.vndk.v28", "myapex_v28")
1970}
1971
Jooyung Han344d5432019-08-23 11:17:39 +09001972func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1973 ctx, _ := testApex(t, `
1974 apex_vndk {
1975 name: "myapex",
1976 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001977 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001978 }
1979
1980 apex_key {
1981 name: "myapex.key",
1982 public_key: "testkey.avbpubkey",
1983 private_key: "testkey.pem",
1984 }
1985
1986 cc_library {
1987 name: "libvndk",
1988 srcs: ["mylib.cpp"],
1989 vendor_available: true,
1990 native_bridge_supported: true,
1991 host_supported: true,
1992 vndk: {
1993 enabled: true,
1994 },
1995 system_shared_libs: [],
1996 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001997 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001998 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001999 `+vndkLibrariesTxtFiles("current"),
2000 withTargets(map[android.OsType][]android.Target{
2001 android.Android: []android.Target{
2002 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2003 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2004 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2005 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2006 },
2007 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002008
Jooyung Hana57af4a2020-01-23 05:36:59 +00002009 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002010 "lib/libvndk.so",
2011 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002012 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002013 })
Jooyung Han344d5432019-08-23 11:17:39 +09002014}
2015
2016func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2017 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2018 apex_vndk {
2019 name: "myapex",
2020 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002021 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002022 native_bridge_supported: true,
2023 }
2024
2025 apex_key {
2026 name: "myapex.key",
2027 public_key: "testkey.avbpubkey",
2028 private_key: "testkey.pem",
2029 }
2030
2031 cc_library {
2032 name: "libvndk",
2033 srcs: ["mylib.cpp"],
2034 vendor_available: true,
2035 native_bridge_supported: true,
2036 host_supported: true,
2037 vndk: {
2038 enabled: true,
2039 },
2040 system_shared_libs: [],
2041 stl: "none",
2042 }
2043 `)
2044}
2045
Jooyung Han31c470b2019-10-18 16:26:59 +09002046func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002047 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002048 apex_vndk {
2049 name: "myapex_v27",
2050 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002051 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002052 vndk_version: "27",
2053 }
2054
2055 apex_key {
2056 name: "myapex.key",
2057 public_key: "testkey.avbpubkey",
2058 private_key: "testkey.pem",
2059 }
2060
2061 vndk_prebuilt_shared {
2062 name: "libvndk27",
2063 version: "27",
2064 target_arch: "arm",
2065 vendor_available: true,
2066 vndk: {
2067 enabled: true,
2068 },
2069 arch: {
2070 arm: {
2071 srcs: ["libvndk27.so"],
2072 }
2073 },
2074 }
2075
2076 vndk_prebuilt_shared {
2077 name: "libvndk27",
2078 version: "27",
2079 target_arch: "arm",
2080 binder32bit: true,
2081 vendor_available: true,
2082 vndk: {
2083 enabled: true,
2084 },
2085 arch: {
2086 arm: {
2087 srcs: ["libvndk27binder32.so"],
2088 }
2089 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002090 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002091 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002092 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002093 withFiles(map[string][]byte{
2094 "libvndk27.so": nil,
2095 "libvndk27binder32.so": nil,
2096 }),
2097 withBinder32bit,
2098 withTargets(map[android.OsType][]android.Target{
2099 android.Android: []android.Target{
2100 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2101 },
2102 }),
2103 )
2104
Jooyung Hana57af4a2020-01-23 05:36:59 +00002105 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002106 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002107 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002108 })
2109}
2110
Jooyung Hane1633032019-08-01 17:41:43 +09002111func TestDependenciesInApexManifest(t *testing.T) {
2112 ctx, _ := testApex(t, `
2113 apex {
2114 name: "myapex_nodep",
2115 key: "myapex.key",
2116 native_shared_libs: ["lib_nodep"],
2117 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002118 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002119 }
2120
2121 apex {
2122 name: "myapex_dep",
2123 key: "myapex.key",
2124 native_shared_libs: ["lib_dep"],
2125 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002126 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002127 }
2128
2129 apex {
2130 name: "myapex_provider",
2131 key: "myapex.key",
2132 native_shared_libs: ["libfoo"],
2133 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002134 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002135 }
2136
2137 apex {
2138 name: "myapex_selfcontained",
2139 key: "myapex.key",
2140 native_shared_libs: ["lib_dep", "libfoo"],
2141 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002142 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002143 }
2144
2145 apex_key {
2146 name: "myapex.key",
2147 public_key: "testkey.avbpubkey",
2148 private_key: "testkey.pem",
2149 }
2150
2151 cc_library {
2152 name: "lib_nodep",
2153 srcs: ["mylib.cpp"],
2154 system_shared_libs: [],
2155 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002156 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002157 }
2158
2159 cc_library {
2160 name: "lib_dep",
2161 srcs: ["mylib.cpp"],
2162 shared_libs: ["libfoo"],
2163 system_shared_libs: [],
2164 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002165 apex_available: [
2166 "myapex_dep",
2167 "myapex_provider",
2168 "myapex_selfcontained",
2169 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002170 }
2171
2172 cc_library {
2173 name: "libfoo",
2174 srcs: ["mytest.cpp"],
2175 stubs: {
2176 versions: ["1"],
2177 },
2178 system_shared_libs: [],
2179 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002180 apex_available: [
2181 "myapex_provider",
2182 "myapex_selfcontained",
2183 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002184 }
2185 `)
2186
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002187 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002188 var provideNativeLibs, requireNativeLibs []string
2189
Sundong Ahnabb64432019-10-22 13:58:29 +09002190 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002191 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2192 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002193 ensureListEmpty(t, provideNativeLibs)
2194 ensureListEmpty(t, requireNativeLibs)
2195
Sundong Ahnabb64432019-10-22 13:58:29 +09002196 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002197 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2198 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002199 ensureListEmpty(t, provideNativeLibs)
2200 ensureListContains(t, requireNativeLibs, "libfoo.so")
2201
Sundong Ahnabb64432019-10-22 13:58:29 +09002202 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002203 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2204 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002205 ensureListContains(t, provideNativeLibs, "libfoo.so")
2206 ensureListEmpty(t, requireNativeLibs)
2207
Sundong Ahnabb64432019-10-22 13:58:29 +09002208 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002209 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2210 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002211 ensureListContains(t, provideNativeLibs, "libfoo.so")
2212 ensureListEmpty(t, requireNativeLibs)
2213}
2214
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002215func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002216 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002217 apex {
2218 name: "myapex",
2219 key: "myapex.key",
2220 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002221 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002222 }
2223
2224 apex_key {
2225 name: "myapex.key",
2226 public_key: "testkey.avbpubkey",
2227 private_key: "testkey.pem",
2228 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002229
2230 cc_library {
2231 name: "mylib",
2232 srcs: ["mylib.cpp"],
2233 system_shared_libs: [],
2234 stl: "none",
2235 apex_available: [
2236 "//apex_available:platform",
2237 "myapex",
2238 ],
2239 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002240 `)
2241
Sundong Ahnabb64432019-10-22 13:58:29 +09002242 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002243 apexManifestRule := module.Rule("apexManifestRule")
2244 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2245 apexRule := module.Rule("apexRule")
2246 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002247
2248 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2249 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2250 name := apexBundle.BaseModuleName()
2251 prefix := "TARGET_"
2252 var builder strings.Builder
2253 data.Custom(&builder, name, prefix, "", data)
2254 androidMk := builder.String()
2255 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2256 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002257}
2258
Alex Light0851b882019-02-07 13:20:53 -08002259func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002260 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002261 apex {
2262 name: "myapex",
2263 key: "myapex.key",
2264 native_shared_libs: ["mylib_common"],
2265 }
2266
2267 apex_key {
2268 name: "myapex.key",
2269 public_key: "testkey.avbpubkey",
2270 private_key: "testkey.pem",
2271 }
2272
2273 cc_library {
2274 name: "mylib_common",
2275 srcs: ["mylib.cpp"],
2276 system_shared_libs: [],
2277 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002278 apex_available: [
2279 "//apex_available:platform",
2280 "myapex",
2281 ],
Alex Light0851b882019-02-07 13:20:53 -08002282 }
2283 `)
2284
Sundong Ahnabb64432019-10-22 13:58:29 +09002285 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002286 apexRule := module.Rule("apexRule")
2287 copyCmds := apexRule.Args["copy_commands"]
2288
2289 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2290 t.Log("Apex was a test apex!")
2291 t.Fail()
2292 }
2293 // Ensure that main rule creates an output
2294 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2295
2296 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002297 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002298
2299 // Ensure that both direct and indirect deps are copied into apex
2300 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2301
Colin Cross7113d202019-11-20 16:39:12 -08002302 // Ensure that the platform variant ends with _shared
2303 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002304
2305 if !android.InAnyApex("mylib_common") {
2306 t.Log("Found mylib_common not in any apex!")
2307 t.Fail()
2308 }
2309}
2310
2311func TestTestApex(t *testing.T) {
2312 if android.InAnyApex("mylib_common_test") {
2313 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!")
2314 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002315 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002316 apex_test {
2317 name: "myapex",
2318 key: "myapex.key",
2319 native_shared_libs: ["mylib_common_test"],
2320 }
2321
2322 apex_key {
2323 name: "myapex.key",
2324 public_key: "testkey.avbpubkey",
2325 private_key: "testkey.pem",
2326 }
2327
2328 cc_library {
2329 name: "mylib_common_test",
2330 srcs: ["mylib.cpp"],
2331 system_shared_libs: [],
2332 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002333 // TODO: remove //apex_available:platform
2334 apex_available: [
2335 "//apex_available:platform",
2336 "myapex",
2337 ],
Alex Light0851b882019-02-07 13:20:53 -08002338 }
2339 `)
2340
Sundong Ahnabb64432019-10-22 13:58:29 +09002341 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002342 apexRule := module.Rule("apexRule")
2343 copyCmds := apexRule.Args["copy_commands"]
2344
2345 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2346 t.Log("Apex was not a test apex!")
2347 t.Fail()
2348 }
2349 // Ensure that main rule creates an output
2350 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2351
2352 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002353 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002354
2355 // Ensure that both direct and indirect deps are copied into apex
2356 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2357
Colin Cross7113d202019-11-20 16:39:12 -08002358 // Ensure that the platform variant ends with _shared
2359 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002360
2361 if android.InAnyApex("mylib_common_test") {
2362 t.Log("Found mylib_common_test in some apex!")
2363 t.Fail()
2364 }
2365}
2366
Alex Light9670d332019-01-29 18:07:33 -08002367func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002368 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002369 apex {
2370 name: "myapex",
2371 key: "myapex.key",
2372 multilib: {
2373 first: {
2374 native_shared_libs: ["mylib_common"],
2375 }
2376 },
2377 target: {
2378 android: {
2379 multilib: {
2380 first: {
2381 native_shared_libs: ["mylib"],
2382 }
2383 }
2384 },
2385 host: {
2386 multilib: {
2387 first: {
2388 native_shared_libs: ["mylib2"],
2389 }
2390 }
2391 }
2392 }
2393 }
2394
2395 apex_key {
2396 name: "myapex.key",
2397 public_key: "testkey.avbpubkey",
2398 private_key: "testkey.pem",
2399 }
2400
2401 cc_library {
2402 name: "mylib",
2403 srcs: ["mylib.cpp"],
2404 system_shared_libs: [],
2405 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002406 // TODO: remove //apex_available:platform
2407 apex_available: [
2408 "//apex_available:platform",
2409 "myapex",
2410 ],
Alex Light9670d332019-01-29 18:07:33 -08002411 }
2412
2413 cc_library {
2414 name: "mylib_common",
2415 srcs: ["mylib.cpp"],
2416 system_shared_libs: [],
2417 stl: "none",
2418 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002419 // TODO: remove //apex_available:platform
2420 apex_available: [
2421 "//apex_available:platform",
2422 "myapex",
2423 ],
Alex Light9670d332019-01-29 18:07:33 -08002424 }
2425
2426 cc_library {
2427 name: "mylib2",
2428 srcs: ["mylib.cpp"],
2429 system_shared_libs: [],
2430 stl: "none",
2431 compile_multilib: "first",
2432 }
2433 `)
2434
Sundong Ahnabb64432019-10-22 13:58:29 +09002435 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002436 copyCmds := apexRule.Args["copy_commands"]
2437
2438 // Ensure that main rule creates an output
2439 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2440
2441 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002442 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2443 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2444 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002445
2446 // Ensure that both direct and indirect deps are copied into apex
2447 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2448 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2449 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2450
Colin Cross7113d202019-11-20 16:39:12 -08002451 // Ensure that the platform variant ends with _shared
2452 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2453 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2454 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002455}
Jiyong Park04480cf2019-02-06 00:16:29 +09002456
2457func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002458 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002459 apex {
2460 name: "myapex",
2461 key: "myapex.key",
2462 binaries: ["myscript"],
2463 }
2464
2465 apex_key {
2466 name: "myapex.key",
2467 public_key: "testkey.avbpubkey",
2468 private_key: "testkey.pem",
2469 }
2470
2471 sh_binary {
2472 name: "myscript",
2473 src: "mylib.cpp",
2474 filename: "myscript.sh",
2475 sub_dir: "script",
2476 }
2477 `)
2478
Sundong Ahnabb64432019-10-22 13:58:29 +09002479 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002480 copyCmds := apexRule.Args["copy_commands"]
2481
2482 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2483}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002484
Jooyung Han91df2082019-11-20 01:49:42 +09002485func TestApexInVariousPartition(t *testing.T) {
2486 testcases := []struct {
2487 propName, parition, flattenedPartition string
2488 }{
2489 {"", "system", "system_ext"},
2490 {"product_specific: true", "product", "product"},
2491 {"soc_specific: true", "vendor", "vendor"},
2492 {"proprietary: true", "vendor", "vendor"},
2493 {"vendor: true", "vendor", "vendor"},
2494 {"system_ext_specific: true", "system_ext", "system_ext"},
2495 }
2496 for _, tc := range testcases {
2497 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2498 ctx, _ := testApex(t, `
2499 apex {
2500 name: "myapex",
2501 key: "myapex.key",
2502 `+tc.propName+`
2503 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002504
Jooyung Han91df2082019-11-20 01:49:42 +09002505 apex_key {
2506 name: "myapex.key",
2507 public_key: "testkey.avbpubkey",
2508 private_key: "testkey.pem",
2509 }
2510 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002511
Jooyung Han91df2082019-11-20 01:49:42 +09002512 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2513 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2514 actual := apex.installDir.String()
2515 if actual != expected {
2516 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2517 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002518
Jooyung Han91df2082019-11-20 01:49:42 +09002519 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2520 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2521 actual = flattened.installDir.String()
2522 if actual != expected {
2523 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2524 }
2525 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002526 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002527}
Jiyong Park67882562019-03-21 01:11:21 +09002528
Jooyung Han54aca7b2019-11-20 02:26:02 +09002529func TestFileContexts(t *testing.T) {
2530 ctx, _ := testApex(t, `
2531 apex {
2532 name: "myapex",
2533 key: "myapex.key",
2534 }
2535
2536 apex_key {
2537 name: "myapex.key",
2538 public_key: "testkey.avbpubkey",
2539 private_key: "testkey.pem",
2540 }
2541 `)
2542 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2543 apexRule := module.Rule("apexRule")
2544 actual := apexRule.Args["file_contexts"]
2545 expected := "system/sepolicy/apex/myapex-file_contexts"
2546 if actual != expected {
2547 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2548 }
2549
2550 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2551 apex {
2552 name: "myapex",
2553 key: "myapex.key",
2554 file_contexts: "my_own_file_contexts",
2555 }
2556
2557 apex_key {
2558 name: "myapex.key",
2559 public_key: "testkey.avbpubkey",
2560 private_key: "testkey.pem",
2561 }
2562 `, withFiles(map[string][]byte{
2563 "my_own_file_contexts": nil,
2564 }))
2565
2566 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2567 apex {
2568 name: "myapex",
2569 key: "myapex.key",
2570 product_specific: true,
2571 file_contexts: "product_specific_file_contexts",
2572 }
2573
2574 apex_key {
2575 name: "myapex.key",
2576 public_key: "testkey.avbpubkey",
2577 private_key: "testkey.pem",
2578 }
2579 `)
2580
2581 ctx, _ = testApex(t, `
2582 apex {
2583 name: "myapex",
2584 key: "myapex.key",
2585 product_specific: true,
2586 file_contexts: "product_specific_file_contexts",
2587 }
2588
2589 apex_key {
2590 name: "myapex.key",
2591 public_key: "testkey.avbpubkey",
2592 private_key: "testkey.pem",
2593 }
2594 `, withFiles(map[string][]byte{
2595 "product_specific_file_contexts": nil,
2596 }))
2597 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2598 apexRule = module.Rule("apexRule")
2599 actual = apexRule.Args["file_contexts"]
2600 expected = "product_specific_file_contexts"
2601 if actual != expected {
2602 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2603 }
2604
2605 ctx, _ = testApex(t, `
2606 apex {
2607 name: "myapex",
2608 key: "myapex.key",
2609 product_specific: true,
2610 file_contexts: ":my-file-contexts",
2611 }
2612
2613 apex_key {
2614 name: "myapex.key",
2615 public_key: "testkey.avbpubkey",
2616 private_key: "testkey.pem",
2617 }
2618
2619 filegroup {
2620 name: "my-file-contexts",
2621 srcs: ["product_specific_file_contexts"],
2622 }
2623 `, withFiles(map[string][]byte{
2624 "product_specific_file_contexts": nil,
2625 }))
2626 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2627 apexRule = module.Rule("apexRule")
2628 actual = apexRule.Args["file_contexts"]
2629 expected = "product_specific_file_contexts"
2630 if actual != expected {
2631 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2632 }
2633}
2634
Jiyong Park67882562019-03-21 01:11:21 +09002635func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002636 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002637 apex_key {
2638 name: "myapex.key",
2639 public_key: ":my.avbpubkey",
2640 private_key: ":my.pem",
2641 product_specific: true,
2642 }
2643
2644 filegroup {
2645 name: "my.avbpubkey",
2646 srcs: ["testkey2.avbpubkey"],
2647 }
2648
2649 filegroup {
2650 name: "my.pem",
2651 srcs: ["testkey2.pem"],
2652 }
2653 `)
2654
2655 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2656 expected_pubkey := "testkey2.avbpubkey"
2657 actual_pubkey := apex_key.public_key_file.String()
2658 if actual_pubkey != expected_pubkey {
2659 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2660 }
2661 expected_privkey := "testkey2.pem"
2662 actual_privkey := apex_key.private_key_file.String()
2663 if actual_privkey != expected_privkey {
2664 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2665 }
2666}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002667
2668func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002669 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002670 prebuilt_apex {
2671 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002672 arch: {
2673 arm64: {
2674 src: "myapex-arm64.apex",
2675 },
2676 arm: {
2677 src: "myapex-arm.apex",
2678 },
2679 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002680 }
2681 `)
2682
2683 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2684
Jiyong Parkc95714e2019-03-29 14:23:10 +09002685 expectedInput := "myapex-arm64.apex"
2686 if prebuilt.inputApex.String() != expectedInput {
2687 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2688 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002689}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002690
2691func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002692 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002693 prebuilt_apex {
2694 name: "myapex",
2695 src: "myapex-arm.apex",
2696 filename: "notmyapex.apex",
2697 }
2698 `)
2699
2700 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2701
2702 expected := "notmyapex.apex"
2703 if p.installFilename != expected {
2704 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2705 }
2706}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002707
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002708func TestPrebuiltOverrides(t *testing.T) {
2709 ctx, config := testApex(t, `
2710 prebuilt_apex {
2711 name: "myapex.prebuilt",
2712 src: "myapex-arm.apex",
2713 overrides: [
2714 "myapex",
2715 ],
2716 }
2717 `)
2718
2719 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2720
2721 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002722 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002723 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002724 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002725 }
2726}
2727
Roland Levillain630846d2019-06-26 12:48:34 +01002728func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002729 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002730 apex_test {
2731 name: "myapex",
2732 key: "myapex.key",
2733 tests: [
2734 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002735 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002736 ],
2737 }
2738
2739 apex_key {
2740 name: "myapex.key",
2741 public_key: "testkey.avbpubkey",
2742 private_key: "testkey.pem",
2743 }
2744
2745 cc_test {
2746 name: "mytest",
2747 gtest: false,
2748 srcs: ["mytest.cpp"],
2749 relative_install_path: "test",
2750 system_shared_libs: [],
2751 static_executable: true,
2752 stl: "none",
2753 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002754
2755 cc_test {
2756 name: "mytests",
2757 gtest: false,
2758 srcs: [
2759 "mytest1.cpp",
2760 "mytest2.cpp",
2761 "mytest3.cpp",
2762 ],
2763 test_per_src: true,
2764 relative_install_path: "test",
2765 system_shared_libs: [],
2766 static_executable: true,
2767 stl: "none",
2768 }
Roland Levillain630846d2019-06-26 12:48:34 +01002769 `)
2770
Sundong Ahnabb64432019-10-22 13:58:29 +09002771 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002772 copyCmds := apexRule.Args["copy_commands"]
2773
2774 // Ensure that test dep is copied into apex.
2775 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002776
2777 // Ensure that test deps built with `test_per_src` are copied into apex.
2778 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2779 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2780 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002781
2782 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002783 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002784 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2785 name := apexBundle.BaseModuleName()
2786 prefix := "TARGET_"
2787 var builder strings.Builder
2788 data.Custom(&builder, name, prefix, "", data)
2789 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002790 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2791 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2792 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2793 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002794 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002795 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002796 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002797}
2798
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002799func TestInstallExtraFlattenedApexes(t *testing.T) {
2800 ctx, config := testApex(t, `
2801 apex {
2802 name: "myapex",
2803 key: "myapex.key",
2804 }
2805 apex_key {
2806 name: "myapex.key",
2807 public_key: "testkey.avbpubkey",
2808 private_key: "testkey.pem",
2809 }
2810 `, func(fs map[string][]byte, config android.Config) {
2811 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2812 })
2813 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002814 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002815 mk := android.AndroidMkDataForTest(t, config, "", ab)
2816 var builder strings.Builder
2817 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2818 androidMk := builder.String()
2819 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2820}
2821
Jooyung Han5c998b92019-06-27 11:30:33 +09002822func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002823 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002824 apex {
2825 name: "myapex",
2826 key: "myapex.key",
2827 native_shared_libs: ["mylib"],
2828 uses: ["commonapex"],
2829 }
2830
2831 apex {
2832 name: "commonapex",
2833 key: "myapex.key",
2834 native_shared_libs: ["libcommon"],
2835 provide_cpp_shared_libs: true,
2836 }
2837
2838 apex_key {
2839 name: "myapex.key",
2840 public_key: "testkey.avbpubkey",
2841 private_key: "testkey.pem",
2842 }
2843
2844 cc_library {
2845 name: "mylib",
2846 srcs: ["mylib.cpp"],
2847 shared_libs: ["libcommon"],
2848 system_shared_libs: [],
2849 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002850 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002851 }
2852
2853 cc_library {
2854 name: "libcommon",
2855 srcs: ["mylib_common.cpp"],
2856 system_shared_libs: [],
2857 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002858 // TODO: remove //apex_available:platform
2859 apex_available: [
2860 "//apex_available:platform",
2861 "commonapex",
2862 "myapex",
2863 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002864 }
2865 `)
2866
Sundong Ahnabb64432019-10-22 13:58:29 +09002867 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002868 apexRule1 := module1.Rule("apexRule")
2869 copyCmds1 := apexRule1.Args["copy_commands"]
2870
Sundong Ahnabb64432019-10-22 13:58:29 +09002871 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002872 apexRule2 := module2.Rule("apexRule")
2873 copyCmds2 := apexRule2.Args["copy_commands"]
2874
Colin Cross7113d202019-11-20 16:39:12 -08002875 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2876 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002877 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2878 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2879 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2880}
2881
2882func TestApexUsesFailsIfNotProvided(t *testing.T) {
2883 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2884 apex {
2885 name: "myapex",
2886 key: "myapex.key",
2887 uses: ["commonapex"],
2888 }
2889
2890 apex {
2891 name: "commonapex",
2892 key: "myapex.key",
2893 }
2894
2895 apex_key {
2896 name: "myapex.key",
2897 public_key: "testkey.avbpubkey",
2898 private_key: "testkey.pem",
2899 }
2900 `)
2901 testApexError(t, `uses: "commonapex" is not a provider`, `
2902 apex {
2903 name: "myapex",
2904 key: "myapex.key",
2905 uses: ["commonapex"],
2906 }
2907
2908 cc_library {
2909 name: "commonapex",
2910 system_shared_libs: [],
2911 stl: "none",
2912 }
2913
2914 apex_key {
2915 name: "myapex.key",
2916 public_key: "testkey.avbpubkey",
2917 private_key: "testkey.pem",
2918 }
2919 `)
2920}
2921
2922func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2923 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2924 apex {
2925 name: "myapex",
2926 key: "myapex.key",
2927 use_vendor: true,
2928 uses: ["commonapex"],
2929 }
2930
2931 apex {
2932 name: "commonapex",
2933 key: "myapex.key",
2934 provide_cpp_shared_libs: true,
2935 }
2936
2937 apex_key {
2938 name: "myapex.key",
2939 public_key: "testkey.avbpubkey",
2940 private_key: "testkey.pem",
2941 }
Jooyung Handc782442019-11-01 03:14:38 +09002942 `, func(fs map[string][]byte, config android.Config) {
2943 setUseVendorWhitelistForTest(config, []string{"myapex"})
2944 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002945}
2946
Jooyung Hand48f3c32019-08-23 11:18:57 +09002947func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2948 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2949 apex {
2950 name: "myapex",
2951 key: "myapex.key",
2952 native_shared_libs: ["libfoo"],
2953 }
2954
2955 apex_key {
2956 name: "myapex.key",
2957 public_key: "testkey.avbpubkey",
2958 private_key: "testkey.pem",
2959 }
2960
2961 cc_library {
2962 name: "libfoo",
2963 stl: "none",
2964 system_shared_libs: [],
2965 enabled: false,
2966 }
2967 `)
2968 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2969 apex {
2970 name: "myapex",
2971 key: "myapex.key",
2972 java_libs: ["myjar"],
2973 }
2974
2975 apex_key {
2976 name: "myapex.key",
2977 public_key: "testkey.avbpubkey",
2978 private_key: "testkey.pem",
2979 }
2980
2981 java_library {
2982 name: "myjar",
2983 srcs: ["foo/bar/MyClass.java"],
2984 sdk_version: "none",
2985 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002986 enabled: false,
2987 }
2988 `)
2989}
2990
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002991func TestApexWithApps(t *testing.T) {
2992 ctx, _ := testApex(t, `
2993 apex {
2994 name: "myapex",
2995 key: "myapex.key",
2996 apps: [
2997 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002998 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002999 ],
3000 }
3001
3002 apex_key {
3003 name: "myapex.key",
3004 public_key: "testkey.avbpubkey",
3005 private_key: "testkey.pem",
3006 }
3007
3008 android_app {
3009 name: "AppFoo",
3010 srcs: ["foo/bar/MyClass.java"],
3011 sdk_version: "none",
3012 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003013 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003014 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003015 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003016
3017 android_app {
3018 name: "AppFooPriv",
3019 srcs: ["foo/bar/MyClass.java"],
3020 sdk_version: "none",
3021 system_modules: "none",
3022 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003023 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003024 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003025
3026 cc_library_shared {
3027 name: "libjni",
3028 srcs: ["mylib.cpp"],
3029 stl: "none",
3030 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003031 apex_available: [ "myapex" ],
Jiyong Park8be103b2019-11-08 15:53:48 +09003032 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003033 `)
3034
Sundong Ahnabb64432019-10-22 13:58:29 +09003035 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003036 apexRule := module.Rule("apexRule")
3037 copyCmds := apexRule.Args["copy_commands"]
3038
3039 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003040 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003041
3042 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003043 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003044 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003045 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3046 // ... uncompressed
3047 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3048 t.Errorf("jni lib is not uncompressed for AppFoo")
3049 }
3050 // ... and not directly inside the APEX
3051 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003052}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003053
Dario Frenicde2a032019-10-27 00:29:22 +01003054func TestApexWithAppImports(t *testing.T) {
3055 ctx, _ := testApex(t, `
3056 apex {
3057 name: "myapex",
3058 key: "myapex.key",
3059 apps: [
3060 "AppFooPrebuilt",
3061 "AppFooPrivPrebuilt",
3062 ],
3063 }
3064
3065 apex_key {
3066 name: "myapex.key",
3067 public_key: "testkey.avbpubkey",
3068 private_key: "testkey.pem",
3069 }
3070
3071 android_app_import {
3072 name: "AppFooPrebuilt",
3073 apk: "PrebuiltAppFoo.apk",
3074 presigned: true,
3075 dex_preopt: {
3076 enabled: false,
3077 },
3078 }
3079
3080 android_app_import {
3081 name: "AppFooPrivPrebuilt",
3082 apk: "PrebuiltAppFooPriv.apk",
3083 privileged: true,
3084 presigned: true,
3085 dex_preopt: {
3086 enabled: false,
3087 },
3088 }
3089 `)
3090
Sundong Ahnabb64432019-10-22 13:58:29 +09003091 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003092 apexRule := module.Rule("apexRule")
3093 copyCmds := apexRule.Args["copy_commands"]
3094
3095 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3096 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003097}
3098
Dario Freni6f3937c2019-12-20 22:58:03 +00003099func TestApexWithTestHelperApp(t *testing.T) {
3100 ctx, _ := testApex(t, `
3101 apex {
3102 name: "myapex",
3103 key: "myapex.key",
3104 apps: [
3105 "TesterHelpAppFoo",
3106 ],
3107 }
3108
3109 apex_key {
3110 name: "myapex.key",
3111 public_key: "testkey.avbpubkey",
3112 private_key: "testkey.pem",
3113 }
3114
3115 android_test_helper_app {
3116 name: "TesterHelpAppFoo",
3117 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003118 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003119 }
3120
3121 `)
3122
3123 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3124 apexRule := module.Rule("apexRule")
3125 copyCmds := apexRule.Args["copy_commands"]
3126
3127 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3128}
3129
Jooyung Han18020ea2019-11-13 10:50:48 +09003130func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3131 // libfoo's apex_available comes from cc_defaults
3132 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3133 apex {
3134 name: "myapex",
3135 key: "myapex.key",
3136 native_shared_libs: ["libfoo"],
3137 }
3138
3139 apex_key {
3140 name: "myapex.key",
3141 public_key: "testkey.avbpubkey",
3142 private_key: "testkey.pem",
3143 }
3144
3145 apex {
3146 name: "otherapex",
3147 key: "myapex.key",
3148 native_shared_libs: ["libfoo"],
3149 }
3150
3151 cc_defaults {
3152 name: "libfoo-defaults",
3153 apex_available: ["otherapex"],
3154 }
3155
3156 cc_library {
3157 name: "libfoo",
3158 defaults: ["libfoo-defaults"],
3159 stl: "none",
3160 system_shared_libs: [],
3161 }`)
3162}
3163
Jiyong Park127b40b2019-09-30 16:04:35 +09003164func TestApexAvailable(t *testing.T) {
3165 // libfoo is not available to myapex, but only to otherapex
3166 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3167 apex {
3168 name: "myapex",
3169 key: "myapex.key",
3170 native_shared_libs: ["libfoo"],
3171 }
3172
3173 apex_key {
3174 name: "myapex.key",
3175 public_key: "testkey.avbpubkey",
3176 private_key: "testkey.pem",
3177 }
3178
3179 apex {
3180 name: "otherapex",
3181 key: "otherapex.key",
3182 native_shared_libs: ["libfoo"],
3183 }
3184
3185 apex_key {
3186 name: "otherapex.key",
3187 public_key: "testkey.avbpubkey",
3188 private_key: "testkey.pem",
3189 }
3190
3191 cc_library {
3192 name: "libfoo",
3193 stl: "none",
3194 system_shared_libs: [],
3195 apex_available: ["otherapex"],
3196 }`)
3197
3198 // libbar is an indirect dep
3199 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3200 apex {
3201 name: "myapex",
3202 key: "myapex.key",
3203 native_shared_libs: ["libfoo"],
3204 }
3205
3206 apex_key {
3207 name: "myapex.key",
3208 public_key: "testkey.avbpubkey",
3209 private_key: "testkey.pem",
3210 }
3211
3212 apex {
3213 name: "otherapex",
3214 key: "otherapex.key",
3215 native_shared_libs: ["libfoo"],
3216 }
3217
3218 apex_key {
3219 name: "otherapex.key",
3220 public_key: "testkey.avbpubkey",
3221 private_key: "testkey.pem",
3222 }
3223
3224 cc_library {
3225 name: "libfoo",
3226 stl: "none",
3227 shared_libs: ["libbar"],
3228 system_shared_libs: [],
3229 apex_available: ["myapex", "otherapex"],
3230 }
3231
3232 cc_library {
3233 name: "libbar",
3234 stl: "none",
3235 system_shared_libs: [],
3236 apex_available: ["otherapex"],
3237 }`)
3238
3239 testApexError(t, "\"otherapex\" is not a valid module name", `
3240 apex {
3241 name: "myapex",
3242 key: "myapex.key",
3243 native_shared_libs: ["libfoo"],
3244 }
3245
3246 apex_key {
3247 name: "myapex.key",
3248 public_key: "testkey.avbpubkey",
3249 private_key: "testkey.pem",
3250 }
3251
3252 cc_library {
3253 name: "libfoo",
3254 stl: "none",
3255 system_shared_libs: [],
3256 apex_available: ["otherapex"],
3257 }`)
3258
3259 ctx, _ := testApex(t, `
3260 apex {
3261 name: "myapex",
3262 key: "myapex.key",
3263 native_shared_libs: ["libfoo", "libbar"],
3264 }
3265
3266 apex_key {
3267 name: "myapex.key",
3268 public_key: "testkey.avbpubkey",
3269 private_key: "testkey.pem",
3270 }
3271
3272 cc_library {
3273 name: "libfoo",
3274 stl: "none",
3275 system_shared_libs: [],
3276 apex_available: ["myapex"],
3277 }
3278
3279 cc_library {
3280 name: "libbar",
3281 stl: "none",
3282 system_shared_libs: [],
3283 apex_available: ["//apex_available:anyapex"],
3284 }`)
3285
3286 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003287 // TODO(jiyong) the checks for the platform variant are removed because we now create
3288 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3289 // the platform variants are not used from other platform modules. When that is done,
3290 // these checks will be replaced by expecting a specific error message that will be
3291 // emitted when the platform variant is used.
3292 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3293 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3294 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3295 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003296
3297 ctx, _ = testApex(t, `
3298 apex {
3299 name: "myapex",
3300 key: "myapex.key",
3301 }
3302
3303 apex_key {
3304 name: "myapex.key",
3305 public_key: "testkey.avbpubkey",
3306 private_key: "testkey.pem",
3307 }
3308
3309 cc_library {
3310 name: "libfoo",
3311 stl: "none",
3312 system_shared_libs: [],
3313 apex_available: ["//apex_available:platform"],
3314 }`)
3315
3316 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003317 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3318 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003319
3320 ctx, _ = testApex(t, `
3321 apex {
3322 name: "myapex",
3323 key: "myapex.key",
3324 native_shared_libs: ["libfoo"],
3325 }
3326
3327 apex_key {
3328 name: "myapex.key",
3329 public_key: "testkey.avbpubkey",
3330 private_key: "testkey.pem",
3331 }
3332
3333 cc_library {
3334 name: "libfoo",
3335 stl: "none",
3336 system_shared_libs: [],
3337 apex_available: ["myapex"],
3338 static: {
3339 apex_available: ["//apex_available:platform"],
3340 },
3341 }`)
3342
3343 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003344 // TODO(jiyong) the checks for the platform variant are removed because we now create
3345 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3346 // the platform variants are not used from other platform modules. When that is done,
3347 // these checks will be replaced by expecting a specific error message that will be
3348 // emitted when the platform variant is used.
3349 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3350 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3351 // // but the static variant is available to both myapex and the platform
3352 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3353 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003354}
3355
Jiyong Park5d790c32019-11-15 18:40:32 +09003356func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003357 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003358 apex {
3359 name: "myapex",
3360 key: "myapex.key",
3361 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003362 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003363 }
3364
3365 override_apex {
3366 name: "override_myapex",
3367 base: "myapex",
3368 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003369 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003370 }
3371
3372 apex_key {
3373 name: "myapex.key",
3374 public_key: "testkey.avbpubkey",
3375 private_key: "testkey.pem",
3376 }
3377
3378 android_app {
3379 name: "app",
3380 srcs: ["foo/bar/MyClass.java"],
3381 package_name: "foo",
3382 sdk_version: "none",
3383 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003384 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003385 }
3386
3387 override_android_app {
3388 name: "override_app",
3389 base: "app",
3390 package_name: "bar",
3391 }
3392 `)
3393
Jiyong Park317645e2019-12-05 13:20:58 +09003394 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3395 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3396 if originalVariant.GetOverriddenBy() != "" {
3397 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3398 }
3399 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3400 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3401 }
3402
Jiyong Park5d790c32019-11-15 18:40:32 +09003403 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3404 apexRule := module.Rule("apexRule")
3405 copyCmds := apexRule.Args["copy_commands"]
3406
3407 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3408 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003409
3410 apexBundle := module.Module().(*apexBundle)
3411 name := apexBundle.Name()
3412 if name != "override_myapex" {
3413 t.Errorf("name should be \"override_myapex\", but was %q", name)
3414 }
3415
3416 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3417 var builder strings.Builder
3418 data.Custom(&builder, name, "TARGET_", "", data)
3419 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003420 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003421 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3422 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003423 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003424 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003425 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003426 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3427 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003428}
3429
Jooyung Han214bf372019-11-12 13:03:50 +09003430func TestLegacyAndroid10Support(t *testing.T) {
3431 ctx, _ := testApex(t, `
3432 apex {
3433 name: "myapex",
3434 key: "myapex.key",
3435 legacy_android10_support: true,
3436 }
3437
3438 apex_key {
3439 name: "myapex.key",
3440 public_key: "testkey.avbpubkey",
3441 private_key: "testkey.pem",
3442 }
3443 `)
3444
3445 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3446 args := module.Rule("apexRule").Args
3447 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003448 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003449}
3450
Jooyung Han58f26ab2019-12-18 15:34:32 +09003451func TestJavaSDKLibrary(t *testing.T) {
3452 ctx, _ := testApex(t, `
3453 apex {
3454 name: "myapex",
3455 key: "myapex.key",
3456 java_libs: ["foo"],
3457 }
3458
3459 apex_key {
3460 name: "myapex.key",
3461 public_key: "testkey.avbpubkey",
3462 private_key: "testkey.pem",
3463 }
3464
3465 java_sdk_library {
3466 name: "foo",
3467 srcs: ["a.java"],
3468 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003469 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003470 }
3471 `, withFiles(map[string][]byte{
3472 "api/current.txt": nil,
3473 "api/removed.txt": nil,
3474 "api/system-current.txt": nil,
3475 "api/system-removed.txt": nil,
3476 "api/test-current.txt": nil,
3477 "api/test-removed.txt": nil,
3478 }))
3479
3480 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003481 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003482 "javalib/foo.jar",
3483 "etc/permissions/foo.xml",
3484 })
3485 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffine74ac732020-02-06 13:51:46 +00003486 sdkLibrary := ctx.ModuleForTests("foo", "android_common_myapex").Module().(*java.SdkLibrary)
3487 xml := sdkLibrary.XmlPermissionsFileContent()
3488 ensureContains(t, xml, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003489}
3490
atrost6e126252020-01-27 17:01:16 +00003491func TestCompatConfig(t *testing.T) {
3492 ctx, _ := testApex(t, `
3493 apex {
3494 name: "myapex",
3495 key: "myapex.key",
3496 prebuilts: ["myjar-platform-compat-config"],
3497 java_libs: ["myjar"],
3498 }
3499
3500 apex_key {
3501 name: "myapex.key",
3502 public_key: "testkey.avbpubkey",
3503 private_key: "testkey.pem",
3504 }
3505
3506 platform_compat_config {
3507 name: "myjar-platform-compat-config",
3508 src: ":myjar",
3509 }
3510
3511 java_library {
3512 name: "myjar",
3513 srcs: ["foo/bar/MyClass.java"],
3514 sdk_version: "none",
3515 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003516 apex_available: [ "myapex" ],
3517 }
3518 `)
3519 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3520 "etc/compatconfig/myjar-platform-compat-config.xml",
3521 "javalib/myjar.jar",
3522 })
3523}
3524
Jiyong Park479321d2019-12-16 11:47:12 +09003525func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3526 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3527 apex {
3528 name: "myapex",
3529 key: "myapex.key",
3530 java_libs: ["myjar"],
3531 }
3532
3533 apex_key {
3534 name: "myapex.key",
3535 public_key: "testkey.avbpubkey",
3536 private_key: "testkey.pem",
3537 }
3538
3539 java_library {
3540 name: "myjar",
3541 srcs: ["foo/bar/MyClass.java"],
3542 sdk_version: "none",
3543 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003544 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003545 }
3546 `)
3547}
3548
Jiyong Park7afd1072019-12-30 16:56:33 +09003549func TestCarryRequiredModuleNames(t *testing.T) {
3550 ctx, config := testApex(t, `
3551 apex {
3552 name: "myapex",
3553 key: "myapex.key",
3554 native_shared_libs: ["mylib"],
3555 }
3556
3557 apex_key {
3558 name: "myapex.key",
3559 public_key: "testkey.avbpubkey",
3560 private_key: "testkey.pem",
3561 }
3562
3563 cc_library {
3564 name: "mylib",
3565 srcs: ["mylib.cpp"],
3566 system_shared_libs: [],
3567 stl: "none",
3568 required: ["a", "b"],
3569 host_required: ["c", "d"],
3570 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003571 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003572 }
3573 `)
3574
3575 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3576 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3577 name := apexBundle.BaseModuleName()
3578 prefix := "TARGET_"
3579 var builder strings.Builder
3580 data.Custom(&builder, name, prefix, "", data)
3581 androidMk := builder.String()
3582 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3583 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3584 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3585}
3586
Jiyong Park7cd10e32020-01-14 09:22:18 +09003587func TestSymlinksFromApexToSystem(t *testing.T) {
3588 bp := `
3589 apex {
3590 name: "myapex",
3591 key: "myapex.key",
3592 native_shared_libs: ["mylib"],
3593 java_libs: ["myjar"],
3594 }
3595
3596 apex_key {
3597 name: "myapex.key",
3598 public_key: "testkey.avbpubkey",
3599 private_key: "testkey.pem",
3600 }
3601
3602 cc_library {
3603 name: "mylib",
3604 srcs: ["mylib.cpp"],
3605 shared_libs: ["myotherlib"],
3606 system_shared_libs: [],
3607 stl: "none",
3608 apex_available: [
3609 "myapex",
3610 "//apex_available:platform",
3611 ],
3612 }
3613
3614 cc_library {
3615 name: "myotherlib",
3616 srcs: ["mylib.cpp"],
3617 system_shared_libs: [],
3618 stl: "none",
3619 apex_available: [
3620 "myapex",
3621 "//apex_available:platform",
3622 ],
3623 }
3624
3625 java_library {
3626 name: "myjar",
3627 srcs: ["foo/bar/MyClass.java"],
3628 sdk_version: "none",
3629 system_modules: "none",
3630 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003631 apex_available: [
3632 "myapex",
3633 "//apex_available:platform",
3634 ],
3635 }
3636
3637 java_library {
3638 name: "myotherjar",
3639 srcs: ["foo/bar/MyClass.java"],
3640 sdk_version: "none",
3641 system_modules: "none",
3642 apex_available: [
3643 "myapex",
3644 "//apex_available:platform",
3645 ],
3646 }
3647 `
3648
3649 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3650 for _, f := range files {
3651 if f.path == file {
3652 if f.isLink {
3653 t.Errorf("%q is not a real file", file)
3654 }
3655 return
3656 }
3657 }
3658 t.Errorf("%q is not found", file)
3659 }
3660
3661 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3662 for _, f := range files {
3663 if f.path == file {
3664 if !f.isLink {
3665 t.Errorf("%q is not a symlink", file)
3666 }
3667 return
3668 }
3669 }
3670 t.Errorf("%q is not found", file)
3671 }
3672
3673 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003674 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003675 ensureRealfileExists(t, files, "javalib/myjar.jar")
3676 ensureRealfileExists(t, files, "lib64/mylib.so")
3677 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3678
3679 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003680 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003681 ensureRealfileExists(t, files, "javalib/myjar.jar")
3682 ensureRealfileExists(t, files, "lib64/mylib.so")
3683 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
3684}
3685
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003686func TestMain(m *testing.M) {
3687 run := func() int {
3688 setUp()
3689 defer tearDown()
3690
3691 return m.Run()
3692 }
3693
3694 os.Exit(run())
3695}