blob: 2103b6ebda1262c8a8ff946eb1d84fd1d5a087e9 [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
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090095 android.ClearApexDependency()
Jiyong Park25fc6a92018-11-18 18:02:45 +090096
97 bp = bp + `
98 toolchain_library {
99 name: "libcompiler_rt-extras",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
106 name: "libatomic",
107 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900108 vendor_available: true,
109 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900110 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
113 toolchain_library {
114 name: "libgcc",
115 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900116 vendor_available: true,
117 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 }
119
120 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700121 name: "libgcc_stripped",
122 src: "",
123 vendor_available: true,
124 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900125 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700126 }
127
128 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129 name: "libclang_rt.builtins-aarch64-android",
130 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900131 vendor_available: true,
132 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900133 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 }
135
136 toolchain_library {
137 name: "libclang_rt.builtins-arm-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900141 native_bridge_supported: true,
142 }
143
144 toolchain_library {
145 name: "libclang_rt.builtins-x86_64-android",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 native_bridge_supported: true,
150 }
151
152 toolchain_library {
153 name: "libclang_rt.builtins-i686-android",
154 src: "",
155 vendor_available: true,
156 recovery_available: true,
157 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtbegin_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 cc_object {
169 name: "crtend_so",
170 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900173 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 }
175
Alex Light3d673592019-01-18 14:37:31 -0800176 cc_object {
177 name: "crtbegin_static",
178 stl: "none",
179 }
180
181 cc_object {
182 name: "crtend_android",
183 stl: "none",
184 }
185
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 llndk_library {
187 name: "libc",
188 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900189 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 }
191
192 llndk_library {
193 name: "libm",
194 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196 }
197
198 llndk_library {
199 name: "libdl",
200 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900201 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900203
204 filegroup {
205 name: "myapex-file_contexts",
206 srcs: [
207 "system/sepolicy/apex/myapex-file_contexts",
208 ],
209 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
Dario Frenicde2a032019-10-27 00:29:22 +0100212 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han344d5432019-08-23 11:17:39 +0900214 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900215 "a.java": nil,
216 "PrebuiltAppFoo.apk": nil,
217 "PrebuiltAppFooPriv.apk": nil,
218 "build/make/target/product/security": nil,
219 "apex_manifest.json": nil,
220 "AndroidManifest.xml": nil,
221 "system/sepolicy/apex/myapex-file_contexts": nil,
Jiyong Park83dc74b2020-01-14 18:38:44 +0900222 "system/sepolicy/apex/myapex2-file_contexts": nil,
Jooyung Han54aca7b2019-11-20 02:26:02 +0900223 "system/sepolicy/apex/otherapex-file_contexts": nil,
224 "system/sepolicy/apex/commonapex-file_contexts": nil,
225 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800226 "mylib.cpp": nil,
227 "mylib_common.cpp": nil,
228 "mytest.cpp": nil,
229 "mytest1.cpp": nil,
230 "mytest2.cpp": nil,
231 "mytest3.cpp": nil,
232 "myprebuilt": nil,
233 "my_include": nil,
234 "foo/bar/MyClass.java": nil,
235 "prebuilt.jar": nil,
236 "vendor/foo/devkeys/test.x509.pem": nil,
237 "vendor/foo/devkeys/test.pk8": nil,
238 "testkey.x509.pem": nil,
239 "testkey.pk8": nil,
240 "testkey.override.x509.pem": nil,
241 "testkey.override.pk8": nil,
242 "vendor/foo/devkeys/testkey.avbpubkey": nil,
243 "vendor/foo/devkeys/testkey.pem": nil,
244 "NOTICE": nil,
245 "custom_notice": nil,
246 "testkey2.avbpubkey": nil,
247 "testkey2.pem": nil,
248 "myapex-arm64.apex": nil,
249 "myapex-arm.apex": nil,
250 "frameworks/base/api/current.txt": nil,
251 "framework/aidl/a.aidl": nil,
252 "build/make/core/proguard.flags": nil,
253 "build/make/core/proguard_basic_keeps.flags": nil,
254 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900255 }
256
257 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800258 // The fs now needs to be populated before creating the config, call handlers twice
259 // for now, once to get any fs changes, and later after the config was created to
260 // set product variables or targets.
261 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
262 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900263 }
264
Colin Cross98be1bb2019-12-13 20:41:13 -0800265 config := android.TestArchConfig(buildDir, nil, bp, fs)
266 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
267 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
268 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
269 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
270 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
271 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
272
273 for _, handler := range handlers {
274 // The fs now needs to be populated before creating the config, call handlers twice
275 // for now, earlier to get any fs changes, and now after the config was created to
276 // set product variables or targets.
277 tempFS := map[string][]byte{}
278 handler(tempFS, config)
279 }
280
281 ctx := android.NewTestArchContext()
282 ctx.RegisterModuleType("apex", BundleFactory)
283 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
284 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
285 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
286 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
287 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
288 ctx.RegisterModuleType("override_apex", overrideApexFactory)
289
Paul Duffin77980a82019-12-19 16:01:36 +0000290 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800291 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
293 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
295 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800296 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000297 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000298 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000299 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900300 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301
302 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.PreDepsMutators(RegisterPreDepsMutators)
304 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
305 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800306
307 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308
Jooyung Han5c998b92019-06-27 11:30:33 +0900309 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310}
311
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700312func setUp() {
313 var err error
314 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700316 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318}
319
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700320func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 os.RemoveAll(buildDir)
322}
323
324// ensure that 'result' contains 'expected'
325func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900326 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 if !strings.Contains(result, expected) {
328 t.Errorf("%q is not found in %q", expected, result)
329 }
330}
331
332// ensures that 'result' does not contain 'notExpected'
333func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900334 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 if strings.Contains(result, notExpected) {
336 t.Errorf("%q is found in %q", notExpected, result)
337 }
338}
339
340func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if !android.InList(expected, result) {
343 t.Errorf("%q is not found in %v", expected, result)
344 }
345}
346
347func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if android.InList(notExpected, result) {
350 t.Errorf("%q is found in %v", notExpected, result)
351 }
352}
353
Jooyung Hane1633032019-08-01 17:41:43 +0900354func ensureListEmpty(t *testing.T, result []string) {
355 t.Helper()
356 if len(result) > 0 {
357 t.Errorf("%q is expected to be empty", result)
358 }
359}
360
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361// Minimal test
362func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700363 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900364 apex_defaults {
365 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900366 manifest: ":myapex.manifest",
367 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 key: "myapex.key",
369 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800370 multilib: {
371 both: {
372 binaries: ["foo",],
373 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900375 java_libs: ["myjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376 }
377
Jiyong Park30ca9372019-02-07 16:27:23 +0900378 apex {
379 name: "myapex",
380 defaults: ["myapex-defaults"],
381 }
382
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 apex_key {
384 name: "myapex.key",
385 public_key: "testkey.avbpubkey",
386 private_key: "testkey.pem",
387 }
388
Jiyong Park809bb722019-02-13 21:33:49 +0900389 filegroup {
390 name: "myapex.manifest",
391 srcs: ["apex_manifest.json"],
392 }
393
394 filegroup {
395 name: "myapex.androidmanifest",
396 srcs: ["AndroidManifest.xml"],
397 }
398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 cc_library {
400 name: "mylib",
401 srcs: ["mylib.cpp"],
402 shared_libs: ["mylib2"],
403 system_shared_libs: [],
404 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000405 // TODO: remove //apex_available:platform
406 apex_available: [
407 "//apex_available:platform",
408 "myapex",
409 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900410 }
411
Alex Light3d673592019-01-18 14:37:31 -0800412 cc_binary {
413 name: "foo",
414 srcs: ["mylib.cpp"],
415 compile_multilib: "both",
416 multilib: {
417 lib32: {
418 suffix: "32",
419 },
420 lib64: {
421 suffix: "64",
422 },
423 },
424 symlinks: ["foo_link_"],
425 symlink_preferred_arch: true,
426 system_shared_libs: [],
427 static_executable: true,
428 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000429 apex_available: [ "myapex" ],
Alex Light3d673592019-01-18 14:37:31 -0800430 }
431
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 cc_library {
433 name: "mylib2",
434 srcs: ["mylib.cpp"],
435 system_shared_libs: [],
436 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900437 notice: "custom_notice",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000438 // TODO: remove //apex_available:platform
439 apex_available: [
440 "//apex_available:platform",
441 "myapex",
442 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900443 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900444
445 java_library {
446 name: "myjar",
447 srcs: ["foo/bar/MyClass.java"],
448 sdk_version: "none",
449 system_modules: "none",
450 compile_dex: true,
451 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900452 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000453 // TODO: remove //apex_available:platform
454 apex_available: [
455 "//apex_available:platform",
456 "myapex",
457 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900458 }
459
460 java_library {
461 name: "myotherjar",
462 srcs: ["foo/bar/MyClass.java"],
463 sdk_version: "none",
464 system_modules: "none",
465 compile_dex: true,
466 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900467
468 java_library {
469 name: "mysharedjar",
470 srcs: ["foo/bar/MyClass.java"],
471 sdk_version: "none",
472 system_modules: "none",
473 compile_dex: true,
474 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900475 `)
476
Sundong Ahnabb64432019-10-22 13:58:29 +0900477 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900478
479 optFlags := apexRule.Args["opt_flags"]
480 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700481 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900482 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900483
Jiyong Park25fc6a92018-11-18 18:02:45 +0900484 copyCmds := apexRule.Args["copy_commands"]
485
486 // Ensure that main rule creates an output
487 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
488
489 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800490 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492
493 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800494 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900495 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900496
497 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800498 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
499 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
501 // .. but not for java libs
502 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900503 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800504
Colin Cross7113d202019-11-20 16:39:12 -0800505 // Ensure that the platform variant ends with _shared or _common
506 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
507 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900508 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
509 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900510 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
511
512 // Ensure that dynamic dependency to java libs are not included
513 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800514
515 // Ensure that all symlinks are present.
516 found_foo_link_64 := false
517 found_foo := false
518 for _, cmd := range strings.Split(copyCmds, " && ") {
519 if strings.HasPrefix(cmd, "ln -s foo64") {
520 if strings.HasSuffix(cmd, "bin/foo") {
521 found_foo = true
522 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
523 found_foo_link_64 = true
524 }
525 }
526 }
527 good := found_foo && found_foo_link_64
528 if !good {
529 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
530 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900531
Sundong Ahnabb64432019-10-22 13:58:29 +0900532 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700533 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 if len(noticeInputs) != 2 {
535 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900536 }
537 ensureListContains(t, noticeInputs, "NOTICE")
538 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900539
540 depsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("myapex-deps-info.txt").Args["content"], "\\n")
541 ensureListContains(t, depsInfo, "internal myjar")
542 ensureListContains(t, depsInfo, "internal mylib")
543 ensureListContains(t, depsInfo, "internal mylib2")
544 ensureListContains(t, depsInfo, "internal myotherjar")
Alex Light5098a612018-11-29 17:12:15 -0800545}
546
Jooyung Hanf21c7972019-12-16 22:32:06 +0900547func TestDefaults(t *testing.T) {
548 ctx, _ := testApex(t, `
549 apex_defaults {
550 name: "myapex-defaults",
551 key: "myapex.key",
552 prebuilts: ["myetc"],
553 native_shared_libs: ["mylib"],
554 java_libs: ["myjar"],
555 apps: ["AppFoo"],
556 }
557
558 prebuilt_etc {
559 name: "myetc",
560 src: "myprebuilt",
561 }
562
563 apex {
564 name: "myapex",
565 defaults: ["myapex-defaults"],
566 }
567
568 apex_key {
569 name: "myapex.key",
570 public_key: "testkey.avbpubkey",
571 private_key: "testkey.pem",
572 }
573
574 cc_library {
575 name: "mylib",
576 system_shared_libs: [],
577 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000578 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900579 }
580
581 java_library {
582 name: "myjar",
583 srcs: ["foo/bar/MyClass.java"],
584 sdk_version: "none",
585 system_modules: "none",
586 compile_dex: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000587 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900588 }
589
590 android_app {
591 name: "AppFoo",
592 srcs: ["foo/bar/MyClass.java"],
593 sdk_version: "none",
594 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000595 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900596 }
597 `)
598 ensureExactContents(t, ctx, "myapex", []string{
599 "etc/myetc",
600 "javalib/myjar.jar",
601 "lib64/mylib.so",
602 "app/AppFoo/AppFoo.apk",
603 })
604}
605
Jooyung Han01a3ee22019-11-02 02:52:25 +0900606func TestApexManifest(t *testing.T) {
607 ctx, _ := testApex(t, `
608 apex {
609 name: "myapex",
610 key: "myapex.key",
611 }
612
613 apex_key {
614 name: "myapex.key",
615 public_key: "testkey.avbpubkey",
616 private_key: "testkey.pem",
617 }
618 `)
619
620 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900621 args := module.Rule("apexRule").Args
622 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
623 t.Error("manifest should be apex_manifest.pb, but " + manifest)
624 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900625}
626
Alex Light5098a612018-11-29 17:12:15 -0800627func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700628 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800629 apex {
630 name: "myapex",
631 key: "myapex.key",
632 payload_type: "zip",
633 native_shared_libs: ["mylib"],
634 }
635
636 apex_key {
637 name: "myapex.key",
638 public_key: "testkey.avbpubkey",
639 private_key: "testkey.pem",
640 }
641
642 cc_library {
643 name: "mylib",
644 srcs: ["mylib.cpp"],
645 shared_libs: ["mylib2"],
646 system_shared_libs: [],
647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800649 }
650
651 cc_library {
652 name: "mylib2",
653 srcs: ["mylib.cpp"],
654 system_shared_libs: [],
655 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800657 }
658 `)
659
Sundong Ahnabb64432019-10-22 13:58:29 +0900660 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800661 copyCmds := zipApexRule.Args["copy_commands"]
662
663 // Ensure that main rule creates an output
664 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
665
666 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800667 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800668
669 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800670 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800671
672 // Ensure that both direct and indirect deps are copied into apex
673 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
674 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900675}
676
677func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700678 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900679 apex {
680 name: "myapex",
681 key: "myapex.key",
682 native_shared_libs: ["mylib", "mylib3"],
683 }
684
685 apex_key {
686 name: "myapex.key",
687 public_key: "testkey.avbpubkey",
688 private_key: "testkey.pem",
689 }
690
691 cc_library {
692 name: "mylib",
693 srcs: ["mylib.cpp"],
694 shared_libs: ["mylib2", "mylib3"],
695 system_shared_libs: [],
696 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000697 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698 }
699
700 cc_library {
701 name: "mylib2",
702 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900703 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704 system_shared_libs: [],
705 stl: "none",
706 stubs: {
707 versions: ["1", "2", "3"],
708 },
709 }
710
711 cc_library {
712 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900713 srcs: ["mylib.cpp"],
714 shared_libs: ["mylib4"],
715 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900716 stl: "none",
717 stubs: {
718 versions: ["10", "11", "12"],
719 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000720 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900721 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900722
723 cc_library {
724 name: "mylib4",
725 srcs: ["mylib.cpp"],
726 system_shared_libs: [],
727 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000728 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900729 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730 `)
731
Sundong Ahnabb64432019-10-22 13:58:29 +0900732 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900733 copyCmds := apexRule.Args["copy_commands"]
734
735 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800736 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900737
738 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800739 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900740
741 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800742 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900743
Colin Cross7113d202019-11-20 16:39:12 -0800744 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900745
746 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900747 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900748 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900749 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900750
751 // 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 -0800752 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900753 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800754 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900755
756 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800757 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900758 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900759
760 // Ensure that genstub is invoked with --apex
Jiyong Park3ff16992019-12-27 14:11:47 +0900761 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900762
763 ensureExactContents(t, ctx, "myapex", []string{
764 "lib64/mylib.so",
765 "lib64/mylib3.so",
766 "lib64/mylib4.so",
767 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900768}
769
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900770func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700771 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900772 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900773 name: "myapex2",
774 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900775 native_shared_libs: ["mylib"],
776 }
777
778 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +0900779 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900780 public_key: "testkey.avbpubkey",
781 private_key: "testkey.pem",
782 }
783
784 cc_library {
785 name: "mylib",
786 srcs: ["mylib.cpp"],
787 shared_libs: ["libfoo#10"],
788 system_shared_libs: [],
789 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000790 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900791 }
792
793 cc_library {
794 name: "libfoo",
795 srcs: ["mylib.cpp"],
796 shared_libs: ["libbar"],
797 system_shared_libs: [],
798 stl: "none",
799 stubs: {
800 versions: ["10", "20", "30"],
801 },
802 }
803
804 cc_library {
805 name: "libbar",
806 srcs: ["mylib.cpp"],
807 system_shared_libs: [],
808 stl: "none",
809 }
810
811 `)
812
Jiyong Park83dc74b2020-01-14 18:38:44 +0900813 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900814 copyCmds := apexRule.Args["copy_commands"]
815
816 // Ensure that direct non-stubs dep is always included
817 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
818
819 // Ensure that indirect stubs dep is not included
820 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
821
822 // Ensure that dependency of stubs is not included
823 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
824
Jiyong Park83dc74b2020-01-14 18:38:44 +0900825 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex2").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900826
827 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900828 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900829 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +0900830 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900831
Jiyong Park3ff16992019-12-27 14:11:47 +0900832 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900833
834 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
835 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900836
837 depsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("myapex2-deps-info.txt").Args["content"], "\\n")
838 ensureListContains(t, depsInfo, "internal mylib")
839 ensureListContains(t, depsInfo, "external libfoo")
840 ensureListNotContains(t, depsInfo, "internal libfoo")
841 ensureListNotContains(t, depsInfo, "external mylib")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900842}
843
Jooyung Hand3639552019-08-09 12:57:43 +0900844func TestApexWithRuntimeLibsDependency(t *testing.T) {
845 /*
846 myapex
847 |
848 v (runtime_libs)
849 mylib ------+------> libfoo [provides stub]
850 |
851 `------> libbar
852 */
853 ctx, _ := testApex(t, `
854 apex {
855 name: "myapex",
856 key: "myapex.key",
857 native_shared_libs: ["mylib"],
858 }
859
860 apex_key {
861 name: "myapex.key",
862 public_key: "testkey.avbpubkey",
863 private_key: "testkey.pem",
864 }
865
866 cc_library {
867 name: "mylib",
868 srcs: ["mylib.cpp"],
869 runtime_libs: ["libfoo", "libbar"],
870 system_shared_libs: [],
871 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000872 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +0900873 }
874
875 cc_library {
876 name: "libfoo",
877 srcs: ["mylib.cpp"],
878 system_shared_libs: [],
879 stl: "none",
880 stubs: {
881 versions: ["10", "20", "30"],
882 },
883 }
884
885 cc_library {
886 name: "libbar",
887 srcs: ["mylib.cpp"],
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 `)
894
Sundong Ahnabb64432019-10-22 13:58:29 +0900895 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900896 copyCmds := apexRule.Args["copy_commands"]
897
898 // Ensure that direct non-stubs dep is always included
899 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
900
901 // Ensure that indirect stubs dep is not included
902 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
903
904 // Ensure that runtime_libs dep in included
905 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
906
Sundong Ahnabb64432019-10-22 13:58:29 +0900907 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900908 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
909 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900910
911}
912
Jooyung Han9c80bae2019-08-20 17:30:57 +0900913func TestApexDependencyToLLNDK(t *testing.T) {
914 ctx, _ := testApex(t, `
915 apex {
916 name: "myapex",
917 key: "myapex.key",
918 use_vendor: true,
919 native_shared_libs: ["mylib"],
920 }
921
922 apex_key {
923 name: "myapex.key",
924 public_key: "testkey.avbpubkey",
925 private_key: "testkey.pem",
926 }
927
928 cc_library {
929 name: "mylib",
930 srcs: ["mylib.cpp"],
931 vendor_available: true,
932 shared_libs: ["libbar"],
933 system_shared_libs: [],
934 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000935 apex_available: [ "myapex" ],
Jooyung Han9c80bae2019-08-20 17:30:57 +0900936 }
937
938 cc_library {
939 name: "libbar",
940 srcs: ["mylib.cpp"],
941 system_shared_libs: [],
942 stl: "none",
943 }
944
945 llndk_library {
946 name: "libbar",
947 symbol_file: "",
948 }
Jooyung Handc782442019-11-01 03:14:38 +0900949 `, func(fs map[string][]byte, config android.Config) {
950 setUseVendorWhitelistForTest(config, []string{"myapex"})
951 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900952
Sundong Ahnabb64432019-10-22 13:58:29 +0900953 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900954 copyCmds := apexRule.Args["copy_commands"]
955
956 // Ensure that LLNDK dep is not included
957 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
958
Sundong Ahnabb64432019-10-22 13:58:29 +0900959 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900960 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900961
962 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900963 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900964
965}
966
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700968 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969 apex {
970 name: "myapex",
971 key: "myapex.key",
972 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
973 }
974
975 apex_key {
976 name: "myapex.key",
977 public_key: "testkey.avbpubkey",
978 private_key: "testkey.pem",
979 }
980
981 cc_library {
982 name: "mylib",
983 srcs: ["mylib.cpp"],
984 shared_libs: ["libdl#27"],
985 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000986 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 }
988
989 cc_library_shared {
990 name: "mylib_shared",
991 srcs: ["mylib.cpp"],
992 shared_libs: ["libdl#27"],
993 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000994 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 }
996
997 cc_library {
998 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700999 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000 nocrt: true,
1001 system_shared_libs: [],
1002 stl: "none",
1003 stubs: {
1004 versions: ["27", "28", "29"],
1005 },
1006 }
1007
1008 cc_library {
1009 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -07001010 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 nocrt: true,
1012 system_shared_libs: [],
1013 stl: "none",
1014 stubs: {
1015 versions: ["27", "28", "29"],
1016 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001017 apex_available: [
1018 "//apex_available:platform",
1019 "myapex"
1020 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021 }
1022
1023 cc_library {
1024 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -07001025 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001026 nocrt: true,
1027 system_shared_libs: [],
1028 stl: "none",
1029 stubs: {
1030 versions: ["27", "28", "29"],
1031 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001032 apex_available: [
1033 "//apex_available:platform",
1034 "myapex"
1035 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 }
Jiyong Parkb0788572018-12-20 22:10:17 +09001037
1038 cc_library {
1039 name: "libBootstrap",
1040 srcs: ["mylib.cpp"],
1041 stl: "none",
1042 bootstrap: true,
1043 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001044 `)
1045
Sundong Ahnabb64432019-10-22 13:58:29 +09001046 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001047 copyCmds := apexRule.Args["copy_commands"]
1048
1049 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001050 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001051 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1052 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001053
1054 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001055 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001056
Colin Cross7113d202019-11-20 16:39:12 -08001057 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1058 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1059 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001060
1061 // For dependency to libc
1062 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001063 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001064 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001065 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001066 // ... Cflags from stub is correctly exported to mylib
1067 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1068 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1069
1070 // For dependency to libm
1071 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001072 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001073 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001074 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001075 // ... and is not compiling with the stub
1076 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1077 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1078
1079 // For dependency to libdl
1080 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001081 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001082 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001083 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1084 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001085 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001086 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001087 // ... Cflags from stub is correctly exported to mylib
1088 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1089 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001090
1091 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001092 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1093 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1094 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1095 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001096}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001097
1098func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001099 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001100 apex {
1101 name: "myapex",
1102 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001103 native_shared_libs: ["mylib"],
1104 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001105 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001106 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001107 }
1108
1109 apex_key {
1110 name: "myapex.key",
1111 public_key: "testkey.avbpubkey",
1112 private_key: "testkey.pem",
1113 }
1114
1115 prebuilt_etc {
1116 name: "myetc",
1117 src: "myprebuilt",
1118 sub_dir: "foo/bar",
1119 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001120
1121 cc_library {
1122 name: "mylib",
1123 srcs: ["mylib.cpp"],
1124 relative_install_path: "foo/bar",
1125 system_shared_libs: [],
1126 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001127 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001128 }
1129
1130 cc_binary {
1131 name: "mybin",
1132 srcs: ["mylib.cpp"],
1133 relative_install_path: "foo/bar",
1134 system_shared_libs: [],
1135 static_executable: true,
1136 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001137 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001138 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001139 `)
1140
Sundong Ahnabb64432019-10-22 13:58:29 +09001141 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001142 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1143
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001144 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001145 ensureListContains(t, dirs, "etc")
1146 ensureListContains(t, dirs, "etc/foo")
1147 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001148 ensureListContains(t, dirs, "lib64")
1149 ensureListContains(t, dirs, "lib64/foo")
1150 ensureListContains(t, dirs, "lib64/foo/bar")
1151 ensureListContains(t, dirs, "lib")
1152 ensureListContains(t, dirs, "lib/foo")
1153 ensureListContains(t, dirs, "lib/foo/bar")
1154
Jiyong Parkbd13e442019-03-15 18:10:35 +09001155 ensureListContains(t, dirs, "bin")
1156 ensureListContains(t, dirs, "bin/foo")
1157 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001158}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001159
1160func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001161 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001162 apex {
1163 name: "myapex",
1164 key: "myapex.key",
1165 native_shared_libs: ["mylib"],
1166 use_vendor: true,
1167 }
1168
1169 apex_key {
1170 name: "myapex.key",
1171 public_key: "testkey.avbpubkey",
1172 private_key: "testkey.pem",
1173 }
1174
1175 cc_library {
1176 name: "mylib",
1177 srcs: ["mylib.cpp"],
1178 shared_libs: ["mylib2"],
1179 system_shared_libs: [],
1180 vendor_available: true,
1181 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001182 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001183 }
1184
1185 cc_library {
1186 name: "mylib2",
1187 srcs: ["mylib.cpp"],
1188 system_shared_libs: [],
1189 vendor_available: true,
1190 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001191 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09001192 }
Jooyung Handc782442019-11-01 03:14:38 +09001193 `, func(fs map[string][]byte, config android.Config) {
1194 setUseVendorWhitelistForTest(config, []string{"myapex"})
1195 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001196
1197 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001198 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001199 for _, implicit := range i.Implicits {
1200 inputsList = append(inputsList, implicit.String())
1201 }
1202 }
1203 inputsString := strings.Join(inputsList, " ")
1204
1205 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001206 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1207 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001208
1209 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001210 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1211 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001212}
Jiyong Park16e91a02018-12-20 18:18:08 +09001213
Jooyung Handc782442019-11-01 03:14:38 +09001214func TestUseVendorRestriction(t *testing.T) {
1215 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1216 apex {
1217 name: "myapex",
1218 key: "myapex.key",
1219 use_vendor: true,
1220 }
1221 apex_key {
1222 name: "myapex.key",
1223 public_key: "testkey.avbpubkey",
1224 private_key: "testkey.pem",
1225 }
1226 `, func(fs map[string][]byte, config android.Config) {
1227 setUseVendorWhitelistForTest(config, []string{""})
1228 })
1229 // no error with whitelist
1230 testApex(t, `
1231 apex {
1232 name: "myapex",
1233 key: "myapex.key",
1234 use_vendor: true,
1235 }
1236 apex_key {
1237 name: "myapex.key",
1238 public_key: "testkey.avbpubkey",
1239 private_key: "testkey.pem",
1240 }
1241 `, func(fs map[string][]byte, config android.Config) {
1242 setUseVendorWhitelistForTest(config, []string{"myapex"})
1243 })
1244}
1245
Jooyung Han5c998b92019-06-27 11:30:33 +09001246func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1247 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1248 apex {
1249 name: "myapex",
1250 key: "myapex.key",
1251 native_shared_libs: ["mylib"],
1252 use_vendor: true,
1253 }
1254
1255 apex_key {
1256 name: "myapex.key",
1257 public_key: "testkey.avbpubkey",
1258 private_key: "testkey.pem",
1259 }
1260
1261 cc_library {
1262 name: "mylib",
1263 srcs: ["mylib.cpp"],
1264 system_shared_libs: [],
1265 stl: "none",
1266 }
1267 `)
1268}
1269
Jiyong Park16e91a02018-12-20 18:18:08 +09001270func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001271 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001272 apex {
1273 name: "myapex",
1274 key: "myapex.key",
1275 native_shared_libs: ["mylib"],
1276 }
1277
1278 apex_key {
1279 name: "myapex.key",
1280 public_key: "testkey.avbpubkey",
1281 private_key: "testkey.pem",
1282 }
1283
1284 cc_library {
1285 name: "mylib",
1286 srcs: ["mylib.cpp"],
1287 system_shared_libs: [],
1288 stl: "none",
1289 stubs: {
1290 versions: ["1", "2", "3"],
1291 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001292 apex_available: [
1293 "//apex_available:platform",
1294 "myapex",
1295 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09001296 }
1297
1298 cc_binary {
1299 name: "not_in_apex",
1300 srcs: ["mylib.cpp"],
1301 static_libs: ["mylib"],
1302 static_executable: true,
1303 system_shared_libs: [],
1304 stl: "none",
1305 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001306 `)
1307
Colin Cross7113d202019-11-20 16:39:12 -08001308 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001309
1310 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001311 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001312}
Jiyong Park9335a262018-12-24 11:31:58 +09001313
1314func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001315 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001316 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001317 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001318 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001319 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001320 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001321 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001322 }
1323
1324 cc_library {
1325 name: "mylib",
1326 srcs: ["mylib.cpp"],
1327 system_shared_libs: [],
1328 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001329 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09001330 }
1331
1332 apex_key {
1333 name: "myapex.key",
1334 public_key: "testkey.avbpubkey",
1335 private_key: "testkey.pem",
1336 }
1337
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001338 android_app_certificate {
1339 name: "myapex.certificate",
1340 certificate: "testkey",
1341 }
1342
1343 android_app_certificate {
1344 name: "myapex.certificate.override",
1345 certificate: "testkey.override",
1346 }
1347
Jiyong Park9335a262018-12-24 11:31:58 +09001348 `)
1349
1350 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001351 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001352
1353 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1354 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1355 "vendor/foo/devkeys/testkey.avbpubkey")
1356 }
1357 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1358 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1359 "vendor/foo/devkeys/testkey.pem")
1360 }
1361
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001362 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001363 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001364 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001365 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001366 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001367 }
1368}
Jiyong Park58e364a2019-01-19 19:24:06 +09001369
Jooyung Hanf121a652019-12-17 14:30:11 +09001370func TestCertificate(t *testing.T) {
1371 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1372 ctx, _ := testApex(t, `
1373 apex {
1374 name: "myapex",
1375 key: "myapex.key",
1376 }
1377 apex_key {
1378 name: "myapex.key",
1379 public_key: "testkey.avbpubkey",
1380 private_key: "testkey.pem",
1381 }`)
1382 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1383 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1384 if actual := rule.Args["certificates"]; actual != expected {
1385 t.Errorf("certificates should be %q, not %q", expected, actual)
1386 }
1387 })
1388 t.Run("override when unspecified", func(t *testing.T) {
1389 ctx, _ := testApex(t, `
1390 apex {
1391 name: "myapex_keytest",
1392 key: "myapex.key",
1393 file_contexts: ":myapex-file_contexts",
1394 }
1395 apex_key {
1396 name: "myapex.key",
1397 public_key: "testkey.avbpubkey",
1398 private_key: "testkey.pem",
1399 }
1400 android_app_certificate {
1401 name: "myapex.certificate.override",
1402 certificate: "testkey.override",
1403 }`)
1404 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1405 expected := "testkey.override.x509.pem testkey.override.pk8"
1406 if actual := rule.Args["certificates"]; actual != expected {
1407 t.Errorf("certificates should be %q, not %q", expected, actual)
1408 }
1409 })
1410 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1411 ctx, _ := testApex(t, `
1412 apex {
1413 name: "myapex",
1414 key: "myapex.key",
1415 certificate: ":myapex.certificate",
1416 }
1417 apex_key {
1418 name: "myapex.key",
1419 public_key: "testkey.avbpubkey",
1420 private_key: "testkey.pem",
1421 }
1422 android_app_certificate {
1423 name: "myapex.certificate",
1424 certificate: "testkey",
1425 }`)
1426 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1427 expected := "testkey.x509.pem testkey.pk8"
1428 if actual := rule.Args["certificates"]; actual != expected {
1429 t.Errorf("certificates should be %q, not %q", expected, actual)
1430 }
1431 })
1432 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1433 ctx, _ := testApex(t, `
1434 apex {
1435 name: "myapex_keytest",
1436 key: "myapex.key",
1437 file_contexts: ":myapex-file_contexts",
1438 certificate: ":myapex.certificate",
1439 }
1440 apex_key {
1441 name: "myapex.key",
1442 public_key: "testkey.avbpubkey",
1443 private_key: "testkey.pem",
1444 }
1445 android_app_certificate {
1446 name: "myapex.certificate.override",
1447 certificate: "testkey.override",
1448 }`)
1449 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1450 expected := "testkey.override.x509.pem testkey.override.pk8"
1451 if actual := rule.Args["certificates"]; actual != expected {
1452 t.Errorf("certificates should be %q, not %q", expected, actual)
1453 }
1454 })
1455 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1456 ctx, _ := testApex(t, `
1457 apex {
1458 name: "myapex",
1459 key: "myapex.key",
1460 certificate: "testkey",
1461 }
1462 apex_key {
1463 name: "myapex.key",
1464 public_key: "testkey.avbpubkey",
1465 private_key: "testkey.pem",
1466 }`)
1467 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1468 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1469 if actual := rule.Args["certificates"]; actual != expected {
1470 t.Errorf("certificates should be %q, not %q", expected, actual)
1471 }
1472 })
1473 t.Run("override when specified as <name>", func(t *testing.T) {
1474 ctx, _ := testApex(t, `
1475 apex {
1476 name: "myapex_keytest",
1477 key: "myapex.key",
1478 file_contexts: ":myapex-file_contexts",
1479 certificate: "testkey",
1480 }
1481 apex_key {
1482 name: "myapex.key",
1483 public_key: "testkey.avbpubkey",
1484 private_key: "testkey.pem",
1485 }
1486 android_app_certificate {
1487 name: "myapex.certificate.override",
1488 certificate: "testkey.override",
1489 }`)
1490 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1491 expected := "testkey.override.x509.pem testkey.override.pk8"
1492 if actual := rule.Args["certificates"]; actual != expected {
1493 t.Errorf("certificates should be %q, not %q", expected, actual)
1494 }
1495 })
1496}
1497
Jiyong Park58e364a2019-01-19 19:24:06 +09001498func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001499 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001500 apex {
1501 name: "myapex",
1502 key: "myapex.key",
1503 native_shared_libs: ["mylib"],
1504 }
1505
1506 apex {
1507 name: "otherapex",
1508 key: "myapex.key",
1509 native_shared_libs: ["mylib"],
1510 }
1511
1512 apex_key {
1513 name: "myapex.key",
1514 public_key: "testkey.avbpubkey",
1515 private_key: "testkey.pem",
1516 }
1517
1518 cc_library {
1519 name: "mylib",
1520 srcs: ["mylib.cpp"],
1521 system_shared_libs: [],
1522 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001523 // TODO: remove //apex_available:platform
1524 apex_available: [
1525 "//apex_available:platform",
1526 "myapex",
1527 "otherapex",
1528 ],
Jiyong Park58e364a2019-01-19 19:24:06 +09001529 }
1530 `)
1531
Jooyung Han6b8459b2019-10-30 08:29:25 +09001532 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001533 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001534 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001535 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1536 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001537
Jooyung Han6b8459b2019-10-30 08:29:25 +09001538 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001539 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001540 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001541 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1542 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001543
Jooyung Han6b8459b2019-10-30 08:29:25 +09001544 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001545 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001546 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001547 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1548 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001549}
Jiyong Park7e636d02019-01-28 16:16:54 +09001550
1551func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001552 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001553 apex {
1554 name: "myapex",
1555 key: "myapex.key",
1556 native_shared_libs: ["mylib"],
1557 }
1558
1559 apex_key {
1560 name: "myapex.key",
1561 public_key: "testkey.avbpubkey",
1562 private_key: "testkey.pem",
1563 }
1564
1565 cc_library_headers {
1566 name: "mylib_headers",
1567 export_include_dirs: ["my_include"],
1568 system_shared_libs: [],
1569 stl: "none",
1570 }
1571
1572 cc_library {
1573 name: "mylib",
1574 srcs: ["mylib.cpp"],
1575 system_shared_libs: [],
1576 stl: "none",
1577 header_libs: ["mylib_headers"],
1578 export_header_lib_headers: ["mylib_headers"],
1579 stubs: {
1580 versions: ["1", "2", "3"],
1581 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001582 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09001583 }
1584
1585 cc_library {
1586 name: "otherlib",
1587 srcs: ["mylib.cpp"],
1588 system_shared_libs: [],
1589 stl: "none",
1590 shared_libs: ["mylib"],
1591 }
1592 `)
1593
Colin Cross7113d202019-11-20 16:39:12 -08001594 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001595
1596 // Ensure that the include path of the header lib is exported to 'otherlib'
1597 ensureContains(t, cFlags, "-Imy_include")
1598}
Alex Light9670d332019-01-29 18:07:33 -08001599
Jooyung Han31c470b2019-10-18 16:26:59 +09001600func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1601 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001602 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001603 copyCmds := apexRule.Args["copy_commands"]
1604 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001605 var failed bool
1606 var surplus []string
1607 filesMatched := make(map[string]bool)
1608 addContent := func(content string) {
1609 for _, expected := range files {
1610 if matched, _ := path.Match(expected, content); matched {
1611 filesMatched[expected] = true
1612 return
1613 }
1614 }
1615 surplus = append(surplus, content)
1616 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001617 for _, cmd := range strings.Split(copyCmds, "&&") {
1618 cmd = strings.TrimSpace(cmd)
1619 if cmd == "" {
1620 continue
1621 }
1622 terms := strings.Split(cmd, " ")
1623 switch terms[0] {
1624 case "mkdir":
1625 case "cp":
1626 if len(terms) != 3 {
1627 t.Fatal("copyCmds contains invalid cp command", cmd)
1628 }
1629 dst := terms[2]
1630 index := strings.Index(dst, imageApexDir)
1631 if index == -1 {
1632 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1633 }
1634 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001635 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001636 default:
1637 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1638 }
1639 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001640
Jooyung Han31c470b2019-10-18 16:26:59 +09001641 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001642 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001643 t.Log("surplus files", surplus)
1644 failed = true
1645 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001646
1647 if len(files) > len(filesMatched) {
1648 var missing []string
1649 for _, expected := range files {
1650 if !filesMatched[expected] {
1651 missing = append(missing, expected)
1652 }
1653 }
1654 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001655 t.Log("missing files", missing)
1656 failed = true
1657 }
1658 if failed {
1659 t.Fail()
1660 }
1661}
1662
Jooyung Han344d5432019-08-23 11:17:39 +09001663func TestVndkApexCurrent(t *testing.T) {
1664 ctx, _ := testApex(t, `
1665 apex_vndk {
1666 name: "myapex",
1667 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001668 }
1669
1670 apex_key {
1671 name: "myapex.key",
1672 public_key: "testkey.avbpubkey",
1673 private_key: "testkey.pem",
1674 }
1675
1676 cc_library {
1677 name: "libvndk",
1678 srcs: ["mylib.cpp"],
1679 vendor_available: true,
1680 vndk: {
1681 enabled: true,
1682 },
1683 system_shared_libs: [],
1684 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001685 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001686 }
1687
1688 cc_library {
1689 name: "libvndksp",
1690 srcs: ["mylib.cpp"],
1691 vendor_available: true,
1692 vndk: {
1693 enabled: true,
1694 support_system_process: true,
1695 },
1696 system_shared_libs: [],
1697 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001698 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001699 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001700 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001701
Jooyung Han31c470b2019-10-18 16:26:59 +09001702 ensureExactContents(t, ctx, "myapex", []string{
1703 "lib/libvndk.so",
1704 "lib/libvndksp.so",
1705 "lib64/libvndk.so",
1706 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001707 "etc/llndk.libraries.VER.txt",
1708 "etc/vndkcore.libraries.VER.txt",
1709 "etc/vndksp.libraries.VER.txt",
1710 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001711 })
Jooyung Han344d5432019-08-23 11:17:39 +09001712}
1713
1714func TestVndkApexWithPrebuilt(t *testing.T) {
1715 ctx, _ := testApex(t, `
1716 apex_vndk {
1717 name: "myapex",
1718 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001719 }
1720
1721 apex_key {
1722 name: "myapex.key",
1723 public_key: "testkey.avbpubkey",
1724 private_key: "testkey.pem",
1725 }
1726
1727 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001728 name: "libvndk",
1729 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001730 vendor_available: true,
1731 vndk: {
1732 enabled: true,
1733 },
1734 system_shared_libs: [],
1735 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001736 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001737 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001738
1739 cc_prebuilt_library_shared {
1740 name: "libvndk.arm",
1741 srcs: ["libvndk.arm.so"],
1742 vendor_available: true,
1743 vndk: {
1744 enabled: true,
1745 },
1746 enabled: false,
1747 arch: {
1748 arm: {
1749 enabled: true,
1750 },
1751 },
1752 system_shared_libs: [],
1753 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001754 apex_available: [ "myapex" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09001755 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001756 `+vndkLibrariesTxtFiles("current"),
1757 withFiles(map[string][]byte{
1758 "libvndk.so": nil,
1759 "libvndk.arm.so": nil,
1760 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001761
Jooyung Han31c470b2019-10-18 16:26:59 +09001762 ensureExactContents(t, ctx, "myapex", []string{
1763 "lib/libvndk.so",
1764 "lib/libvndk.arm.so",
1765 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001766 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001767 })
Jooyung Han344d5432019-08-23 11:17:39 +09001768}
1769
Jooyung Han39edb6c2019-11-06 16:53:07 +09001770func vndkLibrariesTxtFiles(vers ...string) (result string) {
1771 for _, v := range vers {
1772 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001773 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001774 result += `
1775 vndk_libraries_txt {
1776 name: "` + txt + `.libraries.txt",
1777 }
1778 `
1779 }
1780 } else {
1781 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1782 result += `
1783 prebuilt_etc {
1784 name: "` + txt + `.libraries.` + v + `.txt",
1785 src: "dummy.txt",
1786 }
1787 `
1788 }
1789 }
1790 }
1791 return
1792}
1793
Jooyung Han344d5432019-08-23 11:17:39 +09001794func TestVndkApexVersion(t *testing.T) {
1795 ctx, _ := testApex(t, `
1796 apex_vndk {
1797 name: "myapex_v27",
1798 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001799 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001800 vndk_version: "27",
1801 }
1802
1803 apex_key {
1804 name: "myapex.key",
1805 public_key: "testkey.avbpubkey",
1806 private_key: "testkey.pem",
1807 }
1808
Jooyung Han31c470b2019-10-18 16:26:59 +09001809 vndk_prebuilt_shared {
1810 name: "libvndk27",
1811 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001812 vendor_available: true,
1813 vndk: {
1814 enabled: true,
1815 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001816 target_arch: "arm64",
1817 arch: {
1818 arm: {
1819 srcs: ["libvndk27_arm.so"],
1820 },
1821 arm64: {
1822 srcs: ["libvndk27_arm64.so"],
1823 },
1824 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001825 apex_available: [ "myapex_v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001826 }
1827
1828 vndk_prebuilt_shared {
1829 name: "libvndk27",
1830 version: "27",
1831 vendor_available: true,
1832 vndk: {
1833 enabled: true,
1834 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001835 target_arch: "x86_64",
1836 arch: {
1837 x86: {
1838 srcs: ["libvndk27_x86.so"],
1839 },
1840 x86_64: {
1841 srcs: ["libvndk27_x86_64.so"],
1842 },
1843 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001844 }
1845 `+vndkLibrariesTxtFiles("27"),
1846 withFiles(map[string][]byte{
1847 "libvndk27_arm.so": nil,
1848 "libvndk27_arm64.so": nil,
1849 "libvndk27_x86.so": nil,
1850 "libvndk27_x86_64.so": nil,
1851 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001852
Jooyung Han31c470b2019-10-18 16:26:59 +09001853 ensureExactContents(t, ctx, "myapex_v27", []string{
1854 "lib/libvndk27_arm.so",
1855 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001856 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001857 })
Jooyung Han344d5432019-08-23 11:17:39 +09001858}
1859
1860func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1861 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1862 apex_vndk {
1863 name: "myapex_v27",
1864 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001865 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001866 vndk_version: "27",
1867 }
1868 apex_vndk {
1869 name: "myapex_v27_other",
1870 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001871 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001872 vndk_version: "27",
1873 }
1874
1875 apex_key {
1876 name: "myapex.key",
1877 public_key: "testkey.avbpubkey",
1878 private_key: "testkey.pem",
1879 }
1880
1881 cc_library {
1882 name: "libvndk",
1883 srcs: ["mylib.cpp"],
1884 vendor_available: true,
1885 vndk: {
1886 enabled: true,
1887 },
1888 system_shared_libs: [],
1889 stl: "none",
1890 }
1891
1892 vndk_prebuilt_shared {
1893 name: "libvndk",
1894 version: "27",
1895 vendor_available: true,
1896 vndk: {
1897 enabled: true,
1898 },
1899 srcs: ["libvndk.so"],
1900 }
1901 `, withFiles(map[string][]byte{
1902 "libvndk.so": nil,
1903 }))
1904}
1905
Jooyung Han90eee022019-10-01 20:02:42 +09001906func TestVndkApexNameRule(t *testing.T) {
1907 ctx, _ := testApex(t, `
1908 apex_vndk {
1909 name: "myapex",
1910 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001911 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001912 }
1913 apex_vndk {
1914 name: "myapex_v28",
1915 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001916 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001917 vndk_version: "28",
1918 }
1919 apex_key {
1920 name: "myapex.key",
1921 public_key: "testkey.avbpubkey",
1922 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001923 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001924
1925 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001926 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001927 actual := proptools.String(bundle.properties.Apex_name)
1928 if !reflect.DeepEqual(actual, expected) {
1929 t.Errorf("Got '%v', expected '%v'", actual, expected)
1930 }
1931 }
1932
1933 assertApexName("com.android.vndk.vVER", "myapex")
1934 assertApexName("com.android.vndk.v28", "myapex_v28")
1935}
1936
Jooyung Han344d5432019-08-23 11:17:39 +09001937func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1938 ctx, _ := testApex(t, `
1939 apex_vndk {
1940 name: "myapex",
1941 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001942 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001943 }
1944
1945 apex_key {
1946 name: "myapex.key",
1947 public_key: "testkey.avbpubkey",
1948 private_key: "testkey.pem",
1949 }
1950
1951 cc_library {
1952 name: "libvndk",
1953 srcs: ["mylib.cpp"],
1954 vendor_available: true,
1955 native_bridge_supported: true,
1956 host_supported: true,
1957 vndk: {
1958 enabled: true,
1959 },
1960 system_shared_libs: [],
1961 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001962 apex_available: [ "myapex" ],
Jooyung Han344d5432019-08-23 11:17:39 +09001963 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001964 `+vndkLibrariesTxtFiles("current"),
1965 withTargets(map[android.OsType][]android.Target{
1966 android.Android: []android.Target{
1967 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1968 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1969 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1970 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1971 },
1972 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001973
Jooyung Han31c470b2019-10-18 16:26:59 +09001974 ensureExactContents(t, ctx, "myapex", []string{
1975 "lib/libvndk.so",
1976 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001977 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001978 })
Jooyung Han344d5432019-08-23 11:17:39 +09001979}
1980
1981func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1982 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1983 apex_vndk {
1984 name: "myapex",
1985 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001986 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001987 native_bridge_supported: true,
1988 }
1989
1990 apex_key {
1991 name: "myapex.key",
1992 public_key: "testkey.avbpubkey",
1993 private_key: "testkey.pem",
1994 }
1995
1996 cc_library {
1997 name: "libvndk",
1998 srcs: ["mylib.cpp"],
1999 vendor_available: true,
2000 native_bridge_supported: true,
2001 host_supported: true,
2002 vndk: {
2003 enabled: true,
2004 },
2005 system_shared_libs: [],
2006 stl: "none",
2007 }
2008 `)
2009}
2010
Jooyung Han31c470b2019-10-18 16:26:59 +09002011func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09002012 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09002013 apex_vndk {
2014 name: "myapex_v27",
2015 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002016 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09002017 vndk_version: "27",
2018 }
2019
2020 apex_key {
2021 name: "myapex.key",
2022 public_key: "testkey.avbpubkey",
2023 private_key: "testkey.pem",
2024 }
2025
2026 vndk_prebuilt_shared {
2027 name: "libvndk27",
2028 version: "27",
2029 target_arch: "arm",
2030 vendor_available: true,
2031 vndk: {
2032 enabled: true,
2033 },
2034 arch: {
2035 arm: {
2036 srcs: ["libvndk27.so"],
2037 }
2038 },
2039 }
2040
2041 vndk_prebuilt_shared {
2042 name: "libvndk27",
2043 version: "27",
2044 target_arch: "arm",
2045 binder32bit: true,
2046 vendor_available: true,
2047 vndk: {
2048 enabled: true,
2049 },
2050 arch: {
2051 arm: {
2052 srcs: ["libvndk27binder32.so"],
2053 }
2054 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002055 apex_available: [ "myapex_v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09002056 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09002057 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09002058 withFiles(map[string][]byte{
2059 "libvndk27.so": nil,
2060 "libvndk27binder32.so": nil,
2061 }),
2062 withBinder32bit,
2063 withTargets(map[android.OsType][]android.Target{
2064 android.Android: []android.Target{
2065 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
2066 },
2067 }),
2068 )
2069
2070 ensureExactContents(t, ctx, "myapex_v27", []string{
2071 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09002072 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09002073 })
2074}
2075
Jooyung Hane1633032019-08-01 17:41:43 +09002076func TestDependenciesInApexManifest(t *testing.T) {
2077 ctx, _ := testApex(t, `
2078 apex {
2079 name: "myapex_nodep",
2080 key: "myapex.key",
2081 native_shared_libs: ["lib_nodep"],
2082 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002083 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002084 }
2085
2086 apex {
2087 name: "myapex_dep",
2088 key: "myapex.key",
2089 native_shared_libs: ["lib_dep"],
2090 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002091 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002092 }
2093
2094 apex {
2095 name: "myapex_provider",
2096 key: "myapex.key",
2097 native_shared_libs: ["libfoo"],
2098 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002099 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002100 }
2101
2102 apex {
2103 name: "myapex_selfcontained",
2104 key: "myapex.key",
2105 native_shared_libs: ["lib_dep", "libfoo"],
2106 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002107 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002108 }
2109
2110 apex_key {
2111 name: "myapex.key",
2112 public_key: "testkey.avbpubkey",
2113 private_key: "testkey.pem",
2114 }
2115
2116 cc_library {
2117 name: "lib_nodep",
2118 srcs: ["mylib.cpp"],
2119 system_shared_libs: [],
2120 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002121 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09002122 }
2123
2124 cc_library {
2125 name: "lib_dep",
2126 srcs: ["mylib.cpp"],
2127 shared_libs: ["libfoo"],
2128 system_shared_libs: [],
2129 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002130 apex_available: [
2131 "myapex_dep",
2132 "myapex_provider",
2133 "myapex_selfcontained",
2134 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002135 }
2136
2137 cc_library {
2138 name: "libfoo",
2139 srcs: ["mytest.cpp"],
2140 stubs: {
2141 versions: ["1"],
2142 },
2143 system_shared_libs: [],
2144 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002145 apex_available: [
2146 "myapex_provider",
2147 "myapex_selfcontained",
2148 ],
Jooyung Hane1633032019-08-01 17:41:43 +09002149 }
2150 `)
2151
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002152 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002153 var provideNativeLibs, requireNativeLibs []string
2154
Sundong Ahnabb64432019-10-22 13:58:29 +09002155 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002156 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2157 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002158 ensureListEmpty(t, provideNativeLibs)
2159 ensureListEmpty(t, requireNativeLibs)
2160
Sundong Ahnabb64432019-10-22 13:58:29 +09002161 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002162 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2163 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002164 ensureListEmpty(t, provideNativeLibs)
2165 ensureListContains(t, requireNativeLibs, "libfoo.so")
2166
Sundong Ahnabb64432019-10-22 13:58:29 +09002167 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002168 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2169 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002170 ensureListContains(t, provideNativeLibs, "libfoo.so")
2171 ensureListEmpty(t, requireNativeLibs)
2172
Sundong Ahnabb64432019-10-22 13:58:29 +09002173 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002174 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2175 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002176 ensureListContains(t, provideNativeLibs, "libfoo.so")
2177 ensureListEmpty(t, requireNativeLibs)
2178}
2179
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002180func TestApexName(t *testing.T) {
2181 ctx, _ := testApex(t, `
2182 apex {
2183 name: "myapex",
2184 key: "myapex.key",
2185 apex_name: "com.android.myapex",
2186 }
2187
2188 apex_key {
2189 name: "myapex.key",
2190 public_key: "testkey.avbpubkey",
2191 private_key: "testkey.pem",
2192 }
2193 `)
2194
Sundong Ahnabb64432019-10-22 13:58:29 +09002195 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002196 apexManifestRule := module.Rule("apexManifestRule")
2197 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2198 apexRule := module.Rule("apexRule")
2199 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2200}
2201
Alex Light0851b882019-02-07 13:20:53 -08002202func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002203 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002204 apex {
2205 name: "myapex",
2206 key: "myapex.key",
2207 native_shared_libs: ["mylib_common"],
2208 }
2209
2210 apex_key {
2211 name: "myapex.key",
2212 public_key: "testkey.avbpubkey",
2213 private_key: "testkey.pem",
2214 }
2215
2216 cc_library {
2217 name: "mylib_common",
2218 srcs: ["mylib.cpp"],
2219 system_shared_libs: [],
2220 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002221 apex_available: [
2222 "//apex_available:platform",
2223 "myapex",
2224 ],
Alex Light0851b882019-02-07 13:20:53 -08002225 }
2226 `)
2227
Sundong Ahnabb64432019-10-22 13:58:29 +09002228 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002229 apexRule := module.Rule("apexRule")
2230 copyCmds := apexRule.Args["copy_commands"]
2231
2232 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2233 t.Log("Apex was a test apex!")
2234 t.Fail()
2235 }
2236 // Ensure that main rule creates an output
2237 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2238
2239 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002240 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002241
2242 // Ensure that both direct and indirect deps are copied into apex
2243 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2244
Colin Cross7113d202019-11-20 16:39:12 -08002245 // Ensure that the platform variant ends with _shared
2246 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002247
2248 if !android.InAnyApex("mylib_common") {
2249 t.Log("Found mylib_common not in any apex!")
2250 t.Fail()
2251 }
2252}
2253
2254func TestTestApex(t *testing.T) {
2255 if android.InAnyApex("mylib_common_test") {
2256 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!")
2257 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002258 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002259 apex_test {
2260 name: "myapex",
2261 key: "myapex.key",
2262 native_shared_libs: ["mylib_common_test"],
2263 }
2264
2265 apex_key {
2266 name: "myapex.key",
2267 public_key: "testkey.avbpubkey",
2268 private_key: "testkey.pem",
2269 }
2270
2271 cc_library {
2272 name: "mylib_common_test",
2273 srcs: ["mylib.cpp"],
2274 system_shared_libs: [],
2275 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002276 // TODO: remove //apex_available:platform
2277 apex_available: [
2278 "//apex_available:platform",
2279 "myapex",
2280 ],
Alex Light0851b882019-02-07 13:20:53 -08002281 }
2282 `)
2283
Sundong Ahnabb64432019-10-22 13:58:29 +09002284 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002285 apexRule := module.Rule("apexRule")
2286 copyCmds := apexRule.Args["copy_commands"]
2287
2288 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2289 t.Log("Apex was not a test apex!")
2290 t.Fail()
2291 }
2292 // Ensure that main rule creates an output
2293 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2294
2295 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002296 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002297
2298 // Ensure that both direct and indirect deps are copied into apex
2299 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2300
Colin Cross7113d202019-11-20 16:39:12 -08002301 // Ensure that the platform variant ends with _shared
2302 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002303
2304 if android.InAnyApex("mylib_common_test") {
2305 t.Log("Found mylib_common_test in some apex!")
2306 t.Fail()
2307 }
2308}
2309
Alex Light9670d332019-01-29 18:07:33 -08002310func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002311 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002312 apex {
2313 name: "myapex",
2314 key: "myapex.key",
2315 multilib: {
2316 first: {
2317 native_shared_libs: ["mylib_common"],
2318 }
2319 },
2320 target: {
2321 android: {
2322 multilib: {
2323 first: {
2324 native_shared_libs: ["mylib"],
2325 }
2326 }
2327 },
2328 host: {
2329 multilib: {
2330 first: {
2331 native_shared_libs: ["mylib2"],
2332 }
2333 }
2334 }
2335 }
2336 }
2337
2338 apex_key {
2339 name: "myapex.key",
2340 public_key: "testkey.avbpubkey",
2341 private_key: "testkey.pem",
2342 }
2343
2344 cc_library {
2345 name: "mylib",
2346 srcs: ["mylib.cpp"],
2347 system_shared_libs: [],
2348 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002349 // TODO: remove //apex_available:platform
2350 apex_available: [
2351 "//apex_available:platform",
2352 "myapex",
2353 ],
Alex Light9670d332019-01-29 18:07:33 -08002354 }
2355
2356 cc_library {
2357 name: "mylib_common",
2358 srcs: ["mylib.cpp"],
2359 system_shared_libs: [],
2360 stl: "none",
2361 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002362 // TODO: remove //apex_available:platform
2363 apex_available: [
2364 "//apex_available:platform",
2365 "myapex",
2366 ],
Alex Light9670d332019-01-29 18:07:33 -08002367 }
2368
2369 cc_library {
2370 name: "mylib2",
2371 srcs: ["mylib.cpp"],
2372 system_shared_libs: [],
2373 stl: "none",
2374 compile_multilib: "first",
2375 }
2376 `)
2377
Sundong Ahnabb64432019-10-22 13:58:29 +09002378 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002379 copyCmds := apexRule.Args["copy_commands"]
2380
2381 // Ensure that main rule creates an output
2382 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2383
2384 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002385 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2386 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2387 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002388
2389 // Ensure that both direct and indirect deps are copied into apex
2390 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2391 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2392 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2393
Colin Cross7113d202019-11-20 16:39:12 -08002394 // Ensure that the platform variant ends with _shared
2395 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2396 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2397 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002398}
Jiyong Park04480cf2019-02-06 00:16:29 +09002399
2400func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002401 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002402 apex {
2403 name: "myapex",
2404 key: "myapex.key",
2405 binaries: ["myscript"],
2406 }
2407
2408 apex_key {
2409 name: "myapex.key",
2410 public_key: "testkey.avbpubkey",
2411 private_key: "testkey.pem",
2412 }
2413
2414 sh_binary {
2415 name: "myscript",
2416 src: "mylib.cpp",
2417 filename: "myscript.sh",
2418 sub_dir: "script",
2419 }
2420 `)
2421
Sundong Ahnabb64432019-10-22 13:58:29 +09002422 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002423 copyCmds := apexRule.Args["copy_commands"]
2424
2425 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2426}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002427
Jooyung Han91df2082019-11-20 01:49:42 +09002428func TestApexInVariousPartition(t *testing.T) {
2429 testcases := []struct {
2430 propName, parition, flattenedPartition string
2431 }{
2432 {"", "system", "system_ext"},
2433 {"product_specific: true", "product", "product"},
2434 {"soc_specific: true", "vendor", "vendor"},
2435 {"proprietary: true", "vendor", "vendor"},
2436 {"vendor: true", "vendor", "vendor"},
2437 {"system_ext_specific: true", "system_ext", "system_ext"},
2438 }
2439 for _, tc := range testcases {
2440 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2441 ctx, _ := testApex(t, `
2442 apex {
2443 name: "myapex",
2444 key: "myapex.key",
2445 `+tc.propName+`
2446 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002447
Jooyung Han91df2082019-11-20 01:49:42 +09002448 apex_key {
2449 name: "myapex.key",
2450 public_key: "testkey.avbpubkey",
2451 private_key: "testkey.pem",
2452 }
2453 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002454
Jooyung Han91df2082019-11-20 01:49:42 +09002455 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2456 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2457 actual := apex.installDir.String()
2458 if actual != expected {
2459 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2460 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002461
Jooyung Han91df2082019-11-20 01:49:42 +09002462 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2463 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2464 actual = flattened.installDir.String()
2465 if actual != expected {
2466 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2467 }
2468 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002469 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002470}
Jiyong Park67882562019-03-21 01:11:21 +09002471
Jooyung Han54aca7b2019-11-20 02:26:02 +09002472func TestFileContexts(t *testing.T) {
2473 ctx, _ := testApex(t, `
2474 apex {
2475 name: "myapex",
2476 key: "myapex.key",
2477 }
2478
2479 apex_key {
2480 name: "myapex.key",
2481 public_key: "testkey.avbpubkey",
2482 private_key: "testkey.pem",
2483 }
2484 `)
2485 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2486 apexRule := module.Rule("apexRule")
2487 actual := apexRule.Args["file_contexts"]
2488 expected := "system/sepolicy/apex/myapex-file_contexts"
2489 if actual != expected {
2490 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2491 }
2492
2493 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2494 apex {
2495 name: "myapex",
2496 key: "myapex.key",
2497 file_contexts: "my_own_file_contexts",
2498 }
2499
2500 apex_key {
2501 name: "myapex.key",
2502 public_key: "testkey.avbpubkey",
2503 private_key: "testkey.pem",
2504 }
2505 `, withFiles(map[string][]byte{
2506 "my_own_file_contexts": nil,
2507 }))
2508
2509 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2510 apex {
2511 name: "myapex",
2512 key: "myapex.key",
2513 product_specific: true,
2514 file_contexts: "product_specific_file_contexts",
2515 }
2516
2517 apex_key {
2518 name: "myapex.key",
2519 public_key: "testkey.avbpubkey",
2520 private_key: "testkey.pem",
2521 }
2522 `)
2523
2524 ctx, _ = testApex(t, `
2525 apex {
2526 name: "myapex",
2527 key: "myapex.key",
2528 product_specific: true,
2529 file_contexts: "product_specific_file_contexts",
2530 }
2531
2532 apex_key {
2533 name: "myapex.key",
2534 public_key: "testkey.avbpubkey",
2535 private_key: "testkey.pem",
2536 }
2537 `, withFiles(map[string][]byte{
2538 "product_specific_file_contexts": nil,
2539 }))
2540 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2541 apexRule = module.Rule("apexRule")
2542 actual = apexRule.Args["file_contexts"]
2543 expected = "product_specific_file_contexts"
2544 if actual != expected {
2545 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2546 }
2547
2548 ctx, _ = testApex(t, `
2549 apex {
2550 name: "myapex",
2551 key: "myapex.key",
2552 product_specific: true,
2553 file_contexts: ":my-file-contexts",
2554 }
2555
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561
2562 filegroup {
2563 name: "my-file-contexts",
2564 srcs: ["product_specific_file_contexts"],
2565 }
2566 `, withFiles(map[string][]byte{
2567 "product_specific_file_contexts": nil,
2568 }))
2569 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2570 apexRule = module.Rule("apexRule")
2571 actual = apexRule.Args["file_contexts"]
2572 expected = "product_specific_file_contexts"
2573 if actual != expected {
2574 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2575 }
2576}
2577
Jiyong Park67882562019-03-21 01:11:21 +09002578func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002579 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002580 apex_key {
2581 name: "myapex.key",
2582 public_key: ":my.avbpubkey",
2583 private_key: ":my.pem",
2584 product_specific: true,
2585 }
2586
2587 filegroup {
2588 name: "my.avbpubkey",
2589 srcs: ["testkey2.avbpubkey"],
2590 }
2591
2592 filegroup {
2593 name: "my.pem",
2594 srcs: ["testkey2.pem"],
2595 }
2596 `)
2597
2598 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2599 expected_pubkey := "testkey2.avbpubkey"
2600 actual_pubkey := apex_key.public_key_file.String()
2601 if actual_pubkey != expected_pubkey {
2602 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2603 }
2604 expected_privkey := "testkey2.pem"
2605 actual_privkey := apex_key.private_key_file.String()
2606 if actual_privkey != expected_privkey {
2607 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2608 }
2609}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002610
2611func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002612 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002613 prebuilt_apex {
2614 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002615 arch: {
2616 arm64: {
2617 src: "myapex-arm64.apex",
2618 },
2619 arm: {
2620 src: "myapex-arm.apex",
2621 },
2622 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002623 }
2624 `)
2625
2626 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2627
Jiyong Parkc95714e2019-03-29 14:23:10 +09002628 expectedInput := "myapex-arm64.apex"
2629 if prebuilt.inputApex.String() != expectedInput {
2630 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2631 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002632}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002633
2634func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002635 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002636 prebuilt_apex {
2637 name: "myapex",
2638 src: "myapex-arm.apex",
2639 filename: "notmyapex.apex",
2640 }
2641 `)
2642
2643 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2644
2645 expected := "notmyapex.apex"
2646 if p.installFilename != expected {
2647 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2648 }
2649}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002650
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002651func TestPrebuiltOverrides(t *testing.T) {
2652 ctx, config := testApex(t, `
2653 prebuilt_apex {
2654 name: "myapex.prebuilt",
2655 src: "myapex-arm.apex",
2656 overrides: [
2657 "myapex",
2658 ],
2659 }
2660 `)
2661
2662 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2663
2664 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002665 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002666 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002667 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002668 }
2669}
2670
Roland Levillain630846d2019-06-26 12:48:34 +01002671func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002672 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002673 apex_test {
2674 name: "myapex",
2675 key: "myapex.key",
2676 tests: [
2677 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002678 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002679 ],
2680 }
2681
2682 apex_key {
2683 name: "myapex.key",
2684 public_key: "testkey.avbpubkey",
2685 private_key: "testkey.pem",
2686 }
2687
2688 cc_test {
2689 name: "mytest",
2690 gtest: false,
2691 srcs: ["mytest.cpp"],
2692 relative_install_path: "test",
2693 system_shared_libs: [],
2694 static_executable: true,
2695 stl: "none",
2696 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002697
2698 cc_test {
2699 name: "mytests",
2700 gtest: false,
2701 srcs: [
2702 "mytest1.cpp",
2703 "mytest2.cpp",
2704 "mytest3.cpp",
2705 ],
2706 test_per_src: true,
2707 relative_install_path: "test",
2708 system_shared_libs: [],
2709 static_executable: true,
2710 stl: "none",
2711 }
Roland Levillain630846d2019-06-26 12:48:34 +01002712 `)
2713
Sundong Ahnabb64432019-10-22 13:58:29 +09002714 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002715 copyCmds := apexRule.Args["copy_commands"]
2716
2717 // Ensure that test dep is copied into apex.
2718 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002719
2720 // Ensure that test deps built with `test_per_src` are copied into apex.
2721 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2722 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2723 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002724
2725 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002726 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002727 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2728 name := apexBundle.BaseModuleName()
2729 prefix := "TARGET_"
2730 var builder strings.Builder
2731 data.Custom(&builder, name, prefix, "", data)
2732 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002733 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2734 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2735 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2736 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002737 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002738 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002739 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002740}
2741
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002742func TestInstallExtraFlattenedApexes(t *testing.T) {
2743 ctx, config := testApex(t, `
2744 apex {
2745 name: "myapex",
2746 key: "myapex.key",
2747 }
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753 `, func(fs map[string][]byte, config android.Config) {
2754 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2755 })
2756 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09002757 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002758 mk := android.AndroidMkDataForTest(t, config, "", ab)
2759 var builder strings.Builder
2760 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2761 androidMk := builder.String()
2762 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2763}
2764
Jooyung Han5c998b92019-06-27 11:30:33 +09002765func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002766 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002767 apex {
2768 name: "myapex",
2769 key: "myapex.key",
2770 native_shared_libs: ["mylib"],
2771 uses: ["commonapex"],
2772 }
2773
2774 apex {
2775 name: "commonapex",
2776 key: "myapex.key",
2777 native_shared_libs: ["libcommon"],
2778 provide_cpp_shared_libs: true,
2779 }
2780
2781 apex_key {
2782 name: "myapex.key",
2783 public_key: "testkey.avbpubkey",
2784 private_key: "testkey.pem",
2785 }
2786
2787 cc_library {
2788 name: "mylib",
2789 srcs: ["mylib.cpp"],
2790 shared_libs: ["libcommon"],
2791 system_shared_libs: [],
2792 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002793 apex_available: [ "myapex" ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002794 }
2795
2796 cc_library {
2797 name: "libcommon",
2798 srcs: ["mylib_common.cpp"],
2799 system_shared_libs: [],
2800 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002801 // TODO: remove //apex_available:platform
2802 apex_available: [
2803 "//apex_available:platform",
2804 "commonapex",
2805 "myapex",
2806 ],
Jooyung Han5c998b92019-06-27 11:30:33 +09002807 }
2808 `)
2809
Sundong Ahnabb64432019-10-22 13:58:29 +09002810 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002811 apexRule1 := module1.Rule("apexRule")
2812 copyCmds1 := apexRule1.Args["copy_commands"]
2813
Sundong Ahnabb64432019-10-22 13:58:29 +09002814 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002815 apexRule2 := module2.Rule("apexRule")
2816 copyCmds2 := apexRule2.Args["copy_commands"]
2817
Colin Cross7113d202019-11-20 16:39:12 -08002818 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2819 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002820 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2821 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2822 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2823}
2824
2825func TestApexUsesFailsIfNotProvided(t *testing.T) {
2826 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2827 apex {
2828 name: "myapex",
2829 key: "myapex.key",
2830 uses: ["commonapex"],
2831 }
2832
2833 apex {
2834 name: "commonapex",
2835 key: "myapex.key",
2836 }
2837
2838 apex_key {
2839 name: "myapex.key",
2840 public_key: "testkey.avbpubkey",
2841 private_key: "testkey.pem",
2842 }
2843 `)
2844 testApexError(t, `uses: "commonapex" is not a provider`, `
2845 apex {
2846 name: "myapex",
2847 key: "myapex.key",
2848 uses: ["commonapex"],
2849 }
2850
2851 cc_library {
2852 name: "commonapex",
2853 system_shared_libs: [],
2854 stl: "none",
2855 }
2856
2857 apex_key {
2858 name: "myapex.key",
2859 public_key: "testkey.avbpubkey",
2860 private_key: "testkey.pem",
2861 }
2862 `)
2863}
2864
2865func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2866 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2867 apex {
2868 name: "myapex",
2869 key: "myapex.key",
2870 use_vendor: true,
2871 uses: ["commonapex"],
2872 }
2873
2874 apex {
2875 name: "commonapex",
2876 key: "myapex.key",
2877 provide_cpp_shared_libs: true,
2878 }
2879
2880 apex_key {
2881 name: "myapex.key",
2882 public_key: "testkey.avbpubkey",
2883 private_key: "testkey.pem",
2884 }
Jooyung Handc782442019-11-01 03:14:38 +09002885 `, func(fs map[string][]byte, config android.Config) {
2886 setUseVendorWhitelistForTest(config, []string{"myapex"})
2887 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002888}
2889
Jooyung Hand48f3c32019-08-23 11:18:57 +09002890func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2891 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2892 apex {
2893 name: "myapex",
2894 key: "myapex.key",
2895 native_shared_libs: ["libfoo"],
2896 }
2897
2898 apex_key {
2899 name: "myapex.key",
2900 public_key: "testkey.avbpubkey",
2901 private_key: "testkey.pem",
2902 }
2903
2904 cc_library {
2905 name: "libfoo",
2906 stl: "none",
2907 system_shared_libs: [],
2908 enabled: false,
2909 }
2910 `)
2911 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2912 apex {
2913 name: "myapex",
2914 key: "myapex.key",
2915 java_libs: ["myjar"],
2916 }
2917
2918 apex_key {
2919 name: "myapex.key",
2920 public_key: "testkey.avbpubkey",
2921 private_key: "testkey.pem",
2922 }
2923
2924 java_library {
2925 name: "myjar",
2926 srcs: ["foo/bar/MyClass.java"],
2927 sdk_version: "none",
2928 system_modules: "none",
2929 compile_dex: true,
2930 enabled: false,
2931 }
2932 `)
2933}
2934
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002935func TestApexWithApps(t *testing.T) {
2936 ctx, _ := testApex(t, `
2937 apex {
2938 name: "myapex",
2939 key: "myapex.key",
2940 apps: [
2941 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002942 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002943 ],
2944 }
2945
2946 apex_key {
2947 name: "myapex.key",
2948 public_key: "testkey.avbpubkey",
2949 private_key: "testkey.pem",
2950 }
2951
2952 android_app {
2953 name: "AppFoo",
2954 srcs: ["foo/bar/MyClass.java"],
2955 sdk_version: "none",
2956 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002957 jni_libs: ["libjni"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002958 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002959 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002960
2961 android_app {
2962 name: "AppFooPriv",
2963 srcs: ["foo/bar/MyClass.java"],
2964 sdk_version: "none",
2965 system_modules: "none",
2966 privileged: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002967 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09002968 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002969
2970 cc_library_shared {
2971 name: "libjni",
2972 srcs: ["mylib.cpp"],
2973 stl: "none",
2974 system_shared_libs: [],
2975 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002976 `)
2977
Sundong Ahnabb64432019-10-22 13:58:29 +09002978 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002979 apexRule := module.Rule("apexRule")
2980 copyCmds := apexRule.Args["copy_commands"]
2981
2982 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002983 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002984
2985 // JNI libraries are embedded inside APK
2986 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002987 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002988 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2989 // ... uncompressed
2990 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2991 t.Errorf("jni lib is not uncompressed for AppFoo")
2992 }
2993 // ... and not directly inside the APEX
2994 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002995}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002996
Dario Frenicde2a032019-10-27 00:29:22 +01002997func TestApexWithAppImports(t *testing.T) {
2998 ctx, _ := testApex(t, `
2999 apex {
3000 name: "myapex",
3001 key: "myapex.key",
3002 apps: [
3003 "AppFooPrebuilt",
3004 "AppFooPrivPrebuilt",
3005 ],
3006 }
3007
3008 apex_key {
3009 name: "myapex.key",
3010 public_key: "testkey.avbpubkey",
3011 private_key: "testkey.pem",
3012 }
3013
3014 android_app_import {
3015 name: "AppFooPrebuilt",
3016 apk: "PrebuiltAppFoo.apk",
3017 presigned: true,
3018 dex_preopt: {
3019 enabled: false,
3020 },
3021 }
3022
3023 android_app_import {
3024 name: "AppFooPrivPrebuilt",
3025 apk: "PrebuiltAppFooPriv.apk",
3026 privileged: true,
3027 presigned: true,
3028 dex_preopt: {
3029 enabled: false,
3030 },
3031 }
3032 `)
3033
Sundong Ahnabb64432019-10-22 13:58:29 +09003034 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01003035 apexRule := module.Rule("apexRule")
3036 copyCmds := apexRule.Args["copy_commands"]
3037
3038 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
3039 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09003040}
3041
Dario Freni6f3937c2019-12-20 22:58:03 +00003042func TestApexWithTestHelperApp(t *testing.T) {
3043 ctx, _ := testApex(t, `
3044 apex {
3045 name: "myapex",
3046 key: "myapex.key",
3047 apps: [
3048 "TesterHelpAppFoo",
3049 ],
3050 }
3051
3052 apex_key {
3053 name: "myapex.key",
3054 public_key: "testkey.avbpubkey",
3055 private_key: "testkey.pem",
3056 }
3057
3058 android_test_helper_app {
3059 name: "TesterHelpAppFoo",
3060 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003061 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00003062 }
3063
3064 `)
3065
3066 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3067 apexRule := module.Rule("apexRule")
3068 copyCmds := apexRule.Args["copy_commands"]
3069
3070 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
3071}
3072
Jooyung Han18020ea2019-11-13 10:50:48 +09003073func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
3074 // libfoo's apex_available comes from cc_defaults
3075 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
3076 apex {
3077 name: "myapex",
3078 key: "myapex.key",
3079 native_shared_libs: ["libfoo"],
3080 }
3081
3082 apex_key {
3083 name: "myapex.key",
3084 public_key: "testkey.avbpubkey",
3085 private_key: "testkey.pem",
3086 }
3087
3088 apex {
3089 name: "otherapex",
3090 key: "myapex.key",
3091 native_shared_libs: ["libfoo"],
3092 }
3093
3094 cc_defaults {
3095 name: "libfoo-defaults",
3096 apex_available: ["otherapex"],
3097 }
3098
3099 cc_library {
3100 name: "libfoo",
3101 defaults: ["libfoo-defaults"],
3102 stl: "none",
3103 system_shared_libs: [],
3104 }`)
3105}
3106
Jiyong Park127b40b2019-09-30 16:04:35 +09003107func TestApexAvailable(t *testing.T) {
3108 // libfoo is not available to myapex, but only to otherapex
3109 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
3110 apex {
3111 name: "myapex",
3112 key: "myapex.key",
3113 native_shared_libs: ["libfoo"],
3114 }
3115
3116 apex_key {
3117 name: "myapex.key",
3118 public_key: "testkey.avbpubkey",
3119 private_key: "testkey.pem",
3120 }
3121
3122 apex {
3123 name: "otherapex",
3124 key: "otherapex.key",
3125 native_shared_libs: ["libfoo"],
3126 }
3127
3128 apex_key {
3129 name: "otherapex.key",
3130 public_key: "testkey.avbpubkey",
3131 private_key: "testkey.pem",
3132 }
3133
3134 cc_library {
3135 name: "libfoo",
3136 stl: "none",
3137 system_shared_libs: [],
3138 apex_available: ["otherapex"],
3139 }`)
3140
3141 // libbar is an indirect dep
3142 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3143 apex {
3144 name: "myapex",
3145 key: "myapex.key",
3146 native_shared_libs: ["libfoo"],
3147 }
3148
3149 apex_key {
3150 name: "myapex.key",
3151 public_key: "testkey.avbpubkey",
3152 private_key: "testkey.pem",
3153 }
3154
3155 apex {
3156 name: "otherapex",
3157 key: "otherapex.key",
3158 native_shared_libs: ["libfoo"],
3159 }
3160
3161 apex_key {
3162 name: "otherapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }
3166
3167 cc_library {
3168 name: "libfoo",
3169 stl: "none",
3170 shared_libs: ["libbar"],
3171 system_shared_libs: [],
3172 apex_available: ["myapex", "otherapex"],
3173 }
3174
3175 cc_library {
3176 name: "libbar",
3177 stl: "none",
3178 system_shared_libs: [],
3179 apex_available: ["otherapex"],
3180 }`)
3181
3182 testApexError(t, "\"otherapex\" is not a valid module name", `
3183 apex {
3184 name: "myapex",
3185 key: "myapex.key",
3186 native_shared_libs: ["libfoo"],
3187 }
3188
3189 apex_key {
3190 name: "myapex.key",
3191 public_key: "testkey.avbpubkey",
3192 private_key: "testkey.pem",
3193 }
3194
3195 cc_library {
3196 name: "libfoo",
3197 stl: "none",
3198 system_shared_libs: [],
3199 apex_available: ["otherapex"],
3200 }`)
3201
3202 ctx, _ := testApex(t, `
3203 apex {
3204 name: "myapex",
3205 key: "myapex.key",
3206 native_shared_libs: ["libfoo", "libbar"],
3207 }
3208
3209 apex_key {
3210 name: "myapex.key",
3211 public_key: "testkey.avbpubkey",
3212 private_key: "testkey.pem",
3213 }
3214
3215 cc_library {
3216 name: "libfoo",
3217 stl: "none",
3218 system_shared_libs: [],
3219 apex_available: ["myapex"],
3220 }
3221
3222 cc_library {
3223 name: "libbar",
3224 stl: "none",
3225 system_shared_libs: [],
3226 apex_available: ["//apex_available:anyapex"],
3227 }`)
3228
3229 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003230 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3231 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3232 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3233 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003234
3235 ctx, _ = testApex(t, `
3236 apex {
3237 name: "myapex",
3238 key: "myapex.key",
3239 }
3240
3241 apex_key {
3242 name: "myapex.key",
3243 public_key: "testkey.avbpubkey",
3244 private_key: "testkey.pem",
3245 }
3246
3247 cc_library {
3248 name: "libfoo",
3249 stl: "none",
3250 system_shared_libs: [],
3251 apex_available: ["//apex_available:platform"],
3252 }`)
3253
3254 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003255 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3256 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003257
3258 ctx, _ = testApex(t, `
3259 apex {
3260 name: "myapex",
3261 key: "myapex.key",
3262 native_shared_libs: ["libfoo"],
3263 }
3264
3265 apex_key {
3266 name: "myapex.key",
3267 public_key: "testkey.avbpubkey",
3268 private_key: "testkey.pem",
3269 }
3270
3271 cc_library {
3272 name: "libfoo",
3273 stl: "none",
3274 system_shared_libs: [],
3275 apex_available: ["myapex"],
3276 static: {
3277 apex_available: ["//apex_available:platform"],
3278 },
3279 }`)
3280
3281 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003282 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3283 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003284 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003285 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3286 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003287}
3288
Jiyong Park5d790c32019-11-15 18:40:32 +09003289func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003290 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003291 apex {
3292 name: "myapex",
3293 key: "myapex.key",
3294 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003295 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003296 }
3297
3298 override_apex {
3299 name: "override_myapex",
3300 base: "myapex",
3301 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003302 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003303 }
3304
3305 apex_key {
3306 name: "myapex.key",
3307 public_key: "testkey.avbpubkey",
3308 private_key: "testkey.pem",
3309 }
3310
3311 android_app {
3312 name: "app",
3313 srcs: ["foo/bar/MyClass.java"],
3314 package_name: "foo",
3315 sdk_version: "none",
3316 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003317 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09003318 }
3319
3320 override_android_app {
3321 name: "override_app",
3322 base: "app",
3323 package_name: "bar",
3324 }
3325 `)
3326
Jiyong Park317645e2019-12-05 13:20:58 +09003327 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3328 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3329 if originalVariant.GetOverriddenBy() != "" {
3330 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3331 }
3332 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3333 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3334 }
3335
Jiyong Park5d790c32019-11-15 18:40:32 +09003336 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3337 apexRule := module.Rule("apexRule")
3338 copyCmds := apexRule.Args["copy_commands"]
3339
3340 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3341 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003342
3343 apexBundle := module.Module().(*apexBundle)
3344 name := apexBundle.Name()
3345 if name != "override_myapex" {
3346 t.Errorf("name should be \"override_myapex\", but was %q", name)
3347 }
3348
3349 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3350 var builder strings.Builder
3351 data.Custom(&builder, name, "TARGET_", "", data)
3352 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003353 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003354 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3355 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003356 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003357 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003358 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003359 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3360 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003361}
3362
Jooyung Han214bf372019-11-12 13:03:50 +09003363func TestLegacyAndroid10Support(t *testing.T) {
3364 ctx, _ := testApex(t, `
3365 apex {
3366 name: "myapex",
3367 key: "myapex.key",
3368 legacy_android10_support: true,
3369 }
3370
3371 apex_key {
3372 name: "myapex.key",
3373 public_key: "testkey.avbpubkey",
3374 private_key: "testkey.pem",
3375 }
3376 `)
3377
3378 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3379 args := module.Rule("apexRule").Args
3380 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00003381 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Jooyung Han214bf372019-11-12 13:03:50 +09003382}
3383
Jooyung Han58f26ab2019-12-18 15:34:32 +09003384func TestJavaSDKLibrary(t *testing.T) {
3385 ctx, _ := testApex(t, `
3386 apex {
3387 name: "myapex",
3388 key: "myapex.key",
3389 java_libs: ["foo"],
3390 }
3391
3392 apex_key {
3393 name: "myapex.key",
3394 public_key: "testkey.avbpubkey",
3395 private_key: "testkey.pem",
3396 }
3397
3398 java_sdk_library {
3399 name: "foo",
3400 srcs: ["a.java"],
3401 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003402 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09003403 }
3404 `, withFiles(map[string][]byte{
3405 "api/current.txt": nil,
3406 "api/removed.txt": nil,
3407 "api/system-current.txt": nil,
3408 "api/system-removed.txt": nil,
3409 "api/test-current.txt": nil,
3410 "api/test-removed.txt": nil,
3411 }))
3412
3413 // java_sdk_library installs both impl jar and permission XML
3414 ensureExactContents(t, ctx, "myapex", []string{
3415 "javalib/foo.jar",
3416 "etc/permissions/foo.xml",
3417 })
3418 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jooyung Han624058e2019-12-24 18:38:06 +09003419 xml := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml")
3420 ensureContains(t, xml.Args["content"], `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09003421}
3422
Jiyong Park479321d2019-12-16 11:47:12 +09003423func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3424 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3425 apex {
3426 name: "myapex",
3427 key: "myapex.key",
3428 java_libs: ["myjar"],
3429 }
3430
3431 apex_key {
3432 name: "myapex.key",
3433 public_key: "testkey.avbpubkey",
3434 private_key: "testkey.pem",
3435 }
3436
3437 java_library {
3438 name: "myjar",
3439 srcs: ["foo/bar/MyClass.java"],
3440 sdk_version: "none",
3441 system_modules: "none",
3442 }
3443 `)
3444}
3445
Jiyong Park7afd1072019-12-30 16:56:33 +09003446func TestCarryRequiredModuleNames(t *testing.T) {
3447 ctx, config := testApex(t, `
3448 apex {
3449 name: "myapex",
3450 key: "myapex.key",
3451 native_shared_libs: ["mylib"],
3452 }
3453
3454 apex_key {
3455 name: "myapex.key",
3456 public_key: "testkey.avbpubkey",
3457 private_key: "testkey.pem",
3458 }
3459
3460 cc_library {
3461 name: "mylib",
3462 srcs: ["mylib.cpp"],
3463 system_shared_libs: [],
3464 stl: "none",
3465 required: ["a", "b"],
3466 host_required: ["c", "d"],
3467 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003468 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09003469 }
3470 `)
3471
3472 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
3473 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3474 name := apexBundle.BaseModuleName()
3475 prefix := "TARGET_"
3476 var builder strings.Builder
3477 data.Custom(&builder, name, prefix, "", data)
3478 androidMk := builder.String()
3479 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
3480 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
3481 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
3482}
3483
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003484func TestMain(m *testing.M) {
3485 run := func() int {
3486 setUp()
3487 defer tearDown()
3488
3489 return m.Run()
3490 }
3491
3492 os.Exit(run())
3493}