blob: e41b6d5654d054a7b38c56f040b0b77de8816836 [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 Park9d677202020-02-19 16:29:35 +0900226 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900227 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900228 "system/sepolicy/apex/otherapex-file_contexts": nil,
229 "system/sepolicy/apex/commonapex-file_contexts": nil,
230 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800231 "mylib.cpp": nil,
232 "mylib_common.cpp": nil,
233 "mytest.cpp": nil,
234 "mytest1.cpp": nil,
235 "mytest2.cpp": nil,
236 "mytest3.cpp": nil,
237 "myprebuilt": nil,
238 "my_include": nil,
239 "foo/bar/MyClass.java": nil,
240 "prebuilt.jar": nil,
241 "vendor/foo/devkeys/test.x509.pem": nil,
242 "vendor/foo/devkeys/test.pk8": nil,
243 "testkey.x509.pem": nil,
244 "testkey.pk8": nil,
245 "testkey.override.x509.pem": nil,
246 "testkey.override.pk8": nil,
247 "vendor/foo/devkeys/testkey.avbpubkey": nil,
248 "vendor/foo/devkeys/testkey.pem": nil,
249 "NOTICE": nil,
250 "custom_notice": nil,
251 "testkey2.avbpubkey": nil,
252 "testkey2.pem": nil,
253 "myapex-arm64.apex": nil,
254 "myapex-arm.apex": nil,
255 "frameworks/base/api/current.txt": nil,
256 "framework/aidl/a.aidl": nil,
257 "build/make/core/proguard.flags": nil,
258 "build/make/core/proguard_basic_keeps.flags": nil,
259 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900260 }
261
262 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800263 // The fs now needs to be populated before creating the config, call handlers twice
264 // for now, once to get any fs changes, and later after the config was created to
265 // set product variables or targets.
266 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
267 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900268 }
269
Colin Cross98be1bb2019-12-13 20:41:13 -0800270 config := android.TestArchConfig(buildDir, nil, bp, fs)
271 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
272 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
273 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
274 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
275 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
276 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
277
278 for _, handler := range handlers {
279 // The fs now needs to be populated before creating the config, call handlers twice
280 // for now, earlier to get any fs changes, and now after the config was created to
281 // set product variables or targets.
282 tempFS := map[string][]byte{}
283 handler(tempFS, config)
284 }
285
286 ctx := android.NewTestArchContext()
287 ctx.RegisterModuleType("apex", BundleFactory)
288 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
289 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
290 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
291 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
292 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
293 ctx.RegisterModuleType("override_apex", overrideApexFactory)
294
Jooyung Hana57af4a2020-01-23 05:36:59 +0000295 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
296 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
297
Paul Duffin77980a82019-12-19 16:01:36 +0000298 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800299 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800300 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
301 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800302 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
atrost6e126252020-01-27 17:01:16 +0000303 ctx.RegisterModuleType("platform_compat_config", java.PlatformCompatConfigFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800304 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800305 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000306 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000307 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000308 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900309 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800310
Colin Cross98be1bb2019-12-13 20:41:13 -0800311 ctx.PreDepsMutators(RegisterPreDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800312 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800313
314 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315
Jooyung Han5c998b92019-06-27 11:30:33 +0900316 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317}
318
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700319func setUp() {
320 var err error
321 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700323 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325}
326
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700327func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328 os.RemoveAll(buildDir)
329}
330
331// ensure that 'result' contains 'expected'
332func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900333 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334 if !strings.Contains(result, expected) {
335 t.Errorf("%q is not found in %q", expected, result)
336 }
337}
338
339// ensures that 'result' does not contain 'notExpected'
340func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if strings.Contains(result, notExpected) {
343 t.Errorf("%q is found in %q", notExpected, result)
344 }
345}
346
347func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if !android.InList(expected, result) {
350 t.Errorf("%q is not found in %v", expected, result)
351 }
352}
353
354func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900355 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 if android.InList(notExpected, result) {
357 t.Errorf("%q is found in %v", notExpected, result)
358 }
359}
360
Jooyung Hane1633032019-08-01 17:41:43 +0900361func ensureListEmpty(t *testing.T, result []string) {
362 t.Helper()
363 if len(result) > 0 {
364 t.Errorf("%q is expected to be empty", result)
365 }
366}
367
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368// Minimal test
369func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700370 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900371 apex_defaults {
372 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900373 manifest: ":myapex.manifest",
374 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900375 key: "myapex.key",
376 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800377 multilib: {
378 both: {
379 binaries: ["foo",],
380 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900381 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900382 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 }
384
Jiyong Park30ca9372019-02-07 16:27:23 +0900385 apex {
386 name: "myapex",
387 defaults: ["myapex-defaults"],
388 }
389
Jiyong Park25fc6a92018-11-18 18:02:45 +0900390 apex_key {
391 name: "myapex.key",
392 public_key: "testkey.avbpubkey",
393 private_key: "testkey.pem",
394 }
395
Jiyong Park809bb722019-02-13 21:33:49 +0900396 filegroup {
397 name: "myapex.manifest",
398 srcs: ["apex_manifest.json"],
399 }
400
401 filegroup {
402 name: "myapex.androidmanifest",
403 srcs: ["AndroidManifest.xml"],
404 }
405
Jiyong Park25fc6a92018-11-18 18:02:45 +0900406 cc_library {
407 name: "mylib",
408 srcs: ["mylib.cpp"],
409 shared_libs: ["mylib2"],
410 system_shared_libs: [],
411 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000412 // TODO: remove //apex_available:platform
413 apex_available: [
414 "//apex_available:platform",
415 "myapex",
416 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900417 }
418
Alex Light3d673592019-01-18 14:37:31 -0800419 cc_binary {
420 name: "foo",
421 srcs: ["mylib.cpp"],
422 compile_multilib: "both",
423 multilib: {
424 lib32: {
425 suffix: "32",
426 },
427 lib64: {
428 suffix: "64",
429 },
430 },
431 symlinks: ["foo_link_"],
432 symlink_preferred_arch: true,
433 system_shared_libs: [],
434 static_executable: true,
435 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000436 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800437 }
438
Jiyong Park25fc6a92018-11-18 18:02:45 +0900439 cc_library {
440 name: "mylib2",
441 srcs: ["mylib.cpp"],
442 system_shared_libs: [],
443 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900444 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000445 // TODO: remove //apex_available:platform
446 apex_available: [
447 "//apex_available:platform",
448 "myapex",
449 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900450 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900451
452 java_library {
453 name: "myjar",
454 srcs: ["foo/bar/MyClass.java"],
455 sdk_version: "none",
456 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900457 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900458 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900464 }
465
466 java_library {
467 name: "myotherjar",
468 srcs: ["foo/bar/MyClass.java"],
469 sdk_version: "none",
470 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900477
478 java_library {
479 name: "mysharedjar",
480 srcs: ["foo/bar/MyClass.java"],
481 sdk_version: "none",
482 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900484 `)
485
Sundong Ahnabb64432019-10-22 13:58:29 +0900486 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900487
488 optFlags := apexRule.Args["opt_flags"]
489 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700490 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900491 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900492
Jiyong Park25fc6a92018-11-18 18:02:45 +0900493 copyCmds := apexRule.Args["copy_commands"]
494
495 // Ensure that main rule creates an output
496 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
497
498 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800499 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900501
502 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800503 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900504 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900505
506 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800507 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
508 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900509 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
510 // .. but not for java libs
511 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900512 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800513
Colin Cross7113d202019-11-20 16:39:12 -0800514 // Ensure that the platform variant ends with _shared or _common
515 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
516 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900517 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
518 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900519 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
520
521 // Ensure that dynamic dependency to java libs are not included
522 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800523
524 // Ensure that all symlinks are present.
525 found_foo_link_64 := false
526 found_foo := false
527 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900528 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800529 if strings.HasSuffix(cmd, "bin/foo") {
530 found_foo = true
531 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
532 found_foo_link_64 = true
533 }
534 }
535 }
536 good := found_foo && found_foo_link_64
537 if !good {
538 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
539 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900540
Sundong Ahnabb64432019-10-22 13:58:29 +0900541 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700542 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700543 if len(noticeInputs) != 2 {
544 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900545 }
546 ensureListContains(t, noticeInputs, "NOTICE")
547 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900548
549 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900550 ensureListContains(t, depsInfo, "myjar <- myapex")
551 ensureListContains(t, depsInfo, "mylib <- myapex")
552 ensureListContains(t, depsInfo, "mylib2 <- mylib")
553 ensureListContains(t, depsInfo, "myotherjar <- myjar")
554 ensureListContains(t, depsInfo, "mysharedjar (external) <- myjar")
Alex Light5098a612018-11-29 17:12:15 -0800555}
556
Jooyung Hanf21c7972019-12-16 22:32:06 +0900557func TestDefaults(t *testing.T) {
558 ctx, _ := testApex(t, `
559 apex_defaults {
560 name: "myapex-defaults",
561 key: "myapex.key",
562 prebuilts: ["myetc"],
563 native_shared_libs: ["mylib"],
564 java_libs: ["myjar"],
565 apps: ["AppFoo"],
566 }
567
568 prebuilt_etc {
569 name: "myetc",
570 src: "myprebuilt",
571 }
572
573 apex {
574 name: "myapex",
575 defaults: ["myapex-defaults"],
576 }
577
578 apex_key {
579 name: "myapex.key",
580 public_key: "testkey.avbpubkey",
581 private_key: "testkey.pem",
582 }
583
584 cc_library {
585 name: "mylib",
586 system_shared_libs: [],
587 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000588 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900589 }
590
591 java_library {
592 name: "myjar",
593 srcs: ["foo/bar/MyClass.java"],
594 sdk_version: "none",
595 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000596 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900597 }
598
599 android_app {
600 name: "AppFoo",
601 srcs: ["foo/bar/MyClass.java"],
602 sdk_version: "none",
603 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000604 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900605 }
606 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000607 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900608 "etc/myetc",
609 "javalib/myjar.jar",
610 "lib64/mylib.so",
611 "app/AppFoo/AppFoo.apk",
612 })
613}
614
Jooyung Han01a3ee22019-11-02 02:52:25 +0900615func TestApexManifest(t *testing.T) {
616 ctx, _ := testApex(t, `
617 apex {
618 name: "myapex",
619 key: "myapex.key",
620 }
621
622 apex_key {
623 name: "myapex.key",
624 public_key: "testkey.avbpubkey",
625 private_key: "testkey.pem",
626 }
627 `)
628
629 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900630 args := module.Rule("apexRule").Args
631 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
632 t.Error("manifest should be apex_manifest.pb, but " + manifest)
633 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900634}
635
Alex Light5098a612018-11-29 17:12:15 -0800636func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700637 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800638 apex {
639 name: "myapex",
640 key: "myapex.key",
641 payload_type: "zip",
642 native_shared_libs: ["mylib"],
643 }
644
645 apex_key {
646 name: "myapex.key",
647 public_key: "testkey.avbpubkey",
648 private_key: "testkey.pem",
649 }
650
651 cc_library {
652 name: "mylib",
653 srcs: ["mylib.cpp"],
654 shared_libs: ["mylib2"],
655 system_shared_libs: [],
656 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800658 }
659
660 cc_library {
661 name: "mylib2",
662 srcs: ["mylib.cpp"],
663 system_shared_libs: [],
664 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800666 }
667 `)
668
Sundong Ahnabb64432019-10-22 13:58:29 +0900669 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800670 copyCmds := zipApexRule.Args["copy_commands"]
671
672 // Ensure that main rule creates an output
673 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
674
675 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800676 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800677
678 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800679 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800680
681 // Ensure that both direct and indirect deps are copied into apex
682 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
683 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900684}
685
686func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700687 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900688 apex {
689 name: "myapex",
690 key: "myapex.key",
691 native_shared_libs: ["mylib", "mylib3"],
692 }
693
694 apex_key {
695 name: "myapex.key",
696 public_key: "testkey.avbpubkey",
697 private_key: "testkey.pem",
698 }
699
700 cc_library {
701 name: "mylib",
702 srcs: ["mylib.cpp"],
703 shared_libs: ["mylib2", "mylib3"],
704 system_shared_libs: [],
705 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000706 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900707 }
708
709 cc_library {
710 name: "mylib2",
711 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900712 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900713 system_shared_libs: [],
714 stl: "none",
715 stubs: {
716 versions: ["1", "2", "3"],
717 },
718 }
719
720 cc_library {
721 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900722 srcs: ["mylib.cpp"],
723 shared_libs: ["mylib4"],
724 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725 stl: "none",
726 stubs: {
727 versions: ["10", "11", "12"],
728 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000729 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900731
732 cc_library {
733 name: "mylib4",
734 srcs: ["mylib.cpp"],
735 system_shared_libs: [],
736 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000737 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900738 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900739 `)
740
Sundong Ahnabb64432019-10-22 13:58:29 +0900741 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742 copyCmds := apexRule.Args["copy_commands"]
743
744 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800745 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900746
747 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800748 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900749
750 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800751 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900752
Colin Cross7113d202019-11-20 16:39:12 -0800753 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900754
755 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900756 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900757 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900758 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900759
760 // 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 -0800761 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900762 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800763 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900764
765 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900766 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900767 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900768
769 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900770 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900771
Jooyung Hana57af4a2020-01-23 05:36:59 +0000772 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900773 "lib64/mylib.so",
774 "lib64/mylib3.so",
775 "lib64/mylib4.so",
776 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900777}
778
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900779func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700780 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900782 name: "myapex2",
783 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900784 native_shared_libs: ["mylib"],
785 }
786
787 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900788 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900789 public_key: "testkey.avbpubkey",
790 private_key: "testkey.pem",
791 }
792
793 cc_library {
794 name: "mylib",
795 srcs: ["mylib.cpp"],
796 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +0900797 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900798 system_shared_libs: [],
799 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000800 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900801 }
802
803 cc_library {
804 name: "libfoo",
805 srcs: ["mylib.cpp"],
806 shared_libs: ["libbar"],
807 system_shared_libs: [],
808 stl: "none",
809 stubs: {
810 versions: ["10", "20", "30"],
811 },
812 }
813
814 cc_library {
815 name: "libbar",
816 srcs: ["mylib.cpp"],
817 system_shared_libs: [],
818 stl: "none",
819 }
820
Jiyong Park678c8812020-02-07 17:25:49 +0900821 cc_library_static {
822 name: "libbaz",
823 srcs: ["mylib.cpp"],
824 system_shared_libs: [],
825 stl: "none",
826 apex_available: [ "myapex2" ],
827 }
828
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900829 `)
830
Jiyong Park83dc74b2020-01-14 18:38:44 +0900831 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900832 copyCmds := apexRule.Args["copy_commands"]
833
834 // Ensure that direct non-stubs dep is always included
835 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
836
837 // Ensure that indirect stubs dep is not included
838 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
839
840 // Ensure that dependency of stubs is not included
841 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
842
Jiyong Park83dc74b2020-01-14 18:38:44 +0900843 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900844
845 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900846 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900847 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900848 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900849
Jiyong Park3ff16992019-12-27 14:11:47 +0900850 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900851
852 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
853 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900854
855 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
Jiyong Park678c8812020-02-07 17:25:49 +0900856
857 ensureListContains(t, depsInfo, "mylib <- myapex2")
858 ensureListContains(t, depsInfo, "libbaz <- mylib")
859 ensureListContains(t, depsInfo, "libfoo (external) <- mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900860}
861
Jooyung Hand3639552019-08-09 12:57:43 +0900862func TestApexWithRuntimeLibsDependency(t *testing.T) {
863 /*
864 myapex
865 |
866 v (runtime_libs)
867 mylib ------+------> libfoo [provides stub]
868 |
869 `------> libbar
870 */
871 ctx, _ := testApex(t, `
872 apex {
873 name: "myapex",
874 key: "myapex.key",
875 native_shared_libs: ["mylib"],
876 }
877
878 apex_key {
879 name: "myapex.key",
880 public_key: "testkey.avbpubkey",
881 private_key: "testkey.pem",
882 }
883
884 cc_library {
885 name: "mylib",
886 srcs: ["mylib.cpp"],
887 runtime_libs: ["libfoo", "libbar"],
888 system_shared_libs: [],
889 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000890 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900891 }
892
893 cc_library {
894 name: "libfoo",
895 srcs: ["mylib.cpp"],
896 system_shared_libs: [],
897 stl: "none",
898 stubs: {
899 versions: ["10", "20", "30"],
900 },
Jiyong Park0f80c182020-01-31 02:49:53 +0900901 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900902 }
903
904 cc_library {
905 name: "libbar",
906 srcs: ["mylib.cpp"],
907 system_shared_libs: [],
908 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000909 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900910 }
911
912 `)
913
Sundong Ahnabb64432019-10-22 13:58:29 +0900914 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900915 copyCmds := apexRule.Args["copy_commands"]
916
917 // Ensure that direct non-stubs dep is always included
918 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
919
920 // Ensure that indirect stubs dep is not included
921 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
922
923 // Ensure that runtime_libs dep in included
924 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
925
Sundong Ahnabb64432019-10-22 13:58:29 +0900926 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900927 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
928 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900929
930}
931
Jooyung Han9c80bae2019-08-20 17:30:57 +0900932func TestApexDependencyToLLNDK(t *testing.T) {
933 ctx, _ := testApex(t, `
934 apex {
935 name: "myapex",
936 key: "myapex.key",
937 use_vendor: true,
938 native_shared_libs: ["mylib"],
939 }
940
941 apex_key {
942 name: "myapex.key",
943 public_key: "testkey.avbpubkey",
944 private_key: "testkey.pem",
945 }
946
947 cc_library {
948 name: "mylib",
949 srcs: ["mylib.cpp"],
950 vendor_available: true,
951 shared_libs: ["libbar"],
952 system_shared_libs: [],
953 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000954 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900955 }
956
957 cc_library {
958 name: "libbar",
959 srcs: ["mylib.cpp"],
960 system_shared_libs: [],
961 stl: "none",
962 }
963
964 llndk_library {
965 name: "libbar",
966 symbol_file: "",
967 }
Jooyung Handc782442019-11-01 03:14:38 +0900968 `, func(fs map[string][]byte, config android.Config) {
969 setUseVendorWhitelistForTest(config, []string{"myapex"})
970 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900971
Sundong Ahnabb64432019-10-22 13:58:29 +0900972 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900973 copyCmds := apexRule.Args["copy_commands"]
974
975 // Ensure that LLNDK dep is not included
976 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
977
Sundong Ahnabb64432019-10-22 13:58:29 +0900978 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900979 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900980
981 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900982 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900983
984}
985
Jiyong Park25fc6a92018-11-18 18:02:45 +0900986func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700987 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900988 apex {
989 name: "myapex",
990 key: "myapex.key",
991 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
992 }
993
994 apex_key {
995 name: "myapex.key",
996 public_key: "testkey.avbpubkey",
997 private_key: "testkey.pem",
998 }
999
1000 cc_library {
1001 name: "mylib",
1002 srcs: ["mylib.cpp"],
1003 shared_libs: ["libdl#27"],
1004 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001005 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006 }
1007
1008 cc_library_shared {
1009 name: "mylib_shared",
1010 srcs: ["mylib.cpp"],
1011 shared_libs: ["libdl#27"],
1012 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001013 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014 }
1015
1016 cc_library {
1017 name: "libc",
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 },
1025 }
1026
1027 cc_library {
1028 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001029 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001030 nocrt: true,
1031 system_shared_libs: [],
1032 stl: "none",
1033 stubs: {
1034 versions: ["27", "28", "29"],
1035 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001036 apex_available: [
1037 "//apex_available:platform",
1038 "myapex"
1039 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001040 }
1041
1042 cc_library {
1043 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001044 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001045 nocrt: true,
1046 system_shared_libs: [],
1047 stl: "none",
1048 stubs: {
1049 versions: ["27", "28", "29"],
1050 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001051 apex_available: [
1052 "//apex_available:platform",
1053 "myapex"
1054 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001055 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001056
1057 cc_library {
1058 name: "libBootstrap",
1059 srcs: ["mylib.cpp"],
1060 stl: "none",
1061 bootstrap: true,
1062 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001063 `)
1064
Sundong Ahnabb64432019-10-22 13:58:29 +09001065 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001066 copyCmds := apexRule.Args["copy_commands"]
1067
1068 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001069 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001070 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1071 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001072
1073 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001074 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001075
Colin Cross7113d202019-11-20 16:39:12 -08001076 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1077 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1078 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001079
1080 // For dependency to libc
1081 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001082 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001083 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001084 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001085 // ... Cflags from stub is correctly exported to mylib
1086 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1087 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1088
1089 // For dependency to libm
1090 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001091 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001092 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001093 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001094 // ... and is not compiling with the stub
1095 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1096 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1097
1098 // For dependency to libdl
1099 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001100 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001101 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001102 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1103 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001104 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001105 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001106 // ... Cflags from stub is correctly exported to mylib
1107 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1108 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001109
1110 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001111 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1112 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1113 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1114 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001115}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001116
1117func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001118 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001119 apex {
1120 name: "myapex",
1121 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001122 native_shared_libs: ["mylib"],
1123 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001124 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001125 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001126 }
1127
1128 apex_key {
1129 name: "myapex.key",
1130 public_key: "testkey.avbpubkey",
1131 private_key: "testkey.pem",
1132 }
1133
1134 prebuilt_etc {
1135 name: "myetc",
1136 src: "myprebuilt",
1137 sub_dir: "foo/bar",
1138 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001139
1140 cc_library {
1141 name: "mylib",
1142 srcs: ["mylib.cpp"],
1143 relative_install_path: "foo/bar",
1144 system_shared_libs: [],
1145 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001146 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001147 }
1148
1149 cc_binary {
1150 name: "mybin",
1151 srcs: ["mylib.cpp"],
1152 relative_install_path: "foo/bar",
1153 system_shared_libs: [],
1154 static_executable: true,
1155 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001156 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001157 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001158 `)
1159
Sundong Ahnabb64432019-10-22 13:58:29 +09001160 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001161 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1162
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001163 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001164 ensureListContains(t, dirs, "etc")
1165 ensureListContains(t, dirs, "etc/foo")
1166 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001167 ensureListContains(t, dirs, "lib64")
1168 ensureListContains(t, dirs, "lib64/foo")
1169 ensureListContains(t, dirs, "lib64/foo/bar")
1170 ensureListContains(t, dirs, "lib")
1171 ensureListContains(t, dirs, "lib/foo")
1172 ensureListContains(t, dirs, "lib/foo/bar")
1173
Jiyong Parkbd13e442019-03-15 18:10:35 +09001174 ensureListContains(t, dirs, "bin")
1175 ensureListContains(t, dirs, "bin/foo")
1176 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001177}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001178
1179func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001180 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001181 apex {
1182 name: "myapex",
1183 key: "myapex.key",
1184 native_shared_libs: ["mylib"],
1185 use_vendor: true,
1186 }
1187
1188 apex_key {
1189 name: "myapex.key",
1190 public_key: "testkey.avbpubkey",
1191 private_key: "testkey.pem",
1192 }
1193
1194 cc_library {
1195 name: "mylib",
1196 srcs: ["mylib.cpp"],
1197 shared_libs: ["mylib2"],
1198 system_shared_libs: [],
1199 vendor_available: true,
1200 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001201 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001202 }
1203
1204 cc_library {
1205 name: "mylib2",
1206 srcs: ["mylib.cpp"],
1207 system_shared_libs: [],
1208 vendor_available: true,
1209 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001210 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001211 }
Jooyung Handc782442019-11-01 03:14:38 +09001212 `, func(fs map[string][]byte, config android.Config) {
1213 setUseVendorWhitelistForTest(config, []string{"myapex"})
1214 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001215
1216 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001217 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001218 for _, implicit := range i.Implicits {
1219 inputsList = append(inputsList, implicit.String())
1220 }
1221 }
1222 inputsString := strings.Join(inputsList, " ")
1223
1224 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001225 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1226 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001227
1228 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001229 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1230 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001231}
Jiyong Park16e91a02018-12-20 18:18:08 +09001232
Jooyung Handc782442019-11-01 03:14:38 +09001233func TestUseVendorRestriction(t *testing.T) {
1234 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1235 apex {
1236 name: "myapex",
1237 key: "myapex.key",
1238 use_vendor: true,
1239 }
1240 apex_key {
1241 name: "myapex.key",
1242 public_key: "testkey.avbpubkey",
1243 private_key: "testkey.pem",
1244 }
1245 `, func(fs map[string][]byte, config android.Config) {
1246 setUseVendorWhitelistForTest(config, []string{""})
1247 })
1248 // no error with whitelist
1249 testApex(t, `
1250 apex {
1251 name: "myapex",
1252 key: "myapex.key",
1253 use_vendor: true,
1254 }
1255 apex_key {
1256 name: "myapex.key",
1257 public_key: "testkey.avbpubkey",
1258 private_key: "testkey.pem",
1259 }
1260 `, func(fs map[string][]byte, config android.Config) {
1261 setUseVendorWhitelistForTest(config, []string{"myapex"})
1262 })
1263}
1264
Jooyung Han5c998b92019-06-27 11:30:33 +09001265func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1266 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1267 apex {
1268 name: "myapex",
1269 key: "myapex.key",
1270 native_shared_libs: ["mylib"],
1271 use_vendor: true,
1272 }
1273
1274 apex_key {
1275 name: "myapex.key",
1276 public_key: "testkey.avbpubkey",
1277 private_key: "testkey.pem",
1278 }
1279
1280 cc_library {
1281 name: "mylib",
1282 srcs: ["mylib.cpp"],
1283 system_shared_libs: [],
1284 stl: "none",
1285 }
1286 `)
1287}
1288
Jiyong Park16e91a02018-12-20 18:18:08 +09001289func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001290 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001291 apex {
1292 name: "myapex",
1293 key: "myapex.key",
1294 native_shared_libs: ["mylib"],
1295 }
1296
1297 apex_key {
1298 name: "myapex.key",
1299 public_key: "testkey.avbpubkey",
1300 private_key: "testkey.pem",
1301 }
1302
1303 cc_library {
1304 name: "mylib",
1305 srcs: ["mylib.cpp"],
1306 system_shared_libs: [],
1307 stl: "none",
1308 stubs: {
1309 versions: ["1", "2", "3"],
1310 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001311 apex_available: [
1312 "//apex_available:platform",
1313 "myapex",
1314 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001315 }
1316
1317 cc_binary {
1318 name: "not_in_apex",
1319 srcs: ["mylib.cpp"],
1320 static_libs: ["mylib"],
1321 static_executable: true,
1322 system_shared_libs: [],
1323 stl: "none",
1324 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001325 `)
1326
Colin Cross7113d202019-11-20 16:39:12 -08001327 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001328
1329 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001330 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001331}
Jiyong Park9335a262018-12-24 11:31:58 +09001332
1333func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001334 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001335 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001336 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001337 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001338 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001339 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001340 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001341 }
1342
1343 cc_library {
1344 name: "mylib",
1345 srcs: ["mylib.cpp"],
1346 system_shared_libs: [],
1347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001348 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001349 }
1350
1351 apex_key {
1352 name: "myapex.key",
1353 public_key: "testkey.avbpubkey",
1354 private_key: "testkey.pem",
1355 }
1356
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001357 android_app_certificate {
1358 name: "myapex.certificate",
1359 certificate: "testkey",
1360 }
1361
1362 android_app_certificate {
1363 name: "myapex.certificate.override",
1364 certificate: "testkey.override",
1365 }
1366
Jiyong Park9335a262018-12-24 11:31:58 +09001367 `)
1368
1369 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001370 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001371
1372 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1373 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1374 "vendor/foo/devkeys/testkey.avbpubkey")
1375 }
1376 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1377 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1378 "vendor/foo/devkeys/testkey.pem")
1379 }
1380
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001381 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001382 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001383 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001384 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001385 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001386 }
1387}
Jiyong Park58e364a2019-01-19 19:24:06 +09001388
Jooyung Hanf121a652019-12-17 14:30:11 +09001389func TestCertificate(t *testing.T) {
1390 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1391 ctx, _ := testApex(t, `
1392 apex {
1393 name: "myapex",
1394 key: "myapex.key",
1395 }
1396 apex_key {
1397 name: "myapex.key",
1398 public_key: "testkey.avbpubkey",
1399 private_key: "testkey.pem",
1400 }`)
1401 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1402 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1403 if actual := rule.Args["certificates"]; actual != expected {
1404 t.Errorf("certificates should be %q, not %q", expected, actual)
1405 }
1406 })
1407 t.Run("override when unspecified", func(t *testing.T) {
1408 ctx, _ := testApex(t, `
1409 apex {
1410 name: "myapex_keytest",
1411 key: "myapex.key",
1412 file_contexts: ":myapex-file_contexts",
1413 }
1414 apex_key {
1415 name: "myapex.key",
1416 public_key: "testkey.avbpubkey",
1417 private_key: "testkey.pem",
1418 }
1419 android_app_certificate {
1420 name: "myapex.certificate.override",
1421 certificate: "testkey.override",
1422 }`)
1423 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1424 expected := "testkey.override.x509.pem testkey.override.pk8"
1425 if actual := rule.Args["certificates"]; actual != expected {
1426 t.Errorf("certificates should be %q, not %q", expected, actual)
1427 }
1428 })
1429 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1430 ctx, _ := testApex(t, `
1431 apex {
1432 name: "myapex",
1433 key: "myapex.key",
1434 certificate: ":myapex.certificate",
1435 }
1436 apex_key {
1437 name: "myapex.key",
1438 public_key: "testkey.avbpubkey",
1439 private_key: "testkey.pem",
1440 }
1441 android_app_certificate {
1442 name: "myapex.certificate",
1443 certificate: "testkey",
1444 }`)
1445 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1446 expected := "testkey.x509.pem testkey.pk8"
1447 if actual := rule.Args["certificates"]; actual != expected {
1448 t.Errorf("certificates should be %q, not %q", expected, actual)
1449 }
1450 })
1451 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1452 ctx, _ := testApex(t, `
1453 apex {
1454 name: "myapex_keytest",
1455 key: "myapex.key",
1456 file_contexts: ":myapex-file_contexts",
1457 certificate: ":myapex.certificate",
1458 }
1459 apex_key {
1460 name: "myapex.key",
1461 public_key: "testkey.avbpubkey",
1462 private_key: "testkey.pem",
1463 }
1464 android_app_certificate {
1465 name: "myapex.certificate.override",
1466 certificate: "testkey.override",
1467 }`)
1468 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1469 expected := "testkey.override.x509.pem testkey.override.pk8"
1470 if actual := rule.Args["certificates"]; actual != expected {
1471 t.Errorf("certificates should be %q, not %q", expected, actual)
1472 }
1473 })
1474 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1475 ctx, _ := testApex(t, `
1476 apex {
1477 name: "myapex",
1478 key: "myapex.key",
1479 certificate: "testkey",
1480 }
1481 apex_key {
1482 name: "myapex.key",
1483 public_key: "testkey.avbpubkey",
1484 private_key: "testkey.pem",
1485 }`)
1486 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1487 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1488 if actual := rule.Args["certificates"]; actual != expected {
1489 t.Errorf("certificates should be %q, not %q", expected, actual)
1490 }
1491 })
1492 t.Run("override when specified as <name>", func(t *testing.T) {
1493 ctx, _ := testApex(t, `
1494 apex {
1495 name: "myapex_keytest",
1496 key: "myapex.key",
1497 file_contexts: ":myapex-file_contexts",
1498 certificate: "testkey",
1499 }
1500 apex_key {
1501 name: "myapex.key",
1502 public_key: "testkey.avbpubkey",
1503 private_key: "testkey.pem",
1504 }
1505 android_app_certificate {
1506 name: "myapex.certificate.override",
1507 certificate: "testkey.override",
1508 }`)
1509 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1510 expected := "testkey.override.x509.pem testkey.override.pk8"
1511 if actual := rule.Args["certificates"]; actual != expected {
1512 t.Errorf("certificates should be %q, not %q", expected, actual)
1513 }
1514 })
1515}
1516
Jiyong Park58e364a2019-01-19 19:24:06 +09001517func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001518 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001519 apex {
1520 name: "myapex",
1521 key: "myapex.key",
1522 native_shared_libs: ["mylib"],
1523 }
1524
1525 apex {
1526 name: "otherapex",
1527 key: "myapex.key",
1528 native_shared_libs: ["mylib"],
1529 }
1530
1531 apex_key {
1532 name: "myapex.key",
1533 public_key: "testkey.avbpubkey",
1534 private_key: "testkey.pem",
1535 }
1536
1537 cc_library {
1538 name: "mylib",
1539 srcs: ["mylib.cpp"],
1540 system_shared_libs: [],
1541 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001542 // TODO: remove //apex_available:platform
1543 apex_available: [
1544 "//apex_available:platform",
1545 "myapex",
1546 "otherapex",
1547 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001548 }
1549 `)
1550
Jooyung Han6b8459b2019-10-30 08:29:25 +09001551 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001552 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001553 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001554 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1555 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001556
Jooyung Han6b8459b2019-10-30 08:29:25 +09001557 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001558 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001559 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001560 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1561 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001562
Jooyung Han6b8459b2019-10-30 08:29:25 +09001563 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001564 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001565 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001566 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1567 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001568}
Jiyong Park7e636d02019-01-28 16:16:54 +09001569
1570func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001571 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001572 apex {
1573 name: "myapex",
1574 key: "myapex.key",
1575 native_shared_libs: ["mylib"],
1576 }
1577
1578 apex_key {
1579 name: "myapex.key",
1580 public_key: "testkey.avbpubkey",
1581 private_key: "testkey.pem",
1582 }
1583
1584 cc_library_headers {
1585 name: "mylib_headers",
1586 export_include_dirs: ["my_include"],
1587 system_shared_libs: [],
1588 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09001589 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001590 }
1591
1592 cc_library {
1593 name: "mylib",
1594 srcs: ["mylib.cpp"],
1595 system_shared_libs: [],
1596 stl: "none",
1597 header_libs: ["mylib_headers"],
1598 export_header_lib_headers: ["mylib_headers"],
1599 stubs: {
1600 versions: ["1", "2", "3"],
1601 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001602 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001603 }
1604
1605 cc_library {
1606 name: "otherlib",
1607 srcs: ["mylib.cpp"],
1608 system_shared_libs: [],
1609 stl: "none",
1610 shared_libs: ["mylib"],
1611 }
1612 `)
1613
Colin Cross7113d202019-11-20 16:39:12 -08001614 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001615
1616 // Ensure that the include path of the header lib is exported to 'otherlib'
1617 ensureContains(t, cFlags, "-Imy_include")
1618}
Alex Light9670d332019-01-29 18:07:33 -08001619
Jiyong Park7cd10e32020-01-14 09:22:18 +09001620type fileInApex struct {
1621 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00001622 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09001623 isLink bool
1624}
1625
Jooyung Hana57af4a2020-01-23 05:36:59 +00001626func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09001627 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00001628 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001629 copyCmds := apexRule.Args["copy_commands"]
1630 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09001631 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09001632 for _, cmd := range strings.Split(copyCmds, "&&") {
1633 cmd = strings.TrimSpace(cmd)
1634 if cmd == "" {
1635 continue
1636 }
1637 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00001638 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09001639 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09001640 switch terms[0] {
1641 case "mkdir":
1642 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09001643 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09001644 t.Fatal("copyCmds contains invalid cp command", cmd)
1645 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001646 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001647 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001648 isLink = false
1649 case "ln":
1650 if len(terms) != 3 && len(terms) != 4 {
1651 // ln LINK TARGET or ln -s LINK TARGET
1652 t.Fatal("copyCmds contains invalid ln command", cmd)
1653 }
1654 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001655 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09001656 isLink = true
1657 default:
1658 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1659 }
1660 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09001661 index := strings.Index(dst, imageApexDir)
1662 if index == -1 {
1663 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1664 }
1665 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00001666 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09001667 }
1668 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09001669 return ret
1670}
1671
Jooyung Hana57af4a2020-01-23 05:36:59 +00001672func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
1673 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09001674 var failed bool
1675 var surplus []string
1676 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00001677 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09001678 for _, expected := range files {
1679 if matched, _ := path.Match(expected, file.path); matched {
1680 filesMatched[expected] = true
1681 return
1682 }
1683 }
1684 surplus = append(surplus, file.path)
1685 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001686
Jooyung Han31c470b2019-10-18 16:26:59 +09001687 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001688 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001689 t.Log("surplus files", surplus)
1690 failed = true
1691 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001692
1693 if len(files) > len(filesMatched) {
1694 var missing []string
1695 for _, expected := range files {
1696 if !filesMatched[expected] {
1697 missing = append(missing, expected)
1698 }
1699 }
1700 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001701 t.Log("missing files", missing)
1702 failed = true
1703 }
1704 if failed {
1705 t.Fail()
1706 }
1707}
1708
Jooyung Han344d5432019-08-23 11:17:39 +09001709func TestVndkApexCurrent(t *testing.T) {
1710 ctx, _ := testApex(t, `
1711 apex_vndk {
1712 name: "myapex",
1713 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001714 }
1715
1716 apex_key {
1717 name: "myapex.key",
1718 public_key: "testkey.avbpubkey",
1719 private_key: "testkey.pem",
1720 }
1721
1722 cc_library {
1723 name: "libvndk",
1724 srcs: ["mylib.cpp"],
1725 vendor_available: true,
1726 vndk: {
1727 enabled: true,
1728 },
1729 system_shared_libs: [],
1730 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001731 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001732 }
1733
1734 cc_library {
1735 name: "libvndksp",
1736 srcs: ["mylib.cpp"],
1737 vendor_available: true,
1738 vndk: {
1739 enabled: true,
1740 support_system_process: true,
1741 },
1742 system_shared_libs: [],
1743 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001744 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001745 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001746 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001747
Jooyung Hana57af4a2020-01-23 05:36:59 +00001748 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001749 "lib/libvndk.so",
1750 "lib/libvndksp.so",
1751 "lib64/libvndk.so",
1752 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001753 "etc/llndk.libraries.VER.txt",
1754 "etc/vndkcore.libraries.VER.txt",
1755 "etc/vndksp.libraries.VER.txt",
1756 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001757 })
Jooyung Han344d5432019-08-23 11:17:39 +09001758}
1759
1760func TestVndkApexWithPrebuilt(t *testing.T) {
1761 ctx, _ := testApex(t, `
1762 apex_vndk {
1763 name: "myapex",
1764 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001765 }
1766
1767 apex_key {
1768 name: "myapex.key",
1769 public_key: "testkey.avbpubkey",
1770 private_key: "testkey.pem",
1771 }
1772
1773 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001774 name: "libvndk",
1775 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001776 vendor_available: true,
1777 vndk: {
1778 enabled: true,
1779 },
1780 system_shared_libs: [],
1781 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001782 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001783 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001784
1785 cc_prebuilt_library_shared {
1786 name: "libvndk.arm",
1787 srcs: ["libvndk.arm.so"],
1788 vendor_available: true,
1789 vndk: {
1790 enabled: true,
1791 },
1792 enabled: false,
1793 arch: {
1794 arm: {
1795 enabled: true,
1796 },
1797 },
1798 system_shared_libs: [],
1799 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001800 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001801 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001802 `+vndkLibrariesTxtFiles("current"),
1803 withFiles(map[string][]byte{
1804 "libvndk.so": nil,
1805 "libvndk.arm.so": nil,
1806 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001807
Jooyung Hana57af4a2020-01-23 05:36:59 +00001808 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001809 "lib/libvndk.so",
1810 "lib/libvndk.arm.so",
1811 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001812 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001813 })
Jooyung Han344d5432019-08-23 11:17:39 +09001814}
1815
Jooyung Han39edb6c2019-11-06 16:53:07 +09001816func vndkLibrariesTxtFiles(vers ...string) (result string) {
1817 for _, v := range vers {
1818 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001819 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001820 result += `
1821 vndk_libraries_txt {
1822 name: "` + txt + `.libraries.txt",
1823 }
1824 `
1825 }
1826 } else {
1827 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1828 result += `
1829 prebuilt_etc {
1830 name: "` + txt + `.libraries.` + v + `.txt",
1831 src: "dummy.txt",
1832 }
1833 `
1834 }
1835 }
1836 }
1837 return
1838}
1839
Jooyung Han344d5432019-08-23 11:17:39 +09001840func TestVndkApexVersion(t *testing.T) {
1841 ctx, _ := testApex(t, `
1842 apex_vndk {
1843 name: "myapex_v27",
1844 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001845 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001846 vndk_version: "27",
1847 }
1848
1849 apex_key {
1850 name: "myapex.key",
1851 public_key: "testkey.avbpubkey",
1852 private_key: "testkey.pem",
1853 }
1854
Jooyung Han31c470b2019-10-18 16:26:59 +09001855 vndk_prebuilt_shared {
1856 name: "libvndk27",
1857 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001858 vendor_available: true,
1859 vndk: {
1860 enabled: true,
1861 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001862 target_arch: "arm64",
1863 arch: {
1864 arm: {
1865 srcs: ["libvndk27_arm.so"],
1866 },
1867 arm64: {
1868 srcs: ["libvndk27_arm64.so"],
1869 },
1870 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001871 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001872 }
1873
1874 vndk_prebuilt_shared {
1875 name: "libvndk27",
1876 version: "27",
1877 vendor_available: true,
1878 vndk: {
1879 enabled: true,
1880 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001881 target_arch: "x86_64",
1882 arch: {
1883 x86: {
1884 srcs: ["libvndk27_x86.so"],
1885 },
1886 x86_64: {
1887 srcs: ["libvndk27_x86_64.so"],
1888 },
1889 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001890 }
1891 `+vndkLibrariesTxtFiles("27"),
1892 withFiles(map[string][]byte{
1893 "libvndk27_arm.so": nil,
1894 "libvndk27_arm64.so": nil,
1895 "libvndk27_x86.so": nil,
1896 "libvndk27_x86_64.so": nil,
1897 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001898
Jooyung Hana57af4a2020-01-23 05:36:59 +00001899 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09001900 "lib/libvndk27_arm.so",
1901 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001902 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001903 })
Jooyung Han344d5432019-08-23 11:17:39 +09001904}
1905
1906func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1907 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1908 apex_vndk {
1909 name: "myapex_v27",
1910 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001911 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001912 vndk_version: "27",
1913 }
1914 apex_vndk {
1915 name: "myapex_v27_other",
1916 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001917 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001918 vndk_version: "27",
1919 }
1920
1921 apex_key {
1922 name: "myapex.key",
1923 public_key: "testkey.avbpubkey",
1924 private_key: "testkey.pem",
1925 }
1926
1927 cc_library {
1928 name: "libvndk",
1929 srcs: ["mylib.cpp"],
1930 vendor_available: true,
1931 vndk: {
1932 enabled: true,
1933 },
1934 system_shared_libs: [],
1935 stl: "none",
1936 }
1937
1938 vndk_prebuilt_shared {
1939 name: "libvndk",
1940 version: "27",
1941 vendor_available: true,
1942 vndk: {
1943 enabled: true,
1944 },
1945 srcs: ["libvndk.so"],
1946 }
1947 `, withFiles(map[string][]byte{
1948 "libvndk.so": nil,
1949 }))
1950}
1951
Jooyung Han90eee022019-10-01 20:02:42 +09001952func TestVndkApexNameRule(t *testing.T) {
1953 ctx, _ := testApex(t, `
1954 apex_vndk {
1955 name: "myapex",
1956 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001957 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001958 }
1959 apex_vndk {
1960 name: "myapex_v28",
1961 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001962 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001963 vndk_version: "28",
1964 }
1965 apex_key {
1966 name: "myapex.key",
1967 public_key: "testkey.avbpubkey",
1968 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001969 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001970
1971 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00001972 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001973 actual := proptools.String(bundle.properties.Apex_name)
1974 if !reflect.DeepEqual(actual, expected) {
1975 t.Errorf("Got '%v', expected '%v'", actual, expected)
1976 }
1977 }
1978
1979 assertApexName("com.android.vndk.vVER", "myapex")
1980 assertApexName("com.android.vndk.v28", "myapex_v28")
1981}
1982
Jooyung Han344d5432019-08-23 11:17:39 +09001983func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1984 ctx, _ := testApex(t, `
1985 apex_vndk {
1986 name: "myapex",
1987 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001988 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001989 }
1990
1991 apex_key {
1992 name: "myapex.key",
1993 public_key: "testkey.avbpubkey",
1994 private_key: "testkey.pem",
1995 }
1996
1997 cc_library {
1998 name: "libvndk",
1999 srcs: ["mylib.cpp"],
2000 vendor_available: true,
2001 native_bridge_supported: true,
2002 host_supported: true,
2003 vndk: {
2004 enabled: true,
2005 },
2006 system_shared_libs: [],
2007 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002008 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09002009 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002010 `+vndkLibrariesTxtFiles("current"),
2011 withTargets(map[android.OsType][]android.Target{
2012 android.Android: []android.Target{
2013 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2014 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2015 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
2016 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
2017 },
2018 }))
Jooyung Han344d5432019-08-23 11:17:39 +09002019
Jooyung Hana57af4a2020-01-23 05:36:59 +00002020 ensureExactContents(t, ctx, "myapex", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002021 "lib/libvndk.so",
2022 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002023 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002024 })
Jooyung Han344d5432019-08-23 11:17:39 +09002025}
2026
2027func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
2028 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
2029 apex_vndk {
2030 name: "myapex",
2031 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002032 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09002033 native_bridge_supported: true,
2034 }
2035
2036 apex_key {
2037 name: "myapex.key",
2038 public_key: "testkey.avbpubkey",
2039 private_key: "testkey.pem",
2040 }
2041
2042 cc_library {
2043 name: "libvndk",
2044 srcs: ["mylib.cpp"],
2045 vendor_available: true,
2046 native_bridge_supported: true,
2047 host_supported: true,
2048 vndk: {
2049 enabled: true,
2050 },
2051 system_shared_libs: [],
2052 stl: "none",
2053 }
2054 `)
2055}
2056
Jooyung Han31c470b2019-10-18 16:26:59 +09002057func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002058 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002059 apex_vndk {
2060 name: "myapex_v27",
2061 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002062 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002063 vndk_version: "27",
2064 }
2065
2066 apex_key {
2067 name: "myapex.key",
2068 public_key: "testkey.avbpubkey",
2069 private_key: "testkey.pem",
2070 }
2071
2072 vndk_prebuilt_shared {
2073 name: "libvndk27",
2074 version: "27",
2075 target_arch: "arm",
2076 vendor_available: true,
2077 vndk: {
2078 enabled: true,
2079 },
2080 arch: {
2081 arm: {
2082 srcs: ["libvndk27.so"],
2083 }
2084 },
2085 }
2086
2087 vndk_prebuilt_shared {
2088 name: "libvndk27",
2089 version: "27",
2090 target_arch: "arm",
2091 binder32bit: true,
2092 vendor_available: true,
2093 vndk: {
2094 enabled: true,
2095 },
2096 arch: {
2097 arm: {
2098 srcs: ["libvndk27binder32.so"],
2099 }
2100 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002101 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002102 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002103 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002104 withFiles(map[string][]byte{
2105 "libvndk27.so": nil,
2106 "libvndk27binder32.so": nil,
2107 }),
2108 withBinder32bit,
2109 withTargets(map[android.OsType][]android.Target{
2110 android.Android: []android.Target{
2111 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2112 },
2113 }),
2114 )
2115
Jooyung Hana57af4a2020-01-23 05:36:59 +00002116 ensureExactContents(t, ctx, "myapex_v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09002117 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002118 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002119 })
2120}
2121
Jooyung Hane1633032019-08-01 17:41:43 +09002122func TestDependenciesInApexManifest(t *testing.T) {
2123 ctx, _ := testApex(t, `
2124 apex {
2125 name: "myapex_nodep",
2126 key: "myapex.key",
2127 native_shared_libs: ["lib_nodep"],
2128 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002129 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002130 }
2131
2132 apex {
2133 name: "myapex_dep",
2134 key: "myapex.key",
2135 native_shared_libs: ["lib_dep"],
2136 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002137 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002138 }
2139
2140 apex {
2141 name: "myapex_provider",
2142 key: "myapex.key",
2143 native_shared_libs: ["libfoo"],
2144 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002145 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002146 }
2147
2148 apex {
2149 name: "myapex_selfcontained",
2150 key: "myapex.key",
2151 native_shared_libs: ["lib_dep", "libfoo"],
2152 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002153 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002154 }
2155
2156 apex_key {
2157 name: "myapex.key",
2158 public_key: "testkey.avbpubkey",
2159 private_key: "testkey.pem",
2160 }
2161
2162 cc_library {
2163 name: "lib_nodep",
2164 srcs: ["mylib.cpp"],
2165 system_shared_libs: [],
2166 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002167 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002168 }
2169
2170 cc_library {
2171 name: "lib_dep",
2172 srcs: ["mylib.cpp"],
2173 shared_libs: ["libfoo"],
2174 system_shared_libs: [],
2175 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002176 apex_available: [
2177 "myapex_dep",
2178 "myapex_provider",
2179 "myapex_selfcontained",
2180 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002181 }
2182
2183 cc_library {
2184 name: "libfoo",
2185 srcs: ["mytest.cpp"],
2186 stubs: {
2187 versions: ["1"],
2188 },
2189 system_shared_libs: [],
2190 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002191 apex_available: [
2192 "myapex_provider",
2193 "myapex_selfcontained",
2194 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002195 }
2196 `)
2197
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002198 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002199 var provideNativeLibs, requireNativeLibs []string
2200
Sundong Ahnabb64432019-10-22 13:58:29 +09002201 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002202 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2203 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002204 ensureListEmpty(t, provideNativeLibs)
2205 ensureListEmpty(t, requireNativeLibs)
2206
Sundong Ahnabb64432019-10-22 13:58:29 +09002207 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002208 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2209 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002210 ensureListEmpty(t, provideNativeLibs)
2211 ensureListContains(t, requireNativeLibs, "libfoo.so")
2212
Sundong Ahnabb64432019-10-22 13:58:29 +09002213 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002214 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2215 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002216 ensureListContains(t, provideNativeLibs, "libfoo.so")
2217 ensureListEmpty(t, requireNativeLibs)
2218
Sundong Ahnabb64432019-10-22 13:58:29 +09002219 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002220 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2221 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002222 ensureListContains(t, provideNativeLibs, "libfoo.so")
2223 ensureListEmpty(t, requireNativeLibs)
2224}
2225
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002226func TestApexName(t *testing.T) {
Jiyong Parkdb334862020-02-05 17:19:28 +09002227 ctx, config := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002228 apex {
2229 name: "myapex",
2230 key: "myapex.key",
2231 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09002232 native_shared_libs: ["mylib"],
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002233 }
2234
2235 apex_key {
2236 name: "myapex.key",
2237 public_key: "testkey.avbpubkey",
2238 private_key: "testkey.pem",
2239 }
Jiyong Parkdb334862020-02-05 17:19:28 +09002240
2241 cc_library {
2242 name: "mylib",
2243 srcs: ["mylib.cpp"],
2244 system_shared_libs: [],
2245 stl: "none",
2246 apex_available: [
2247 "//apex_available:platform",
2248 "myapex",
2249 ],
2250 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002251 `)
2252
Sundong Ahnabb64432019-10-22 13:58:29 +09002253 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002254 apexManifestRule := module.Rule("apexManifestRule")
2255 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2256 apexRule := module.Rule("apexRule")
2257 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09002258
2259 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2260 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2261 name := apexBundle.BaseModuleName()
2262 prefix := "TARGET_"
2263 var builder strings.Builder
2264 data.Custom(&builder, name, prefix, "", data)
2265 androidMk := builder.String()
2266 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
2267 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002268}
2269
Alex Light0851b882019-02-07 13:20:53 -08002270func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002271 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002272 apex {
2273 name: "myapex",
2274 key: "myapex.key",
2275 native_shared_libs: ["mylib_common"],
2276 }
2277
2278 apex_key {
2279 name: "myapex.key",
2280 public_key: "testkey.avbpubkey",
2281 private_key: "testkey.pem",
2282 }
2283
2284 cc_library {
2285 name: "mylib_common",
2286 srcs: ["mylib.cpp"],
2287 system_shared_libs: [],
2288 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002289 apex_available: [
2290 "//apex_available:platform",
2291 "myapex",
2292 ],
Alex Light0851b882019-02-07 13:20:53 -08002293 }
2294 `)
2295
Sundong Ahnabb64432019-10-22 13:58:29 +09002296 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002297 apexRule := module.Rule("apexRule")
2298 copyCmds := apexRule.Args["copy_commands"]
2299
2300 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2301 t.Log("Apex was a test apex!")
2302 t.Fail()
2303 }
2304 // Ensure that main rule creates an output
2305 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2306
2307 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002308 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002309
2310 // Ensure that both direct and indirect deps are copied into apex
2311 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2312
Colin Cross7113d202019-11-20 16:39:12 -08002313 // Ensure that the platform variant ends with _shared
2314 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002315
2316 if !android.InAnyApex("mylib_common") {
2317 t.Log("Found mylib_common not in any apex!")
2318 t.Fail()
2319 }
2320}
2321
2322func TestTestApex(t *testing.T) {
2323 if android.InAnyApex("mylib_common_test") {
2324 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!")
2325 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002326 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002327 apex_test {
2328 name: "myapex",
2329 key: "myapex.key",
2330 native_shared_libs: ["mylib_common_test"],
2331 }
2332
2333 apex_key {
2334 name: "myapex.key",
2335 public_key: "testkey.avbpubkey",
2336 private_key: "testkey.pem",
2337 }
2338
2339 cc_library {
2340 name: "mylib_common_test",
2341 srcs: ["mylib.cpp"],
2342 system_shared_libs: [],
2343 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002344 // TODO: remove //apex_available:platform
2345 apex_available: [
2346 "//apex_available:platform",
2347 "myapex",
2348 ],
Alex Light0851b882019-02-07 13:20:53 -08002349 }
2350 `)
2351
Sundong Ahnabb64432019-10-22 13:58:29 +09002352 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002353 apexRule := module.Rule("apexRule")
2354 copyCmds := apexRule.Args["copy_commands"]
2355
2356 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2357 t.Log("Apex was not a test apex!")
2358 t.Fail()
2359 }
2360 // Ensure that main rule creates an output
2361 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2362
2363 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002364 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002365
2366 // Ensure that both direct and indirect deps are copied into apex
2367 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2368
Colin Cross7113d202019-11-20 16:39:12 -08002369 // Ensure that the platform variant ends with _shared
2370 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002371}
2372
Alex Light9670d332019-01-29 18:07:33 -08002373func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002374 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002375 apex {
2376 name: "myapex",
2377 key: "myapex.key",
2378 multilib: {
2379 first: {
2380 native_shared_libs: ["mylib_common"],
2381 }
2382 },
2383 target: {
2384 android: {
2385 multilib: {
2386 first: {
2387 native_shared_libs: ["mylib"],
2388 }
2389 }
2390 },
2391 host: {
2392 multilib: {
2393 first: {
2394 native_shared_libs: ["mylib2"],
2395 }
2396 }
2397 }
2398 }
2399 }
2400
2401 apex_key {
2402 name: "myapex.key",
2403 public_key: "testkey.avbpubkey",
2404 private_key: "testkey.pem",
2405 }
2406
2407 cc_library {
2408 name: "mylib",
2409 srcs: ["mylib.cpp"],
2410 system_shared_libs: [],
2411 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002412 // TODO: remove //apex_available:platform
2413 apex_available: [
2414 "//apex_available:platform",
2415 "myapex",
2416 ],
Alex Light9670d332019-01-29 18:07:33 -08002417 }
2418
2419 cc_library {
2420 name: "mylib_common",
2421 srcs: ["mylib.cpp"],
2422 system_shared_libs: [],
2423 stl: "none",
2424 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002425 // TODO: remove //apex_available:platform
2426 apex_available: [
2427 "//apex_available:platform",
2428 "myapex",
2429 ],
Alex Light9670d332019-01-29 18:07:33 -08002430 }
2431
2432 cc_library {
2433 name: "mylib2",
2434 srcs: ["mylib.cpp"],
2435 system_shared_libs: [],
2436 stl: "none",
2437 compile_multilib: "first",
2438 }
2439 `)
2440
Sundong Ahnabb64432019-10-22 13:58:29 +09002441 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002442 copyCmds := apexRule.Args["copy_commands"]
2443
2444 // Ensure that main rule creates an output
2445 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2446
2447 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002448 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2449 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2450 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002451
2452 // Ensure that both direct and indirect deps are copied into apex
2453 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2454 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2455 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2456
Colin Cross7113d202019-11-20 16:39:12 -08002457 // Ensure that the platform variant ends with _shared
2458 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2459 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2460 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002461}
Jiyong Park04480cf2019-02-06 00:16:29 +09002462
2463func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002464 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002465 apex {
2466 name: "myapex",
2467 key: "myapex.key",
2468 binaries: ["myscript"],
2469 }
2470
2471 apex_key {
2472 name: "myapex.key",
2473 public_key: "testkey.avbpubkey",
2474 private_key: "testkey.pem",
2475 }
2476
2477 sh_binary {
2478 name: "myscript",
2479 src: "mylib.cpp",
2480 filename: "myscript.sh",
2481 sub_dir: "script",
2482 }
2483 `)
2484
Sundong Ahnabb64432019-10-22 13:58:29 +09002485 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002486 copyCmds := apexRule.Args["copy_commands"]
2487
2488 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2489}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002490
Jooyung Han91df2082019-11-20 01:49:42 +09002491func TestApexInVariousPartition(t *testing.T) {
2492 testcases := []struct {
2493 propName, parition, flattenedPartition string
2494 }{
2495 {"", "system", "system_ext"},
2496 {"product_specific: true", "product", "product"},
2497 {"soc_specific: true", "vendor", "vendor"},
2498 {"proprietary: true", "vendor", "vendor"},
2499 {"vendor: true", "vendor", "vendor"},
2500 {"system_ext_specific: true", "system_ext", "system_ext"},
2501 }
2502 for _, tc := range testcases {
2503 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2504 ctx, _ := testApex(t, `
2505 apex {
2506 name: "myapex",
2507 key: "myapex.key",
2508 `+tc.propName+`
2509 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002510
Jooyung Han91df2082019-11-20 01:49:42 +09002511 apex_key {
2512 name: "myapex.key",
2513 public_key: "testkey.avbpubkey",
2514 private_key: "testkey.pem",
2515 }
2516 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002517
Jooyung Han91df2082019-11-20 01:49:42 +09002518 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2519 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2520 actual := apex.installDir.String()
2521 if actual != expected {
2522 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2523 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002524
Jooyung Han91df2082019-11-20 01:49:42 +09002525 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2526 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2527 actual = flattened.installDir.String()
2528 if actual != expected {
2529 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2530 }
2531 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002532 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002533}
Jiyong Park67882562019-03-21 01:11:21 +09002534
Jooyung Han54aca7b2019-11-20 02:26:02 +09002535func TestFileContexts(t *testing.T) {
2536 ctx, _ := testApex(t, `
2537 apex {
2538 name: "myapex",
2539 key: "myapex.key",
2540 }
2541
2542 apex_key {
2543 name: "myapex.key",
2544 public_key: "testkey.avbpubkey",
2545 private_key: "testkey.pem",
2546 }
2547 `)
2548 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2549 apexRule := module.Rule("apexRule")
2550 actual := apexRule.Args["file_contexts"]
2551 expected := "system/sepolicy/apex/myapex-file_contexts"
2552 if actual != expected {
2553 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2554 }
2555
2556 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2557 apex {
2558 name: "myapex",
2559 key: "myapex.key",
2560 file_contexts: "my_own_file_contexts",
2561 }
2562
2563 apex_key {
2564 name: "myapex.key",
2565 public_key: "testkey.avbpubkey",
2566 private_key: "testkey.pem",
2567 }
2568 `, withFiles(map[string][]byte{
2569 "my_own_file_contexts": nil,
2570 }))
2571
2572 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2573 apex {
2574 name: "myapex",
2575 key: "myapex.key",
2576 product_specific: true,
2577 file_contexts: "product_specific_file_contexts",
2578 }
2579
2580 apex_key {
2581 name: "myapex.key",
2582 public_key: "testkey.avbpubkey",
2583 private_key: "testkey.pem",
2584 }
2585 `)
2586
2587 ctx, _ = testApex(t, `
2588 apex {
2589 name: "myapex",
2590 key: "myapex.key",
2591 product_specific: true,
2592 file_contexts: "product_specific_file_contexts",
2593 }
2594
2595 apex_key {
2596 name: "myapex.key",
2597 public_key: "testkey.avbpubkey",
2598 private_key: "testkey.pem",
2599 }
2600 `, withFiles(map[string][]byte{
2601 "product_specific_file_contexts": nil,
2602 }))
2603 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2604 apexRule = module.Rule("apexRule")
2605 actual = apexRule.Args["file_contexts"]
2606 expected = "product_specific_file_contexts"
2607 if actual != expected {
2608 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2609 }
2610
2611 ctx, _ = testApex(t, `
2612 apex {
2613 name: "myapex",
2614 key: "myapex.key",
2615 product_specific: true,
2616 file_contexts: ":my-file-contexts",
2617 }
2618
2619 apex_key {
2620 name: "myapex.key",
2621 public_key: "testkey.avbpubkey",
2622 private_key: "testkey.pem",
2623 }
2624
2625 filegroup {
2626 name: "my-file-contexts",
2627 srcs: ["product_specific_file_contexts"],
2628 }
2629 `, withFiles(map[string][]byte{
2630 "product_specific_file_contexts": nil,
2631 }))
2632 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2633 apexRule = module.Rule("apexRule")
2634 actual = apexRule.Args["file_contexts"]
2635 expected = "product_specific_file_contexts"
2636 if actual != expected {
2637 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2638 }
2639}
2640
Jiyong Park67882562019-03-21 01:11:21 +09002641func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002642 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002643 apex_key {
2644 name: "myapex.key",
2645 public_key: ":my.avbpubkey",
2646 private_key: ":my.pem",
2647 product_specific: true,
2648 }
2649
2650 filegroup {
2651 name: "my.avbpubkey",
2652 srcs: ["testkey2.avbpubkey"],
2653 }
2654
2655 filegroup {
2656 name: "my.pem",
2657 srcs: ["testkey2.pem"],
2658 }
2659 `)
2660
2661 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2662 expected_pubkey := "testkey2.avbpubkey"
2663 actual_pubkey := apex_key.public_key_file.String()
2664 if actual_pubkey != expected_pubkey {
2665 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2666 }
2667 expected_privkey := "testkey2.pem"
2668 actual_privkey := apex_key.private_key_file.String()
2669 if actual_privkey != expected_privkey {
2670 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2671 }
2672}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002673
2674func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002675 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002676 prebuilt_apex {
2677 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002678 arch: {
2679 arm64: {
2680 src: "myapex-arm64.apex",
2681 },
2682 arm: {
2683 src: "myapex-arm.apex",
2684 },
2685 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002686 }
2687 `)
2688
2689 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2690
Jiyong Parkc95714e2019-03-29 14:23:10 +09002691 expectedInput := "myapex-arm64.apex"
2692 if prebuilt.inputApex.String() != expectedInput {
2693 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2694 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002695}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002696
2697func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002698 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002699 prebuilt_apex {
2700 name: "myapex",
2701 src: "myapex-arm.apex",
2702 filename: "notmyapex.apex",
2703 }
2704 `)
2705
2706 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2707
2708 expected := "notmyapex.apex"
2709 if p.installFilename != expected {
2710 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2711 }
2712}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002713
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002714func TestPrebuiltOverrides(t *testing.T) {
2715 ctx, config := testApex(t, `
2716 prebuilt_apex {
2717 name: "myapex.prebuilt",
2718 src: "myapex-arm.apex",
2719 overrides: [
2720 "myapex",
2721 ],
2722 }
2723 `)
2724
2725 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2726
2727 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002728 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002729 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002730 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002731 }
2732}
2733
Roland Levillain630846d2019-06-26 12:48:34 +01002734func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002735 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002736 apex_test {
2737 name: "myapex",
2738 key: "myapex.key",
2739 tests: [
2740 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002741 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002742 ],
2743 }
2744
2745 apex_key {
2746 name: "myapex.key",
2747 public_key: "testkey.avbpubkey",
2748 private_key: "testkey.pem",
2749 }
2750
2751 cc_test {
2752 name: "mytest",
2753 gtest: false,
2754 srcs: ["mytest.cpp"],
2755 relative_install_path: "test",
2756 system_shared_libs: [],
2757 static_executable: true,
2758 stl: "none",
2759 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002760
2761 cc_test {
2762 name: "mytests",
2763 gtest: false,
2764 srcs: [
2765 "mytest1.cpp",
2766 "mytest2.cpp",
2767 "mytest3.cpp",
2768 ],
2769 test_per_src: true,
2770 relative_install_path: "test",
2771 system_shared_libs: [],
2772 static_executable: true,
2773 stl: "none",
2774 }
Roland Levillain630846d2019-06-26 12:48:34 +01002775 `)
2776
Sundong Ahnabb64432019-10-22 13:58:29 +09002777 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002778 copyCmds := apexRule.Args["copy_commands"]
2779
2780 // Ensure that test dep is copied into apex.
2781 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002782
2783 // Ensure that test deps built with `test_per_src` are copied into apex.
2784 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2785 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2786 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002787
2788 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002789 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002790 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2791 name := apexBundle.BaseModuleName()
2792 prefix := "TARGET_"
2793 var builder strings.Builder
2794 data.Custom(&builder, name, prefix, "", data)
2795 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002796 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2797 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2798 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2799 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002800 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002801 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002802 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002803}
2804
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002805func TestInstallExtraFlattenedApexes(t *testing.T) {
2806 ctx, config := testApex(t, `
2807 apex {
2808 name: "myapex",
2809 key: "myapex.key",
2810 }
2811 apex_key {
2812 name: "myapex.key",
2813 public_key: "testkey.avbpubkey",
2814 private_key: "testkey.pem",
2815 }
2816 `, func(fs map[string][]byte, config android.Config) {
2817 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2818 })
2819 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002820 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002821 mk := android.AndroidMkDataForTest(t, config, "", ab)
2822 var builder strings.Builder
2823 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2824 androidMk := builder.String()
2825 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2826}
2827
Jooyung Han5c998b92019-06-27 11:30:33 +09002828func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002829 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002830 apex {
2831 name: "myapex",
2832 key: "myapex.key",
2833 native_shared_libs: ["mylib"],
2834 uses: ["commonapex"],
2835 }
2836
2837 apex {
2838 name: "commonapex",
2839 key: "myapex.key",
2840 native_shared_libs: ["libcommon"],
2841 provide_cpp_shared_libs: true,
2842 }
2843
2844 apex_key {
2845 name: "myapex.key",
2846 public_key: "testkey.avbpubkey",
2847 private_key: "testkey.pem",
2848 }
2849
2850 cc_library {
2851 name: "mylib",
2852 srcs: ["mylib.cpp"],
2853 shared_libs: ["libcommon"],
2854 system_shared_libs: [],
2855 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002856 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002857 }
2858
2859 cc_library {
2860 name: "libcommon",
2861 srcs: ["mylib_common.cpp"],
2862 system_shared_libs: [],
2863 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002864 // TODO: remove //apex_available:platform
2865 apex_available: [
2866 "//apex_available:platform",
2867 "commonapex",
2868 "myapex",
2869 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002870 }
2871 `)
2872
Sundong Ahnabb64432019-10-22 13:58:29 +09002873 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002874 apexRule1 := module1.Rule("apexRule")
2875 copyCmds1 := apexRule1.Args["copy_commands"]
2876
Sundong Ahnabb64432019-10-22 13:58:29 +09002877 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002878 apexRule2 := module2.Rule("apexRule")
2879 copyCmds2 := apexRule2.Args["copy_commands"]
2880
Colin Cross7113d202019-11-20 16:39:12 -08002881 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2882 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002883 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2884 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2885 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2886}
2887
2888func TestApexUsesFailsIfNotProvided(t *testing.T) {
2889 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2890 apex {
2891 name: "myapex",
2892 key: "myapex.key",
2893 uses: ["commonapex"],
2894 }
2895
2896 apex {
2897 name: "commonapex",
2898 key: "myapex.key",
2899 }
2900
2901 apex_key {
2902 name: "myapex.key",
2903 public_key: "testkey.avbpubkey",
2904 private_key: "testkey.pem",
2905 }
2906 `)
2907 testApexError(t, `uses: "commonapex" is not a provider`, `
2908 apex {
2909 name: "myapex",
2910 key: "myapex.key",
2911 uses: ["commonapex"],
2912 }
2913
2914 cc_library {
2915 name: "commonapex",
2916 system_shared_libs: [],
2917 stl: "none",
2918 }
2919
2920 apex_key {
2921 name: "myapex.key",
2922 public_key: "testkey.avbpubkey",
2923 private_key: "testkey.pem",
2924 }
2925 `)
2926}
2927
2928func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2929 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2930 apex {
2931 name: "myapex",
2932 key: "myapex.key",
2933 use_vendor: true,
2934 uses: ["commonapex"],
2935 }
2936
2937 apex {
2938 name: "commonapex",
2939 key: "myapex.key",
2940 provide_cpp_shared_libs: true,
2941 }
2942
2943 apex_key {
2944 name: "myapex.key",
2945 public_key: "testkey.avbpubkey",
2946 private_key: "testkey.pem",
2947 }
Jooyung Handc782442019-11-01 03:14:38 +09002948 `, func(fs map[string][]byte, config android.Config) {
2949 setUseVendorWhitelistForTest(config, []string{"myapex"})
2950 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002951}
2952
Jooyung Hand48f3c32019-08-23 11:18:57 +09002953func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2954 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2955 apex {
2956 name: "myapex",
2957 key: "myapex.key",
2958 native_shared_libs: ["libfoo"],
2959 }
2960
2961 apex_key {
2962 name: "myapex.key",
2963 public_key: "testkey.avbpubkey",
2964 private_key: "testkey.pem",
2965 }
2966
2967 cc_library {
2968 name: "libfoo",
2969 stl: "none",
2970 system_shared_libs: [],
2971 enabled: false,
2972 }
2973 `)
2974 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2975 apex {
2976 name: "myapex",
2977 key: "myapex.key",
2978 java_libs: ["myjar"],
2979 }
2980
2981 apex_key {
2982 name: "myapex.key",
2983 public_key: "testkey.avbpubkey",
2984 private_key: "testkey.pem",
2985 }
2986
2987 java_library {
2988 name: "myjar",
2989 srcs: ["foo/bar/MyClass.java"],
2990 sdk_version: "none",
2991 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09002992 enabled: false,
2993 }
2994 `)
2995}
2996
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002997func TestApexWithApps(t *testing.T) {
2998 ctx, _ := testApex(t, `
2999 apex {
3000 name: "myapex",
3001 key: "myapex.key",
3002 apps: [
3003 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09003004 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003005 ],
3006 }
3007
3008 apex_key {
3009 name: "myapex.key",
3010 public_key: "testkey.avbpubkey",
3011 private_key: "testkey.pem",
3012 }
3013
3014 android_app {
3015 name: "AppFoo",
3016 srcs: ["foo/bar/MyClass.java"],
3017 sdk_version: "none",
3018 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09003019 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003020 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003021 }
Jiyong Parkf7487312019-10-17 12:54:30 +09003022
3023 android_app {
3024 name: "AppFooPriv",
3025 srcs: ["foo/bar/MyClass.java"],
3026 sdk_version: "none",
3027 system_modules: "none",
3028 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003029 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09003030 }
Jiyong Park8be103b2019-11-08 15:53:48 +09003031
3032 cc_library_shared {
3033 name: "libjni",
3034 srcs: ["mylib.cpp"],
3035 stl: "none",
3036 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09003037 apex_available: [ "myapex" ],
Jiyong Park8be103b2019-11-08 15:53:48 +09003038 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003039 `)
3040
Sundong Ahnabb64432019-10-22 13:58:29 +09003041 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003042 apexRule := module.Rule("apexRule")
3043 copyCmds := apexRule.Args["copy_commands"]
3044
3045 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09003046 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09003047
3048 // JNI libraries are embedded inside APK
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00003049 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Description("zip jni lib")
Colin Cross7113d202019-11-20 16:39:12 -08003050 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09003051 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
3052 // ... uncompressed
3053 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
3054 t.Errorf("jni lib is not uncompressed for AppFoo")
3055 }
3056 // ... and not directly inside the APEX
3057 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01003058}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003059
Dario Frenicde2a032019-10-27 00:29:22 +01003060func TestApexWithAppImports(t *testing.T) {
3061 ctx, _ := testApex(t, `
3062 apex {
3063 name: "myapex",
3064 key: "myapex.key",
3065 apps: [
3066 "AppFooPrebuilt",
3067 "AppFooPrivPrebuilt",
3068 ],
3069 }
3070
3071 apex_key {
3072 name: "myapex.key",
3073 public_key: "testkey.avbpubkey",
3074 private_key: "testkey.pem",
3075 }
3076
3077 android_app_import {
3078 name: "AppFooPrebuilt",
3079 apk: "PrebuiltAppFoo.apk",
3080 presigned: true,
3081 dex_preopt: {
3082 enabled: false,
3083 },
3084 }
3085
3086 android_app_import {
3087 name: "AppFooPrivPrebuilt",
3088 apk: "PrebuiltAppFooPriv.apk",
3089 privileged: true,
3090 presigned: true,
3091 dex_preopt: {
3092 enabled: false,
3093 },
3094 }
3095 `)
3096
Sundong Ahnabb64432019-10-22 13:58:29 +09003097 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003098 apexRule := module.Rule("apexRule")
3099 copyCmds := apexRule.Args["copy_commands"]
3100
3101 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3102 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003103}
3104
Dario Freni6f3937c2019-12-20 22:58:03 +00003105func TestApexWithTestHelperApp(t *testing.T) {
3106 ctx, _ := testApex(t, `
3107 apex {
3108 name: "myapex",
3109 key: "myapex.key",
3110 apps: [
3111 "TesterHelpAppFoo",
3112 ],
3113 }
3114
3115 apex_key {
3116 name: "myapex.key",
3117 public_key: "testkey.avbpubkey",
3118 private_key: "testkey.pem",
3119 }
3120
3121 android_test_helper_app {
3122 name: "TesterHelpAppFoo",
3123 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003124 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003125 }
3126
3127 `)
3128
3129 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3130 apexRule := module.Rule("apexRule")
3131 copyCmds := apexRule.Args["copy_commands"]
3132
3133 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3134}
3135
Jooyung Han18020ea2019-11-13 10:50:48 +09003136func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3137 // libfoo's apex_available comes from cc_defaults
3138 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3139 apex {
3140 name: "myapex",
3141 key: "myapex.key",
3142 native_shared_libs: ["libfoo"],
3143 }
3144
3145 apex_key {
3146 name: "myapex.key",
3147 public_key: "testkey.avbpubkey",
3148 private_key: "testkey.pem",
3149 }
3150
3151 apex {
3152 name: "otherapex",
3153 key: "myapex.key",
3154 native_shared_libs: ["libfoo"],
3155 }
3156
3157 cc_defaults {
3158 name: "libfoo-defaults",
3159 apex_available: ["otherapex"],
3160 }
3161
3162 cc_library {
3163 name: "libfoo",
3164 defaults: ["libfoo-defaults"],
3165 stl: "none",
3166 system_shared_libs: [],
3167 }`)
3168}
3169
Jiyong Park127b40b2019-09-30 16:04:35 +09003170func TestApexAvailable(t *testing.T) {
3171 // libfoo is not available to myapex, but only to otherapex
3172 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3173 apex {
3174 name: "myapex",
3175 key: "myapex.key",
3176 native_shared_libs: ["libfoo"],
3177 }
3178
3179 apex_key {
3180 name: "myapex.key",
3181 public_key: "testkey.avbpubkey",
3182 private_key: "testkey.pem",
3183 }
3184
3185 apex {
3186 name: "otherapex",
3187 key: "otherapex.key",
3188 native_shared_libs: ["libfoo"],
3189 }
3190
3191 apex_key {
3192 name: "otherapex.key",
3193 public_key: "testkey.avbpubkey",
3194 private_key: "testkey.pem",
3195 }
3196
3197 cc_library {
3198 name: "libfoo",
3199 stl: "none",
3200 system_shared_libs: [],
3201 apex_available: ["otherapex"],
3202 }`)
3203
3204 // libbar is an indirect dep
3205 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3206 apex {
3207 name: "myapex",
3208 key: "myapex.key",
3209 native_shared_libs: ["libfoo"],
3210 }
3211
3212 apex_key {
3213 name: "myapex.key",
3214 public_key: "testkey.avbpubkey",
3215 private_key: "testkey.pem",
3216 }
3217
3218 apex {
3219 name: "otherapex",
3220 key: "otherapex.key",
3221 native_shared_libs: ["libfoo"],
3222 }
3223
3224 apex_key {
3225 name: "otherapex.key",
3226 public_key: "testkey.avbpubkey",
3227 private_key: "testkey.pem",
3228 }
3229
3230 cc_library {
3231 name: "libfoo",
3232 stl: "none",
3233 shared_libs: ["libbar"],
3234 system_shared_libs: [],
3235 apex_available: ["myapex", "otherapex"],
3236 }
3237
3238 cc_library {
3239 name: "libbar",
3240 stl: "none",
3241 system_shared_libs: [],
3242 apex_available: ["otherapex"],
3243 }`)
3244
3245 testApexError(t, "\"otherapex\" is not a valid module name", `
3246 apex {
3247 name: "myapex",
3248 key: "myapex.key",
3249 native_shared_libs: ["libfoo"],
3250 }
3251
3252 apex_key {
3253 name: "myapex.key",
3254 public_key: "testkey.avbpubkey",
3255 private_key: "testkey.pem",
3256 }
3257
3258 cc_library {
3259 name: "libfoo",
3260 stl: "none",
3261 system_shared_libs: [],
3262 apex_available: ["otherapex"],
3263 }`)
3264
3265 ctx, _ := testApex(t, `
3266 apex {
3267 name: "myapex",
3268 key: "myapex.key",
3269 native_shared_libs: ["libfoo", "libbar"],
3270 }
3271
3272 apex_key {
3273 name: "myapex.key",
3274 public_key: "testkey.avbpubkey",
3275 private_key: "testkey.pem",
3276 }
3277
3278 cc_library {
3279 name: "libfoo",
3280 stl: "none",
3281 system_shared_libs: [],
3282 apex_available: ["myapex"],
3283 }
3284
3285 cc_library {
3286 name: "libbar",
3287 stl: "none",
3288 system_shared_libs: [],
3289 apex_available: ["//apex_available:anyapex"],
3290 }`)
3291
3292 // check that libfoo and libbar are created only for myapex, but not for the platform
Jiyong Park0f80c182020-01-31 02:49:53 +09003293 // TODO(jiyong) the checks for the platform variant are removed because we now create
3294 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3295 // the platform variants are not used from other platform modules. When that is done,
3296 // these checks will be replaced by expecting a specific error message that will be
3297 // emitted when the platform variant is used.
3298 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3299 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3300 // ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3301 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003302
3303 ctx, _ = testApex(t, `
3304 apex {
3305 name: "myapex",
3306 key: "myapex.key",
3307 }
3308
3309 apex_key {
3310 name: "myapex.key",
3311 public_key: "testkey.avbpubkey",
3312 private_key: "testkey.pem",
3313 }
3314
3315 cc_library {
3316 name: "libfoo",
3317 stl: "none",
3318 system_shared_libs: [],
3319 apex_available: ["//apex_available:platform"],
3320 }`)
3321
3322 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003323 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3324 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003325
3326 ctx, _ = testApex(t, `
3327 apex {
3328 name: "myapex",
3329 key: "myapex.key",
3330 native_shared_libs: ["libfoo"],
3331 }
3332
3333 apex_key {
3334 name: "myapex.key",
3335 public_key: "testkey.avbpubkey",
3336 private_key: "testkey.pem",
3337 }
3338
3339 cc_library {
3340 name: "libfoo",
3341 stl: "none",
3342 system_shared_libs: [],
3343 apex_available: ["myapex"],
3344 static: {
3345 apex_available: ["//apex_available:platform"],
3346 },
3347 }`)
3348
3349 // shared variant of libfoo is only available to myapex
Jiyong Park0f80c182020-01-31 02:49:53 +09003350 // TODO(jiyong) the checks for the platform variant are removed because we now create
3351 // the platform variant regardless of the apex_availability. Instead, we will make sure that
3352 // the platform variants are not used from other platform modules. When that is done,
3353 // these checks will be replaced by expecting a specific error message that will be
3354 // emitted when the platform variant is used.
3355 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3356 // ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3357 // // but the static variant is available to both myapex and the platform
3358 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3359 // ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003360}
3361
Jiyong Park5d790c32019-11-15 18:40:32 +09003362func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003363 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003364 apex {
3365 name: "myapex",
3366 key: "myapex.key",
3367 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003368 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003369 }
3370
3371 override_apex {
3372 name: "override_myapex",
3373 base: "myapex",
3374 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003375 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003376 }
3377
3378 apex_key {
3379 name: "myapex.key",
3380 public_key: "testkey.avbpubkey",
3381 private_key: "testkey.pem",
3382 }
3383
3384 android_app {
3385 name: "app",
3386 srcs: ["foo/bar/MyClass.java"],
3387 package_name: "foo",
3388 sdk_version: "none",
3389 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003390 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003391 }
3392
3393 override_android_app {
3394 name: "override_app",
3395 base: "app",
3396 package_name: "bar",
3397 }
3398 `)
3399
Jiyong Park317645e2019-12-05 13:20:58 +09003400 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3401 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3402 if originalVariant.GetOverriddenBy() != "" {
3403 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3404 }
3405 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3406 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3407 }
3408
Jiyong Park5d790c32019-11-15 18:40:32 +09003409 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3410 apexRule := module.Rule("apexRule")
3411 copyCmds := apexRule.Args["copy_commands"]
3412
3413 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3414 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003415
3416 apexBundle := module.Module().(*apexBundle)
3417 name := apexBundle.Name()
3418 if name != "override_myapex" {
3419 t.Errorf("name should be \"override_myapex\", but was %q", name)
3420 }
3421
3422 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3423 var builder strings.Builder
3424 data.Custom(&builder, name, "TARGET_", "", data)
3425 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003426 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003427 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3428 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003429 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003430 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003431 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003432 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3433 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003434}
3435
Jooyung Han214bf372019-11-12 13:03:50 +09003436func TestLegacyAndroid10Support(t *testing.T) {
3437 ctx, _ := testApex(t, `
3438 apex {
3439 name: "myapex",
3440 key: "myapex.key",
3441 legacy_android10_support: true,
3442 }
3443
3444 apex_key {
3445 name: "myapex.key",
3446 public_key: "testkey.avbpubkey",
3447 private_key: "testkey.pem",
3448 }
3449 `)
3450
3451 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3452 args := module.Rule("apexRule").Args
3453 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003454 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003455}
3456
Jooyung Han58f26ab2019-12-18 15:34:32 +09003457func TestJavaSDKLibrary(t *testing.T) {
3458 ctx, _ := testApex(t, `
3459 apex {
3460 name: "myapex",
3461 key: "myapex.key",
3462 java_libs: ["foo"],
3463 }
3464
3465 apex_key {
3466 name: "myapex.key",
3467 public_key: "testkey.avbpubkey",
3468 private_key: "testkey.pem",
3469 }
3470
3471 java_sdk_library {
3472 name: "foo",
3473 srcs: ["a.java"],
3474 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003475 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003476 }
3477 `, withFiles(map[string][]byte{
3478 "api/current.txt": nil,
3479 "api/removed.txt": nil,
3480 "api/system-current.txt": nil,
3481 "api/system-removed.txt": nil,
3482 "api/test-current.txt": nil,
3483 "api/test-removed.txt": nil,
3484 }))
3485
3486 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00003487 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09003488 "javalib/foo.jar",
3489 "etc/permissions/foo.xml",
3490 })
3491 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffine74ac732020-02-06 13:51:46 +00003492 sdkLibrary := ctx.ModuleForTests("foo", "android_common_myapex").Module().(*java.SdkLibrary)
3493 xml := sdkLibrary.XmlPermissionsFileContent()
3494 ensureContains(t, xml, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003495}
3496
atrost6e126252020-01-27 17:01:16 +00003497func TestCompatConfig(t *testing.T) {
3498 ctx, _ := testApex(t, `
3499 apex {
3500 name: "myapex",
3501 key: "myapex.key",
3502 prebuilts: ["myjar-platform-compat-config"],
3503 java_libs: ["myjar"],
3504 }
3505
3506 apex_key {
3507 name: "myapex.key",
3508 public_key: "testkey.avbpubkey",
3509 private_key: "testkey.pem",
3510 }
3511
3512 platform_compat_config {
3513 name: "myjar-platform-compat-config",
3514 src: ":myjar",
3515 }
3516
3517 java_library {
3518 name: "myjar",
3519 srcs: ["foo/bar/MyClass.java"],
3520 sdk_version: "none",
3521 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00003522 apex_available: [ "myapex" ],
3523 }
3524 `)
3525 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3526 "etc/compatconfig/myjar-platform-compat-config.xml",
3527 "javalib/myjar.jar",
3528 })
3529}
3530
Jiyong Park479321d2019-12-16 11:47:12 +09003531func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3532 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3533 apex {
3534 name: "myapex",
3535 key: "myapex.key",
3536 java_libs: ["myjar"],
3537 }
3538
3539 apex_key {
3540 name: "myapex.key",
3541 public_key: "testkey.avbpubkey",
3542 private_key: "testkey.pem",
3543 }
3544
3545 java_library {
3546 name: "myjar",
3547 srcs: ["foo/bar/MyClass.java"],
3548 sdk_version: "none",
3549 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09003550 compile_dex: false,
Jiyong Park479321d2019-12-16 11:47:12 +09003551 }
3552 `)
3553}
3554
Jiyong Park7afd1072019-12-30 16:56:33 +09003555func TestCarryRequiredModuleNames(t *testing.T) {
3556 ctx, config := testApex(t, `
3557 apex {
3558 name: "myapex",
3559 key: "myapex.key",
3560 native_shared_libs: ["mylib"],
3561 }
3562
3563 apex_key {
3564 name: "myapex.key",
3565 public_key: "testkey.avbpubkey",
3566 private_key: "testkey.pem",
3567 }
3568
3569 cc_library {
3570 name: "mylib",
3571 srcs: ["mylib.cpp"],
3572 system_shared_libs: [],
3573 stl: "none",
3574 required: ["a", "b"],
3575 host_required: ["c", "d"],
3576 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003577 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003578 }
3579 `)
3580
3581 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3582 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3583 name := apexBundle.BaseModuleName()
3584 prefix := "TARGET_"
3585 var builder strings.Builder
3586 data.Custom(&builder, name, prefix, "", data)
3587 androidMk := builder.String()
3588 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3589 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3590 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3591}
3592
Jiyong Park7cd10e32020-01-14 09:22:18 +09003593func TestSymlinksFromApexToSystem(t *testing.T) {
3594 bp := `
3595 apex {
3596 name: "myapex",
3597 key: "myapex.key",
3598 native_shared_libs: ["mylib"],
3599 java_libs: ["myjar"],
3600 }
3601
Jiyong Park9d677202020-02-19 16:29:35 +09003602 apex {
3603 name: "myapex.updatable",
3604 key: "myapex.key",
3605 native_shared_libs: ["mylib"],
3606 java_libs: ["myjar"],
3607 updatable: true,
3608 }
3609
Jiyong Park7cd10e32020-01-14 09:22:18 +09003610 apex_key {
3611 name: "myapex.key",
3612 public_key: "testkey.avbpubkey",
3613 private_key: "testkey.pem",
3614 }
3615
3616 cc_library {
3617 name: "mylib",
3618 srcs: ["mylib.cpp"],
3619 shared_libs: ["myotherlib"],
3620 system_shared_libs: [],
3621 stl: "none",
3622 apex_available: [
3623 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003624 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003625 "//apex_available:platform",
3626 ],
3627 }
3628
3629 cc_library {
3630 name: "myotherlib",
3631 srcs: ["mylib.cpp"],
3632 system_shared_libs: [],
3633 stl: "none",
3634 apex_available: [
3635 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003636 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003637 "//apex_available:platform",
3638 ],
3639 }
3640
3641 java_library {
3642 name: "myjar",
3643 srcs: ["foo/bar/MyClass.java"],
3644 sdk_version: "none",
3645 system_modules: "none",
3646 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09003647 apex_available: [
3648 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003649 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003650 "//apex_available:platform",
3651 ],
3652 }
3653
3654 java_library {
3655 name: "myotherjar",
3656 srcs: ["foo/bar/MyClass.java"],
3657 sdk_version: "none",
3658 system_modules: "none",
3659 apex_available: [
3660 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09003661 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09003662 "//apex_available:platform",
3663 ],
3664 }
3665 `
3666
3667 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
3668 for _, f := range files {
3669 if f.path == file {
3670 if f.isLink {
3671 t.Errorf("%q is not a real file", file)
3672 }
3673 return
3674 }
3675 }
3676 t.Errorf("%q is not found", file)
3677 }
3678
3679 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
3680 for _, f := range files {
3681 if f.path == file {
3682 if !f.isLink {
3683 t.Errorf("%q is not a symlink", file)
3684 }
3685 return
3686 }
3687 }
3688 t.Errorf("%q is not found", file)
3689 }
3690
Jiyong Park9d677202020-02-19 16:29:35 +09003691 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
3692 // is updatable or not
Jiyong Park7cd10e32020-01-14 09:22:18 +09003693 ctx, _ := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003694 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003695 ensureRealfileExists(t, files, "javalib/myjar.jar")
3696 ensureRealfileExists(t, files, "lib64/mylib.so")
3697 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3698
Jiyong Park9d677202020-02-19 16:29:35 +09003699 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3700 ensureRealfileExists(t, files, "javalib/myjar.jar")
3701 ensureRealfileExists(t, files, "lib64/mylib.so")
3702 ensureRealfileExists(t, files, "lib64/myotherlib.so")
3703
3704 // For bundled build, symlink to the system for the non-updatable APEXes only
Jiyong Park7cd10e32020-01-14 09:22:18 +09003705 ctx, _ = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003706 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09003707 ensureRealfileExists(t, files, "javalib/myjar.jar")
3708 ensureRealfileExists(t, files, "lib64/mylib.so")
3709 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09003710
3711 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
3712 ensureRealfileExists(t, files, "javalib/myjar.jar")
3713 ensureRealfileExists(t, files, "lib64/mylib.so")
3714 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09003715}
3716
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003717func TestMain(m *testing.M) {
3718 run := func() int {
3719 setUp()
3720 defer tearDown()
3721
3722 return m.Run()
3723 }
3724
3725 os.Exit(run())
3726}