blob: 6cf43373f79f374c359b4bc51963355485192fee [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070095 config := android.TestArchConfig(buildDir, nil)
96 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
97 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
98 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
99 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
100 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jooyung Han344d5432019-08-23 11:17:39 +0900101 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900102
103 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -0800104 ctx.RegisterModuleType("apex", BundleFactory)
105 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
106 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
107 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
108 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
109 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
110 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111
Colin Cross4b49b762019-11-22 15:25:03 -0800112 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
113 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
114 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
115 ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory)
116 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
117 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
118 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
119 ctx.RegisterModuleType("cc_defaults", func() android.Module {
Jooyung Han18020ea2019-11-13 10:50:48 +0900120 return cc.DefaultsFactory()
Colin Cross4b49b762019-11-22 15:25:03 -0800121 })
122 ctx.RegisterModuleType("cc_test", cc.TestFactory)
123 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
124 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
125 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
126 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
127 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
128 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
129 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
130 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
131 ctx.RegisterModuleType("java_library", java.LibraryFactory)
132 ctx.RegisterModuleType("java_import", java.ImportFactory)
133 ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory)
134 ctx.RegisterModuleType("android_app", java.AndroidAppFactory)
135 ctx.RegisterModuleType("android_app_import", java.AndroidAppImportFactory)
136 ctx.RegisterModuleType("override_android_app", java.OverrideAndroidAppModuleFactory)
Jiyong Park7f7766d2019-07-25 22:02:35 +0900137
Jooyung Han344d5432019-08-23 11:17:39 +0900138 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700139 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
140 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
141 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900142 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900143 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jooyung Han214bf372019-11-12 13:03:50 +0900144 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100145 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900146 ctx.BottomUp("version", cc.VersionMutator).Parallel()
147 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
Jooyung Han344d5432019-08-23 11:17:39 +0900148 })
Jooyung Han31c470b2019-10-18 16:26:59 +0900149 ctx.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Park5d790c32019-11-15 18:40:32 +0900150 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Jooyung Han31c470b2019-10-18 16:26:59 +0900151 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han344d5432019-08-23 11:17:39 +0900152 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900153 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
154 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900155 })
156
157 ctx.Register()
158
159 bp = bp + `
160 toolchain_library {
161 name: "libcompiler_rt-extras",
162 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900165 }
166
167 toolchain_library {
168 name: "libatomic",
169 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900170 vendor_available: true,
171 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900172 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900173 }
174
175 toolchain_library {
176 name: "libgcc",
177 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900178 vendor_available: true,
179 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900180 }
181
182 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700183 name: "libgcc_stripped",
184 src: "",
185 vendor_available: true,
186 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900187 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700188 }
189
190 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900191 name: "libclang_rt.builtins-aarch64-android",
192 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900193 vendor_available: true,
194 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900196 }
197
198 toolchain_library {
199 name: "libclang_rt.builtins-arm-android",
200 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900201 vendor_available: true,
202 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900203 native_bridge_supported: true,
204 }
205
206 toolchain_library {
207 name: "libclang_rt.builtins-x86_64-android",
208 src: "",
209 vendor_available: true,
210 recovery_available: true,
211 native_bridge_supported: true,
212 }
213
214 toolchain_library {
215 name: "libclang_rt.builtins-i686-android",
216 src: "",
217 vendor_available: true,
218 recovery_available: true,
219 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220 }
221
222 cc_object {
223 name: "crtbegin_so",
224 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900225 vendor_available: true,
226 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900227 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 }
229
230 cc_object {
231 name: "crtend_so",
232 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900233 vendor_available: true,
234 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900235 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236 }
237
Alex Light3d673592019-01-18 14:37:31 -0800238 cc_object {
239 name: "crtbegin_static",
240 stl: "none",
241 }
242
243 cc_object {
244 name: "crtend_android",
245 stl: "none",
246 }
247
Jiyong Parkda6eb592018-12-19 17:12:36 +0900248 llndk_library {
249 name: "libc",
250 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900251 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900252 }
253
254 llndk_library {
255 name: "libm",
256 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900257 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900258 }
259
260 llndk_library {
261 name: "libdl",
262 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900263 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900264 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900265
266 filegroup {
267 name: "myapex-file_contexts",
268 srcs: [
269 "system/sepolicy/apex/myapex-file_contexts",
270 ],
271 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 `
Dario Frenicde2a032019-10-27 00:29:22 +0100273 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274
Jooyung Han344d5432019-08-23 11:17:39 +0900275 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900276 "Android.bp": []byte(bp),
277 "a.java": nil,
278 "PrebuiltAppFoo.apk": nil,
279 "PrebuiltAppFooPriv.apk": nil,
280 "build/make/target/product/security": nil,
281 "apex_manifest.json": nil,
282 "AndroidManifest.xml": nil,
283 "system/sepolicy/apex/myapex-file_contexts": nil,
284 "system/sepolicy/apex/otherapex-file_contexts": nil,
285 "system/sepolicy/apex/commonapex-file_contexts": nil,
286 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
287 "mylib.cpp": nil,
288 "mylib_common.cpp": nil,
289 "mytest.cpp": nil,
290 "mytest1.cpp": nil,
291 "mytest2.cpp": nil,
292 "mytest3.cpp": nil,
293 "myprebuilt": nil,
294 "my_include": nil,
295 "foo/bar/MyClass.java": nil,
296 "prebuilt.jar": nil,
297 "vendor/foo/devkeys/test.x509.pem": nil,
298 "vendor/foo/devkeys/test.pk8": nil,
299 "testkey.x509.pem": nil,
300 "testkey.pk8": nil,
301 "testkey.override.x509.pem": nil,
302 "testkey.override.pk8": nil,
303 "vendor/foo/devkeys/testkey.avbpubkey": nil,
304 "vendor/foo/devkeys/testkey.pem": nil,
305 "NOTICE": nil,
306 "custom_notice": nil,
307 "testkey2.avbpubkey": nil,
308 "testkey2.pem": nil,
309 "myapex-arm64.apex": nil,
310 "myapex-arm.apex": nil,
311 "frameworks/base/api/current.txt": nil,
312 "framework/aidl/a.aidl": nil,
313 "build/make/core/proguard.flags": nil,
314 "build/make/core/proguard_basic_keeps.flags": nil,
315 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900316 }
317
318 for _, handler := range handlers {
319 handler(fs, config)
320 }
321
322 ctx.MockFileSystem(fs)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323
Jooyung Han5c998b92019-06-27 11:30:33 +0900324 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325}
326
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700327func setUp() {
328 var err error
329 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700331 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333}
334
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700335func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900336 os.RemoveAll(buildDir)
337}
338
339// ensure that 'result' contains 'expected'
340func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if !strings.Contains(result, expected) {
343 t.Errorf("%q is not found in %q", expected, result)
344 }
345}
346
347// ensures that 'result' does not contain 'notExpected'
348func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900349 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900350 if strings.Contains(result, notExpected) {
351 t.Errorf("%q is found in %q", notExpected, result)
352 }
353}
354
355func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900356 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900357 if !android.InList(expected, result) {
358 t.Errorf("%q is not found in %v", expected, result)
359 }
360}
361
362func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900363 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 if android.InList(notExpected, result) {
365 t.Errorf("%q is found in %v", notExpected, result)
366 }
367}
368
Jooyung Hane1633032019-08-01 17:41:43 +0900369func ensureListEmpty(t *testing.T, result []string) {
370 t.Helper()
371 if len(result) > 0 {
372 t.Errorf("%q is expected to be empty", result)
373 }
374}
375
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376// Minimal test
377func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700378 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900379 apex_defaults {
380 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900381 manifest: ":myapex.manifest",
382 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 key: "myapex.key",
384 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800385 multilib: {
386 both: {
387 binaries: ["foo",],
388 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900389 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900390 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900391 }
392
Jiyong Park30ca9372019-02-07 16:27:23 +0900393 apex {
394 name: "myapex",
395 defaults: ["myapex-defaults"],
396 }
397
Jiyong Park25fc6a92018-11-18 18:02:45 +0900398 apex_key {
399 name: "myapex.key",
400 public_key: "testkey.avbpubkey",
401 private_key: "testkey.pem",
402 }
403
Jiyong Park809bb722019-02-13 21:33:49 +0900404 filegroup {
405 name: "myapex.manifest",
406 srcs: ["apex_manifest.json"],
407 }
408
409 filegroup {
410 name: "myapex.androidmanifest",
411 srcs: ["AndroidManifest.xml"],
412 }
413
Jiyong Park25fc6a92018-11-18 18:02:45 +0900414 cc_library {
415 name: "mylib",
416 srcs: ["mylib.cpp"],
417 shared_libs: ["mylib2"],
418 system_shared_libs: [],
419 stl: "none",
420 }
421
Alex Light3d673592019-01-18 14:37:31 -0800422 cc_binary {
423 name: "foo",
424 srcs: ["mylib.cpp"],
425 compile_multilib: "both",
426 multilib: {
427 lib32: {
428 suffix: "32",
429 },
430 lib64: {
431 suffix: "64",
432 },
433 },
434 symlinks: ["foo_link_"],
435 symlink_preferred_arch: true,
436 system_shared_libs: [],
437 static_executable: true,
438 stl: "none",
439 }
440
Jiyong Park25fc6a92018-11-18 18:02:45 +0900441 cc_library {
442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900446 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900447 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900448
449 java_library {
450 name: "myjar",
451 srcs: ["foo/bar/MyClass.java"],
452 sdk_version: "none",
453 system_modules: "none",
454 compile_dex: true,
455 static_libs: ["myotherjar"],
456 }
457
458 java_library {
459 name: "myotherjar",
460 srcs: ["foo/bar/MyClass.java"],
461 sdk_version: "none",
462 system_modules: "none",
463 compile_dex: true,
464 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900465
466 java_import {
467 name: "myprebuiltjar",
468 jars: ["prebuilt.jar"],
469 installable: true,
470 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471 `)
472
Sundong Ahnabb64432019-10-22 13:58:29 +0900473 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900474
475 optFlags := apexRule.Args["opt_flags"]
476 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700477 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900478 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900479
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480 copyCmds := apexRule.Args["copy_commands"]
481
482 // Ensure that main rule creates an output
483 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
484
485 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800486 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_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
Colin Cross7113d202019-11-20 16:39:12 -0800491 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_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
Colin Cross7113d202019-11-20 16:39:12 -0800502 // Ensure that the platform variant ends with _shared or _common
503 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
504 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_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")
Jooyung Han214bf372019-11-12 13:03:50 +0900550 args := module.Rule("apexRule").Args
551 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
552 t.Error("manifest should be apex_manifest.pb, but " + manifest)
553 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900554}
555
Alex Light5098a612018-11-29 17:12:15 -0800556func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700557 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800558 apex {
559 name: "myapex",
560 key: "myapex.key",
561 payload_type: "zip",
562 native_shared_libs: ["mylib"],
563 }
564
565 apex_key {
566 name: "myapex.key",
567 public_key: "testkey.avbpubkey",
568 private_key: "testkey.pem",
569 }
570
571 cc_library {
572 name: "mylib",
573 srcs: ["mylib.cpp"],
574 shared_libs: ["mylib2"],
575 system_shared_libs: [],
576 stl: "none",
577 }
578
579 cc_library {
580 name: "mylib2",
581 srcs: ["mylib.cpp"],
582 system_shared_libs: [],
583 stl: "none",
584 }
585 `)
586
Sundong Ahnabb64432019-10-22 13:58:29 +0900587 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800588 copyCmds := zipApexRule.Args["copy_commands"]
589
590 // Ensure that main rule creates an output
591 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
592
593 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800594 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800595
596 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800597 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800598
599 // Ensure that both direct and indirect deps are copied into apex
600 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
601 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900602}
603
604func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700605 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900606 apex {
607 name: "myapex",
608 key: "myapex.key",
609 native_shared_libs: ["mylib", "mylib3"],
610 }
611
612 apex_key {
613 name: "myapex.key",
614 public_key: "testkey.avbpubkey",
615 private_key: "testkey.pem",
616 }
617
618 cc_library {
619 name: "mylib",
620 srcs: ["mylib.cpp"],
621 shared_libs: ["mylib2", "mylib3"],
622 system_shared_libs: [],
623 stl: "none",
624 }
625
626 cc_library {
627 name: "mylib2",
628 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900629 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900630 system_shared_libs: [],
631 stl: "none",
632 stubs: {
633 versions: ["1", "2", "3"],
634 },
635 }
636
637 cc_library {
638 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900639 srcs: ["mylib.cpp"],
640 shared_libs: ["mylib4"],
641 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900642 stl: "none",
643 stubs: {
644 versions: ["10", "11", "12"],
645 },
646 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900647
648 cc_library {
649 name: "mylib4",
650 srcs: ["mylib.cpp"],
651 system_shared_libs: [],
652 stl: "none",
653 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900654 `)
655
Sundong Ahnabb64432019-10-22 13:58:29 +0900656 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900657 copyCmds := apexRule.Args["copy_commands"]
658
659 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800660 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900661
662 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800663 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900664
665 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800666 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900667
Colin Cross7113d202019-11-20 16:39:12 -0800668 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669
670 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800671 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800673 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674
675 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800676 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800678 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900679
680 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800681 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900682 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900683
684 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800685 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686}
687
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900688func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700689 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900690 apex {
691 name: "myapex",
692 key: "myapex.key",
693 native_shared_libs: ["mylib"],
694 }
695
696 apex_key {
697 name: "myapex.key",
698 public_key: "testkey.avbpubkey",
699 private_key: "testkey.pem",
700 }
701
702 cc_library {
703 name: "mylib",
704 srcs: ["mylib.cpp"],
705 shared_libs: ["libfoo#10"],
706 system_shared_libs: [],
707 stl: "none",
708 }
709
710 cc_library {
711 name: "libfoo",
712 srcs: ["mylib.cpp"],
713 shared_libs: ["libbar"],
714 system_shared_libs: [],
715 stl: "none",
716 stubs: {
717 versions: ["10", "20", "30"],
718 },
719 }
720
721 cc_library {
722 name: "libbar",
723 srcs: ["mylib.cpp"],
724 system_shared_libs: [],
725 stl: "none",
726 }
727
728 `)
729
Sundong Ahnabb64432019-10-22 13:58:29 +0900730 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900731 copyCmds := apexRule.Args["copy_commands"]
732
733 // Ensure that direct non-stubs dep is always included
734 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
735
736 // Ensure that indirect stubs dep is not included
737 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
738
739 // Ensure that dependency of stubs is not included
740 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
741
Colin Cross7113d202019-11-20 16:39:12 -0800742 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900743
744 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800745 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900746 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800747 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900748
Colin Cross7113d202019-11-20 16:39:12 -0800749 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900750
751 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
752 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
753}
754
Jooyung Hand3639552019-08-09 12:57:43 +0900755func TestApexWithRuntimeLibsDependency(t *testing.T) {
756 /*
757 myapex
758 |
759 v (runtime_libs)
760 mylib ------+------> libfoo [provides stub]
761 |
762 `------> libbar
763 */
764 ctx, _ := testApex(t, `
765 apex {
766 name: "myapex",
767 key: "myapex.key",
768 native_shared_libs: ["mylib"],
769 }
770
771 apex_key {
772 name: "myapex.key",
773 public_key: "testkey.avbpubkey",
774 private_key: "testkey.pem",
775 }
776
777 cc_library {
778 name: "mylib",
779 srcs: ["mylib.cpp"],
780 runtime_libs: ["libfoo", "libbar"],
781 system_shared_libs: [],
782 stl: "none",
783 }
784
785 cc_library {
786 name: "libfoo",
787 srcs: ["mylib.cpp"],
788 system_shared_libs: [],
789 stl: "none",
790 stubs: {
791 versions: ["10", "20", "30"],
792 },
793 }
794
795 cc_library {
796 name: "libbar",
797 srcs: ["mylib.cpp"],
798 system_shared_libs: [],
799 stl: "none",
800 }
801
802 `)
803
Sundong Ahnabb64432019-10-22 13:58:29 +0900804 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900805 copyCmds := apexRule.Args["copy_commands"]
806
807 // Ensure that direct non-stubs dep is always included
808 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
809
810 // Ensure that indirect stubs dep is not included
811 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
812
813 // Ensure that runtime_libs dep in included
814 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
815
Sundong Ahnabb64432019-10-22 13:58:29 +0900816 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900817 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
818 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900819
820}
821
Jooyung Han9c80bae2019-08-20 17:30:57 +0900822func TestApexDependencyToLLNDK(t *testing.T) {
823 ctx, _ := testApex(t, `
824 apex {
825 name: "myapex",
826 key: "myapex.key",
827 use_vendor: true,
828 native_shared_libs: ["mylib"],
829 }
830
831 apex_key {
832 name: "myapex.key",
833 public_key: "testkey.avbpubkey",
834 private_key: "testkey.pem",
835 }
836
837 cc_library {
838 name: "mylib",
839 srcs: ["mylib.cpp"],
840 vendor_available: true,
841 shared_libs: ["libbar"],
842 system_shared_libs: [],
843 stl: "none",
844 }
845
846 cc_library {
847 name: "libbar",
848 srcs: ["mylib.cpp"],
849 system_shared_libs: [],
850 stl: "none",
851 }
852
853 llndk_library {
854 name: "libbar",
855 symbol_file: "",
856 }
Jooyung Handc782442019-11-01 03:14:38 +0900857 `, func(fs map[string][]byte, config android.Config) {
858 setUseVendorWhitelistForTest(config, []string{"myapex"})
859 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900860
Sundong Ahnabb64432019-10-22 13:58:29 +0900861 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900862 copyCmds := apexRule.Args["copy_commands"]
863
864 // Ensure that LLNDK dep is not included
865 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
866
Sundong Ahnabb64432019-10-22 13:58:29 +0900867 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900868 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900869
870 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900871 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900872
873}
874
Jiyong Park25fc6a92018-11-18 18:02:45 +0900875func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700876 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
881 }
882
883 apex_key {
884 name: "myapex.key",
885 public_key: "testkey.avbpubkey",
886 private_key: "testkey.pem",
887 }
888
889 cc_library {
890 name: "mylib",
891 srcs: ["mylib.cpp"],
892 shared_libs: ["libdl#27"],
893 stl: "none",
894 }
895
896 cc_library_shared {
897 name: "mylib_shared",
898 srcs: ["mylib.cpp"],
899 shared_libs: ["libdl#27"],
900 stl: "none",
901 }
902
903 cc_library {
904 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700905 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900906 nocrt: true,
907 system_shared_libs: [],
908 stl: "none",
909 stubs: {
910 versions: ["27", "28", "29"],
911 },
912 }
913
914 cc_library {
915 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700916 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900917 nocrt: true,
918 system_shared_libs: [],
919 stl: "none",
920 stubs: {
921 versions: ["27", "28", "29"],
922 },
923 }
924
925 cc_library {
926 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700927 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 nocrt: true,
929 system_shared_libs: [],
930 stl: "none",
931 stubs: {
932 versions: ["27", "28", "29"],
933 },
934 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900935
936 cc_library {
937 name: "libBootstrap",
938 srcs: ["mylib.cpp"],
939 stl: "none",
940 bootstrap: true,
941 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900942 `)
943
Sundong Ahnabb64432019-10-22 13:58:29 +0900944 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900945 copyCmds := apexRule.Args["copy_commands"]
946
947 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800948 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900949 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
950 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900951
952 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900953 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900954
Colin Cross7113d202019-11-20 16:39:12 -0800955 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
956 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
957 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900958
959 // For dependency to libc
960 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800961 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800963 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 // ... Cflags from stub is correctly exported to mylib
965 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
966 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
967
968 // For dependency to libm
969 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800970 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -0800972 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973 // ... and is not compiling with the stub
974 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
975 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
976
977 // For dependency to libdl
978 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800979 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800981 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
982 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900983 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800984 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985 // ... Cflags from stub is correctly exported to mylib
986 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
987 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900988
989 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800990 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
991 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
992 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
993 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900994}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900995
996func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700997 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +0900998 apex {
999 name: "myapex",
1000 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001001 native_shared_libs: ["mylib"],
1002 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001003 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001004 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001005 }
1006
1007 apex_key {
1008 name: "myapex.key",
1009 public_key: "testkey.avbpubkey",
1010 private_key: "testkey.pem",
1011 }
1012
1013 prebuilt_etc {
1014 name: "myetc",
1015 src: "myprebuilt",
1016 sub_dir: "foo/bar",
1017 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001018
1019 cc_library {
1020 name: "mylib",
1021 srcs: ["mylib.cpp"],
1022 relative_install_path: "foo/bar",
1023 system_shared_libs: [],
1024 stl: "none",
1025 }
1026
1027 cc_binary {
1028 name: "mybin",
1029 srcs: ["mylib.cpp"],
1030 relative_install_path: "foo/bar",
1031 system_shared_libs: [],
1032 static_executable: true,
1033 stl: "none",
1034 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001035 `)
1036
Sundong Ahnabb64432019-10-22 13:58:29 +09001037 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001038 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1039
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001040 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001041 ensureListContains(t, dirs, "etc")
1042 ensureListContains(t, dirs, "etc/foo")
1043 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001044 ensureListContains(t, dirs, "lib64")
1045 ensureListContains(t, dirs, "lib64/foo")
1046 ensureListContains(t, dirs, "lib64/foo/bar")
1047 ensureListContains(t, dirs, "lib")
1048 ensureListContains(t, dirs, "lib/foo")
1049 ensureListContains(t, dirs, "lib/foo/bar")
1050
Jiyong Parkbd13e442019-03-15 18:10:35 +09001051 ensureListContains(t, dirs, "bin")
1052 ensureListContains(t, dirs, "bin/foo")
1053 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001054}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001055
1056func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001057 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001058 apex {
1059 name: "myapex",
1060 key: "myapex.key",
1061 native_shared_libs: ["mylib"],
1062 use_vendor: true,
1063 }
1064
1065 apex_key {
1066 name: "myapex.key",
1067 public_key: "testkey.avbpubkey",
1068 private_key: "testkey.pem",
1069 }
1070
1071 cc_library {
1072 name: "mylib",
1073 srcs: ["mylib.cpp"],
1074 shared_libs: ["mylib2"],
1075 system_shared_libs: [],
1076 vendor_available: true,
1077 stl: "none",
1078 }
1079
1080 cc_library {
1081 name: "mylib2",
1082 srcs: ["mylib.cpp"],
1083 system_shared_libs: [],
1084 vendor_available: true,
1085 stl: "none",
1086 }
Jooyung Handc782442019-11-01 03:14:38 +09001087 `, func(fs map[string][]byte, config android.Config) {
1088 setUseVendorWhitelistForTest(config, []string{"myapex"})
1089 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001090
1091 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001092 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001093 for _, implicit := range i.Implicits {
1094 inputsList = append(inputsList, implicit.String())
1095 }
1096 }
1097 inputsString := strings.Join(inputsList, " ")
1098
1099 // ensure that the apex includes vendor variants of the direct and indirect deps
Inseob Kim64c43952019-08-26 16:52:35 +09001100 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib.so")
1101 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001102
1103 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001104 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1105 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001106}
Jiyong Park16e91a02018-12-20 18:18:08 +09001107
Jooyung Handc782442019-11-01 03:14:38 +09001108func TestUseVendorRestriction(t *testing.T) {
1109 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1110 apex {
1111 name: "myapex",
1112 key: "myapex.key",
1113 use_vendor: true,
1114 }
1115 apex_key {
1116 name: "myapex.key",
1117 public_key: "testkey.avbpubkey",
1118 private_key: "testkey.pem",
1119 }
1120 `, func(fs map[string][]byte, config android.Config) {
1121 setUseVendorWhitelistForTest(config, []string{""})
1122 })
1123 // no error with whitelist
1124 testApex(t, `
1125 apex {
1126 name: "myapex",
1127 key: "myapex.key",
1128 use_vendor: true,
1129 }
1130 apex_key {
1131 name: "myapex.key",
1132 public_key: "testkey.avbpubkey",
1133 private_key: "testkey.pem",
1134 }
1135 `, func(fs map[string][]byte, config android.Config) {
1136 setUseVendorWhitelistForTest(config, []string{"myapex"})
1137 })
1138}
1139
Jooyung Han5c998b92019-06-27 11:30:33 +09001140func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1141 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1142 apex {
1143 name: "myapex",
1144 key: "myapex.key",
1145 native_shared_libs: ["mylib"],
1146 use_vendor: true,
1147 }
1148
1149 apex_key {
1150 name: "myapex.key",
1151 public_key: "testkey.avbpubkey",
1152 private_key: "testkey.pem",
1153 }
1154
1155 cc_library {
1156 name: "mylib",
1157 srcs: ["mylib.cpp"],
1158 system_shared_libs: [],
1159 stl: "none",
1160 }
1161 `)
1162}
1163
Jiyong Park16e91a02018-12-20 18:18:08 +09001164func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001165 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001166 apex {
1167 name: "myapex",
1168 key: "myapex.key",
1169 native_shared_libs: ["mylib"],
1170 }
1171
1172 apex_key {
1173 name: "myapex.key",
1174 public_key: "testkey.avbpubkey",
1175 private_key: "testkey.pem",
1176 }
1177
1178 cc_library {
1179 name: "mylib",
1180 srcs: ["mylib.cpp"],
1181 system_shared_libs: [],
1182 stl: "none",
1183 stubs: {
1184 versions: ["1", "2", "3"],
1185 },
1186 }
1187
1188 cc_binary {
1189 name: "not_in_apex",
1190 srcs: ["mylib.cpp"],
1191 static_libs: ["mylib"],
1192 static_executable: true,
1193 system_shared_libs: [],
1194 stl: "none",
1195 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001196 `)
1197
Colin Cross7113d202019-11-20 16:39:12 -08001198 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001199
1200 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001201 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001202}
Jiyong Park9335a262018-12-24 11:31:58 +09001203
1204func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001205 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001206 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001207 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001208 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001209 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001210 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001211 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001212 }
1213
1214 cc_library {
1215 name: "mylib",
1216 srcs: ["mylib.cpp"],
1217 system_shared_libs: [],
1218 stl: "none",
1219 }
1220
1221 apex_key {
1222 name: "myapex.key",
1223 public_key: "testkey.avbpubkey",
1224 private_key: "testkey.pem",
1225 }
1226
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001227 android_app_certificate {
1228 name: "myapex.certificate",
1229 certificate: "testkey",
1230 }
1231
1232 android_app_certificate {
1233 name: "myapex.certificate.override",
1234 certificate: "testkey.override",
1235 }
1236
Jiyong Park9335a262018-12-24 11:31:58 +09001237 `)
1238
1239 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001240 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001241
1242 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1243 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1244 "vendor/foo/devkeys/testkey.avbpubkey")
1245 }
1246 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1247 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1248 "vendor/foo/devkeys/testkey.pem")
1249 }
1250
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001251 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001252 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001253 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001254 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001255 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001256 }
1257}
Jiyong Park58e364a2019-01-19 19:24:06 +09001258
1259func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001260 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001261 apex {
1262 name: "myapex",
1263 key: "myapex.key",
1264 native_shared_libs: ["mylib"],
1265 }
1266
1267 apex {
1268 name: "otherapex",
1269 key: "myapex.key",
1270 native_shared_libs: ["mylib"],
1271 }
1272
1273 apex_key {
1274 name: "myapex.key",
1275 public_key: "testkey.avbpubkey",
1276 private_key: "testkey.pem",
1277 }
1278
1279 cc_library {
1280 name: "mylib",
1281 srcs: ["mylib.cpp"],
1282 system_shared_libs: [],
1283 stl: "none",
1284 }
1285 `)
1286
Jooyung Han6b8459b2019-10-30 08:29:25 +09001287 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001288 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001289 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001290 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1291 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001292
Jooyung Han6b8459b2019-10-30 08:29:25 +09001293 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001294 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001295 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001296 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1297 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001298
Jooyung Han6b8459b2019-10-30 08:29:25 +09001299 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001300 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001301 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001302 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1303 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001304}
Jiyong Park7e636d02019-01-28 16:16:54 +09001305
1306func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001307 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001308 apex {
1309 name: "myapex",
1310 key: "myapex.key",
1311 native_shared_libs: ["mylib"],
1312 }
1313
1314 apex_key {
1315 name: "myapex.key",
1316 public_key: "testkey.avbpubkey",
1317 private_key: "testkey.pem",
1318 }
1319
1320 cc_library_headers {
1321 name: "mylib_headers",
1322 export_include_dirs: ["my_include"],
1323 system_shared_libs: [],
1324 stl: "none",
1325 }
1326
1327 cc_library {
1328 name: "mylib",
1329 srcs: ["mylib.cpp"],
1330 system_shared_libs: [],
1331 stl: "none",
1332 header_libs: ["mylib_headers"],
1333 export_header_lib_headers: ["mylib_headers"],
1334 stubs: {
1335 versions: ["1", "2", "3"],
1336 },
1337 }
1338
1339 cc_library {
1340 name: "otherlib",
1341 srcs: ["mylib.cpp"],
1342 system_shared_libs: [],
1343 stl: "none",
1344 shared_libs: ["mylib"],
1345 }
1346 `)
1347
Colin Cross7113d202019-11-20 16:39:12 -08001348 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001349
1350 // Ensure that the include path of the header lib is exported to 'otherlib'
1351 ensureContains(t, cFlags, "-Imy_include")
1352}
Alex Light9670d332019-01-29 18:07:33 -08001353
Jooyung Han31c470b2019-10-18 16:26:59 +09001354func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1355 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001356 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001357 copyCmds := apexRule.Args["copy_commands"]
1358 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001359 var failed bool
1360 var surplus []string
1361 filesMatched := make(map[string]bool)
1362 addContent := func(content string) {
1363 for _, expected := range files {
1364 if matched, _ := path.Match(expected, content); matched {
1365 filesMatched[expected] = true
1366 return
1367 }
1368 }
1369 surplus = append(surplus, content)
1370 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001371 for _, cmd := range strings.Split(copyCmds, "&&") {
1372 cmd = strings.TrimSpace(cmd)
1373 if cmd == "" {
1374 continue
1375 }
1376 terms := strings.Split(cmd, " ")
1377 switch terms[0] {
1378 case "mkdir":
1379 case "cp":
1380 if len(terms) != 3 {
1381 t.Fatal("copyCmds contains invalid cp command", cmd)
1382 }
1383 dst := terms[2]
1384 index := strings.Index(dst, imageApexDir)
1385 if index == -1 {
1386 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1387 }
1388 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001389 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001390 default:
1391 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1392 }
1393 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001394
Jooyung Han31c470b2019-10-18 16:26:59 +09001395 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001396 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001397 t.Log("surplus files", surplus)
1398 failed = true
1399 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001400
1401 if len(files) > len(filesMatched) {
1402 var missing []string
1403 for _, expected := range files {
1404 if !filesMatched[expected] {
1405 missing = append(missing, expected)
1406 }
1407 }
1408 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001409 t.Log("missing files", missing)
1410 failed = true
1411 }
1412 if failed {
1413 t.Fail()
1414 }
1415}
1416
Jooyung Han344d5432019-08-23 11:17:39 +09001417func TestVndkApexCurrent(t *testing.T) {
1418 ctx, _ := testApex(t, `
1419 apex_vndk {
1420 name: "myapex",
1421 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001422 }
1423
1424 apex_key {
1425 name: "myapex.key",
1426 public_key: "testkey.avbpubkey",
1427 private_key: "testkey.pem",
1428 }
1429
1430 cc_library {
1431 name: "libvndk",
1432 srcs: ["mylib.cpp"],
1433 vendor_available: true,
1434 vndk: {
1435 enabled: true,
1436 },
1437 system_shared_libs: [],
1438 stl: "none",
1439 }
1440
1441 cc_library {
1442 name: "libvndksp",
1443 srcs: ["mylib.cpp"],
1444 vendor_available: true,
1445 vndk: {
1446 enabled: true,
1447 support_system_process: true,
1448 },
1449 system_shared_libs: [],
1450 stl: "none",
1451 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001452 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001453
Jooyung Han31c470b2019-10-18 16:26:59 +09001454 ensureExactContents(t, ctx, "myapex", []string{
1455 "lib/libvndk.so",
1456 "lib/libvndksp.so",
1457 "lib64/libvndk.so",
1458 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001459 "etc/llndk.libraries.VER.txt",
1460 "etc/vndkcore.libraries.VER.txt",
1461 "etc/vndksp.libraries.VER.txt",
1462 "etc/vndkprivate.libraries.VER.txt",
1463 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001464 })
Jooyung Han344d5432019-08-23 11:17:39 +09001465}
1466
1467func TestVndkApexWithPrebuilt(t *testing.T) {
1468 ctx, _ := testApex(t, `
1469 apex_vndk {
1470 name: "myapex",
1471 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001472 }
1473
1474 apex_key {
1475 name: "myapex.key",
1476 public_key: "testkey.avbpubkey",
1477 private_key: "testkey.pem",
1478 }
1479
1480 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001481 name: "libvndk",
1482 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001483 vendor_available: true,
1484 vndk: {
1485 enabled: true,
1486 },
1487 system_shared_libs: [],
1488 stl: "none",
1489 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001490
1491 cc_prebuilt_library_shared {
1492 name: "libvndk.arm",
1493 srcs: ["libvndk.arm.so"],
1494 vendor_available: true,
1495 vndk: {
1496 enabled: true,
1497 },
1498 enabled: false,
1499 arch: {
1500 arm: {
1501 enabled: true,
1502 },
1503 },
1504 system_shared_libs: [],
1505 stl: "none",
1506 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001507 `+vndkLibrariesTxtFiles("current"),
1508 withFiles(map[string][]byte{
1509 "libvndk.so": nil,
1510 "libvndk.arm.so": nil,
1511 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001512
Jooyung Han31c470b2019-10-18 16:26:59 +09001513 ensureExactContents(t, ctx, "myapex", []string{
1514 "lib/libvndk.so",
1515 "lib/libvndk.arm.so",
1516 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001517 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001518 })
Jooyung Han344d5432019-08-23 11:17:39 +09001519}
1520
Jooyung Han39edb6c2019-11-06 16:53:07 +09001521func vndkLibrariesTxtFiles(vers ...string) (result string) {
1522 for _, v := range vers {
1523 if v == "current" {
1524 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1525 result += `
1526 vndk_libraries_txt {
1527 name: "` + txt + `.libraries.txt",
1528 }
1529 `
1530 }
1531 } else {
1532 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1533 result += `
1534 prebuilt_etc {
1535 name: "` + txt + `.libraries.` + v + `.txt",
1536 src: "dummy.txt",
1537 }
1538 `
1539 }
1540 }
1541 }
1542 return
1543}
1544
Jooyung Han344d5432019-08-23 11:17:39 +09001545func TestVndkApexVersion(t *testing.T) {
1546 ctx, _ := testApex(t, `
1547 apex_vndk {
1548 name: "myapex_v27",
1549 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001550 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001551 vndk_version: "27",
1552 }
1553
1554 apex_key {
1555 name: "myapex.key",
1556 public_key: "testkey.avbpubkey",
1557 private_key: "testkey.pem",
1558 }
1559
Jooyung Han31c470b2019-10-18 16:26:59 +09001560 vndk_prebuilt_shared {
1561 name: "libvndk27",
1562 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001563 vendor_available: true,
1564 vndk: {
1565 enabled: true,
1566 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001567 target_arch: "arm64",
1568 arch: {
1569 arm: {
1570 srcs: ["libvndk27_arm.so"],
1571 },
1572 arm64: {
1573 srcs: ["libvndk27_arm64.so"],
1574 },
1575 },
Jooyung Han344d5432019-08-23 11:17:39 +09001576 }
1577
1578 vndk_prebuilt_shared {
1579 name: "libvndk27",
1580 version: "27",
1581 vendor_available: true,
1582 vndk: {
1583 enabled: true,
1584 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001585 target_arch: "x86_64",
1586 arch: {
1587 x86: {
1588 srcs: ["libvndk27_x86.so"],
1589 },
1590 x86_64: {
1591 srcs: ["libvndk27_x86_64.so"],
1592 },
1593 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001594 }
1595 `+vndkLibrariesTxtFiles("27"),
1596 withFiles(map[string][]byte{
1597 "libvndk27_arm.so": nil,
1598 "libvndk27_arm64.so": nil,
1599 "libvndk27_x86.so": nil,
1600 "libvndk27_x86_64.so": nil,
1601 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001602
Jooyung Han31c470b2019-10-18 16:26:59 +09001603 ensureExactContents(t, ctx, "myapex_v27", []string{
1604 "lib/libvndk27_arm.so",
1605 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001606 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001607 })
Jooyung Han344d5432019-08-23 11:17:39 +09001608}
1609
1610func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1611 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1612 apex_vndk {
1613 name: "myapex_v27",
1614 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001615 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001616 vndk_version: "27",
1617 }
1618 apex_vndk {
1619 name: "myapex_v27_other",
1620 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001621 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001622 vndk_version: "27",
1623 }
1624
1625 apex_key {
1626 name: "myapex.key",
1627 public_key: "testkey.avbpubkey",
1628 private_key: "testkey.pem",
1629 }
1630
1631 cc_library {
1632 name: "libvndk",
1633 srcs: ["mylib.cpp"],
1634 vendor_available: true,
1635 vndk: {
1636 enabled: true,
1637 },
1638 system_shared_libs: [],
1639 stl: "none",
1640 }
1641
1642 vndk_prebuilt_shared {
1643 name: "libvndk",
1644 version: "27",
1645 vendor_available: true,
1646 vndk: {
1647 enabled: true,
1648 },
1649 srcs: ["libvndk.so"],
1650 }
1651 `, withFiles(map[string][]byte{
1652 "libvndk.so": nil,
1653 }))
1654}
1655
Jooyung Han90eee022019-10-01 20:02:42 +09001656func TestVndkApexNameRule(t *testing.T) {
1657 ctx, _ := testApex(t, `
1658 apex_vndk {
1659 name: "myapex",
1660 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001661 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001662 }
1663 apex_vndk {
1664 name: "myapex_v28",
1665 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001666 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001667 vndk_version: "28",
1668 }
1669 apex_key {
1670 name: "myapex.key",
1671 public_key: "testkey.avbpubkey",
1672 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001673 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001674
1675 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001676 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001677 actual := proptools.String(bundle.properties.Apex_name)
1678 if !reflect.DeepEqual(actual, expected) {
1679 t.Errorf("Got '%v', expected '%v'", actual, expected)
1680 }
1681 }
1682
1683 assertApexName("com.android.vndk.vVER", "myapex")
1684 assertApexName("com.android.vndk.v28", "myapex_v28")
1685}
1686
Jooyung Han344d5432019-08-23 11:17:39 +09001687func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1688 ctx, _ := testApex(t, `
1689 apex_vndk {
1690 name: "myapex",
1691 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001692 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001693 }
1694
1695 apex_key {
1696 name: "myapex.key",
1697 public_key: "testkey.avbpubkey",
1698 private_key: "testkey.pem",
1699 }
1700
1701 cc_library {
1702 name: "libvndk",
1703 srcs: ["mylib.cpp"],
1704 vendor_available: true,
1705 native_bridge_supported: true,
1706 host_supported: true,
1707 vndk: {
1708 enabled: true,
1709 },
1710 system_shared_libs: [],
1711 stl: "none",
1712 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001713 `+vndkLibrariesTxtFiles("current"),
1714 withTargets(map[android.OsType][]android.Target{
1715 android.Android: []android.Target{
1716 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1717 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1718 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1719 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1720 },
1721 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001722
Jooyung Han31c470b2019-10-18 16:26:59 +09001723 ensureExactContents(t, ctx, "myapex", []string{
1724 "lib/libvndk.so",
1725 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001726 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001727 })
Jooyung Han344d5432019-08-23 11:17:39 +09001728}
1729
1730func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1731 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1732 apex_vndk {
1733 name: "myapex",
1734 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001735 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001736 native_bridge_supported: true,
1737 }
1738
1739 apex_key {
1740 name: "myapex.key",
1741 public_key: "testkey.avbpubkey",
1742 private_key: "testkey.pem",
1743 }
1744
1745 cc_library {
1746 name: "libvndk",
1747 srcs: ["mylib.cpp"],
1748 vendor_available: true,
1749 native_bridge_supported: true,
1750 host_supported: true,
1751 vndk: {
1752 enabled: true,
1753 },
1754 system_shared_libs: [],
1755 stl: "none",
1756 }
1757 `)
1758}
1759
Jooyung Han31c470b2019-10-18 16:26:59 +09001760func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001761 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001762 apex_vndk {
1763 name: "myapex_v27",
1764 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001765 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001766 vndk_version: "27",
1767 }
1768
1769 apex_key {
1770 name: "myapex.key",
1771 public_key: "testkey.avbpubkey",
1772 private_key: "testkey.pem",
1773 }
1774
1775 vndk_prebuilt_shared {
1776 name: "libvndk27",
1777 version: "27",
1778 target_arch: "arm",
1779 vendor_available: true,
1780 vndk: {
1781 enabled: true,
1782 },
1783 arch: {
1784 arm: {
1785 srcs: ["libvndk27.so"],
1786 }
1787 },
1788 }
1789
1790 vndk_prebuilt_shared {
1791 name: "libvndk27",
1792 version: "27",
1793 target_arch: "arm",
1794 binder32bit: true,
1795 vendor_available: true,
1796 vndk: {
1797 enabled: true,
1798 },
1799 arch: {
1800 arm: {
1801 srcs: ["libvndk27binder32.so"],
1802 }
1803 },
1804 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001805 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001806 withFiles(map[string][]byte{
1807 "libvndk27.so": nil,
1808 "libvndk27binder32.so": nil,
1809 }),
1810 withBinder32bit,
1811 withTargets(map[android.OsType][]android.Target{
1812 android.Android: []android.Target{
1813 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1814 },
1815 }),
1816 )
1817
1818 ensureExactContents(t, ctx, "myapex_v27", []string{
1819 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001820 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001821 })
1822}
1823
Jooyung Hane1633032019-08-01 17:41:43 +09001824func TestDependenciesInApexManifest(t *testing.T) {
1825 ctx, _ := testApex(t, `
1826 apex {
1827 name: "myapex_nodep",
1828 key: "myapex.key",
1829 native_shared_libs: ["lib_nodep"],
1830 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001831 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001832 }
1833
1834 apex {
1835 name: "myapex_dep",
1836 key: "myapex.key",
1837 native_shared_libs: ["lib_dep"],
1838 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001839 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001840 }
1841
1842 apex {
1843 name: "myapex_provider",
1844 key: "myapex.key",
1845 native_shared_libs: ["libfoo"],
1846 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001847 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001848 }
1849
1850 apex {
1851 name: "myapex_selfcontained",
1852 key: "myapex.key",
1853 native_shared_libs: ["lib_dep", "libfoo"],
1854 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001855 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001856 }
1857
1858 apex_key {
1859 name: "myapex.key",
1860 public_key: "testkey.avbpubkey",
1861 private_key: "testkey.pem",
1862 }
1863
1864 cc_library {
1865 name: "lib_nodep",
1866 srcs: ["mylib.cpp"],
1867 system_shared_libs: [],
1868 stl: "none",
1869 }
1870
1871 cc_library {
1872 name: "lib_dep",
1873 srcs: ["mylib.cpp"],
1874 shared_libs: ["libfoo"],
1875 system_shared_libs: [],
1876 stl: "none",
1877 }
1878
1879 cc_library {
1880 name: "libfoo",
1881 srcs: ["mytest.cpp"],
1882 stubs: {
1883 versions: ["1"],
1884 },
1885 system_shared_libs: [],
1886 stl: "none",
1887 }
1888 `)
1889
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001890 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09001891 var provideNativeLibs, requireNativeLibs []string
1892
Sundong Ahnabb64432019-10-22 13:58:29 +09001893 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001894 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1895 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001896 ensureListEmpty(t, provideNativeLibs)
1897 ensureListEmpty(t, requireNativeLibs)
1898
Sundong Ahnabb64432019-10-22 13:58:29 +09001899 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001900 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1901 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001902 ensureListEmpty(t, provideNativeLibs)
1903 ensureListContains(t, requireNativeLibs, "libfoo.so")
1904
Sundong Ahnabb64432019-10-22 13:58:29 +09001905 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001906 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1907 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001908 ensureListContains(t, provideNativeLibs, "libfoo.so")
1909 ensureListEmpty(t, requireNativeLibs)
1910
Sundong Ahnabb64432019-10-22 13:58:29 +09001911 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001912 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1913 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001914 ensureListContains(t, provideNativeLibs, "libfoo.so")
1915 ensureListEmpty(t, requireNativeLibs)
1916}
1917
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001918func TestApexName(t *testing.T) {
1919 ctx, _ := testApex(t, `
1920 apex {
1921 name: "myapex",
1922 key: "myapex.key",
1923 apex_name: "com.android.myapex",
1924 }
1925
1926 apex_key {
1927 name: "myapex.key",
1928 public_key: "testkey.avbpubkey",
1929 private_key: "testkey.pem",
1930 }
1931 `)
1932
Sundong Ahnabb64432019-10-22 13:58:29 +09001933 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001934 apexManifestRule := module.Rule("apexManifestRule")
1935 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
1936 apexRule := module.Rule("apexRule")
1937 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
1938}
1939
Alex Light0851b882019-02-07 13:20:53 -08001940func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001941 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001942 apex {
1943 name: "myapex",
1944 key: "myapex.key",
1945 native_shared_libs: ["mylib_common"],
1946 }
1947
1948 apex_key {
1949 name: "myapex.key",
1950 public_key: "testkey.avbpubkey",
1951 private_key: "testkey.pem",
1952 }
1953
1954 cc_library {
1955 name: "mylib_common",
1956 srcs: ["mylib.cpp"],
1957 system_shared_libs: [],
1958 stl: "none",
1959 }
1960 `)
1961
Sundong Ahnabb64432019-10-22 13:58:29 +09001962 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08001963 apexRule := module.Rule("apexRule")
1964 copyCmds := apexRule.Args["copy_commands"]
1965
1966 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1967 t.Log("Apex was a test apex!")
1968 t.Fail()
1969 }
1970 // Ensure that main rule creates an output
1971 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1972
1973 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08001974 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08001975
1976 // Ensure that both direct and indirect deps are copied into apex
1977 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1978
Colin Cross7113d202019-11-20 16:39:12 -08001979 // Ensure that the platform variant ends with _shared
1980 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08001981
1982 if !android.InAnyApex("mylib_common") {
1983 t.Log("Found mylib_common not in any apex!")
1984 t.Fail()
1985 }
1986}
1987
1988func TestTestApex(t *testing.T) {
1989 if android.InAnyApex("mylib_common_test") {
1990 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!")
1991 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001992 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001993 apex_test {
1994 name: "myapex",
1995 key: "myapex.key",
1996 native_shared_libs: ["mylib_common_test"],
1997 }
1998
1999 apex_key {
2000 name: "myapex.key",
2001 public_key: "testkey.avbpubkey",
2002 private_key: "testkey.pem",
2003 }
2004
2005 cc_library {
2006 name: "mylib_common_test",
2007 srcs: ["mylib.cpp"],
2008 system_shared_libs: [],
2009 stl: "none",
2010 }
2011 `)
2012
Sundong Ahnabb64432019-10-22 13:58:29 +09002013 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002014 apexRule := module.Rule("apexRule")
2015 copyCmds := apexRule.Args["copy_commands"]
2016
2017 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2018 t.Log("Apex was not a test apex!")
2019 t.Fail()
2020 }
2021 // Ensure that main rule creates an output
2022 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2023
2024 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002025 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002026
2027 // Ensure that both direct and indirect deps are copied into apex
2028 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2029
Colin Cross7113d202019-11-20 16:39:12 -08002030 // Ensure that the platform variant ends with _shared
2031 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002032
2033 if android.InAnyApex("mylib_common_test") {
2034 t.Log("Found mylib_common_test in some apex!")
2035 t.Fail()
2036 }
2037}
2038
Alex Light9670d332019-01-29 18:07:33 -08002039func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002040 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002041 apex {
2042 name: "myapex",
2043 key: "myapex.key",
2044 multilib: {
2045 first: {
2046 native_shared_libs: ["mylib_common"],
2047 }
2048 },
2049 target: {
2050 android: {
2051 multilib: {
2052 first: {
2053 native_shared_libs: ["mylib"],
2054 }
2055 }
2056 },
2057 host: {
2058 multilib: {
2059 first: {
2060 native_shared_libs: ["mylib2"],
2061 }
2062 }
2063 }
2064 }
2065 }
2066
2067 apex_key {
2068 name: "myapex.key",
2069 public_key: "testkey.avbpubkey",
2070 private_key: "testkey.pem",
2071 }
2072
2073 cc_library {
2074 name: "mylib",
2075 srcs: ["mylib.cpp"],
2076 system_shared_libs: [],
2077 stl: "none",
2078 }
2079
2080 cc_library {
2081 name: "mylib_common",
2082 srcs: ["mylib.cpp"],
2083 system_shared_libs: [],
2084 stl: "none",
2085 compile_multilib: "first",
2086 }
2087
2088 cc_library {
2089 name: "mylib2",
2090 srcs: ["mylib.cpp"],
2091 system_shared_libs: [],
2092 stl: "none",
2093 compile_multilib: "first",
2094 }
2095 `)
2096
Sundong Ahnabb64432019-10-22 13:58:29 +09002097 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002098 copyCmds := apexRule.Args["copy_commands"]
2099
2100 // Ensure that main rule creates an output
2101 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2102
2103 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002104 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2105 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2106 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002107
2108 // Ensure that both direct and indirect deps are copied into apex
2109 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2110 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2111 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2112
Colin Cross7113d202019-11-20 16:39:12 -08002113 // Ensure that the platform variant ends with _shared
2114 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2115 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2116 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002117}
Jiyong Park04480cf2019-02-06 00:16:29 +09002118
2119func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002120 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002121 apex {
2122 name: "myapex",
2123 key: "myapex.key",
2124 binaries: ["myscript"],
2125 }
2126
2127 apex_key {
2128 name: "myapex.key",
2129 public_key: "testkey.avbpubkey",
2130 private_key: "testkey.pem",
2131 }
2132
2133 sh_binary {
2134 name: "myscript",
2135 src: "mylib.cpp",
2136 filename: "myscript.sh",
2137 sub_dir: "script",
2138 }
2139 `)
2140
Sundong Ahnabb64432019-10-22 13:58:29 +09002141 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002142 copyCmds := apexRule.Args["copy_commands"]
2143
2144 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2145}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002146
Jooyung Han91df2082019-11-20 01:49:42 +09002147func TestApexInVariousPartition(t *testing.T) {
2148 testcases := []struct {
2149 propName, parition, flattenedPartition string
2150 }{
2151 {"", "system", "system_ext"},
2152 {"product_specific: true", "product", "product"},
2153 {"soc_specific: true", "vendor", "vendor"},
2154 {"proprietary: true", "vendor", "vendor"},
2155 {"vendor: true", "vendor", "vendor"},
2156 {"system_ext_specific: true", "system_ext", "system_ext"},
2157 }
2158 for _, tc := range testcases {
2159 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2160 ctx, _ := testApex(t, `
2161 apex {
2162 name: "myapex",
2163 key: "myapex.key",
2164 `+tc.propName+`
2165 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002166
Jooyung Han91df2082019-11-20 01:49:42 +09002167 apex_key {
2168 name: "myapex.key",
2169 public_key: "testkey.avbpubkey",
2170 private_key: "testkey.pem",
2171 }
2172 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002173
Jooyung Han91df2082019-11-20 01:49:42 +09002174 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2175 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2176 actual := apex.installDir.String()
2177 if actual != expected {
2178 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2179 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002180
Jooyung Han91df2082019-11-20 01:49:42 +09002181 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2182 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2183 actual = flattened.installDir.String()
2184 if actual != expected {
2185 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2186 }
2187 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002188 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002189}
Jiyong Park67882562019-03-21 01:11:21 +09002190
Jooyung Han54aca7b2019-11-20 02:26:02 +09002191func TestFileContexts(t *testing.T) {
2192 ctx, _ := testApex(t, `
2193 apex {
2194 name: "myapex",
2195 key: "myapex.key",
2196 }
2197
2198 apex_key {
2199 name: "myapex.key",
2200 public_key: "testkey.avbpubkey",
2201 private_key: "testkey.pem",
2202 }
2203 `)
2204 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2205 apexRule := module.Rule("apexRule")
2206 actual := apexRule.Args["file_contexts"]
2207 expected := "system/sepolicy/apex/myapex-file_contexts"
2208 if actual != expected {
2209 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2210 }
2211
2212 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2213 apex {
2214 name: "myapex",
2215 key: "myapex.key",
2216 file_contexts: "my_own_file_contexts",
2217 }
2218
2219 apex_key {
2220 name: "myapex.key",
2221 public_key: "testkey.avbpubkey",
2222 private_key: "testkey.pem",
2223 }
2224 `, withFiles(map[string][]byte{
2225 "my_own_file_contexts": nil,
2226 }))
2227
2228 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2229 apex {
2230 name: "myapex",
2231 key: "myapex.key",
2232 product_specific: true,
2233 file_contexts: "product_specific_file_contexts",
2234 }
2235
2236 apex_key {
2237 name: "myapex.key",
2238 public_key: "testkey.avbpubkey",
2239 private_key: "testkey.pem",
2240 }
2241 `)
2242
2243 ctx, _ = testApex(t, `
2244 apex {
2245 name: "myapex",
2246 key: "myapex.key",
2247 product_specific: true,
2248 file_contexts: "product_specific_file_contexts",
2249 }
2250
2251 apex_key {
2252 name: "myapex.key",
2253 public_key: "testkey.avbpubkey",
2254 private_key: "testkey.pem",
2255 }
2256 `, withFiles(map[string][]byte{
2257 "product_specific_file_contexts": nil,
2258 }))
2259 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2260 apexRule = module.Rule("apexRule")
2261 actual = apexRule.Args["file_contexts"]
2262 expected = "product_specific_file_contexts"
2263 if actual != expected {
2264 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2265 }
2266
2267 ctx, _ = testApex(t, `
2268 apex {
2269 name: "myapex",
2270 key: "myapex.key",
2271 product_specific: true,
2272 file_contexts: ":my-file-contexts",
2273 }
2274
2275 apex_key {
2276 name: "myapex.key",
2277 public_key: "testkey.avbpubkey",
2278 private_key: "testkey.pem",
2279 }
2280
2281 filegroup {
2282 name: "my-file-contexts",
2283 srcs: ["product_specific_file_contexts"],
2284 }
2285 `, withFiles(map[string][]byte{
2286 "product_specific_file_contexts": nil,
2287 }))
2288 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2289 apexRule = module.Rule("apexRule")
2290 actual = apexRule.Args["file_contexts"]
2291 expected = "product_specific_file_contexts"
2292 if actual != expected {
2293 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2294 }
2295}
2296
Jiyong Park67882562019-03-21 01:11:21 +09002297func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002298 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002299 apex_key {
2300 name: "myapex.key",
2301 public_key: ":my.avbpubkey",
2302 private_key: ":my.pem",
2303 product_specific: true,
2304 }
2305
2306 filegroup {
2307 name: "my.avbpubkey",
2308 srcs: ["testkey2.avbpubkey"],
2309 }
2310
2311 filegroup {
2312 name: "my.pem",
2313 srcs: ["testkey2.pem"],
2314 }
2315 `)
2316
2317 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2318 expected_pubkey := "testkey2.avbpubkey"
2319 actual_pubkey := apex_key.public_key_file.String()
2320 if actual_pubkey != expected_pubkey {
2321 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2322 }
2323 expected_privkey := "testkey2.pem"
2324 actual_privkey := apex_key.private_key_file.String()
2325 if actual_privkey != expected_privkey {
2326 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2327 }
2328}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002329
2330func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002331 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002332 prebuilt_apex {
2333 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002334 arch: {
2335 arm64: {
2336 src: "myapex-arm64.apex",
2337 },
2338 arm: {
2339 src: "myapex-arm.apex",
2340 },
2341 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002342 }
2343 `)
2344
2345 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2346
Jiyong Parkc95714e2019-03-29 14:23:10 +09002347 expectedInput := "myapex-arm64.apex"
2348 if prebuilt.inputApex.String() != expectedInput {
2349 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2350 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002351}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002352
2353func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002354 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002355 prebuilt_apex {
2356 name: "myapex",
2357 src: "myapex-arm.apex",
2358 filename: "notmyapex.apex",
2359 }
2360 `)
2361
2362 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2363
2364 expected := "notmyapex.apex"
2365 if p.installFilename != expected {
2366 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2367 }
2368}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002369
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002370func TestPrebuiltOverrides(t *testing.T) {
2371 ctx, config := testApex(t, `
2372 prebuilt_apex {
2373 name: "myapex.prebuilt",
2374 src: "myapex-arm.apex",
2375 overrides: [
2376 "myapex",
2377 ],
2378 }
2379 `)
2380
2381 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2382
2383 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002384 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002385 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002386 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002387 }
2388}
2389
Roland Levillain630846d2019-06-26 12:48:34 +01002390func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002391 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002392 apex_test {
2393 name: "myapex",
2394 key: "myapex.key",
2395 tests: [
2396 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002397 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002398 ],
2399 }
2400
2401 apex_key {
2402 name: "myapex.key",
2403 public_key: "testkey.avbpubkey",
2404 private_key: "testkey.pem",
2405 }
2406
2407 cc_test {
2408 name: "mytest",
2409 gtest: false,
2410 srcs: ["mytest.cpp"],
2411 relative_install_path: "test",
2412 system_shared_libs: [],
2413 static_executable: true,
2414 stl: "none",
2415 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002416
2417 cc_test {
2418 name: "mytests",
2419 gtest: false,
2420 srcs: [
2421 "mytest1.cpp",
2422 "mytest2.cpp",
2423 "mytest3.cpp",
2424 ],
2425 test_per_src: true,
2426 relative_install_path: "test",
2427 system_shared_libs: [],
2428 static_executable: true,
2429 stl: "none",
2430 }
Roland Levillain630846d2019-06-26 12:48:34 +01002431 `)
2432
Sundong Ahnabb64432019-10-22 13:58:29 +09002433 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002434 copyCmds := apexRule.Args["copy_commands"]
2435
2436 // Ensure that test dep is copied into apex.
2437 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002438
2439 // Ensure that test deps built with `test_per_src` are copied into apex.
2440 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2441 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2442 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002443
2444 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002445 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002446 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2447 name := apexBundle.BaseModuleName()
2448 prefix := "TARGET_"
2449 var builder strings.Builder
2450 data.Custom(&builder, name, prefix, "", data)
2451 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002452 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2453 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2454 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2455 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002456 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002457 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002458 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002459}
2460
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002461func TestInstallExtraFlattenedApexes(t *testing.T) {
2462 ctx, config := testApex(t, `
2463 apex {
2464 name: "myapex",
2465 key: "myapex.key",
2466 }
2467 apex_key {
2468 name: "myapex.key",
2469 public_key: "testkey.avbpubkey",
2470 private_key: "testkey.pem",
2471 }
2472 `, func(fs map[string][]byte, config android.Config) {
2473 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2474 })
2475 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2476 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2477 mk := android.AndroidMkDataForTest(t, config, "", ab)
2478 var builder strings.Builder
2479 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2480 androidMk := builder.String()
2481 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2482}
2483
Jooyung Han5c998b92019-06-27 11:30:33 +09002484func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002485 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002486 apex {
2487 name: "myapex",
2488 key: "myapex.key",
2489 native_shared_libs: ["mylib"],
2490 uses: ["commonapex"],
2491 }
2492
2493 apex {
2494 name: "commonapex",
2495 key: "myapex.key",
2496 native_shared_libs: ["libcommon"],
2497 provide_cpp_shared_libs: true,
2498 }
2499
2500 apex_key {
2501 name: "myapex.key",
2502 public_key: "testkey.avbpubkey",
2503 private_key: "testkey.pem",
2504 }
2505
2506 cc_library {
2507 name: "mylib",
2508 srcs: ["mylib.cpp"],
2509 shared_libs: ["libcommon"],
2510 system_shared_libs: [],
2511 stl: "none",
2512 }
2513
2514 cc_library {
2515 name: "libcommon",
2516 srcs: ["mylib_common.cpp"],
2517 system_shared_libs: [],
2518 stl: "none",
2519 }
2520 `)
2521
Sundong Ahnabb64432019-10-22 13:58:29 +09002522 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002523 apexRule1 := module1.Rule("apexRule")
2524 copyCmds1 := apexRule1.Args["copy_commands"]
2525
Sundong Ahnabb64432019-10-22 13:58:29 +09002526 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002527 apexRule2 := module2.Rule("apexRule")
2528 copyCmds2 := apexRule2.Args["copy_commands"]
2529
Colin Cross7113d202019-11-20 16:39:12 -08002530 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2531 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002532 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2533 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2534 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2535}
2536
2537func TestApexUsesFailsIfNotProvided(t *testing.T) {
2538 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2539 apex {
2540 name: "myapex",
2541 key: "myapex.key",
2542 uses: ["commonapex"],
2543 }
2544
2545 apex {
2546 name: "commonapex",
2547 key: "myapex.key",
2548 }
2549
2550 apex_key {
2551 name: "myapex.key",
2552 public_key: "testkey.avbpubkey",
2553 private_key: "testkey.pem",
2554 }
2555 `)
2556 testApexError(t, `uses: "commonapex" is not a provider`, `
2557 apex {
2558 name: "myapex",
2559 key: "myapex.key",
2560 uses: ["commonapex"],
2561 }
2562
2563 cc_library {
2564 name: "commonapex",
2565 system_shared_libs: [],
2566 stl: "none",
2567 }
2568
2569 apex_key {
2570 name: "myapex.key",
2571 public_key: "testkey.avbpubkey",
2572 private_key: "testkey.pem",
2573 }
2574 `)
2575}
2576
2577func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2578 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2579 apex {
2580 name: "myapex",
2581 key: "myapex.key",
2582 use_vendor: true,
2583 uses: ["commonapex"],
2584 }
2585
2586 apex {
2587 name: "commonapex",
2588 key: "myapex.key",
2589 provide_cpp_shared_libs: true,
2590 }
2591
2592 apex_key {
2593 name: "myapex.key",
2594 public_key: "testkey.avbpubkey",
2595 private_key: "testkey.pem",
2596 }
Jooyung Handc782442019-11-01 03:14:38 +09002597 `, func(fs map[string][]byte, config android.Config) {
2598 setUseVendorWhitelistForTest(config, []string{"myapex"})
2599 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002600}
2601
Jooyung Hand48f3c32019-08-23 11:18:57 +09002602func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2603 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2604 apex {
2605 name: "myapex",
2606 key: "myapex.key",
2607 native_shared_libs: ["libfoo"],
2608 }
2609
2610 apex_key {
2611 name: "myapex.key",
2612 public_key: "testkey.avbpubkey",
2613 private_key: "testkey.pem",
2614 }
2615
2616 cc_library {
2617 name: "libfoo",
2618 stl: "none",
2619 system_shared_libs: [],
2620 enabled: false,
2621 }
2622 `)
2623 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2624 apex {
2625 name: "myapex",
2626 key: "myapex.key",
2627 java_libs: ["myjar"],
2628 }
2629
2630 apex_key {
2631 name: "myapex.key",
2632 public_key: "testkey.avbpubkey",
2633 private_key: "testkey.pem",
2634 }
2635
2636 java_library {
2637 name: "myjar",
2638 srcs: ["foo/bar/MyClass.java"],
2639 sdk_version: "none",
2640 system_modules: "none",
2641 compile_dex: true,
2642 enabled: false,
2643 }
2644 `)
2645}
2646
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002647func TestApexWithApps(t *testing.T) {
2648 ctx, _ := testApex(t, `
2649 apex {
2650 name: "myapex",
2651 key: "myapex.key",
2652 apps: [
2653 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002654 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002655 ],
2656 }
2657
2658 apex_key {
2659 name: "myapex.key",
2660 public_key: "testkey.avbpubkey",
2661 private_key: "testkey.pem",
2662 }
2663
2664 android_app {
2665 name: "AppFoo",
2666 srcs: ["foo/bar/MyClass.java"],
2667 sdk_version: "none",
2668 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002669 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002670 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002671
2672 android_app {
2673 name: "AppFooPriv",
2674 srcs: ["foo/bar/MyClass.java"],
2675 sdk_version: "none",
2676 system_modules: "none",
2677 privileged: true,
2678 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002679
2680 cc_library_shared {
2681 name: "libjni",
2682 srcs: ["mylib.cpp"],
2683 stl: "none",
2684 system_shared_libs: [],
2685 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002686 `)
2687
Sundong Ahnabb64432019-10-22 13:58:29 +09002688 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002689 apexRule := module.Rule("apexRule")
2690 copyCmds := apexRule.Args["copy_commands"]
2691
2692 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002693 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002694
2695 // JNI libraries are embedded inside APK
2696 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002697 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002698 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2699 // ... uncompressed
2700 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2701 t.Errorf("jni lib is not uncompressed for AppFoo")
2702 }
2703 // ... and not directly inside the APEX
2704 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002705}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002706
Dario Frenicde2a032019-10-27 00:29:22 +01002707func TestApexWithAppImports(t *testing.T) {
2708 ctx, _ := testApex(t, `
2709 apex {
2710 name: "myapex",
2711 key: "myapex.key",
2712 apps: [
2713 "AppFooPrebuilt",
2714 "AppFooPrivPrebuilt",
2715 ],
2716 }
2717
2718 apex_key {
2719 name: "myapex.key",
2720 public_key: "testkey.avbpubkey",
2721 private_key: "testkey.pem",
2722 }
2723
2724 android_app_import {
2725 name: "AppFooPrebuilt",
2726 apk: "PrebuiltAppFoo.apk",
2727 presigned: true,
2728 dex_preopt: {
2729 enabled: false,
2730 },
2731 }
2732
2733 android_app_import {
2734 name: "AppFooPrivPrebuilt",
2735 apk: "PrebuiltAppFooPriv.apk",
2736 privileged: true,
2737 presigned: true,
2738 dex_preopt: {
2739 enabled: false,
2740 },
2741 }
2742 `)
2743
Sundong Ahnabb64432019-10-22 13:58:29 +09002744 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002745 apexRule := module.Rule("apexRule")
2746 copyCmds := apexRule.Args["copy_commands"]
2747
2748 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2749 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002750}
2751
Jooyung Han18020ea2019-11-13 10:50:48 +09002752func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2753 // libfoo's apex_available comes from cc_defaults
2754 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2755 apex {
2756 name: "myapex",
2757 key: "myapex.key",
2758 native_shared_libs: ["libfoo"],
2759 }
2760
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }
2766
2767 apex {
2768 name: "otherapex",
2769 key: "myapex.key",
2770 native_shared_libs: ["libfoo"],
2771 }
2772
2773 cc_defaults {
2774 name: "libfoo-defaults",
2775 apex_available: ["otherapex"],
2776 }
2777
2778 cc_library {
2779 name: "libfoo",
2780 defaults: ["libfoo-defaults"],
2781 stl: "none",
2782 system_shared_libs: [],
2783 }`)
2784}
2785
Jiyong Park127b40b2019-09-30 16:04:35 +09002786func TestApexAvailable(t *testing.T) {
2787 // libfoo is not available to myapex, but only to otherapex
2788 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2789 apex {
2790 name: "myapex",
2791 key: "myapex.key",
2792 native_shared_libs: ["libfoo"],
2793 }
2794
2795 apex_key {
2796 name: "myapex.key",
2797 public_key: "testkey.avbpubkey",
2798 private_key: "testkey.pem",
2799 }
2800
2801 apex {
2802 name: "otherapex",
2803 key: "otherapex.key",
2804 native_shared_libs: ["libfoo"],
2805 }
2806
2807 apex_key {
2808 name: "otherapex.key",
2809 public_key: "testkey.avbpubkey",
2810 private_key: "testkey.pem",
2811 }
2812
2813 cc_library {
2814 name: "libfoo",
2815 stl: "none",
2816 system_shared_libs: [],
2817 apex_available: ["otherapex"],
2818 }`)
2819
2820 // libbar is an indirect dep
2821 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2822 apex {
2823 name: "myapex",
2824 key: "myapex.key",
2825 native_shared_libs: ["libfoo"],
2826 }
2827
2828 apex_key {
2829 name: "myapex.key",
2830 public_key: "testkey.avbpubkey",
2831 private_key: "testkey.pem",
2832 }
2833
2834 apex {
2835 name: "otherapex",
2836 key: "otherapex.key",
2837 native_shared_libs: ["libfoo"],
2838 }
2839
2840 apex_key {
2841 name: "otherapex.key",
2842 public_key: "testkey.avbpubkey",
2843 private_key: "testkey.pem",
2844 }
2845
2846 cc_library {
2847 name: "libfoo",
2848 stl: "none",
2849 shared_libs: ["libbar"],
2850 system_shared_libs: [],
2851 apex_available: ["myapex", "otherapex"],
2852 }
2853
2854 cc_library {
2855 name: "libbar",
2856 stl: "none",
2857 system_shared_libs: [],
2858 apex_available: ["otherapex"],
2859 }`)
2860
2861 testApexError(t, "\"otherapex\" is not a valid module name", `
2862 apex {
2863 name: "myapex",
2864 key: "myapex.key",
2865 native_shared_libs: ["libfoo"],
2866 }
2867
2868 apex_key {
2869 name: "myapex.key",
2870 public_key: "testkey.avbpubkey",
2871 private_key: "testkey.pem",
2872 }
2873
2874 cc_library {
2875 name: "libfoo",
2876 stl: "none",
2877 system_shared_libs: [],
2878 apex_available: ["otherapex"],
2879 }`)
2880
2881 ctx, _ := testApex(t, `
2882 apex {
2883 name: "myapex",
2884 key: "myapex.key",
2885 native_shared_libs: ["libfoo", "libbar"],
2886 }
2887
2888 apex_key {
2889 name: "myapex.key",
2890 public_key: "testkey.avbpubkey",
2891 private_key: "testkey.pem",
2892 }
2893
2894 cc_library {
2895 name: "libfoo",
2896 stl: "none",
2897 system_shared_libs: [],
2898 apex_available: ["myapex"],
2899 }
2900
2901 cc_library {
2902 name: "libbar",
2903 stl: "none",
2904 system_shared_libs: [],
2905 apex_available: ["//apex_available:anyapex"],
2906 }`)
2907
2908 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08002909 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
2910 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
2911 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
2912 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09002913
2914 ctx, _ = testApex(t, `
2915 apex {
2916 name: "myapex",
2917 key: "myapex.key",
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: ["//apex_available:platform"],
2931 }`)
2932
2933 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08002934 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
2935 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002936
2937 ctx, _ = testApex(t, `
2938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
2941 native_shared_libs: ["libfoo"],
2942 }
2943
2944 apex_key {
2945 name: "myapex.key",
2946 public_key: "testkey.avbpubkey",
2947 private_key: "testkey.pem",
2948 }
2949
2950 cc_library {
2951 name: "libfoo",
2952 stl: "none",
2953 system_shared_libs: [],
2954 apex_available: ["myapex"],
2955 static: {
2956 apex_available: ["//apex_available:platform"],
2957 },
2958 }`)
2959
2960 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08002961 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
2962 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002963 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08002964 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
2965 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09002966}
2967
Jiyong Park5d790c32019-11-15 18:40:32 +09002968func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002969 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09002970 apex {
2971 name: "myapex",
2972 key: "myapex.key",
2973 apps: ["app"],
2974 }
2975
2976 override_apex {
2977 name: "override_myapex",
2978 base: "myapex",
2979 apps: ["override_app"],
2980 }
2981
2982 apex_key {
2983 name: "myapex.key",
2984 public_key: "testkey.avbpubkey",
2985 private_key: "testkey.pem",
2986 }
2987
2988 android_app {
2989 name: "app",
2990 srcs: ["foo/bar/MyClass.java"],
2991 package_name: "foo",
2992 sdk_version: "none",
2993 system_modules: "none",
2994 }
2995
2996 override_android_app {
2997 name: "override_app",
2998 base: "app",
2999 package_name: "bar",
3000 }
3001 `)
3002
Jiyong Park317645e2019-12-05 13:20:58 +09003003 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3004 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3005 if originalVariant.GetOverriddenBy() != "" {
3006 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3007 }
3008 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3009 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3010 }
3011
Jiyong Park5d790c32019-11-15 18:40:32 +09003012 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3013 apexRule := module.Rule("apexRule")
3014 copyCmds := apexRule.Args["copy_commands"]
3015
3016 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3017 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003018
3019 apexBundle := module.Module().(*apexBundle)
3020 name := apexBundle.Name()
3021 if name != "override_myapex" {
3022 t.Errorf("name should be \"override_myapex\", but was %q", name)
3023 }
3024
3025 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3026 var builder strings.Builder
3027 data.Custom(&builder, name, "TARGET_", "", data)
3028 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003029 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003030 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3031 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3032 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003033 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003034 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3035 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003036}
3037
Jooyung Han214bf372019-11-12 13:03:50 +09003038func TestLegacyAndroid10Support(t *testing.T) {
3039 ctx, _ := testApex(t, `
3040 apex {
3041 name: "myapex",
3042 key: "myapex.key",
3043 legacy_android10_support: true,
3044 }
3045
3046 apex_key {
3047 name: "myapex.key",
3048 public_key: "testkey.avbpubkey",
3049 private_key: "testkey.pem",
3050 }
3051 `)
3052
3053 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3054 args := module.Rule("apexRule").Args
3055 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3056}
3057
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003058func TestMain(m *testing.M) {
3059 run := func() int {
3060 setUp()
3061 defer tearDown()
3062
3063 return m.Run()
3064 }
3065
3066 os.Exit(run())
3067}