blob: ee5d0f2ad35f968d76ca6c89f5fe6dcc38584e98 [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) {
143 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900144 ctx.BottomUp("vndk", cc.VndkMutator).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 Han01a3ee22019-11-02 02:52:25 +0900535func TestApexManifest(t *testing.T) {
536 ctx, _ := testApex(t, `
537 apex {
538 name: "myapex",
539 key: "myapex.key",
540 }
541
542 apex_key {
543 name: "myapex.key",
544 public_key: "testkey.avbpubkey",
545 private_key: "testkey.pem",
546 }
547 `)
548
549 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
550 module.Output("apex_manifest.pb")
551 module.Output("apex_manifest.json")
552 module.Output("apex_manifest_full.json")
553}
554
Alex Light5098a612018-11-29 17:12:15 -0800555func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700556 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800557 apex {
558 name: "myapex",
559 key: "myapex.key",
560 payload_type: "zip",
561 native_shared_libs: ["mylib"],
562 }
563
564 apex_key {
565 name: "myapex.key",
566 public_key: "testkey.avbpubkey",
567 private_key: "testkey.pem",
568 }
569
570 cc_library {
571 name: "mylib",
572 srcs: ["mylib.cpp"],
573 shared_libs: ["mylib2"],
574 system_shared_libs: [],
575 stl: "none",
576 }
577
578 cc_library {
579 name: "mylib2",
580 srcs: ["mylib.cpp"],
581 system_shared_libs: [],
582 stl: "none",
583 }
584 `)
585
Sundong Ahnabb64432019-10-22 13:58:29 +0900586 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800587 copyCmds := zipApexRule.Args["copy_commands"]
588
589 // Ensure that main rule creates an output
590 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
591
592 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900593 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800594
595 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900596 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800597
598 // Ensure that both direct and indirect deps are copied into apex
599 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
600 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900601}
602
603func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700604 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900605 apex {
606 name: "myapex",
607 key: "myapex.key",
608 native_shared_libs: ["mylib", "mylib3"],
609 }
610
611 apex_key {
612 name: "myapex.key",
613 public_key: "testkey.avbpubkey",
614 private_key: "testkey.pem",
615 }
616
617 cc_library {
618 name: "mylib",
619 srcs: ["mylib.cpp"],
620 shared_libs: ["mylib2", "mylib3"],
621 system_shared_libs: [],
622 stl: "none",
623 }
624
625 cc_library {
626 name: "mylib2",
627 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900628 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900629 system_shared_libs: [],
630 stl: "none",
631 stubs: {
632 versions: ["1", "2", "3"],
633 },
634 }
635
636 cc_library {
637 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900638 srcs: ["mylib.cpp"],
639 shared_libs: ["mylib4"],
640 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900641 stl: "none",
642 stubs: {
643 versions: ["10", "11", "12"],
644 },
645 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900646
647 cc_library {
648 name: "mylib4",
649 srcs: ["mylib.cpp"],
650 system_shared_libs: [],
651 stl: "none",
652 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653 `)
654
Sundong Ahnabb64432019-10-22 13:58:29 +0900655 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656 copyCmds := apexRule.Args["copy_commands"]
657
658 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800659 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900660
661 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800662 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900663
664 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800665 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666
Jiyong Parkda6eb592018-12-19 17:12:36 +0900667 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900668
669 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900670 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900672 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900673
674 // 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 +0900675 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900677 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900678
679 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900680 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900681 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900682
683 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900684 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 +0900685}
686
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900687func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700688 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900689 apex {
690 name: "myapex",
691 key: "myapex.key",
692 native_shared_libs: ["mylib"],
693 }
694
695 apex_key {
696 name: "myapex.key",
697 public_key: "testkey.avbpubkey",
698 private_key: "testkey.pem",
699 }
700
701 cc_library {
702 name: "mylib",
703 srcs: ["mylib.cpp"],
704 shared_libs: ["libfoo#10"],
705 system_shared_libs: [],
706 stl: "none",
707 }
708
709 cc_library {
710 name: "libfoo",
711 srcs: ["mylib.cpp"],
712 shared_libs: ["libbar"],
713 system_shared_libs: [],
714 stl: "none",
715 stubs: {
716 versions: ["10", "20", "30"],
717 },
718 }
719
720 cc_library {
721 name: "libbar",
722 srcs: ["mylib.cpp"],
723 system_shared_libs: [],
724 stl: "none",
725 }
726
727 `)
728
Sundong Ahnabb64432019-10-22 13:58:29 +0900729 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900730 copyCmds := apexRule.Args["copy_commands"]
731
732 // Ensure that direct non-stubs dep is always included
733 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
734
735 // Ensure that indirect stubs dep is not included
736 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
737
738 // Ensure that dependency of stubs is not included
739 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
740
Jiyong Parkda6eb592018-12-19 17:12:36 +0900741 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900742
743 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900744 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900745 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900746 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900747
Jiyong Parkda6eb592018-12-19 17:12:36 +0900748 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900749
750 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
751 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
752}
753
Jooyung Hand3639552019-08-09 12:57:43 +0900754func TestApexWithRuntimeLibsDependency(t *testing.T) {
755 /*
756 myapex
757 |
758 v (runtime_libs)
759 mylib ------+------> libfoo [provides stub]
760 |
761 `------> libbar
762 */
763 ctx, _ := testApex(t, `
764 apex {
765 name: "myapex",
766 key: "myapex.key",
767 native_shared_libs: ["mylib"],
768 }
769
770 apex_key {
771 name: "myapex.key",
772 public_key: "testkey.avbpubkey",
773 private_key: "testkey.pem",
774 }
775
776 cc_library {
777 name: "mylib",
778 srcs: ["mylib.cpp"],
779 runtime_libs: ["libfoo", "libbar"],
780 system_shared_libs: [],
781 stl: "none",
782 }
783
784 cc_library {
785 name: "libfoo",
786 srcs: ["mylib.cpp"],
787 system_shared_libs: [],
788 stl: "none",
789 stubs: {
790 versions: ["10", "20", "30"],
791 },
792 }
793
794 cc_library {
795 name: "libbar",
796 srcs: ["mylib.cpp"],
797 system_shared_libs: [],
798 stl: "none",
799 }
800
801 `)
802
Sundong Ahnabb64432019-10-22 13:58:29 +0900803 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900804 copyCmds := apexRule.Args["copy_commands"]
805
806 // Ensure that direct non-stubs dep is always included
807 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
808
809 // Ensure that indirect stubs dep is not included
810 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
811
812 // Ensure that runtime_libs dep in included
813 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
814
Sundong Ahnabb64432019-10-22 13:58:29 +0900815 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900816 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
817 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900818
819}
820
Jooyung Han9c80bae2019-08-20 17:30:57 +0900821func TestApexDependencyToLLNDK(t *testing.T) {
822 ctx, _ := testApex(t, `
823 apex {
824 name: "myapex",
825 key: "myapex.key",
826 use_vendor: true,
827 native_shared_libs: ["mylib"],
828 }
829
830 apex_key {
831 name: "myapex.key",
832 public_key: "testkey.avbpubkey",
833 private_key: "testkey.pem",
834 }
835
836 cc_library {
837 name: "mylib",
838 srcs: ["mylib.cpp"],
839 vendor_available: true,
840 shared_libs: ["libbar"],
841 system_shared_libs: [],
842 stl: "none",
843 }
844
845 cc_library {
846 name: "libbar",
847 srcs: ["mylib.cpp"],
848 system_shared_libs: [],
849 stl: "none",
850 }
851
852 llndk_library {
853 name: "libbar",
854 symbol_file: "",
855 }
Jooyung Handc782442019-11-01 03:14:38 +0900856 `, func(fs map[string][]byte, config android.Config) {
857 setUseVendorWhitelistForTest(config, []string{"myapex"})
858 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900859
Sundong Ahnabb64432019-10-22 13:58:29 +0900860 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900861 copyCmds := apexRule.Args["copy_commands"]
862
863 // Ensure that LLNDK dep is not included
864 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
865
Sundong Ahnabb64432019-10-22 13:58:29 +0900866 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900867 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900868
869 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900870 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900871
872}
873
Jiyong Park25fc6a92018-11-18 18:02:45 +0900874func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700875 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900876 apex {
877 name: "myapex",
878 key: "myapex.key",
879 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
880 }
881
882 apex_key {
883 name: "myapex.key",
884 public_key: "testkey.avbpubkey",
885 private_key: "testkey.pem",
886 }
887
888 cc_library {
889 name: "mylib",
890 srcs: ["mylib.cpp"],
891 shared_libs: ["libdl#27"],
892 stl: "none",
893 }
894
895 cc_library_shared {
896 name: "mylib_shared",
897 srcs: ["mylib.cpp"],
898 shared_libs: ["libdl#27"],
899 stl: "none",
900 }
901
902 cc_library {
903 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700904 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900905 nocrt: true,
906 system_shared_libs: [],
907 stl: "none",
908 stubs: {
909 versions: ["27", "28", "29"],
910 },
911 }
912
913 cc_library {
914 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700915 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 nocrt: true,
917 system_shared_libs: [],
918 stl: "none",
919 stubs: {
920 versions: ["27", "28", "29"],
921 },
922 }
923
924 cc_library {
925 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700926 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 nocrt: true,
928 system_shared_libs: [],
929 stl: "none",
930 stubs: {
931 versions: ["27", "28", "29"],
932 },
933 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900934
935 cc_library {
936 name: "libBootstrap",
937 srcs: ["mylib.cpp"],
938 stl: "none",
939 bootstrap: true,
940 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900941 `)
942
Sundong Ahnabb64432019-10-22 13:58:29 +0900943 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 copyCmds := apexRule.Args["copy_commands"]
945
946 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800947 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900948 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
949 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900950
951 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900952 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900953
Jiyong Parkda6eb592018-12-19 17:12:36 +0900954 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
955 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
956 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957
958 // For dependency to libc
959 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900960 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900961 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900962 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900963 // ... Cflags from stub is correctly exported to mylib
964 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
965 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
966
967 // For dependency to libm
968 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900969 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900970 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900971 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // ... and is not compiling with the stub
973 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
974 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
975
976 // For dependency to libdl
977 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900978 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900980 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
981 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900983 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900984 // ... Cflags from stub is correctly exported to mylib
985 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
986 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900987
988 // Ensure that libBootstrap is depending on the platform variant of bionic libs
989 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
990 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
991 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
992 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900994
995func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700996 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900997 apex {
998 name: "myapex",
999 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001000 native_shared_libs: ["mylib"],
1001 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001002 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001003 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001004 }
1005
1006 apex_key {
1007 name: "myapex.key",
1008 public_key: "testkey.avbpubkey",
1009 private_key: "testkey.pem",
1010 }
1011
1012 prebuilt_etc {
1013 name: "myetc",
1014 src: "myprebuilt",
1015 sub_dir: "foo/bar",
1016 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001017
1018 cc_library {
1019 name: "mylib",
1020 srcs: ["mylib.cpp"],
1021 relative_install_path: "foo/bar",
1022 system_shared_libs: [],
1023 stl: "none",
1024 }
1025
1026 cc_binary {
1027 name: "mybin",
1028 srcs: ["mylib.cpp"],
1029 relative_install_path: "foo/bar",
1030 system_shared_libs: [],
1031 static_executable: true,
1032 stl: "none",
1033 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001034 `)
1035
Sundong Ahnabb64432019-10-22 13:58:29 +09001036 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001037 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1038
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001039 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001040 ensureListContains(t, dirs, "etc")
1041 ensureListContains(t, dirs, "etc/foo")
1042 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001043 ensureListContains(t, dirs, "lib64")
1044 ensureListContains(t, dirs, "lib64/foo")
1045 ensureListContains(t, dirs, "lib64/foo/bar")
1046 ensureListContains(t, dirs, "lib")
1047 ensureListContains(t, dirs, "lib/foo")
1048 ensureListContains(t, dirs, "lib/foo/bar")
1049
Jiyong Parkbd13e442019-03-15 18:10:35 +09001050 ensureListContains(t, dirs, "bin")
1051 ensureListContains(t, dirs, "bin/foo")
1052 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001053}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001054
1055func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001056 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001057 apex {
1058 name: "myapex",
1059 key: "myapex.key",
1060 native_shared_libs: ["mylib"],
1061 use_vendor: true,
1062 }
1063
1064 apex_key {
1065 name: "myapex.key",
1066 public_key: "testkey.avbpubkey",
1067 private_key: "testkey.pem",
1068 }
1069
1070 cc_library {
1071 name: "mylib",
1072 srcs: ["mylib.cpp"],
1073 shared_libs: ["mylib2"],
1074 system_shared_libs: [],
1075 vendor_available: true,
1076 stl: "none",
1077 }
1078
1079 cc_library {
1080 name: "mylib2",
1081 srcs: ["mylib.cpp"],
1082 system_shared_libs: [],
1083 vendor_available: true,
1084 stl: "none",
1085 }
Jooyung Handc782442019-11-01 03:14:38 +09001086 `, func(fs map[string][]byte, config android.Config) {
1087 setUseVendorWhitelistForTest(config, []string{"myapex"})
1088 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001089
1090 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001091 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001092 for _, implicit := range i.Implicits {
1093 inputsList = append(inputsList, implicit.String())
1094 }
1095 }
1096 inputsString := strings.Join(inputsList, " ")
1097
1098 // ensure that the apex includes vendor variants of the direct and indirect deps
Inseob Kim64c43952019-08-26 16:52:35 +09001099 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib.so")
1100 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001101
1102 // ensure that the apex does not include core variants
1103 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
1104 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
1105}
Jiyong Park16e91a02018-12-20 18:18:08 +09001106
Jooyung Handc782442019-11-01 03:14:38 +09001107func TestUseVendorRestriction(t *testing.T) {
1108 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1109 apex {
1110 name: "myapex",
1111 key: "myapex.key",
1112 use_vendor: true,
1113 }
1114 apex_key {
1115 name: "myapex.key",
1116 public_key: "testkey.avbpubkey",
1117 private_key: "testkey.pem",
1118 }
1119 `, func(fs map[string][]byte, config android.Config) {
1120 setUseVendorWhitelistForTest(config, []string{""})
1121 })
1122 // no error with whitelist
1123 testApex(t, `
1124 apex {
1125 name: "myapex",
1126 key: "myapex.key",
1127 use_vendor: true,
1128 }
1129 apex_key {
1130 name: "myapex.key",
1131 public_key: "testkey.avbpubkey",
1132 private_key: "testkey.pem",
1133 }
1134 `, func(fs map[string][]byte, config android.Config) {
1135 setUseVendorWhitelistForTest(config, []string{"myapex"})
1136 })
1137}
1138
Jooyung Han5c998b92019-06-27 11:30:33 +09001139func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1140 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1141 apex {
1142 name: "myapex",
1143 key: "myapex.key",
1144 native_shared_libs: ["mylib"],
1145 use_vendor: true,
1146 }
1147
1148 apex_key {
1149 name: "myapex.key",
1150 public_key: "testkey.avbpubkey",
1151 private_key: "testkey.pem",
1152 }
1153
1154 cc_library {
1155 name: "mylib",
1156 srcs: ["mylib.cpp"],
1157 system_shared_libs: [],
1158 stl: "none",
1159 }
1160 `)
1161}
1162
Jiyong Park16e91a02018-12-20 18:18:08 +09001163func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001164 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001165 apex {
1166 name: "myapex",
1167 key: "myapex.key",
1168 native_shared_libs: ["mylib"],
1169 }
1170
1171 apex_key {
1172 name: "myapex.key",
1173 public_key: "testkey.avbpubkey",
1174 private_key: "testkey.pem",
1175 }
1176
1177 cc_library {
1178 name: "mylib",
1179 srcs: ["mylib.cpp"],
1180 system_shared_libs: [],
1181 stl: "none",
1182 stubs: {
1183 versions: ["1", "2", "3"],
1184 },
1185 }
1186
1187 cc_binary {
1188 name: "not_in_apex",
1189 srcs: ["mylib.cpp"],
1190 static_libs: ["mylib"],
1191 static_executable: true,
1192 system_shared_libs: [],
1193 stl: "none",
1194 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001195 `)
1196
1197 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
1198
1199 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +08001200 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001201}
Jiyong Park9335a262018-12-24 11:31:58 +09001202
1203func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001204 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001205 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001206 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001207 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001208 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001209 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001210 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001211 }
1212
1213 cc_library {
1214 name: "mylib",
1215 srcs: ["mylib.cpp"],
1216 system_shared_libs: [],
1217 stl: "none",
1218 }
1219
1220 apex_key {
1221 name: "myapex.key",
1222 public_key: "testkey.avbpubkey",
1223 private_key: "testkey.pem",
1224 }
1225
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001226 android_app_certificate {
1227 name: "myapex.certificate",
1228 certificate: "testkey",
1229 }
1230
1231 android_app_certificate {
1232 name: "myapex.certificate.override",
1233 certificate: "testkey.override",
1234 }
1235
Jiyong Park9335a262018-12-24 11:31:58 +09001236 `)
1237
1238 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001239 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001240
1241 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1242 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1243 "vendor/foo/devkeys/testkey.avbpubkey")
1244 }
1245 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1246 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1247 "vendor/foo/devkeys/testkey.pem")
1248 }
1249
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001250 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001251 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001252 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001253 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001254 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001255 }
1256}
Jiyong Park58e364a2019-01-19 19:24:06 +09001257
1258func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001259 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001260 apex {
1261 name: "myapex",
1262 key: "myapex.key",
1263 native_shared_libs: ["mylib"],
1264 }
1265
1266 apex {
1267 name: "otherapex",
1268 key: "myapex.key",
1269 native_shared_libs: ["mylib"],
1270 }
1271
1272 apex_key {
1273 name: "myapex.key",
1274 public_key: "testkey.avbpubkey",
1275 private_key: "testkey.pem",
1276 }
1277
1278 cc_library {
1279 name: "mylib",
1280 srcs: ["mylib.cpp"],
1281 system_shared_libs: [],
1282 stl: "none",
1283 }
1284 `)
1285
Jooyung Han6b8459b2019-10-30 08:29:25 +09001286 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Jiyong Park58e364a2019-01-19 19:24:06 +09001287 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001288 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001289 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1290 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001291
Jooyung Han6b8459b2019-10-30 08:29:25 +09001292 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Jiyong Park58e364a2019-01-19 19:24:06 +09001293 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001294 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001295 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1296 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001297
Jooyung Han6b8459b2019-10-30 08:29:25 +09001298 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Jiyong Park58e364a2019-01-19 19:24:06 +09001299 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001300 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001301 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1302 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001303}
Jiyong Park7e636d02019-01-28 16:16:54 +09001304
1305func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001306 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001307 apex {
1308 name: "myapex",
1309 key: "myapex.key",
1310 native_shared_libs: ["mylib"],
1311 }
1312
1313 apex_key {
1314 name: "myapex.key",
1315 public_key: "testkey.avbpubkey",
1316 private_key: "testkey.pem",
1317 }
1318
1319 cc_library_headers {
1320 name: "mylib_headers",
1321 export_include_dirs: ["my_include"],
1322 system_shared_libs: [],
1323 stl: "none",
1324 }
1325
1326 cc_library {
1327 name: "mylib",
1328 srcs: ["mylib.cpp"],
1329 system_shared_libs: [],
1330 stl: "none",
1331 header_libs: ["mylib_headers"],
1332 export_header_lib_headers: ["mylib_headers"],
1333 stubs: {
1334 versions: ["1", "2", "3"],
1335 },
1336 }
1337
1338 cc_library {
1339 name: "otherlib",
1340 srcs: ["mylib.cpp"],
1341 system_shared_libs: [],
1342 stl: "none",
1343 shared_libs: ["mylib"],
1344 }
1345 `)
1346
1347 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1348
1349 // Ensure that the include path of the header lib is exported to 'otherlib'
1350 ensureContains(t, cFlags, "-Imy_include")
1351}
Alex Light9670d332019-01-29 18:07:33 -08001352
Jooyung Han31c470b2019-10-18 16:26:59 +09001353func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1354 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001355 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001356 copyCmds := apexRule.Args["copy_commands"]
1357 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001358 var failed bool
1359 var surplus []string
1360 filesMatched := make(map[string]bool)
1361 addContent := func(content string) {
1362 for _, expected := range files {
1363 if matched, _ := path.Match(expected, content); matched {
1364 filesMatched[expected] = true
1365 return
1366 }
1367 }
1368 surplus = append(surplus, content)
1369 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001370 for _, cmd := range strings.Split(copyCmds, "&&") {
1371 cmd = strings.TrimSpace(cmd)
1372 if cmd == "" {
1373 continue
1374 }
1375 terms := strings.Split(cmd, " ")
1376 switch terms[0] {
1377 case "mkdir":
1378 case "cp":
1379 if len(terms) != 3 {
1380 t.Fatal("copyCmds contains invalid cp command", cmd)
1381 }
1382 dst := terms[2]
1383 index := strings.Index(dst, imageApexDir)
1384 if index == -1 {
1385 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1386 }
1387 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001388 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001389 default:
1390 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1391 }
1392 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001393
Jooyung Han31c470b2019-10-18 16:26:59 +09001394 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001395 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001396 t.Log("surplus files", surplus)
1397 failed = true
1398 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001399
1400 if len(files) > len(filesMatched) {
1401 var missing []string
1402 for _, expected := range files {
1403 if !filesMatched[expected] {
1404 missing = append(missing, expected)
1405 }
1406 }
1407 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001408 t.Log("missing files", missing)
1409 failed = true
1410 }
1411 if failed {
1412 t.Fail()
1413 }
1414}
1415
Jooyung Han344d5432019-08-23 11:17:39 +09001416func TestVndkApexCurrent(t *testing.T) {
1417 ctx, _ := testApex(t, `
1418 apex_vndk {
1419 name: "myapex",
1420 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001421 }
1422
1423 apex_key {
1424 name: "myapex.key",
1425 public_key: "testkey.avbpubkey",
1426 private_key: "testkey.pem",
1427 }
1428
1429 cc_library {
1430 name: "libvndk",
1431 srcs: ["mylib.cpp"],
1432 vendor_available: true,
1433 vndk: {
1434 enabled: true,
1435 },
1436 system_shared_libs: [],
1437 stl: "none",
1438 }
1439
1440 cc_library {
1441 name: "libvndksp",
1442 srcs: ["mylib.cpp"],
1443 vendor_available: true,
1444 vndk: {
1445 enabled: true,
1446 support_system_process: true,
1447 },
1448 system_shared_libs: [],
1449 stl: "none",
1450 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001451 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001452
Jooyung Han31c470b2019-10-18 16:26:59 +09001453 ensureExactContents(t, ctx, "myapex", []string{
1454 "lib/libvndk.so",
1455 "lib/libvndksp.so",
1456 "lib64/libvndk.so",
1457 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001458 "etc/llndk.libraries.VER.txt",
1459 "etc/vndkcore.libraries.VER.txt",
1460 "etc/vndksp.libraries.VER.txt",
1461 "etc/vndkprivate.libraries.VER.txt",
1462 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001463 })
Jooyung Han344d5432019-08-23 11:17:39 +09001464}
1465
1466func TestVndkApexWithPrebuilt(t *testing.T) {
1467 ctx, _ := testApex(t, `
1468 apex_vndk {
1469 name: "myapex",
1470 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001471 }
1472
1473 apex_key {
1474 name: "myapex.key",
1475 public_key: "testkey.avbpubkey",
1476 private_key: "testkey.pem",
1477 }
1478
1479 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001480 name: "libvndk",
1481 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001482 vendor_available: true,
1483 vndk: {
1484 enabled: true,
1485 },
1486 system_shared_libs: [],
1487 stl: "none",
1488 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001489
1490 cc_prebuilt_library_shared {
1491 name: "libvndk.arm",
1492 srcs: ["libvndk.arm.so"],
1493 vendor_available: true,
1494 vndk: {
1495 enabled: true,
1496 },
1497 enabled: false,
1498 arch: {
1499 arm: {
1500 enabled: true,
1501 },
1502 },
1503 system_shared_libs: [],
1504 stl: "none",
1505 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001506 `+vndkLibrariesTxtFiles("current"),
1507 withFiles(map[string][]byte{
1508 "libvndk.so": nil,
1509 "libvndk.arm.so": nil,
1510 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001511
Jooyung Han31c470b2019-10-18 16:26:59 +09001512 ensureExactContents(t, ctx, "myapex", []string{
1513 "lib/libvndk.so",
1514 "lib/libvndk.arm.so",
1515 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001516 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001517 })
Jooyung Han344d5432019-08-23 11:17:39 +09001518}
1519
Jooyung Han39edb6c2019-11-06 16:53:07 +09001520func vndkLibrariesTxtFiles(vers ...string) (result string) {
1521 for _, v := range vers {
1522 if v == "current" {
1523 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1524 result += `
1525 vndk_libraries_txt {
1526 name: "` + txt + `.libraries.txt",
1527 }
1528 `
1529 }
1530 } else {
1531 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1532 result += `
1533 prebuilt_etc {
1534 name: "` + txt + `.libraries.` + v + `.txt",
1535 src: "dummy.txt",
1536 }
1537 `
1538 }
1539 }
1540 }
1541 return
1542}
1543
Jooyung Han344d5432019-08-23 11:17:39 +09001544func TestVndkApexVersion(t *testing.T) {
1545 ctx, _ := testApex(t, `
1546 apex_vndk {
1547 name: "myapex_v27",
1548 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001549 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001550 vndk_version: "27",
1551 }
1552
1553 apex_key {
1554 name: "myapex.key",
1555 public_key: "testkey.avbpubkey",
1556 private_key: "testkey.pem",
1557 }
1558
Jooyung Han31c470b2019-10-18 16:26:59 +09001559 vndk_prebuilt_shared {
1560 name: "libvndk27",
1561 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001562 vendor_available: true,
1563 vndk: {
1564 enabled: true,
1565 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001566 target_arch: "arm64",
1567 arch: {
1568 arm: {
1569 srcs: ["libvndk27_arm.so"],
1570 },
1571 arm64: {
1572 srcs: ["libvndk27_arm64.so"],
1573 },
1574 },
Jooyung Han344d5432019-08-23 11:17:39 +09001575 }
1576
1577 vndk_prebuilt_shared {
1578 name: "libvndk27",
1579 version: "27",
1580 vendor_available: true,
1581 vndk: {
1582 enabled: true,
1583 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001584 target_arch: "x86_64",
1585 arch: {
1586 x86: {
1587 srcs: ["libvndk27_x86.so"],
1588 },
1589 x86_64: {
1590 srcs: ["libvndk27_x86_64.so"],
1591 },
1592 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001593 }
1594 `+vndkLibrariesTxtFiles("27"),
1595 withFiles(map[string][]byte{
1596 "libvndk27_arm.so": nil,
1597 "libvndk27_arm64.so": nil,
1598 "libvndk27_x86.so": nil,
1599 "libvndk27_x86_64.so": nil,
1600 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001601
Jooyung Han31c470b2019-10-18 16:26:59 +09001602 ensureExactContents(t, ctx, "myapex_v27", []string{
1603 "lib/libvndk27_arm.so",
1604 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001605 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001606 })
Jooyung Han344d5432019-08-23 11:17:39 +09001607}
1608
1609func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1610 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1611 apex_vndk {
1612 name: "myapex_v27",
1613 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001614 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001615 vndk_version: "27",
1616 }
1617 apex_vndk {
1618 name: "myapex_v27_other",
1619 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001620 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001621 vndk_version: "27",
1622 }
1623
1624 apex_key {
1625 name: "myapex.key",
1626 public_key: "testkey.avbpubkey",
1627 private_key: "testkey.pem",
1628 }
1629
1630 cc_library {
1631 name: "libvndk",
1632 srcs: ["mylib.cpp"],
1633 vendor_available: true,
1634 vndk: {
1635 enabled: true,
1636 },
1637 system_shared_libs: [],
1638 stl: "none",
1639 }
1640
1641 vndk_prebuilt_shared {
1642 name: "libvndk",
1643 version: "27",
1644 vendor_available: true,
1645 vndk: {
1646 enabled: true,
1647 },
1648 srcs: ["libvndk.so"],
1649 }
1650 `, withFiles(map[string][]byte{
1651 "libvndk.so": nil,
1652 }))
1653}
1654
Jooyung Han90eee022019-10-01 20:02:42 +09001655func TestVndkApexNameRule(t *testing.T) {
1656 ctx, _ := testApex(t, `
1657 apex_vndk {
1658 name: "myapex",
1659 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001660 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001661 }
1662 apex_vndk {
1663 name: "myapex_v28",
1664 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001665 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001666 vndk_version: "28",
1667 }
1668 apex_key {
1669 name: "myapex.key",
1670 public_key: "testkey.avbpubkey",
1671 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001672 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001673
1674 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001675 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001676 actual := proptools.String(bundle.properties.Apex_name)
1677 if !reflect.DeepEqual(actual, expected) {
1678 t.Errorf("Got '%v', expected '%v'", actual, expected)
1679 }
1680 }
1681
1682 assertApexName("com.android.vndk.vVER", "myapex")
1683 assertApexName("com.android.vndk.v28", "myapex_v28")
1684}
1685
Jooyung Han344d5432019-08-23 11:17:39 +09001686func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1687 ctx, _ := testApex(t, `
1688 apex_vndk {
1689 name: "myapex",
1690 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001691 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001692 }
1693
1694 apex_key {
1695 name: "myapex.key",
1696 public_key: "testkey.avbpubkey",
1697 private_key: "testkey.pem",
1698 }
1699
1700 cc_library {
1701 name: "libvndk",
1702 srcs: ["mylib.cpp"],
1703 vendor_available: true,
1704 native_bridge_supported: true,
1705 host_supported: true,
1706 vndk: {
1707 enabled: true,
1708 },
1709 system_shared_libs: [],
1710 stl: "none",
1711 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001712 `+vndkLibrariesTxtFiles("current"),
1713 withTargets(map[android.OsType][]android.Target{
1714 android.Android: []android.Target{
1715 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1716 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1717 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1718 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1719 },
1720 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001721
Jooyung Han31c470b2019-10-18 16:26:59 +09001722 ensureExactContents(t, ctx, "myapex", []string{
1723 "lib/libvndk.so",
1724 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001725 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001726 })
Jooyung Han344d5432019-08-23 11:17:39 +09001727}
1728
1729func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1730 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1731 apex_vndk {
1732 name: "myapex",
1733 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001734 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001735 native_bridge_supported: true,
1736 }
1737
1738 apex_key {
1739 name: "myapex.key",
1740 public_key: "testkey.avbpubkey",
1741 private_key: "testkey.pem",
1742 }
1743
1744 cc_library {
1745 name: "libvndk",
1746 srcs: ["mylib.cpp"],
1747 vendor_available: true,
1748 native_bridge_supported: true,
1749 host_supported: true,
1750 vndk: {
1751 enabled: true,
1752 },
1753 system_shared_libs: [],
1754 stl: "none",
1755 }
1756 `)
1757}
1758
Jooyung Han31c470b2019-10-18 16:26:59 +09001759func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001760 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001761 apex_vndk {
1762 name: "myapex_v27",
1763 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001764 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001765 vndk_version: "27",
1766 }
1767
1768 apex_key {
1769 name: "myapex.key",
1770 public_key: "testkey.avbpubkey",
1771 private_key: "testkey.pem",
1772 }
1773
1774 vndk_prebuilt_shared {
1775 name: "libvndk27",
1776 version: "27",
1777 target_arch: "arm",
1778 vendor_available: true,
1779 vndk: {
1780 enabled: true,
1781 },
1782 arch: {
1783 arm: {
1784 srcs: ["libvndk27.so"],
1785 }
1786 },
1787 }
1788
1789 vndk_prebuilt_shared {
1790 name: "libvndk27",
1791 version: "27",
1792 target_arch: "arm",
1793 binder32bit: true,
1794 vendor_available: true,
1795 vndk: {
1796 enabled: true,
1797 },
1798 arch: {
1799 arm: {
1800 srcs: ["libvndk27binder32.so"],
1801 }
1802 },
1803 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001804 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001805 withFiles(map[string][]byte{
1806 "libvndk27.so": nil,
1807 "libvndk27binder32.so": nil,
1808 }),
1809 withBinder32bit,
1810 withTargets(map[android.OsType][]android.Target{
1811 android.Android: []android.Target{
1812 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1813 },
1814 }),
1815 )
1816
1817 ensureExactContents(t, ctx, "myapex_v27", []string{
1818 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001819 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001820 })
1821}
1822
Jooyung Hane1633032019-08-01 17:41:43 +09001823func TestDependenciesInApexManifest(t *testing.T) {
1824 ctx, _ := testApex(t, `
1825 apex {
1826 name: "myapex_nodep",
1827 key: "myapex.key",
1828 native_shared_libs: ["lib_nodep"],
1829 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001830 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001831 }
1832
1833 apex {
1834 name: "myapex_dep",
1835 key: "myapex.key",
1836 native_shared_libs: ["lib_dep"],
1837 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001838 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001839 }
1840
1841 apex {
1842 name: "myapex_provider",
1843 key: "myapex.key",
1844 native_shared_libs: ["libfoo"],
1845 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001846 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001847 }
1848
1849 apex {
1850 name: "myapex_selfcontained",
1851 key: "myapex.key",
1852 native_shared_libs: ["lib_dep", "libfoo"],
1853 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001854 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001855 }
1856
1857 apex_key {
1858 name: "myapex.key",
1859 public_key: "testkey.avbpubkey",
1860 private_key: "testkey.pem",
1861 }
1862
1863 cc_library {
1864 name: "lib_nodep",
1865 srcs: ["mylib.cpp"],
1866 system_shared_libs: [],
1867 stl: "none",
1868 }
1869
1870 cc_library {
1871 name: "lib_dep",
1872 srcs: ["mylib.cpp"],
1873 shared_libs: ["libfoo"],
1874 system_shared_libs: [],
1875 stl: "none",
1876 }
1877
1878 cc_library {
1879 name: "libfoo",
1880 srcs: ["mytest.cpp"],
1881 stubs: {
1882 versions: ["1"],
1883 },
1884 system_shared_libs: [],
1885 stl: "none",
1886 }
1887 `)
1888
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001889 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09001890 var provideNativeLibs, requireNativeLibs []string
1891
Sundong Ahnabb64432019-10-22 13:58:29 +09001892 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001893 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1894 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001895 ensureListEmpty(t, provideNativeLibs)
1896 ensureListEmpty(t, requireNativeLibs)
1897
Sundong Ahnabb64432019-10-22 13:58:29 +09001898 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001899 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1900 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001901 ensureListEmpty(t, provideNativeLibs)
1902 ensureListContains(t, requireNativeLibs, "libfoo.so")
1903
Sundong Ahnabb64432019-10-22 13:58:29 +09001904 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001905 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1906 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001907 ensureListContains(t, provideNativeLibs, "libfoo.so")
1908 ensureListEmpty(t, requireNativeLibs)
1909
Sundong Ahnabb64432019-10-22 13:58:29 +09001910 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001911 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1912 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001913 ensureListContains(t, provideNativeLibs, "libfoo.so")
1914 ensureListEmpty(t, requireNativeLibs)
1915}
1916
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001917func TestApexName(t *testing.T) {
1918 ctx, _ := testApex(t, `
1919 apex {
1920 name: "myapex",
1921 key: "myapex.key",
1922 apex_name: "com.android.myapex",
1923 }
1924
1925 apex_key {
1926 name: "myapex.key",
1927 public_key: "testkey.avbpubkey",
1928 private_key: "testkey.pem",
1929 }
1930 `)
1931
Sundong Ahnabb64432019-10-22 13:58:29 +09001932 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001933 apexManifestRule := module.Rule("apexManifestRule")
1934 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
1935 apexRule := module.Rule("apexRule")
1936 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
1937}
1938
Alex Light0851b882019-02-07 13:20:53 -08001939func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001940 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001941 apex {
1942 name: "myapex",
1943 key: "myapex.key",
1944 native_shared_libs: ["mylib_common"],
1945 }
1946
1947 apex_key {
1948 name: "myapex.key",
1949 public_key: "testkey.avbpubkey",
1950 private_key: "testkey.pem",
1951 }
1952
1953 cc_library {
1954 name: "mylib_common",
1955 srcs: ["mylib.cpp"],
1956 system_shared_libs: [],
1957 stl: "none",
1958 }
1959 `)
1960
Sundong Ahnabb64432019-10-22 13:58:29 +09001961 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08001962 apexRule := module.Rule("apexRule")
1963 copyCmds := apexRule.Args["copy_commands"]
1964
1965 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1966 t.Log("Apex was a test apex!")
1967 t.Fail()
1968 }
1969 // Ensure that main rule creates an output
1970 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1971
1972 // Ensure that apex variant is created for the direct dep
1973 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1974
1975 // Ensure that both direct and indirect deps are copied into apex
1976 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1977
1978 // Ensure that the platform variant ends with _core_shared
1979 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1980
1981 if !android.InAnyApex("mylib_common") {
1982 t.Log("Found mylib_common not in any apex!")
1983 t.Fail()
1984 }
1985}
1986
1987func TestTestApex(t *testing.T) {
1988 if android.InAnyApex("mylib_common_test") {
1989 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!")
1990 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001991 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001992 apex_test {
1993 name: "myapex",
1994 key: "myapex.key",
1995 native_shared_libs: ["mylib_common_test"],
1996 }
1997
1998 apex_key {
1999 name: "myapex.key",
2000 public_key: "testkey.avbpubkey",
2001 private_key: "testkey.pem",
2002 }
2003
2004 cc_library {
2005 name: "mylib_common_test",
2006 srcs: ["mylib.cpp"],
2007 system_shared_libs: [],
2008 stl: "none",
2009 }
2010 `)
2011
Sundong Ahnabb64432019-10-22 13:58:29 +09002012 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002013 apexRule := module.Rule("apexRule")
2014 copyCmds := apexRule.Args["copy_commands"]
2015
2016 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2017 t.Log("Apex was not a test apex!")
2018 t.Fail()
2019 }
2020 // Ensure that main rule creates an output
2021 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2022
2023 // Ensure that apex variant is created for the direct dep
2024 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
2025
2026 // Ensure that both direct and indirect deps are copied into apex
2027 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2028
2029 // Ensure that the platform variant ends with _core_shared
2030 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
2031
2032 if android.InAnyApex("mylib_common_test") {
2033 t.Log("Found mylib_common_test in some apex!")
2034 t.Fail()
2035 }
2036}
2037
Alex Light9670d332019-01-29 18:07:33 -08002038func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002039 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002040 apex {
2041 name: "myapex",
2042 key: "myapex.key",
2043 multilib: {
2044 first: {
2045 native_shared_libs: ["mylib_common"],
2046 }
2047 },
2048 target: {
2049 android: {
2050 multilib: {
2051 first: {
2052 native_shared_libs: ["mylib"],
2053 }
2054 }
2055 },
2056 host: {
2057 multilib: {
2058 first: {
2059 native_shared_libs: ["mylib2"],
2060 }
2061 }
2062 }
2063 }
2064 }
2065
2066 apex_key {
2067 name: "myapex.key",
2068 public_key: "testkey.avbpubkey",
2069 private_key: "testkey.pem",
2070 }
2071
2072 cc_library {
2073 name: "mylib",
2074 srcs: ["mylib.cpp"],
2075 system_shared_libs: [],
2076 stl: "none",
2077 }
2078
2079 cc_library {
2080 name: "mylib_common",
2081 srcs: ["mylib.cpp"],
2082 system_shared_libs: [],
2083 stl: "none",
2084 compile_multilib: "first",
2085 }
2086
2087 cc_library {
2088 name: "mylib2",
2089 srcs: ["mylib.cpp"],
2090 system_shared_libs: [],
2091 stl: "none",
2092 compile_multilib: "first",
2093 }
2094 `)
2095
Sundong Ahnabb64432019-10-22 13:58:29 +09002096 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002097 copyCmds := apexRule.Args["copy_commands"]
2098
2099 // Ensure that main rule creates an output
2100 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2101
2102 // Ensure that apex variant is created for the direct dep
2103 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2104 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
2105 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
2106
2107 // Ensure that both direct and indirect deps are copied into apex
2108 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2109 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2110 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2111
2112 // Ensure that the platform variant ends with _core_shared
2113 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
2114 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
2115 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
2116}
Jiyong Park04480cf2019-02-06 00:16:29 +09002117
2118func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002119 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002120 apex {
2121 name: "myapex",
2122 key: "myapex.key",
2123 binaries: ["myscript"],
2124 }
2125
2126 apex_key {
2127 name: "myapex.key",
2128 public_key: "testkey.avbpubkey",
2129 private_key: "testkey.pem",
2130 }
2131
2132 sh_binary {
2133 name: "myscript",
2134 src: "mylib.cpp",
2135 filename: "myscript.sh",
2136 sub_dir: "script",
2137 }
2138 `)
2139
Sundong Ahnabb64432019-10-22 13:58:29 +09002140 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002141 copyCmds := apexRule.Args["copy_commands"]
2142
2143 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2144}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002145
Jooyung Han91df2082019-11-20 01:49:42 +09002146func TestApexInVariousPartition(t *testing.T) {
2147 testcases := []struct {
2148 propName, parition, flattenedPartition string
2149 }{
2150 {"", "system", "system_ext"},
2151 {"product_specific: true", "product", "product"},
2152 {"soc_specific: true", "vendor", "vendor"},
2153 {"proprietary: true", "vendor", "vendor"},
2154 {"vendor: true", "vendor", "vendor"},
2155 {"system_ext_specific: true", "system_ext", "system_ext"},
2156 }
2157 for _, tc := range testcases {
2158 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2159 ctx, _ := testApex(t, `
2160 apex {
2161 name: "myapex",
2162 key: "myapex.key",
2163 `+tc.propName+`
2164 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002165
Jooyung Han91df2082019-11-20 01:49:42 +09002166 apex_key {
2167 name: "myapex.key",
2168 public_key: "testkey.avbpubkey",
2169 private_key: "testkey.pem",
2170 }
2171 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002172
Jooyung Han91df2082019-11-20 01:49:42 +09002173 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2174 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2175 actual := apex.installDir.String()
2176 if actual != expected {
2177 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2178 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002179
Jooyung Han91df2082019-11-20 01:49:42 +09002180 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2181 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2182 actual = flattened.installDir.String()
2183 if actual != expected {
2184 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2185 }
2186 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002187 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002188}
Jiyong Park67882562019-03-21 01:11:21 +09002189
Jooyung Han54aca7b2019-11-20 02:26:02 +09002190func TestFileContexts(t *testing.T) {
2191 ctx, _ := testApex(t, `
2192 apex {
2193 name: "myapex",
2194 key: "myapex.key",
2195 }
2196
2197 apex_key {
2198 name: "myapex.key",
2199 public_key: "testkey.avbpubkey",
2200 private_key: "testkey.pem",
2201 }
2202 `)
2203 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2204 apexRule := module.Rule("apexRule")
2205 actual := apexRule.Args["file_contexts"]
2206 expected := "system/sepolicy/apex/myapex-file_contexts"
2207 if actual != expected {
2208 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2209 }
2210
2211 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2212 apex {
2213 name: "myapex",
2214 key: "myapex.key",
2215 file_contexts: "my_own_file_contexts",
2216 }
2217
2218 apex_key {
2219 name: "myapex.key",
2220 public_key: "testkey.avbpubkey",
2221 private_key: "testkey.pem",
2222 }
2223 `, withFiles(map[string][]byte{
2224 "my_own_file_contexts": nil,
2225 }))
2226
2227 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2228 apex {
2229 name: "myapex",
2230 key: "myapex.key",
2231 product_specific: true,
2232 file_contexts: "product_specific_file_contexts",
2233 }
2234
2235 apex_key {
2236 name: "myapex.key",
2237 public_key: "testkey.avbpubkey",
2238 private_key: "testkey.pem",
2239 }
2240 `)
2241
2242 ctx, _ = testApex(t, `
2243 apex {
2244 name: "myapex",
2245 key: "myapex.key",
2246 product_specific: true,
2247 file_contexts: "product_specific_file_contexts",
2248 }
2249
2250 apex_key {
2251 name: "myapex.key",
2252 public_key: "testkey.avbpubkey",
2253 private_key: "testkey.pem",
2254 }
2255 `, withFiles(map[string][]byte{
2256 "product_specific_file_contexts": nil,
2257 }))
2258 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2259 apexRule = module.Rule("apexRule")
2260 actual = apexRule.Args["file_contexts"]
2261 expected = "product_specific_file_contexts"
2262 if actual != expected {
2263 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2264 }
2265
2266 ctx, _ = testApex(t, `
2267 apex {
2268 name: "myapex",
2269 key: "myapex.key",
2270 product_specific: true,
2271 file_contexts: ":my-file-contexts",
2272 }
2273
2274 apex_key {
2275 name: "myapex.key",
2276 public_key: "testkey.avbpubkey",
2277 private_key: "testkey.pem",
2278 }
2279
2280 filegroup {
2281 name: "my-file-contexts",
2282 srcs: ["product_specific_file_contexts"],
2283 }
2284 `, withFiles(map[string][]byte{
2285 "product_specific_file_contexts": nil,
2286 }))
2287 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2288 apexRule = module.Rule("apexRule")
2289 actual = apexRule.Args["file_contexts"]
2290 expected = "product_specific_file_contexts"
2291 if actual != expected {
2292 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2293 }
2294}
2295
Jiyong Park67882562019-03-21 01:11:21 +09002296func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002297 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002298 apex_key {
2299 name: "myapex.key",
2300 public_key: ":my.avbpubkey",
2301 private_key: ":my.pem",
2302 product_specific: true,
2303 }
2304
2305 filegroup {
2306 name: "my.avbpubkey",
2307 srcs: ["testkey2.avbpubkey"],
2308 }
2309
2310 filegroup {
2311 name: "my.pem",
2312 srcs: ["testkey2.pem"],
2313 }
2314 `)
2315
2316 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2317 expected_pubkey := "testkey2.avbpubkey"
2318 actual_pubkey := apex_key.public_key_file.String()
2319 if actual_pubkey != expected_pubkey {
2320 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2321 }
2322 expected_privkey := "testkey2.pem"
2323 actual_privkey := apex_key.private_key_file.String()
2324 if actual_privkey != expected_privkey {
2325 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2326 }
2327}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002328
2329func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002330 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002331 prebuilt_apex {
2332 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002333 arch: {
2334 arm64: {
2335 src: "myapex-arm64.apex",
2336 },
2337 arm: {
2338 src: "myapex-arm.apex",
2339 },
2340 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002341 }
2342 `)
2343
2344 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2345
Jiyong Parkc95714e2019-03-29 14:23:10 +09002346 expectedInput := "myapex-arm64.apex"
2347 if prebuilt.inputApex.String() != expectedInput {
2348 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2349 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002350}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002351
2352func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002353 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002354 prebuilt_apex {
2355 name: "myapex",
2356 src: "myapex-arm.apex",
2357 filename: "notmyapex.apex",
2358 }
2359 `)
2360
2361 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2362
2363 expected := "notmyapex.apex"
2364 if p.installFilename != expected {
2365 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2366 }
2367}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002368
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002369func TestPrebuiltOverrides(t *testing.T) {
2370 ctx, config := testApex(t, `
2371 prebuilt_apex {
2372 name: "myapex.prebuilt",
2373 src: "myapex-arm.apex",
2374 overrides: [
2375 "myapex",
2376 ],
2377 }
2378 `)
2379
2380 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2381
2382 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002383 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002384 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002385 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002386 }
2387}
2388
Roland Levillain630846d2019-06-26 12:48:34 +01002389func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002390 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002391 apex_test {
2392 name: "myapex",
2393 key: "myapex.key",
2394 tests: [
2395 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002396 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002397 ],
2398 }
2399
2400 apex_key {
2401 name: "myapex.key",
2402 public_key: "testkey.avbpubkey",
2403 private_key: "testkey.pem",
2404 }
2405
2406 cc_test {
2407 name: "mytest",
2408 gtest: false,
2409 srcs: ["mytest.cpp"],
2410 relative_install_path: "test",
2411 system_shared_libs: [],
2412 static_executable: true,
2413 stl: "none",
2414 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002415
2416 cc_test {
2417 name: "mytests",
2418 gtest: false,
2419 srcs: [
2420 "mytest1.cpp",
2421 "mytest2.cpp",
2422 "mytest3.cpp",
2423 ],
2424 test_per_src: true,
2425 relative_install_path: "test",
2426 system_shared_libs: [],
2427 static_executable: true,
2428 stl: "none",
2429 }
Roland Levillain630846d2019-06-26 12:48:34 +01002430 `)
2431
Sundong Ahnabb64432019-10-22 13:58:29 +09002432 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002433 copyCmds := apexRule.Args["copy_commands"]
2434
2435 // Ensure that test dep is copied into apex.
2436 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002437
2438 // Ensure that test deps built with `test_per_src` are copied into apex.
2439 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2440 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2441 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002442
2443 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002444 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002445 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2446 name := apexBundle.BaseModuleName()
2447 prefix := "TARGET_"
2448 var builder strings.Builder
2449 data.Custom(&builder, name, prefix, "", data)
2450 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002451 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2452 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2453 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2454 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
2455 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.json.myapex\n")
2456 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002457 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002458}
2459
Jooyung Han5c998b92019-06-27 11:30:33 +09002460func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002461 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002462 apex {
2463 name: "myapex",
2464 key: "myapex.key",
2465 native_shared_libs: ["mylib"],
2466 uses: ["commonapex"],
2467 }
2468
2469 apex {
2470 name: "commonapex",
2471 key: "myapex.key",
2472 native_shared_libs: ["libcommon"],
2473 provide_cpp_shared_libs: true,
2474 }
2475
2476 apex_key {
2477 name: "myapex.key",
2478 public_key: "testkey.avbpubkey",
2479 private_key: "testkey.pem",
2480 }
2481
2482 cc_library {
2483 name: "mylib",
2484 srcs: ["mylib.cpp"],
2485 shared_libs: ["libcommon"],
2486 system_shared_libs: [],
2487 stl: "none",
2488 }
2489
2490 cc_library {
2491 name: "libcommon",
2492 srcs: ["mylib_common.cpp"],
2493 system_shared_libs: [],
2494 stl: "none",
2495 }
2496 `)
2497
Sundong Ahnabb64432019-10-22 13:58:29 +09002498 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002499 apexRule1 := module1.Rule("apexRule")
2500 copyCmds1 := apexRule1.Args["copy_commands"]
2501
Sundong Ahnabb64432019-10-22 13:58:29 +09002502 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002503 apexRule2 := module2.Rule("apexRule")
2504 copyCmds2 := apexRule2.Args["copy_commands"]
2505
2506 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2507 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
2508 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2509 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2510 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2511}
2512
2513func TestApexUsesFailsIfNotProvided(t *testing.T) {
2514 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2515 apex {
2516 name: "myapex",
2517 key: "myapex.key",
2518 uses: ["commonapex"],
2519 }
2520
2521 apex {
2522 name: "commonapex",
2523 key: "myapex.key",
2524 }
2525
2526 apex_key {
2527 name: "myapex.key",
2528 public_key: "testkey.avbpubkey",
2529 private_key: "testkey.pem",
2530 }
2531 `)
2532 testApexError(t, `uses: "commonapex" is not a provider`, `
2533 apex {
2534 name: "myapex",
2535 key: "myapex.key",
2536 uses: ["commonapex"],
2537 }
2538
2539 cc_library {
2540 name: "commonapex",
2541 system_shared_libs: [],
2542 stl: "none",
2543 }
2544
2545 apex_key {
2546 name: "myapex.key",
2547 public_key: "testkey.avbpubkey",
2548 private_key: "testkey.pem",
2549 }
2550 `)
2551}
2552
2553func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2554 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2555 apex {
2556 name: "myapex",
2557 key: "myapex.key",
2558 use_vendor: true,
2559 uses: ["commonapex"],
2560 }
2561
2562 apex {
2563 name: "commonapex",
2564 key: "myapex.key",
2565 provide_cpp_shared_libs: true,
2566 }
2567
2568 apex_key {
2569 name: "myapex.key",
2570 public_key: "testkey.avbpubkey",
2571 private_key: "testkey.pem",
2572 }
Jooyung Handc782442019-11-01 03:14:38 +09002573 `, func(fs map[string][]byte, config android.Config) {
2574 setUseVendorWhitelistForTest(config, []string{"myapex"})
2575 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002576}
2577
Jooyung Hand48f3c32019-08-23 11:18:57 +09002578func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2579 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2580 apex {
2581 name: "myapex",
2582 key: "myapex.key",
2583 native_shared_libs: ["libfoo"],
2584 }
2585
2586 apex_key {
2587 name: "myapex.key",
2588 public_key: "testkey.avbpubkey",
2589 private_key: "testkey.pem",
2590 }
2591
2592 cc_library {
2593 name: "libfoo",
2594 stl: "none",
2595 system_shared_libs: [],
2596 enabled: false,
2597 }
2598 `)
2599 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2600 apex {
2601 name: "myapex",
2602 key: "myapex.key",
2603 java_libs: ["myjar"],
2604 }
2605
2606 apex_key {
2607 name: "myapex.key",
2608 public_key: "testkey.avbpubkey",
2609 private_key: "testkey.pem",
2610 }
2611
2612 java_library {
2613 name: "myjar",
2614 srcs: ["foo/bar/MyClass.java"],
2615 sdk_version: "none",
2616 system_modules: "none",
2617 compile_dex: true,
2618 enabled: false,
2619 }
2620 `)
2621}
2622
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002623func TestApexWithApps(t *testing.T) {
2624 ctx, _ := testApex(t, `
2625 apex {
2626 name: "myapex",
2627 key: "myapex.key",
2628 apps: [
2629 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002630 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002631 ],
2632 }
2633
2634 apex_key {
2635 name: "myapex.key",
2636 public_key: "testkey.avbpubkey",
2637 private_key: "testkey.pem",
2638 }
2639
2640 android_app {
2641 name: "AppFoo",
2642 srcs: ["foo/bar/MyClass.java"],
2643 sdk_version: "none",
2644 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002645 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002646 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002647
2648 android_app {
2649 name: "AppFooPriv",
2650 srcs: ["foo/bar/MyClass.java"],
2651 sdk_version: "none",
2652 system_modules: "none",
2653 privileged: true,
2654 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002655
2656 cc_library_shared {
2657 name: "libjni",
2658 srcs: ["mylib.cpp"],
2659 stl: "none",
2660 system_shared_libs: [],
2661 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002662 `)
2663
Sundong Ahnabb64432019-10-22 13:58:29 +09002664 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002665 apexRule := module.Rule("apexRule")
2666 copyCmds := apexRule.Args["copy_commands"]
2667
2668 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002669 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002670
2671 // JNI libraries are embedded inside APK
2672 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
2673 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_core_shared_myapex").Module().(*cc.Module).OutputFile()
2674 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2675 // ... uncompressed
2676 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2677 t.Errorf("jni lib is not uncompressed for AppFoo")
2678 }
2679 // ... and not directly inside the APEX
2680 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002681}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002682
Dario Frenicde2a032019-10-27 00:29:22 +01002683func TestApexWithAppImports(t *testing.T) {
2684 ctx, _ := testApex(t, `
2685 apex {
2686 name: "myapex",
2687 key: "myapex.key",
2688 apps: [
2689 "AppFooPrebuilt",
2690 "AppFooPrivPrebuilt",
2691 ],
2692 }
2693
2694 apex_key {
2695 name: "myapex.key",
2696 public_key: "testkey.avbpubkey",
2697 private_key: "testkey.pem",
2698 }
2699
2700 android_app_import {
2701 name: "AppFooPrebuilt",
2702 apk: "PrebuiltAppFoo.apk",
2703 presigned: true,
2704 dex_preopt: {
2705 enabled: false,
2706 },
2707 }
2708
2709 android_app_import {
2710 name: "AppFooPrivPrebuilt",
2711 apk: "PrebuiltAppFooPriv.apk",
2712 privileged: true,
2713 presigned: true,
2714 dex_preopt: {
2715 enabled: false,
2716 },
2717 }
2718 `)
2719
Sundong Ahnabb64432019-10-22 13:58:29 +09002720 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002721 apexRule := module.Rule("apexRule")
2722 copyCmds := apexRule.Args["copy_commands"]
2723
2724 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2725 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002726}
2727
Jooyung Han18020ea2019-11-13 10:50:48 +09002728func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2729 // libfoo's apex_available comes from cc_defaults
2730 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2731 apex {
2732 name: "myapex",
2733 key: "myapex.key",
2734 native_shared_libs: ["libfoo"],
2735 }
2736
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742
2743 apex {
2744 name: "otherapex",
2745 key: "myapex.key",
2746 native_shared_libs: ["libfoo"],
2747 }
2748
2749 cc_defaults {
2750 name: "libfoo-defaults",
2751 apex_available: ["otherapex"],
2752 }
2753
2754 cc_library {
2755 name: "libfoo",
2756 defaults: ["libfoo-defaults"],
2757 stl: "none",
2758 system_shared_libs: [],
2759 }`)
2760}
2761
Jiyong Park127b40b2019-09-30 16:04:35 +09002762func TestApexAvailable(t *testing.T) {
2763 // libfoo is not available to myapex, but only to otherapex
2764 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2765 apex {
2766 name: "myapex",
2767 key: "myapex.key",
2768 native_shared_libs: ["libfoo"],
2769 }
2770
2771 apex_key {
2772 name: "myapex.key",
2773 public_key: "testkey.avbpubkey",
2774 private_key: "testkey.pem",
2775 }
2776
2777 apex {
2778 name: "otherapex",
2779 key: "otherapex.key",
2780 native_shared_libs: ["libfoo"],
2781 }
2782
2783 apex_key {
2784 name: "otherapex.key",
2785 public_key: "testkey.avbpubkey",
2786 private_key: "testkey.pem",
2787 }
2788
2789 cc_library {
2790 name: "libfoo",
2791 stl: "none",
2792 system_shared_libs: [],
2793 apex_available: ["otherapex"],
2794 }`)
2795
2796 // libbar is an indirect dep
2797 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2798 apex {
2799 name: "myapex",
2800 key: "myapex.key",
2801 native_shared_libs: ["libfoo"],
2802 }
2803
2804 apex_key {
2805 name: "myapex.key",
2806 public_key: "testkey.avbpubkey",
2807 private_key: "testkey.pem",
2808 }
2809
2810 apex {
2811 name: "otherapex",
2812 key: "otherapex.key",
2813 native_shared_libs: ["libfoo"],
2814 }
2815
2816 apex_key {
2817 name: "otherapex.key",
2818 public_key: "testkey.avbpubkey",
2819 private_key: "testkey.pem",
2820 }
2821
2822 cc_library {
2823 name: "libfoo",
2824 stl: "none",
2825 shared_libs: ["libbar"],
2826 system_shared_libs: [],
2827 apex_available: ["myapex", "otherapex"],
2828 }
2829
2830 cc_library {
2831 name: "libbar",
2832 stl: "none",
2833 system_shared_libs: [],
2834 apex_available: ["otherapex"],
2835 }`)
2836
2837 testApexError(t, "\"otherapex\" is not a valid module name", `
2838 apex {
2839 name: "myapex",
2840 key: "myapex.key",
2841 native_shared_libs: ["libfoo"],
2842 }
2843
2844 apex_key {
2845 name: "myapex.key",
2846 public_key: "testkey.avbpubkey",
2847 private_key: "testkey.pem",
2848 }
2849
2850 cc_library {
2851 name: "libfoo",
2852 stl: "none",
2853 system_shared_libs: [],
2854 apex_available: ["otherapex"],
2855 }`)
2856
2857 ctx, _ := testApex(t, `
2858 apex {
2859 name: "myapex",
2860 key: "myapex.key",
2861 native_shared_libs: ["libfoo", "libbar"],
2862 }
2863
2864 apex_key {
2865 name: "myapex.key",
2866 public_key: "testkey.avbpubkey",
2867 private_key: "testkey.pem",
2868 }
2869
2870 cc_library {
2871 name: "libfoo",
2872 stl: "none",
2873 system_shared_libs: [],
2874 apex_available: ["myapex"],
2875 }
2876
2877 cc_library {
2878 name: "libbar",
2879 stl: "none",
2880 system_shared_libs: [],
2881 apex_available: ["//apex_available:anyapex"],
2882 }`)
2883
2884 // check that libfoo and libbar are created only for myapex, but not for the platform
2885 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2886 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2887 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared_myapex")
2888 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared")
2889
2890 ctx, _ = testApex(t, `
2891 apex {
2892 name: "myapex",
2893 key: "myapex.key",
2894 }
2895
2896 apex_key {
2897 name: "myapex.key",
2898 public_key: "testkey.avbpubkey",
2899 private_key: "testkey.pem",
2900 }
2901
2902 cc_library {
2903 name: "libfoo",
2904 stl: "none",
2905 system_shared_libs: [],
2906 apex_available: ["//apex_available:platform"],
2907 }`)
2908
2909 // check that libfoo is created only for the platform
2910 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2911 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002912
2913 ctx, _ = testApex(t, `
2914 apex {
2915 name: "myapex",
2916 key: "myapex.key",
2917 native_shared_libs: ["libfoo"],
2918 }
2919
2920 apex_key {
2921 name: "myapex.key",
2922 public_key: "testkey.avbpubkey",
2923 private_key: "testkey.pem",
2924 }
2925
2926 cc_library {
2927 name: "libfoo",
2928 stl: "none",
2929 system_shared_libs: [],
2930 apex_available: ["myapex"],
2931 static: {
2932 apex_available: ["//apex_available:platform"],
2933 },
2934 }`)
2935
2936 // shared variant of libfoo is only available to myapex
2937 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2938 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2939 // but the static variant is available to both myapex and the platform
2940 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static_myapex")
2941 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09002942}
2943
Jiyong Park5d790c32019-11-15 18:40:32 +09002944func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002945 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09002946 apex {
2947 name: "myapex",
2948 key: "myapex.key",
2949 apps: ["app"],
2950 }
2951
2952 override_apex {
2953 name: "override_myapex",
2954 base: "myapex",
2955 apps: ["override_app"],
2956 }
2957
2958 apex_key {
2959 name: "myapex.key",
2960 public_key: "testkey.avbpubkey",
2961 private_key: "testkey.pem",
2962 }
2963
2964 android_app {
2965 name: "app",
2966 srcs: ["foo/bar/MyClass.java"],
2967 package_name: "foo",
2968 sdk_version: "none",
2969 system_modules: "none",
2970 }
2971
2972 override_android_app {
2973 name: "override_app",
2974 base: "app",
2975 package_name: "bar",
2976 }
2977 `)
2978
Jiyong Park317645e2019-12-05 13:20:58 +09002979 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
2980 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
2981 if originalVariant.GetOverriddenBy() != "" {
2982 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
2983 }
2984 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
2985 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
2986 }
2987
Jiyong Park5d790c32019-11-15 18:40:32 +09002988 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
2989 apexRule := module.Rule("apexRule")
2990 copyCmds := apexRule.Args["copy_commands"]
2991
2992 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
2993 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002994
2995 apexBundle := module.Module().(*apexBundle)
2996 name := apexBundle.Name()
2997 if name != "override_myapex" {
2998 t.Errorf("name should be \"override_myapex\", but was %q", name)
2999 }
3000
3001 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3002 var builder strings.Builder
3003 data.Custom(&builder, name, "TARGET_", "", data)
3004 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003005 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003006 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3007 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3008 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003009 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003010 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3011 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003012}
3013
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003014func TestMain(m *testing.M) {
3015 run := func() int {
3016 setUp()
3017 defer tearDown()
3018
3019 return m.Run()
3020 }
3021
3022 os.Exit(run())
3023}