blob: 70d74ca6a7d8890af212e0c75f6f85e603c2e889 [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) {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070095 config := android.TestArchConfig(buildDir, nil)
96 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
97 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
98 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
99 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
100 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jooyung Han344d5432019-08-23 11:17:39 +0900101 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900102
103 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -0800104 ctx.RegisterModuleType("apex", BundleFactory)
105 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
106 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
107 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
108 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
109 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
110 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111
Colin Cross4b49b762019-11-22 15:25:03 -0800112 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
113 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
114 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
115 ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory)
116 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
117 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
118 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
119 ctx.RegisterModuleType("cc_defaults", func() android.Module {
Jooyung Han18020ea2019-11-13 10:50:48 +0900120 return cc.DefaultsFactory()
Colin Cross4b49b762019-11-22 15:25:03 -0800121 })
122 ctx.RegisterModuleType("cc_test", cc.TestFactory)
123 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
124 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
125 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
126 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
127 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
128 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
129 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
130 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
131 ctx.RegisterModuleType("java_library", java.LibraryFactory)
132 ctx.RegisterModuleType("java_import", java.ImportFactory)
133 ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory)
134 ctx.RegisterModuleType("android_app", java.AndroidAppFactory)
135 ctx.RegisterModuleType("android_app_import", java.AndroidAppImportFactory)
136 ctx.RegisterModuleType("override_android_app", java.OverrideAndroidAppModuleFactory)
Jiyong Park7f7766d2019-07-25 22:02:35 +0900137
Jooyung Han344d5432019-08-23 11:17:39 +0900138 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700139 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
140 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
141 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900142 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jooyung Han214bf372019-11-12 13:03:50 +0900144 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100145 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900146 ctx.BottomUp("version", cc.VersionMutator).Parallel()
147 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
Jooyung Han344d5432019-08-23 11:17:39 +0900148 })
Jooyung Han31c470b2019-10-18 16:26:59 +0900149 ctx.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Park5d790c32019-11-15 18:40:32 +0900150 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Jooyung Han31c470b2019-10-18 16:26:59 +0900151 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han344d5432019-08-23 11:17:39 +0900152 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900153 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
154 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900155 })
156
157 ctx.Register()
158
159 bp = bp + `
160 toolchain_library {
161 name: "libcompiler_rt-extras",
162 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900165 }
166
167 toolchain_library {
168 name: "libatomic",
169 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900170 vendor_available: true,
171 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900172 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900173 }
174
175 toolchain_library {
176 name: "libgcc",
177 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900178 vendor_available: true,
179 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900180 }
181
182 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700183 name: "libgcc_stripped",
184 src: "",
185 vendor_available: true,
186 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900187 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700188 }
189
190 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900191 name: "libclang_rt.builtins-aarch64-android",
192 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900193 vendor_available: true,
194 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900196 }
197
198 toolchain_library {
199 name: "libclang_rt.builtins-arm-android",
200 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900201 vendor_available: true,
202 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900203 native_bridge_supported: true,
204 }
205
206 toolchain_library {
207 name: "libclang_rt.builtins-x86_64-android",
208 src: "",
209 vendor_available: true,
210 recovery_available: true,
211 native_bridge_supported: true,
212 }
213
214 toolchain_library {
215 name: "libclang_rt.builtins-i686-android",
216 src: "",
217 vendor_available: true,
218 recovery_available: true,
219 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220 }
221
222 cc_object {
223 name: "crtbegin_so",
224 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900225 vendor_available: true,
226 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900227 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 }
229
230 cc_object {
231 name: "crtend_so",
232 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900233 vendor_available: true,
234 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900235 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236 }
237
Alex Light3d673592019-01-18 14:37:31 -0800238 cc_object {
239 name: "crtbegin_static",
240 stl: "none",
241 }
242
243 cc_object {
244 name: "crtend_android",
245 stl: "none",
246 }
247
Jiyong Parkda6eb592018-12-19 17:12:36 +0900248 llndk_library {
249 name: "libc",
250 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900251 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900252 }
253
254 llndk_library {
255 name: "libm",
256 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900257 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900258 }
259
260 llndk_library {
261 name: "libdl",
262 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900263 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900264 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900265
266 filegroup {
267 name: "myapex-file_contexts",
268 srcs: [
269 "system/sepolicy/apex/myapex-file_contexts",
270 ],
271 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 `
Dario Frenicde2a032019-10-27 00:29:22 +0100273 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274
Jooyung Han344d5432019-08-23 11:17:39 +0900275 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900276 "Android.bp": []byte(bp),
277 "a.java": nil,
278 "PrebuiltAppFoo.apk": nil,
279 "PrebuiltAppFooPriv.apk": nil,
280 "build/make/target/product/security": nil,
281 "apex_manifest.json": nil,
282 "AndroidManifest.xml": nil,
283 "system/sepolicy/apex/myapex-file_contexts": nil,
284 "system/sepolicy/apex/otherapex-file_contexts": nil,
285 "system/sepolicy/apex/commonapex-file_contexts": nil,
286 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
287 "mylib.cpp": nil,
288 "mylib_common.cpp": nil,
289 "mytest.cpp": nil,
290 "mytest1.cpp": nil,
291 "mytest2.cpp": nil,
292 "mytest3.cpp": nil,
293 "myprebuilt": nil,
294 "my_include": nil,
295 "foo/bar/MyClass.java": nil,
296 "prebuilt.jar": nil,
297 "vendor/foo/devkeys/test.x509.pem": nil,
298 "vendor/foo/devkeys/test.pk8": nil,
299 "testkey.x509.pem": nil,
300 "testkey.pk8": nil,
301 "testkey.override.x509.pem": nil,
302 "testkey.override.pk8": nil,
303 "vendor/foo/devkeys/testkey.avbpubkey": nil,
304 "vendor/foo/devkeys/testkey.pem": nil,
305 "NOTICE": nil,
306 "custom_notice": nil,
307 "testkey2.avbpubkey": nil,
308 "testkey2.pem": nil,
309 "myapex-arm64.apex": nil,
310 "myapex-arm.apex": nil,
311 "frameworks/base/api/current.txt": nil,
312 "framework/aidl/a.aidl": nil,
313 "build/make/core/proguard.flags": nil,
314 "build/make/core/proguard_basic_keeps.flags": nil,
315 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900316 }
317
318 for _, handler := range handlers {
319 handler(fs, config)
320 }
321
322 ctx.MockFileSystem(fs)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323
Jooyung Han5c998b92019-06-27 11:30:33 +0900324 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325}
326
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700327func setUp() {
328 var err error
329 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700331 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333}
334
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700335func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 os.RemoveAll(buildDir)
337}
338
339// ensure that 'result' contains 'expected'
340func ensureContains(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 !strings.Contains(result, expected) {
343 t.Errorf("%q is not found in %q", expected, result)
344 }
345}
346
347// ensures that 'result' does not contain 'notExpected'
348func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900349 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900350 if strings.Contains(result, notExpected) {
351 t.Errorf("%q is found in %q", notExpected, result)
352 }
353}
354
355func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900356 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900357 if !android.InList(expected, result) {
358 t.Errorf("%q is not found in %v", expected, result)
359 }
360}
361
362func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900363 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 if android.InList(notExpected, result) {
365 t.Errorf("%q is found in %v", notExpected, result)
366 }
367}
368
Jooyung Hane1633032019-08-01 17:41:43 +0900369func ensureListEmpty(t *testing.T, result []string) {
370 t.Helper()
371 if len(result) > 0 {
372 t.Errorf("%q is expected to be empty", result)
373 }
374}
375
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376// Minimal test
377func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700378 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900379 apex_defaults {
380 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900381 manifest: ":myapex.manifest",
382 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 key: "myapex.key",
384 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800385 multilib: {
386 both: {
387 binaries: ["foo",],
388 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900389 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900390 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900391 }
392
Jiyong Park30ca9372019-02-07 16:27:23 +0900393 apex {
394 name: "myapex",
395 defaults: ["myapex-defaults"],
396 }
397
Jiyong Park25fc6a92018-11-18 18:02:45 +0900398 apex_key {
399 name: "myapex.key",
400 public_key: "testkey.avbpubkey",
401 private_key: "testkey.pem",
402 }
403
Jiyong Park809bb722019-02-13 21:33:49 +0900404 filegroup {
405 name: "myapex.manifest",
406 srcs: ["apex_manifest.json"],
407 }
408
409 filegroup {
410 name: "myapex.androidmanifest",
411 srcs: ["AndroidManifest.xml"],
412 }
413
Jiyong Park25fc6a92018-11-18 18:02:45 +0900414 cc_library {
415 name: "mylib",
416 srcs: ["mylib.cpp"],
417 shared_libs: ["mylib2"],
418 system_shared_libs: [],
419 stl: "none",
420 }
421
Alex Light3d673592019-01-18 14:37:31 -0800422 cc_binary {
423 name: "foo",
424 srcs: ["mylib.cpp"],
425 compile_multilib: "both",
426 multilib: {
427 lib32: {
428 suffix: "32",
429 },
430 lib64: {
431 suffix: "64",
432 },
433 },
434 symlinks: ["foo_link_"],
435 symlink_preferred_arch: true,
436 system_shared_libs: [],
437 static_executable: true,
438 stl: "none",
439 }
440
Jiyong Park25fc6a92018-11-18 18:02:45 +0900441 cc_library {
442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900446 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900447 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900448
449 java_library {
450 name: "myjar",
451 srcs: ["foo/bar/MyClass.java"],
452 sdk_version: "none",
453 system_modules: "none",
454 compile_dex: true,
455 static_libs: ["myotherjar"],
456 }
457
458 java_library {
459 name: "myotherjar",
460 srcs: ["foo/bar/MyClass.java"],
461 sdk_version: "none",
462 system_modules: "none",
463 compile_dex: true,
464 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900465
466 java_import {
467 name: "myprebuiltjar",
468 jars: ["prebuilt.jar"],
469 installable: true,
470 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471 `)
472
Sundong Ahnabb64432019-10-22 13:58:29 +0900473 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900474
475 optFlags := apexRule.Args["opt_flags"]
476 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700477 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900478 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900479
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480 copyCmds := apexRule.Args["copy_commands"]
481
482 // Ensure that main rule creates an output
483 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
484
485 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900486 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900487 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900488 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900489
490 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900493
494 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800495 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
496 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900497 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900498 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900499 // .. but not for java libs
500 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800501
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 // Ensure that the platform variant ends with _core_shared or _common
Logan Chien3aeedc92018-12-26 15:32:21 +0800503 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
504 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900505 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
506 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900507 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800508
509 // Ensure that all symlinks are present.
510 found_foo_link_64 := false
511 found_foo := false
512 for _, cmd := range strings.Split(copyCmds, " && ") {
513 if strings.HasPrefix(cmd, "ln -s foo64") {
514 if strings.HasSuffix(cmd, "bin/foo") {
515 found_foo = true
516 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
517 found_foo_link_64 = true
518 }
519 }
520 }
521 good := found_foo && found_foo_link_64
522 if !good {
523 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
524 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900525
Sundong Ahnabb64432019-10-22 13:58:29 +0900526 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700527 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700528 if len(noticeInputs) != 2 {
529 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900530 }
531 ensureListContains(t, noticeInputs, "NOTICE")
532 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800533}
534
Jooyung Hanf21c7972019-12-16 22:32:06 +0900535func TestDefaults(t *testing.T) {
536 ctx, _ := testApex(t, `
537 apex_defaults {
538 name: "myapex-defaults",
539 key: "myapex.key",
540 prebuilts: ["myetc"],
541 native_shared_libs: ["mylib"],
542 java_libs: ["myjar"],
543 apps: ["AppFoo"],
544 }
545
546 prebuilt_etc {
547 name: "myetc",
548 src: "myprebuilt",
549 }
550
551 apex {
552 name: "myapex",
553 defaults: ["myapex-defaults"],
554 }
555
556 apex_key {
557 name: "myapex.key",
558 public_key: "testkey.avbpubkey",
559 private_key: "testkey.pem",
560 }
561
562 cc_library {
563 name: "mylib",
564 system_shared_libs: [],
565 stl: "none",
566 }
567
568 java_library {
569 name: "myjar",
570 srcs: ["foo/bar/MyClass.java"],
571 sdk_version: "none",
572 system_modules: "none",
573 compile_dex: true,
574 }
575
576 android_app {
577 name: "AppFoo",
578 srcs: ["foo/bar/MyClass.java"],
579 sdk_version: "none",
580 system_modules: "none",
581 }
582 `)
583 ensureExactContents(t, ctx, "myapex", []string{
584 "etc/myetc",
585 "javalib/myjar.jar",
586 "lib64/mylib.so",
587 "app/AppFoo/AppFoo.apk",
588 })
589}
590
Jooyung Han01a3ee22019-11-02 02:52:25 +0900591func TestApexManifest(t *testing.T) {
592 ctx, _ := testApex(t, `
593 apex {
594 name: "myapex",
595 key: "myapex.key",
596 }
597
598 apex_key {
599 name: "myapex.key",
600 public_key: "testkey.avbpubkey",
601 private_key: "testkey.pem",
602 }
603 `)
604
605 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900606 args := module.Rule("apexRule").Args
607 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
608 t.Error("manifest should be apex_manifest.pb, but " + manifest)
609 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900610}
611
Alex Light5098a612018-11-29 17:12:15 -0800612func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700613 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800614 apex {
615 name: "myapex",
616 key: "myapex.key",
617 payload_type: "zip",
618 native_shared_libs: ["mylib"],
619 }
620
621 apex_key {
622 name: "myapex.key",
623 public_key: "testkey.avbpubkey",
624 private_key: "testkey.pem",
625 }
626
627 cc_library {
628 name: "mylib",
629 srcs: ["mylib.cpp"],
630 shared_libs: ["mylib2"],
631 system_shared_libs: [],
632 stl: "none",
633 }
634
635 cc_library {
636 name: "mylib2",
637 srcs: ["mylib.cpp"],
638 system_shared_libs: [],
639 stl: "none",
640 }
641 `)
642
Sundong Ahnabb64432019-10-22 13:58:29 +0900643 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800644 copyCmds := zipApexRule.Args["copy_commands"]
645
646 // Ensure that main rule creates an output
647 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
648
649 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900650 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800651
652 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900653 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800654
655 // Ensure that both direct and indirect deps are copied into apex
656 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
657 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658}
659
660func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700661 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900662 apex {
663 name: "myapex",
664 key: "myapex.key",
665 native_shared_libs: ["mylib", "mylib3"],
666 }
667
668 apex_key {
669 name: "myapex.key",
670 public_key: "testkey.avbpubkey",
671 private_key: "testkey.pem",
672 }
673
674 cc_library {
675 name: "mylib",
676 srcs: ["mylib.cpp"],
677 shared_libs: ["mylib2", "mylib3"],
678 system_shared_libs: [],
679 stl: "none",
680 }
681
682 cc_library {
683 name: "mylib2",
684 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900685 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686 system_shared_libs: [],
687 stl: "none",
688 stubs: {
689 versions: ["1", "2", "3"],
690 },
691 }
692
693 cc_library {
694 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900695 srcs: ["mylib.cpp"],
696 shared_libs: ["mylib4"],
697 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698 stl: "none",
699 stubs: {
700 versions: ["10", "11", "12"],
701 },
702 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900703
704 cc_library {
705 name: "mylib4",
706 srcs: ["mylib.cpp"],
707 system_shared_libs: [],
708 stl: "none",
709 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710 `)
711
Sundong Ahnabb64432019-10-22 13:58:29 +0900712 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900713 copyCmds := apexRule.Args["copy_commands"]
714
715 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800716 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900717
718 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800719 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900720
721 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800722 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900723
Jiyong Parkda6eb592018-12-19 17:12:36 +0900724 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900725
726 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900727 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900728 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900729 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900730
731 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900732 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900733 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900734 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900735
736 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900737 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900738 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900739
740 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900741 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742}
743
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900744func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700745 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900746 apex {
747 name: "myapex",
748 key: "myapex.key",
749 native_shared_libs: ["mylib"],
750 }
751
752 apex_key {
753 name: "myapex.key",
754 public_key: "testkey.avbpubkey",
755 private_key: "testkey.pem",
756 }
757
758 cc_library {
759 name: "mylib",
760 srcs: ["mylib.cpp"],
761 shared_libs: ["libfoo#10"],
762 system_shared_libs: [],
763 stl: "none",
764 }
765
766 cc_library {
767 name: "libfoo",
768 srcs: ["mylib.cpp"],
769 shared_libs: ["libbar"],
770 system_shared_libs: [],
771 stl: "none",
772 stubs: {
773 versions: ["10", "20", "30"],
774 },
775 }
776
777 cc_library {
778 name: "libbar",
779 srcs: ["mylib.cpp"],
780 system_shared_libs: [],
781 stl: "none",
782 }
783
784 `)
785
Sundong Ahnabb64432019-10-22 13:58:29 +0900786 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900787 copyCmds := apexRule.Args["copy_commands"]
788
789 // Ensure that direct non-stubs dep is always included
790 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
791
792 // Ensure that indirect stubs dep is not included
793 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
794
795 // Ensure that dependency of stubs is not included
796 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
797
Jiyong Parkda6eb592018-12-19 17:12:36 +0900798 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900799
800 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900801 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900802 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900803 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900804
Jiyong Parkda6eb592018-12-19 17:12:36 +0900805 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900806
807 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
808 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
809}
810
Jooyung Hand3639552019-08-09 12:57:43 +0900811func TestApexWithRuntimeLibsDependency(t *testing.T) {
812 /*
813 myapex
814 |
815 v (runtime_libs)
816 mylib ------+------> libfoo [provides stub]
817 |
818 `------> libbar
819 */
820 ctx, _ := testApex(t, `
821 apex {
822 name: "myapex",
823 key: "myapex.key",
824 native_shared_libs: ["mylib"],
825 }
826
827 apex_key {
828 name: "myapex.key",
829 public_key: "testkey.avbpubkey",
830 private_key: "testkey.pem",
831 }
832
833 cc_library {
834 name: "mylib",
835 srcs: ["mylib.cpp"],
836 runtime_libs: ["libfoo", "libbar"],
837 system_shared_libs: [],
838 stl: "none",
839 }
840
841 cc_library {
842 name: "libfoo",
843 srcs: ["mylib.cpp"],
844 system_shared_libs: [],
845 stl: "none",
846 stubs: {
847 versions: ["10", "20", "30"],
848 },
849 }
850
851 cc_library {
852 name: "libbar",
853 srcs: ["mylib.cpp"],
854 system_shared_libs: [],
855 stl: "none",
856 }
857
858 `)
859
Sundong Ahnabb64432019-10-22 13:58:29 +0900860 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900861 copyCmds := apexRule.Args["copy_commands"]
862
863 // Ensure that direct non-stubs dep is always included
864 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
865
866 // Ensure that indirect stubs dep is not included
867 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
868
869 // Ensure that runtime_libs dep in included
870 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
871
Sundong Ahnabb64432019-10-22 13:58:29 +0900872 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900873 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
874 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900875
876}
877
Jooyung Han9c80bae2019-08-20 17:30:57 +0900878func TestApexDependencyToLLNDK(t *testing.T) {
879 ctx, _ := testApex(t, `
880 apex {
881 name: "myapex",
882 key: "myapex.key",
883 use_vendor: true,
884 native_shared_libs: ["mylib"],
885 }
886
887 apex_key {
888 name: "myapex.key",
889 public_key: "testkey.avbpubkey",
890 private_key: "testkey.pem",
891 }
892
893 cc_library {
894 name: "mylib",
895 srcs: ["mylib.cpp"],
896 vendor_available: true,
897 shared_libs: ["libbar"],
898 system_shared_libs: [],
899 stl: "none",
900 }
901
902 cc_library {
903 name: "libbar",
904 srcs: ["mylib.cpp"],
905 system_shared_libs: [],
906 stl: "none",
907 }
908
909 llndk_library {
910 name: "libbar",
911 symbol_file: "",
912 }
Jooyung Handc782442019-11-01 03:14:38 +0900913 `, func(fs map[string][]byte, config android.Config) {
914 setUseVendorWhitelistForTest(config, []string{"myapex"})
915 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900916
Sundong Ahnabb64432019-10-22 13:58:29 +0900917 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900918 copyCmds := apexRule.Args["copy_commands"]
919
920 // Ensure that LLNDK dep is not included
921 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
922
Sundong Ahnabb64432019-10-22 13:58:29 +0900923 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900924 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900925
926 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900927 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900928
929}
930
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700932 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 apex {
934 name: "myapex",
935 key: "myapex.key",
936 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
937 }
938
939 apex_key {
940 name: "myapex.key",
941 public_key: "testkey.avbpubkey",
942 private_key: "testkey.pem",
943 }
944
945 cc_library {
946 name: "mylib",
947 srcs: ["mylib.cpp"],
948 shared_libs: ["libdl#27"],
949 stl: "none",
950 }
951
952 cc_library_shared {
953 name: "mylib_shared",
954 srcs: ["mylib.cpp"],
955 shared_libs: ["libdl#27"],
956 stl: "none",
957 }
958
959 cc_library {
960 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700961 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962 nocrt: true,
963 system_shared_libs: [],
964 stl: "none",
965 stubs: {
966 versions: ["27", "28", "29"],
967 },
968 }
969
970 cc_library {
971 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700972 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973 nocrt: true,
974 system_shared_libs: [],
975 stl: "none",
976 stubs: {
977 versions: ["27", "28", "29"],
978 },
979 }
980
981 cc_library {
982 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700983 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900984 nocrt: true,
985 system_shared_libs: [],
986 stl: "none",
987 stubs: {
988 versions: ["27", "28", "29"],
989 },
990 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900991
992 cc_library {
993 name: "libBootstrap",
994 srcs: ["mylib.cpp"],
995 stl: "none",
996 bootstrap: true,
997 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998 `)
999
Sundong Ahnabb64432019-10-22 13:58:29 +09001000 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001 copyCmds := apexRule.Args["copy_commands"]
1002
1003 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001004 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001005 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1006 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001007
1008 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001009 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010
Jiyong Parkda6eb592018-12-19 17:12:36 +09001011 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
1012 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
1013 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001014
1015 // For dependency to libc
1016 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +09001017 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001018 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +09001019 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001020 // ... Cflags from stub is correctly exported to mylib
1021 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1022 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1023
1024 // For dependency to libm
1025 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +09001026 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +09001028 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001029 // ... and is not compiling with the stub
1030 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1031 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1032
1033 // For dependency to libdl
1034 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +09001035 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001036 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +09001037 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
1038 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001039 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +09001040 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041 // ... Cflags from stub is correctly exported to mylib
1042 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1043 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001044
1045 // Ensure that libBootstrap is depending on the platform variant of bionic libs
1046 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
1047 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
1048 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
1049 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001050}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001051
1052func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001053 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001054 apex {
1055 name: "myapex",
1056 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001057 native_shared_libs: ["mylib"],
1058 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001059 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001060 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001061 }
1062
1063 apex_key {
1064 name: "myapex.key",
1065 public_key: "testkey.avbpubkey",
1066 private_key: "testkey.pem",
1067 }
1068
1069 prebuilt_etc {
1070 name: "myetc",
1071 src: "myprebuilt",
1072 sub_dir: "foo/bar",
1073 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001074
1075 cc_library {
1076 name: "mylib",
1077 srcs: ["mylib.cpp"],
1078 relative_install_path: "foo/bar",
1079 system_shared_libs: [],
1080 stl: "none",
1081 }
1082
1083 cc_binary {
1084 name: "mybin",
1085 srcs: ["mylib.cpp"],
1086 relative_install_path: "foo/bar",
1087 system_shared_libs: [],
1088 static_executable: true,
1089 stl: "none",
1090 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001091 `)
1092
Sundong Ahnabb64432019-10-22 13:58:29 +09001093 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001094 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1095
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001096 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001097 ensureListContains(t, dirs, "etc")
1098 ensureListContains(t, dirs, "etc/foo")
1099 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001100 ensureListContains(t, dirs, "lib64")
1101 ensureListContains(t, dirs, "lib64/foo")
1102 ensureListContains(t, dirs, "lib64/foo/bar")
1103 ensureListContains(t, dirs, "lib")
1104 ensureListContains(t, dirs, "lib/foo")
1105 ensureListContains(t, dirs, "lib/foo/bar")
1106
Jiyong Parkbd13e442019-03-15 18:10:35 +09001107 ensureListContains(t, dirs, "bin")
1108 ensureListContains(t, dirs, "bin/foo")
1109 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001110}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001111
1112func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001113 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001114 apex {
1115 name: "myapex",
1116 key: "myapex.key",
1117 native_shared_libs: ["mylib"],
1118 use_vendor: true,
1119 }
1120
1121 apex_key {
1122 name: "myapex.key",
1123 public_key: "testkey.avbpubkey",
1124 private_key: "testkey.pem",
1125 }
1126
1127 cc_library {
1128 name: "mylib",
1129 srcs: ["mylib.cpp"],
1130 shared_libs: ["mylib2"],
1131 system_shared_libs: [],
1132 vendor_available: true,
1133 stl: "none",
1134 }
1135
1136 cc_library {
1137 name: "mylib2",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 vendor_available: true,
1141 stl: "none",
1142 }
Jooyung Handc782442019-11-01 03:14:38 +09001143 `, func(fs map[string][]byte, config android.Config) {
1144 setUseVendorWhitelistForTest(config, []string{"myapex"})
1145 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001146
1147 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001148 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001149 for _, implicit := range i.Implicits {
1150 inputsList = append(inputsList, implicit.String())
1151 }
1152 }
1153 inputsString := strings.Join(inputsList, " ")
1154
1155 // ensure that the apex includes vendor variants of the direct and indirect deps
Inseob Kim64c43952019-08-26 16:52:35 +09001156 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib.so")
1157 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001158
1159 // ensure that the apex does not include core variants
1160 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
1161 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
1162}
Jiyong Park16e91a02018-12-20 18:18:08 +09001163
Jooyung Handc782442019-11-01 03:14:38 +09001164func TestUseVendorRestriction(t *testing.T) {
1165 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1166 apex {
1167 name: "myapex",
1168 key: "myapex.key",
1169 use_vendor: true,
1170 }
1171 apex_key {
1172 name: "myapex.key",
1173 public_key: "testkey.avbpubkey",
1174 private_key: "testkey.pem",
1175 }
1176 `, func(fs map[string][]byte, config android.Config) {
1177 setUseVendorWhitelistForTest(config, []string{""})
1178 })
1179 // no error with whitelist
1180 testApex(t, `
1181 apex {
1182 name: "myapex",
1183 key: "myapex.key",
1184 use_vendor: true,
1185 }
1186 apex_key {
1187 name: "myapex.key",
1188 public_key: "testkey.avbpubkey",
1189 private_key: "testkey.pem",
1190 }
1191 `, func(fs map[string][]byte, config android.Config) {
1192 setUseVendorWhitelistForTest(config, []string{"myapex"})
1193 })
1194}
1195
Jooyung Han5c998b92019-06-27 11:30:33 +09001196func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1197 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1198 apex {
1199 name: "myapex",
1200 key: "myapex.key",
1201 native_shared_libs: ["mylib"],
1202 use_vendor: true,
1203 }
1204
1205 apex_key {
1206 name: "myapex.key",
1207 public_key: "testkey.avbpubkey",
1208 private_key: "testkey.pem",
1209 }
1210
1211 cc_library {
1212 name: "mylib",
1213 srcs: ["mylib.cpp"],
1214 system_shared_libs: [],
1215 stl: "none",
1216 }
1217 `)
1218}
1219
Jiyong Park16e91a02018-12-20 18:18:08 +09001220func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001221 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001222 apex {
1223 name: "myapex",
1224 key: "myapex.key",
1225 native_shared_libs: ["mylib"],
1226 }
1227
1228 apex_key {
1229 name: "myapex.key",
1230 public_key: "testkey.avbpubkey",
1231 private_key: "testkey.pem",
1232 }
1233
1234 cc_library {
1235 name: "mylib",
1236 srcs: ["mylib.cpp"],
1237 system_shared_libs: [],
1238 stl: "none",
1239 stubs: {
1240 versions: ["1", "2", "3"],
1241 },
1242 }
1243
1244 cc_binary {
1245 name: "not_in_apex",
1246 srcs: ["mylib.cpp"],
1247 static_libs: ["mylib"],
1248 static_executable: true,
1249 system_shared_libs: [],
1250 stl: "none",
1251 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001252 `)
1253
1254 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
1255
1256 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +08001257 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001258}
Jiyong Park9335a262018-12-24 11:31:58 +09001259
1260func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001261 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001262 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001263 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001264 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001265 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001266 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001267 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001268 }
1269
1270 cc_library {
1271 name: "mylib",
1272 srcs: ["mylib.cpp"],
1273 system_shared_libs: [],
1274 stl: "none",
1275 }
1276
1277 apex_key {
1278 name: "myapex.key",
1279 public_key: "testkey.avbpubkey",
1280 private_key: "testkey.pem",
1281 }
1282
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001283 android_app_certificate {
1284 name: "myapex.certificate",
1285 certificate: "testkey",
1286 }
1287
1288 android_app_certificate {
1289 name: "myapex.certificate.override",
1290 certificate: "testkey.override",
1291 }
1292
Jiyong Park9335a262018-12-24 11:31:58 +09001293 `)
1294
1295 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001296 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001297
1298 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1299 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1300 "vendor/foo/devkeys/testkey.avbpubkey")
1301 }
1302 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1303 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1304 "vendor/foo/devkeys/testkey.pem")
1305 }
1306
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001307 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001308 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001309 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001310 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001311 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001312 }
1313}
Jiyong Park58e364a2019-01-19 19:24:06 +09001314
1315func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001316 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001317 apex {
1318 name: "myapex",
1319 key: "myapex.key",
1320 native_shared_libs: ["mylib"],
1321 }
1322
1323 apex {
1324 name: "otherapex",
1325 key: "myapex.key",
1326 native_shared_libs: ["mylib"],
1327 }
1328
1329 apex_key {
1330 name: "myapex.key",
1331 public_key: "testkey.avbpubkey",
1332 private_key: "testkey.pem",
1333 }
1334
1335 cc_library {
1336 name: "mylib",
1337 srcs: ["mylib.cpp"],
1338 system_shared_libs: [],
1339 stl: "none",
1340 }
1341 `)
1342
Jooyung Han6b8459b2019-10-30 08:29:25 +09001343 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Jiyong Park58e364a2019-01-19 19:24:06 +09001344 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001345 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001346 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1347 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001348
Jooyung Han6b8459b2019-10-30 08:29:25 +09001349 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Jiyong Park58e364a2019-01-19 19:24:06 +09001350 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001351 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001352 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1353 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001354
Jooyung Han6b8459b2019-10-30 08:29:25 +09001355 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Jiyong Park58e364a2019-01-19 19:24:06 +09001356 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001357 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001358 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1359 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001360}
Jiyong Park7e636d02019-01-28 16:16:54 +09001361
1362func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001363 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001364 apex {
1365 name: "myapex",
1366 key: "myapex.key",
1367 native_shared_libs: ["mylib"],
1368 }
1369
1370 apex_key {
1371 name: "myapex.key",
1372 public_key: "testkey.avbpubkey",
1373 private_key: "testkey.pem",
1374 }
1375
1376 cc_library_headers {
1377 name: "mylib_headers",
1378 export_include_dirs: ["my_include"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 }
1382
1383 cc_library {
1384 name: "mylib",
1385 srcs: ["mylib.cpp"],
1386 system_shared_libs: [],
1387 stl: "none",
1388 header_libs: ["mylib_headers"],
1389 export_header_lib_headers: ["mylib_headers"],
1390 stubs: {
1391 versions: ["1", "2", "3"],
1392 },
1393 }
1394
1395 cc_library {
1396 name: "otherlib",
1397 srcs: ["mylib.cpp"],
1398 system_shared_libs: [],
1399 stl: "none",
1400 shared_libs: ["mylib"],
1401 }
1402 `)
1403
1404 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1405
1406 // Ensure that the include path of the header lib is exported to 'otherlib'
1407 ensureContains(t, cFlags, "-Imy_include")
1408}
Alex Light9670d332019-01-29 18:07:33 -08001409
Jooyung Han31c470b2019-10-18 16:26:59 +09001410func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1411 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001412 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001413 copyCmds := apexRule.Args["copy_commands"]
1414 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001415 var failed bool
1416 var surplus []string
1417 filesMatched := make(map[string]bool)
1418 addContent := func(content string) {
1419 for _, expected := range files {
1420 if matched, _ := path.Match(expected, content); matched {
1421 filesMatched[expected] = true
1422 return
1423 }
1424 }
1425 surplus = append(surplus, content)
1426 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001427 for _, cmd := range strings.Split(copyCmds, "&&") {
1428 cmd = strings.TrimSpace(cmd)
1429 if cmd == "" {
1430 continue
1431 }
1432 terms := strings.Split(cmd, " ")
1433 switch terms[0] {
1434 case "mkdir":
1435 case "cp":
1436 if len(terms) != 3 {
1437 t.Fatal("copyCmds contains invalid cp command", cmd)
1438 }
1439 dst := terms[2]
1440 index := strings.Index(dst, imageApexDir)
1441 if index == -1 {
1442 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1443 }
1444 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001445 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001446 default:
1447 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1448 }
1449 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001450
Jooyung Han31c470b2019-10-18 16:26:59 +09001451 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001452 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001453 t.Log("surplus files", surplus)
1454 failed = true
1455 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001456
1457 if len(files) > len(filesMatched) {
1458 var missing []string
1459 for _, expected := range files {
1460 if !filesMatched[expected] {
1461 missing = append(missing, expected)
1462 }
1463 }
1464 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001465 t.Log("missing files", missing)
1466 failed = true
1467 }
1468 if failed {
1469 t.Fail()
1470 }
1471}
1472
Jooyung Han344d5432019-08-23 11:17:39 +09001473func TestVndkApexCurrent(t *testing.T) {
1474 ctx, _ := testApex(t, `
1475 apex_vndk {
1476 name: "myapex",
1477 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001478 }
1479
1480 apex_key {
1481 name: "myapex.key",
1482 public_key: "testkey.avbpubkey",
1483 private_key: "testkey.pem",
1484 }
1485
1486 cc_library {
1487 name: "libvndk",
1488 srcs: ["mylib.cpp"],
1489 vendor_available: true,
1490 vndk: {
1491 enabled: true,
1492 },
1493 system_shared_libs: [],
1494 stl: "none",
1495 }
1496
1497 cc_library {
1498 name: "libvndksp",
1499 srcs: ["mylib.cpp"],
1500 vendor_available: true,
1501 vndk: {
1502 enabled: true,
1503 support_system_process: true,
1504 },
1505 system_shared_libs: [],
1506 stl: "none",
1507 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001508 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001509
Jooyung Han31c470b2019-10-18 16:26:59 +09001510 ensureExactContents(t, ctx, "myapex", []string{
1511 "lib/libvndk.so",
1512 "lib/libvndksp.so",
1513 "lib64/libvndk.so",
1514 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001515 "etc/llndk.libraries.VER.txt",
1516 "etc/vndkcore.libraries.VER.txt",
1517 "etc/vndksp.libraries.VER.txt",
1518 "etc/vndkprivate.libraries.VER.txt",
1519 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001520 })
Jooyung Han344d5432019-08-23 11:17:39 +09001521}
1522
1523func TestVndkApexWithPrebuilt(t *testing.T) {
1524 ctx, _ := testApex(t, `
1525 apex_vndk {
1526 name: "myapex",
1527 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001528 }
1529
1530 apex_key {
1531 name: "myapex.key",
1532 public_key: "testkey.avbpubkey",
1533 private_key: "testkey.pem",
1534 }
1535
1536 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001537 name: "libvndk",
1538 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001539 vendor_available: true,
1540 vndk: {
1541 enabled: true,
1542 },
1543 system_shared_libs: [],
1544 stl: "none",
1545 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001546
1547 cc_prebuilt_library_shared {
1548 name: "libvndk.arm",
1549 srcs: ["libvndk.arm.so"],
1550 vendor_available: true,
1551 vndk: {
1552 enabled: true,
1553 },
1554 enabled: false,
1555 arch: {
1556 arm: {
1557 enabled: true,
1558 },
1559 },
1560 system_shared_libs: [],
1561 stl: "none",
1562 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001563 `+vndkLibrariesTxtFiles("current"),
1564 withFiles(map[string][]byte{
1565 "libvndk.so": nil,
1566 "libvndk.arm.so": nil,
1567 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001568
Jooyung Han31c470b2019-10-18 16:26:59 +09001569 ensureExactContents(t, ctx, "myapex", []string{
1570 "lib/libvndk.so",
1571 "lib/libvndk.arm.so",
1572 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001573 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001574 })
Jooyung Han344d5432019-08-23 11:17:39 +09001575}
1576
Jooyung Han39edb6c2019-11-06 16:53:07 +09001577func vndkLibrariesTxtFiles(vers ...string) (result string) {
1578 for _, v := range vers {
1579 if v == "current" {
1580 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1581 result += `
1582 vndk_libraries_txt {
1583 name: "` + txt + `.libraries.txt",
1584 }
1585 `
1586 }
1587 } else {
1588 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1589 result += `
1590 prebuilt_etc {
1591 name: "` + txt + `.libraries.` + v + `.txt",
1592 src: "dummy.txt",
1593 }
1594 `
1595 }
1596 }
1597 }
1598 return
1599}
1600
Jooyung Han344d5432019-08-23 11:17:39 +09001601func TestVndkApexVersion(t *testing.T) {
1602 ctx, _ := testApex(t, `
1603 apex_vndk {
1604 name: "myapex_v27",
1605 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001606 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001607 vndk_version: "27",
1608 }
1609
1610 apex_key {
1611 name: "myapex.key",
1612 public_key: "testkey.avbpubkey",
1613 private_key: "testkey.pem",
1614 }
1615
Jooyung Han31c470b2019-10-18 16:26:59 +09001616 vndk_prebuilt_shared {
1617 name: "libvndk27",
1618 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001619 vendor_available: true,
1620 vndk: {
1621 enabled: true,
1622 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001623 target_arch: "arm64",
1624 arch: {
1625 arm: {
1626 srcs: ["libvndk27_arm.so"],
1627 },
1628 arm64: {
1629 srcs: ["libvndk27_arm64.so"],
1630 },
1631 },
Jooyung Han344d5432019-08-23 11:17:39 +09001632 }
1633
1634 vndk_prebuilt_shared {
1635 name: "libvndk27",
1636 version: "27",
1637 vendor_available: true,
1638 vndk: {
1639 enabled: true,
1640 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001641 target_arch: "x86_64",
1642 arch: {
1643 x86: {
1644 srcs: ["libvndk27_x86.so"],
1645 },
1646 x86_64: {
1647 srcs: ["libvndk27_x86_64.so"],
1648 },
1649 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001650 }
1651 `+vndkLibrariesTxtFiles("27"),
1652 withFiles(map[string][]byte{
1653 "libvndk27_arm.so": nil,
1654 "libvndk27_arm64.so": nil,
1655 "libvndk27_x86.so": nil,
1656 "libvndk27_x86_64.so": nil,
1657 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001658
Jooyung Han31c470b2019-10-18 16:26:59 +09001659 ensureExactContents(t, ctx, "myapex_v27", []string{
1660 "lib/libvndk27_arm.so",
1661 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001662 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001663 })
Jooyung Han344d5432019-08-23 11:17:39 +09001664}
1665
1666func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1667 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1668 apex_vndk {
1669 name: "myapex_v27",
1670 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001671 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001672 vndk_version: "27",
1673 }
1674 apex_vndk {
1675 name: "myapex_v27_other",
1676 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001677 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001678 vndk_version: "27",
1679 }
1680
1681 apex_key {
1682 name: "myapex.key",
1683 public_key: "testkey.avbpubkey",
1684 private_key: "testkey.pem",
1685 }
1686
1687 cc_library {
1688 name: "libvndk",
1689 srcs: ["mylib.cpp"],
1690 vendor_available: true,
1691 vndk: {
1692 enabled: true,
1693 },
1694 system_shared_libs: [],
1695 stl: "none",
1696 }
1697
1698 vndk_prebuilt_shared {
1699 name: "libvndk",
1700 version: "27",
1701 vendor_available: true,
1702 vndk: {
1703 enabled: true,
1704 },
1705 srcs: ["libvndk.so"],
1706 }
1707 `, withFiles(map[string][]byte{
1708 "libvndk.so": nil,
1709 }))
1710}
1711
Jooyung Han90eee022019-10-01 20:02:42 +09001712func TestVndkApexNameRule(t *testing.T) {
1713 ctx, _ := testApex(t, `
1714 apex_vndk {
1715 name: "myapex",
1716 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001717 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001718 }
1719 apex_vndk {
1720 name: "myapex_v28",
1721 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001722 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001723 vndk_version: "28",
1724 }
1725 apex_key {
1726 name: "myapex.key",
1727 public_key: "testkey.avbpubkey",
1728 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001729 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001730
1731 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001732 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001733 actual := proptools.String(bundle.properties.Apex_name)
1734 if !reflect.DeepEqual(actual, expected) {
1735 t.Errorf("Got '%v', expected '%v'", actual, expected)
1736 }
1737 }
1738
1739 assertApexName("com.android.vndk.vVER", "myapex")
1740 assertApexName("com.android.vndk.v28", "myapex_v28")
1741}
1742
Jooyung Han344d5432019-08-23 11:17:39 +09001743func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1744 ctx, _ := testApex(t, `
1745 apex_vndk {
1746 name: "myapex",
1747 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001748 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001749 }
1750
1751 apex_key {
1752 name: "myapex.key",
1753 public_key: "testkey.avbpubkey",
1754 private_key: "testkey.pem",
1755 }
1756
1757 cc_library {
1758 name: "libvndk",
1759 srcs: ["mylib.cpp"],
1760 vendor_available: true,
1761 native_bridge_supported: true,
1762 host_supported: true,
1763 vndk: {
1764 enabled: true,
1765 },
1766 system_shared_libs: [],
1767 stl: "none",
1768 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001769 `+vndkLibrariesTxtFiles("current"),
1770 withTargets(map[android.OsType][]android.Target{
1771 android.Android: []android.Target{
1772 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1773 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1774 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1775 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1776 },
1777 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001778
Jooyung Han31c470b2019-10-18 16:26:59 +09001779 ensureExactContents(t, ctx, "myapex", []string{
1780 "lib/libvndk.so",
1781 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001782 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001783 })
Jooyung Han344d5432019-08-23 11:17:39 +09001784}
1785
1786func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1787 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1788 apex_vndk {
1789 name: "myapex",
1790 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001791 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001792 native_bridge_supported: true,
1793 }
1794
1795 apex_key {
1796 name: "myapex.key",
1797 public_key: "testkey.avbpubkey",
1798 private_key: "testkey.pem",
1799 }
1800
1801 cc_library {
1802 name: "libvndk",
1803 srcs: ["mylib.cpp"],
1804 vendor_available: true,
1805 native_bridge_supported: true,
1806 host_supported: true,
1807 vndk: {
1808 enabled: true,
1809 },
1810 system_shared_libs: [],
1811 stl: "none",
1812 }
1813 `)
1814}
1815
Jooyung Han31c470b2019-10-18 16:26:59 +09001816func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001817 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001818 apex_vndk {
1819 name: "myapex_v27",
1820 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001821 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001822 vndk_version: "27",
1823 }
1824
1825 apex_key {
1826 name: "myapex.key",
1827 public_key: "testkey.avbpubkey",
1828 private_key: "testkey.pem",
1829 }
1830
1831 vndk_prebuilt_shared {
1832 name: "libvndk27",
1833 version: "27",
1834 target_arch: "arm",
1835 vendor_available: true,
1836 vndk: {
1837 enabled: true,
1838 },
1839 arch: {
1840 arm: {
1841 srcs: ["libvndk27.so"],
1842 }
1843 },
1844 }
1845
1846 vndk_prebuilt_shared {
1847 name: "libvndk27",
1848 version: "27",
1849 target_arch: "arm",
1850 binder32bit: true,
1851 vendor_available: true,
1852 vndk: {
1853 enabled: true,
1854 },
1855 arch: {
1856 arm: {
1857 srcs: ["libvndk27binder32.so"],
1858 }
1859 },
1860 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001861 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001862 withFiles(map[string][]byte{
1863 "libvndk27.so": nil,
1864 "libvndk27binder32.so": nil,
1865 }),
1866 withBinder32bit,
1867 withTargets(map[android.OsType][]android.Target{
1868 android.Android: []android.Target{
1869 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1870 },
1871 }),
1872 )
1873
1874 ensureExactContents(t, ctx, "myapex_v27", []string{
1875 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001876 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001877 })
1878}
1879
Jooyung Hane1633032019-08-01 17:41:43 +09001880func TestDependenciesInApexManifest(t *testing.T) {
1881 ctx, _ := testApex(t, `
1882 apex {
1883 name: "myapex_nodep",
1884 key: "myapex.key",
1885 native_shared_libs: ["lib_nodep"],
1886 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001887 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001888 }
1889
1890 apex {
1891 name: "myapex_dep",
1892 key: "myapex.key",
1893 native_shared_libs: ["lib_dep"],
1894 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001895 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001896 }
1897
1898 apex {
1899 name: "myapex_provider",
1900 key: "myapex.key",
1901 native_shared_libs: ["libfoo"],
1902 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001903 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001904 }
1905
1906 apex {
1907 name: "myapex_selfcontained",
1908 key: "myapex.key",
1909 native_shared_libs: ["lib_dep", "libfoo"],
1910 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001911 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001912 }
1913
1914 apex_key {
1915 name: "myapex.key",
1916 public_key: "testkey.avbpubkey",
1917 private_key: "testkey.pem",
1918 }
1919
1920 cc_library {
1921 name: "lib_nodep",
1922 srcs: ["mylib.cpp"],
1923 system_shared_libs: [],
1924 stl: "none",
1925 }
1926
1927 cc_library {
1928 name: "lib_dep",
1929 srcs: ["mylib.cpp"],
1930 shared_libs: ["libfoo"],
1931 system_shared_libs: [],
1932 stl: "none",
1933 }
1934
1935 cc_library {
1936 name: "libfoo",
1937 srcs: ["mytest.cpp"],
1938 stubs: {
1939 versions: ["1"],
1940 },
1941 system_shared_libs: [],
1942 stl: "none",
1943 }
1944 `)
1945
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001946 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09001947 var provideNativeLibs, requireNativeLibs []string
1948
Sundong Ahnabb64432019-10-22 13:58:29 +09001949 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001950 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1951 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001952 ensureListEmpty(t, provideNativeLibs)
1953 ensureListEmpty(t, requireNativeLibs)
1954
Sundong Ahnabb64432019-10-22 13:58:29 +09001955 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001956 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1957 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001958 ensureListEmpty(t, provideNativeLibs)
1959 ensureListContains(t, requireNativeLibs, "libfoo.so")
1960
Sundong Ahnabb64432019-10-22 13:58:29 +09001961 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001962 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1963 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001964 ensureListContains(t, provideNativeLibs, "libfoo.so")
1965 ensureListEmpty(t, requireNativeLibs)
1966
Sundong Ahnabb64432019-10-22 13:58:29 +09001967 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001968 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1969 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001970 ensureListContains(t, provideNativeLibs, "libfoo.so")
1971 ensureListEmpty(t, requireNativeLibs)
1972}
1973
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001974func TestApexName(t *testing.T) {
1975 ctx, _ := testApex(t, `
1976 apex {
1977 name: "myapex",
1978 key: "myapex.key",
1979 apex_name: "com.android.myapex",
1980 }
1981
1982 apex_key {
1983 name: "myapex.key",
1984 public_key: "testkey.avbpubkey",
1985 private_key: "testkey.pem",
1986 }
1987 `)
1988
Sundong Ahnabb64432019-10-22 13:58:29 +09001989 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001990 apexManifestRule := module.Rule("apexManifestRule")
1991 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
1992 apexRule := module.Rule("apexRule")
1993 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
1994}
1995
Alex Light0851b882019-02-07 13:20:53 -08001996func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001997 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001998 apex {
1999 name: "myapex",
2000 key: "myapex.key",
2001 native_shared_libs: ["mylib_common"],
2002 }
2003
2004 apex_key {
2005 name: "myapex.key",
2006 public_key: "testkey.avbpubkey",
2007 private_key: "testkey.pem",
2008 }
2009
2010 cc_library {
2011 name: "mylib_common",
2012 srcs: ["mylib.cpp"],
2013 system_shared_libs: [],
2014 stl: "none",
2015 }
2016 `)
2017
Sundong Ahnabb64432019-10-22 13:58:29 +09002018 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002019 apexRule := module.Rule("apexRule")
2020 copyCmds := apexRule.Args["copy_commands"]
2021
2022 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2023 t.Log("Apex was a test apex!")
2024 t.Fail()
2025 }
2026 // Ensure that main rule creates an output
2027 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2028
2029 // Ensure that apex variant is created for the direct dep
2030 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
2031
2032 // Ensure that both direct and indirect deps are copied into apex
2033 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2034
2035 // Ensure that the platform variant ends with _core_shared
2036 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
2037
2038 if !android.InAnyApex("mylib_common") {
2039 t.Log("Found mylib_common not in any apex!")
2040 t.Fail()
2041 }
2042}
2043
2044func TestTestApex(t *testing.T) {
2045 if android.InAnyApex("mylib_common_test") {
2046 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!")
2047 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002048 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002049 apex_test {
2050 name: "myapex",
2051 key: "myapex.key",
2052 native_shared_libs: ["mylib_common_test"],
2053 }
2054
2055 apex_key {
2056 name: "myapex.key",
2057 public_key: "testkey.avbpubkey",
2058 private_key: "testkey.pem",
2059 }
2060
2061 cc_library {
2062 name: "mylib_common_test",
2063 srcs: ["mylib.cpp"],
2064 system_shared_libs: [],
2065 stl: "none",
2066 }
2067 `)
2068
Sundong Ahnabb64432019-10-22 13:58:29 +09002069 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002070 apexRule := module.Rule("apexRule")
2071 copyCmds := apexRule.Args["copy_commands"]
2072
2073 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2074 t.Log("Apex was not a test apex!")
2075 t.Fail()
2076 }
2077 // Ensure that main rule creates an output
2078 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2079
2080 // Ensure that apex variant is created for the direct dep
2081 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
2082
2083 // Ensure that both direct and indirect deps are copied into apex
2084 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2085
2086 // Ensure that the platform variant ends with _core_shared
2087 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
2088
2089 if android.InAnyApex("mylib_common_test") {
2090 t.Log("Found mylib_common_test in some apex!")
2091 t.Fail()
2092 }
2093}
2094
Alex Light9670d332019-01-29 18:07:33 -08002095func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002096 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002097 apex {
2098 name: "myapex",
2099 key: "myapex.key",
2100 multilib: {
2101 first: {
2102 native_shared_libs: ["mylib_common"],
2103 }
2104 },
2105 target: {
2106 android: {
2107 multilib: {
2108 first: {
2109 native_shared_libs: ["mylib"],
2110 }
2111 }
2112 },
2113 host: {
2114 multilib: {
2115 first: {
2116 native_shared_libs: ["mylib2"],
2117 }
2118 }
2119 }
2120 }
2121 }
2122
2123 apex_key {
2124 name: "myapex.key",
2125 public_key: "testkey.avbpubkey",
2126 private_key: "testkey.pem",
2127 }
2128
2129 cc_library {
2130 name: "mylib",
2131 srcs: ["mylib.cpp"],
2132 system_shared_libs: [],
2133 stl: "none",
2134 }
2135
2136 cc_library {
2137 name: "mylib_common",
2138 srcs: ["mylib.cpp"],
2139 system_shared_libs: [],
2140 stl: "none",
2141 compile_multilib: "first",
2142 }
2143
2144 cc_library {
2145 name: "mylib2",
2146 srcs: ["mylib.cpp"],
2147 system_shared_libs: [],
2148 stl: "none",
2149 compile_multilib: "first",
2150 }
2151 `)
2152
Sundong Ahnabb64432019-10-22 13:58:29 +09002153 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002154 copyCmds := apexRule.Args["copy_commands"]
2155
2156 // Ensure that main rule creates an output
2157 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2158
2159 // Ensure that apex variant is created for the direct dep
2160 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2161 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
2162 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
2163
2164 // Ensure that both direct and indirect deps are copied into apex
2165 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2166 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2167 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2168
2169 // Ensure that the platform variant ends with _core_shared
2170 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
2171 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
2172 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
2173}
Jiyong Park04480cf2019-02-06 00:16:29 +09002174
2175func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002176 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002177 apex {
2178 name: "myapex",
2179 key: "myapex.key",
2180 binaries: ["myscript"],
2181 }
2182
2183 apex_key {
2184 name: "myapex.key",
2185 public_key: "testkey.avbpubkey",
2186 private_key: "testkey.pem",
2187 }
2188
2189 sh_binary {
2190 name: "myscript",
2191 src: "mylib.cpp",
2192 filename: "myscript.sh",
2193 sub_dir: "script",
2194 }
2195 `)
2196
Sundong Ahnabb64432019-10-22 13:58:29 +09002197 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002198 copyCmds := apexRule.Args["copy_commands"]
2199
2200 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2201}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002202
Jooyung Han91df2082019-11-20 01:49:42 +09002203func TestApexInVariousPartition(t *testing.T) {
2204 testcases := []struct {
2205 propName, parition, flattenedPartition string
2206 }{
2207 {"", "system", "system_ext"},
2208 {"product_specific: true", "product", "product"},
2209 {"soc_specific: true", "vendor", "vendor"},
2210 {"proprietary: true", "vendor", "vendor"},
2211 {"vendor: true", "vendor", "vendor"},
2212 {"system_ext_specific: true", "system_ext", "system_ext"},
2213 }
2214 for _, tc := range testcases {
2215 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2216 ctx, _ := testApex(t, `
2217 apex {
2218 name: "myapex",
2219 key: "myapex.key",
2220 `+tc.propName+`
2221 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002222
Jooyung Han91df2082019-11-20 01:49:42 +09002223 apex_key {
2224 name: "myapex.key",
2225 public_key: "testkey.avbpubkey",
2226 private_key: "testkey.pem",
2227 }
2228 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002229
Jooyung Han91df2082019-11-20 01:49:42 +09002230 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2231 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2232 actual := apex.installDir.String()
2233 if actual != expected {
2234 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2235 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002236
Jooyung Han91df2082019-11-20 01:49:42 +09002237 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2238 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2239 actual = flattened.installDir.String()
2240 if actual != expected {
2241 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2242 }
2243 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002244 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002245}
Jiyong Park67882562019-03-21 01:11:21 +09002246
Jooyung Han54aca7b2019-11-20 02:26:02 +09002247func TestFileContexts(t *testing.T) {
2248 ctx, _ := testApex(t, `
2249 apex {
2250 name: "myapex",
2251 key: "myapex.key",
2252 }
2253
2254 apex_key {
2255 name: "myapex.key",
2256 public_key: "testkey.avbpubkey",
2257 private_key: "testkey.pem",
2258 }
2259 `)
2260 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2261 apexRule := module.Rule("apexRule")
2262 actual := apexRule.Args["file_contexts"]
2263 expected := "system/sepolicy/apex/myapex-file_contexts"
2264 if actual != expected {
2265 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2266 }
2267
2268 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2269 apex {
2270 name: "myapex",
2271 key: "myapex.key",
2272 file_contexts: "my_own_file_contexts",
2273 }
2274
2275 apex_key {
2276 name: "myapex.key",
2277 public_key: "testkey.avbpubkey",
2278 private_key: "testkey.pem",
2279 }
2280 `, withFiles(map[string][]byte{
2281 "my_own_file_contexts": nil,
2282 }))
2283
2284 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2285 apex {
2286 name: "myapex",
2287 key: "myapex.key",
2288 product_specific: true,
2289 file_contexts: "product_specific_file_contexts",
2290 }
2291
2292 apex_key {
2293 name: "myapex.key",
2294 public_key: "testkey.avbpubkey",
2295 private_key: "testkey.pem",
2296 }
2297 `)
2298
2299 ctx, _ = testApex(t, `
2300 apex {
2301 name: "myapex",
2302 key: "myapex.key",
2303 product_specific: true,
2304 file_contexts: "product_specific_file_contexts",
2305 }
2306
2307 apex_key {
2308 name: "myapex.key",
2309 public_key: "testkey.avbpubkey",
2310 private_key: "testkey.pem",
2311 }
2312 `, withFiles(map[string][]byte{
2313 "product_specific_file_contexts": nil,
2314 }))
2315 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2316 apexRule = module.Rule("apexRule")
2317 actual = apexRule.Args["file_contexts"]
2318 expected = "product_specific_file_contexts"
2319 if actual != expected {
2320 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2321 }
2322
2323 ctx, _ = testApex(t, `
2324 apex {
2325 name: "myapex",
2326 key: "myapex.key",
2327 product_specific: true,
2328 file_contexts: ":my-file-contexts",
2329 }
2330
2331 apex_key {
2332 name: "myapex.key",
2333 public_key: "testkey.avbpubkey",
2334 private_key: "testkey.pem",
2335 }
2336
2337 filegroup {
2338 name: "my-file-contexts",
2339 srcs: ["product_specific_file_contexts"],
2340 }
2341 `, withFiles(map[string][]byte{
2342 "product_specific_file_contexts": nil,
2343 }))
2344 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2345 apexRule = module.Rule("apexRule")
2346 actual = apexRule.Args["file_contexts"]
2347 expected = "product_specific_file_contexts"
2348 if actual != expected {
2349 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2350 }
2351}
2352
Jiyong Park67882562019-03-21 01:11:21 +09002353func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002354 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002355 apex_key {
2356 name: "myapex.key",
2357 public_key: ":my.avbpubkey",
2358 private_key: ":my.pem",
2359 product_specific: true,
2360 }
2361
2362 filegroup {
2363 name: "my.avbpubkey",
2364 srcs: ["testkey2.avbpubkey"],
2365 }
2366
2367 filegroup {
2368 name: "my.pem",
2369 srcs: ["testkey2.pem"],
2370 }
2371 `)
2372
2373 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2374 expected_pubkey := "testkey2.avbpubkey"
2375 actual_pubkey := apex_key.public_key_file.String()
2376 if actual_pubkey != expected_pubkey {
2377 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2378 }
2379 expected_privkey := "testkey2.pem"
2380 actual_privkey := apex_key.private_key_file.String()
2381 if actual_privkey != expected_privkey {
2382 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2383 }
2384}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002385
2386func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002387 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002388 prebuilt_apex {
2389 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002390 arch: {
2391 arm64: {
2392 src: "myapex-arm64.apex",
2393 },
2394 arm: {
2395 src: "myapex-arm.apex",
2396 },
2397 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002398 }
2399 `)
2400
2401 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2402
Jiyong Parkc95714e2019-03-29 14:23:10 +09002403 expectedInput := "myapex-arm64.apex"
2404 if prebuilt.inputApex.String() != expectedInput {
2405 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2406 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002407}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002408
2409func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002410 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002411 prebuilt_apex {
2412 name: "myapex",
2413 src: "myapex-arm.apex",
2414 filename: "notmyapex.apex",
2415 }
2416 `)
2417
2418 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2419
2420 expected := "notmyapex.apex"
2421 if p.installFilename != expected {
2422 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2423 }
2424}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002425
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002426func TestPrebuiltOverrides(t *testing.T) {
2427 ctx, config := testApex(t, `
2428 prebuilt_apex {
2429 name: "myapex.prebuilt",
2430 src: "myapex-arm.apex",
2431 overrides: [
2432 "myapex",
2433 ],
2434 }
2435 `)
2436
2437 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2438
2439 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002440 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002441 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002442 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002443 }
2444}
2445
Roland Levillain630846d2019-06-26 12:48:34 +01002446func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002447 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002448 apex_test {
2449 name: "myapex",
2450 key: "myapex.key",
2451 tests: [
2452 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002453 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002454 ],
2455 }
2456
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462
2463 cc_test {
2464 name: "mytest",
2465 gtest: false,
2466 srcs: ["mytest.cpp"],
2467 relative_install_path: "test",
2468 system_shared_libs: [],
2469 static_executable: true,
2470 stl: "none",
2471 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002472
2473 cc_test {
2474 name: "mytests",
2475 gtest: false,
2476 srcs: [
2477 "mytest1.cpp",
2478 "mytest2.cpp",
2479 "mytest3.cpp",
2480 ],
2481 test_per_src: true,
2482 relative_install_path: "test",
2483 system_shared_libs: [],
2484 static_executable: true,
2485 stl: "none",
2486 }
Roland Levillain630846d2019-06-26 12:48:34 +01002487 `)
2488
Sundong Ahnabb64432019-10-22 13:58:29 +09002489 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002490 copyCmds := apexRule.Args["copy_commands"]
2491
2492 // Ensure that test dep is copied into apex.
2493 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002494
2495 // Ensure that test deps built with `test_per_src` are copied into apex.
2496 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2497 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2498 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002499
2500 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002501 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002502 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2503 name := apexBundle.BaseModuleName()
2504 prefix := "TARGET_"
2505 var builder strings.Builder
2506 data.Custom(&builder, name, prefix, "", data)
2507 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002508 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2509 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2510 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2511 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002512 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002513 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002514 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002515}
2516
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002517func TestInstallExtraFlattenedApexes(t *testing.T) {
2518 ctx, config := testApex(t, `
2519 apex {
2520 name: "myapex",
2521 key: "myapex.key",
2522 }
2523 apex_key {
2524 name: "myapex.key",
2525 public_key: "testkey.avbpubkey",
2526 private_key: "testkey.pem",
2527 }
2528 `, func(fs map[string][]byte, config android.Config) {
2529 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2530 })
2531 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2532 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2533 mk := android.AndroidMkDataForTest(t, config, "", ab)
2534 var builder strings.Builder
2535 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2536 androidMk := builder.String()
2537 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2538}
2539
Jooyung Han5c998b92019-06-27 11:30:33 +09002540func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002541 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002542 apex {
2543 name: "myapex",
2544 key: "myapex.key",
2545 native_shared_libs: ["mylib"],
2546 uses: ["commonapex"],
2547 }
2548
2549 apex {
2550 name: "commonapex",
2551 key: "myapex.key",
2552 native_shared_libs: ["libcommon"],
2553 provide_cpp_shared_libs: true,
2554 }
2555
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561
2562 cc_library {
2563 name: "mylib",
2564 srcs: ["mylib.cpp"],
2565 shared_libs: ["libcommon"],
2566 system_shared_libs: [],
2567 stl: "none",
2568 }
2569
2570 cc_library {
2571 name: "libcommon",
2572 srcs: ["mylib_common.cpp"],
2573 system_shared_libs: [],
2574 stl: "none",
2575 }
2576 `)
2577
Sundong Ahnabb64432019-10-22 13:58:29 +09002578 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002579 apexRule1 := module1.Rule("apexRule")
2580 copyCmds1 := apexRule1.Args["copy_commands"]
2581
Sundong Ahnabb64432019-10-22 13:58:29 +09002582 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002583 apexRule2 := module2.Rule("apexRule")
2584 copyCmds2 := apexRule2.Args["copy_commands"]
2585
2586 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2587 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
2588 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2589 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2590 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2591}
2592
2593func TestApexUsesFailsIfNotProvided(t *testing.T) {
2594 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2595 apex {
2596 name: "myapex",
2597 key: "myapex.key",
2598 uses: ["commonapex"],
2599 }
2600
2601 apex {
2602 name: "commonapex",
2603 key: "myapex.key",
2604 }
2605
2606 apex_key {
2607 name: "myapex.key",
2608 public_key: "testkey.avbpubkey",
2609 private_key: "testkey.pem",
2610 }
2611 `)
2612 testApexError(t, `uses: "commonapex" is not a provider`, `
2613 apex {
2614 name: "myapex",
2615 key: "myapex.key",
2616 uses: ["commonapex"],
2617 }
2618
2619 cc_library {
2620 name: "commonapex",
2621 system_shared_libs: [],
2622 stl: "none",
2623 }
2624
2625 apex_key {
2626 name: "myapex.key",
2627 public_key: "testkey.avbpubkey",
2628 private_key: "testkey.pem",
2629 }
2630 `)
2631}
2632
2633func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2634 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2635 apex {
2636 name: "myapex",
2637 key: "myapex.key",
2638 use_vendor: true,
2639 uses: ["commonapex"],
2640 }
2641
2642 apex {
2643 name: "commonapex",
2644 key: "myapex.key",
2645 provide_cpp_shared_libs: true,
2646 }
2647
2648 apex_key {
2649 name: "myapex.key",
2650 public_key: "testkey.avbpubkey",
2651 private_key: "testkey.pem",
2652 }
Jooyung Handc782442019-11-01 03:14:38 +09002653 `, func(fs map[string][]byte, config android.Config) {
2654 setUseVendorWhitelistForTest(config, []string{"myapex"})
2655 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002656}
2657
Jooyung Hand48f3c32019-08-23 11:18:57 +09002658func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2659 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2660 apex {
2661 name: "myapex",
2662 key: "myapex.key",
2663 native_shared_libs: ["libfoo"],
2664 }
2665
2666 apex_key {
2667 name: "myapex.key",
2668 public_key: "testkey.avbpubkey",
2669 private_key: "testkey.pem",
2670 }
2671
2672 cc_library {
2673 name: "libfoo",
2674 stl: "none",
2675 system_shared_libs: [],
2676 enabled: false,
2677 }
2678 `)
2679 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2680 apex {
2681 name: "myapex",
2682 key: "myapex.key",
2683 java_libs: ["myjar"],
2684 }
2685
2686 apex_key {
2687 name: "myapex.key",
2688 public_key: "testkey.avbpubkey",
2689 private_key: "testkey.pem",
2690 }
2691
2692 java_library {
2693 name: "myjar",
2694 srcs: ["foo/bar/MyClass.java"],
2695 sdk_version: "none",
2696 system_modules: "none",
2697 compile_dex: true,
2698 enabled: false,
2699 }
2700 `)
2701}
2702
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002703func TestApexWithApps(t *testing.T) {
2704 ctx, _ := testApex(t, `
2705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 apps: [
2709 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002710 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002711 ],
2712 }
2713
2714 apex_key {
2715 name: "myapex.key",
2716 public_key: "testkey.avbpubkey",
2717 private_key: "testkey.pem",
2718 }
2719
2720 android_app {
2721 name: "AppFoo",
2722 srcs: ["foo/bar/MyClass.java"],
2723 sdk_version: "none",
2724 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002725 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002726 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002727
2728 android_app {
2729 name: "AppFooPriv",
2730 srcs: ["foo/bar/MyClass.java"],
2731 sdk_version: "none",
2732 system_modules: "none",
2733 privileged: true,
2734 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002735
2736 cc_library_shared {
2737 name: "libjni",
2738 srcs: ["mylib.cpp"],
2739 stl: "none",
2740 system_shared_libs: [],
2741 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002742 `)
2743
Sundong Ahnabb64432019-10-22 13:58:29 +09002744 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002745 apexRule := module.Rule("apexRule")
2746 copyCmds := apexRule.Args["copy_commands"]
2747
2748 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002749 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002750
2751 // JNI libraries are embedded inside APK
2752 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
2753 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_core_shared_myapex").Module().(*cc.Module).OutputFile()
2754 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2755 // ... uncompressed
2756 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2757 t.Errorf("jni lib is not uncompressed for AppFoo")
2758 }
2759 // ... and not directly inside the APEX
2760 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002761}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002762
Dario Frenicde2a032019-10-27 00:29:22 +01002763func TestApexWithAppImports(t *testing.T) {
2764 ctx, _ := testApex(t, `
2765 apex {
2766 name: "myapex",
2767 key: "myapex.key",
2768 apps: [
2769 "AppFooPrebuilt",
2770 "AppFooPrivPrebuilt",
2771 ],
2772 }
2773
2774 apex_key {
2775 name: "myapex.key",
2776 public_key: "testkey.avbpubkey",
2777 private_key: "testkey.pem",
2778 }
2779
2780 android_app_import {
2781 name: "AppFooPrebuilt",
2782 apk: "PrebuiltAppFoo.apk",
2783 presigned: true,
2784 dex_preopt: {
2785 enabled: false,
2786 },
2787 }
2788
2789 android_app_import {
2790 name: "AppFooPrivPrebuilt",
2791 apk: "PrebuiltAppFooPriv.apk",
2792 privileged: true,
2793 presigned: true,
2794 dex_preopt: {
2795 enabled: false,
2796 },
2797 }
2798 `)
2799
Sundong Ahnabb64432019-10-22 13:58:29 +09002800 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002801 apexRule := module.Rule("apexRule")
2802 copyCmds := apexRule.Args["copy_commands"]
2803
2804 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2805 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002806}
2807
Jooyung Han18020ea2019-11-13 10:50:48 +09002808func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2809 // libfoo's apex_available comes from cc_defaults
2810 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2811 apex {
2812 name: "myapex",
2813 key: "myapex.key",
2814 native_shared_libs: ["libfoo"],
2815 }
2816
2817 apex_key {
2818 name: "myapex.key",
2819 public_key: "testkey.avbpubkey",
2820 private_key: "testkey.pem",
2821 }
2822
2823 apex {
2824 name: "otherapex",
2825 key: "myapex.key",
2826 native_shared_libs: ["libfoo"],
2827 }
2828
2829 cc_defaults {
2830 name: "libfoo-defaults",
2831 apex_available: ["otherapex"],
2832 }
2833
2834 cc_library {
2835 name: "libfoo",
2836 defaults: ["libfoo-defaults"],
2837 stl: "none",
2838 system_shared_libs: [],
2839 }`)
2840}
2841
Jiyong Park127b40b2019-09-30 16:04:35 +09002842func TestApexAvailable(t *testing.T) {
2843 // libfoo is not available to myapex, but only to otherapex
2844 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2845 apex {
2846 name: "myapex",
2847 key: "myapex.key",
2848 native_shared_libs: ["libfoo"],
2849 }
2850
2851 apex_key {
2852 name: "myapex.key",
2853 public_key: "testkey.avbpubkey",
2854 private_key: "testkey.pem",
2855 }
2856
2857 apex {
2858 name: "otherapex",
2859 key: "otherapex.key",
2860 native_shared_libs: ["libfoo"],
2861 }
2862
2863 apex_key {
2864 name: "otherapex.key",
2865 public_key: "testkey.avbpubkey",
2866 private_key: "testkey.pem",
2867 }
2868
2869 cc_library {
2870 name: "libfoo",
2871 stl: "none",
2872 system_shared_libs: [],
2873 apex_available: ["otherapex"],
2874 }`)
2875
2876 // libbar is an indirect dep
2877 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2878 apex {
2879 name: "myapex",
2880 key: "myapex.key",
2881 native_shared_libs: ["libfoo"],
2882 }
2883
2884 apex_key {
2885 name: "myapex.key",
2886 public_key: "testkey.avbpubkey",
2887 private_key: "testkey.pem",
2888 }
2889
2890 apex {
2891 name: "otherapex",
2892 key: "otherapex.key",
2893 native_shared_libs: ["libfoo"],
2894 }
2895
2896 apex_key {
2897 name: "otherapex.key",
2898 public_key: "testkey.avbpubkey",
2899 private_key: "testkey.pem",
2900 }
2901
2902 cc_library {
2903 name: "libfoo",
2904 stl: "none",
2905 shared_libs: ["libbar"],
2906 system_shared_libs: [],
2907 apex_available: ["myapex", "otherapex"],
2908 }
2909
2910 cc_library {
2911 name: "libbar",
2912 stl: "none",
2913 system_shared_libs: [],
2914 apex_available: ["otherapex"],
2915 }`)
2916
2917 testApexError(t, "\"otherapex\" is not a valid module name", `
2918 apex {
2919 name: "myapex",
2920 key: "myapex.key",
2921 native_shared_libs: ["libfoo"],
2922 }
2923
2924 apex_key {
2925 name: "myapex.key",
2926 public_key: "testkey.avbpubkey",
2927 private_key: "testkey.pem",
2928 }
2929
2930 cc_library {
2931 name: "libfoo",
2932 stl: "none",
2933 system_shared_libs: [],
2934 apex_available: ["otherapex"],
2935 }`)
2936
2937 ctx, _ := testApex(t, `
2938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
2941 native_shared_libs: ["libfoo", "libbar"],
2942 }
2943
2944 apex_key {
2945 name: "myapex.key",
2946 public_key: "testkey.avbpubkey",
2947 private_key: "testkey.pem",
2948 }
2949
2950 cc_library {
2951 name: "libfoo",
2952 stl: "none",
2953 system_shared_libs: [],
2954 apex_available: ["myapex"],
2955 }
2956
2957 cc_library {
2958 name: "libbar",
2959 stl: "none",
2960 system_shared_libs: [],
2961 apex_available: ["//apex_available:anyapex"],
2962 }`)
2963
2964 // check that libfoo and libbar are created only for myapex, but not for the platform
2965 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2966 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2967 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared_myapex")
2968 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared")
2969
2970 ctx, _ = testApex(t, `
2971 apex {
2972 name: "myapex",
2973 key: "myapex.key",
2974 }
2975
2976 apex_key {
2977 name: "myapex.key",
2978 public_key: "testkey.avbpubkey",
2979 private_key: "testkey.pem",
2980 }
2981
2982 cc_library {
2983 name: "libfoo",
2984 stl: "none",
2985 system_shared_libs: [],
2986 apex_available: ["//apex_available:platform"],
2987 }`)
2988
2989 // check that libfoo is created only for the platform
2990 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2991 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002992
2993 ctx, _ = testApex(t, `
2994 apex {
2995 name: "myapex",
2996 key: "myapex.key",
2997 native_shared_libs: ["libfoo"],
2998 }
2999
3000 apex_key {
3001 name: "myapex.key",
3002 public_key: "testkey.avbpubkey",
3003 private_key: "testkey.pem",
3004 }
3005
3006 cc_library {
3007 name: "libfoo",
3008 stl: "none",
3009 system_shared_libs: [],
3010 apex_available: ["myapex"],
3011 static: {
3012 apex_available: ["//apex_available:platform"],
3013 },
3014 }`)
3015
3016 // shared variant of libfoo is only available to myapex
3017 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
3018 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
3019 // but the static variant is available to both myapex and the platform
3020 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static_myapex")
3021 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003022}
3023
Jiyong Park5d790c32019-11-15 18:40:32 +09003024func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003025 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003026 apex {
3027 name: "myapex",
3028 key: "myapex.key",
3029 apps: ["app"],
3030 }
3031
3032 override_apex {
3033 name: "override_myapex",
3034 base: "myapex",
3035 apps: ["override_app"],
3036 }
3037
3038 apex_key {
3039 name: "myapex.key",
3040 public_key: "testkey.avbpubkey",
3041 private_key: "testkey.pem",
3042 }
3043
3044 android_app {
3045 name: "app",
3046 srcs: ["foo/bar/MyClass.java"],
3047 package_name: "foo",
3048 sdk_version: "none",
3049 system_modules: "none",
3050 }
3051
3052 override_android_app {
3053 name: "override_app",
3054 base: "app",
3055 package_name: "bar",
3056 }
3057 `)
3058
Jiyong Park317645e2019-12-05 13:20:58 +09003059 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3060 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3061 if originalVariant.GetOverriddenBy() != "" {
3062 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3063 }
3064 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3065 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3066 }
3067
Jiyong Park5d790c32019-11-15 18:40:32 +09003068 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3069 apexRule := module.Rule("apexRule")
3070 copyCmds := apexRule.Args["copy_commands"]
3071
3072 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3073 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003074
3075 apexBundle := module.Module().(*apexBundle)
3076 name := apexBundle.Name()
3077 if name != "override_myapex" {
3078 t.Errorf("name should be \"override_myapex\", but was %q", name)
3079 }
3080
3081 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3082 var builder strings.Builder
3083 data.Custom(&builder, name, "TARGET_", "", data)
3084 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003085 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003086 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3087 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3088 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003089 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003090 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3091 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003092}
3093
Jooyung Han214bf372019-11-12 13:03:50 +09003094func TestLegacyAndroid10Support(t *testing.T) {
3095 ctx, _ := testApex(t, `
3096 apex {
3097 name: "myapex",
3098 key: "myapex.key",
3099 legacy_android10_support: true,
3100 }
3101
3102 apex_key {
3103 name: "myapex.key",
3104 public_key: "testkey.avbpubkey",
3105 private_key: "testkey.pem",
3106 }
3107 `)
3108
3109 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3110 args := module.Rule("apexRule").Args
3111 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3112}
3113
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003114func TestMain(m *testing.M) {
3115 run := func() int {
3116 setUp()
3117 defer tearDown()
3118
3119 return m.Run()
3120 }
3121
3122 os.Exit(run())
3123}