blob: 509d76039e6415aa9a51e1fc52552f6982f8b9ff [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) {
Colin Cross7228ecd2019-11-18 16:00:16 -0800143 ctx.BottomUp("image", android.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900144 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +0900145 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Roland Levillain9b5fde92019-06-28 15:41:19 +0100146 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900147 ctx.BottomUp("version", cc.VersionMutator).Parallel()
148 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
Jooyung Han344d5432019-08-23 11:17:39 +0900149 })
Jooyung Han31c470b2019-10-18 16:26:59 +0900150 ctx.PreDepsMutators(RegisterPreDepsMutators)
Jiyong Park5d790c32019-11-15 18:40:32 +0900151 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Jooyung Han31c470b2019-10-18 16:26:59 +0900152 ctx.PostDepsMutators(RegisterPostDepsMutators)
Jooyung Han344d5432019-08-23 11:17:39 +0900153 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jooyung Han344d5432019-08-23 11:17:39 +0900154 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
155 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900156 })
157
158 ctx.Register()
159
160 bp = bp + `
161 toolchain_library {
162 name: "libcompiler_rt-extras",
163 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900164 vendor_available: true,
165 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 toolchain_library {
169 name: "libatomic",
170 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900171 vendor_available: true,
172 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900173 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 }
175
176 toolchain_library {
177 name: "libgcc",
178 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900179 vendor_available: true,
180 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900181 }
182
183 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700184 name: "libgcc_stripped",
185 src: "",
186 vendor_available: true,
187 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900188 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700189 }
190
191 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900192 name: "libclang_rt.builtins-aarch64-android",
193 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900194 vendor_available: true,
195 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900196 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900197 }
198
199 toolchain_library {
200 name: "libclang_rt.builtins-arm-android",
201 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 vendor_available: true,
203 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900204 native_bridge_supported: true,
205 }
206
207 toolchain_library {
208 name: "libclang_rt.builtins-x86_64-android",
209 src: "",
210 vendor_available: true,
211 recovery_available: true,
212 native_bridge_supported: true,
213 }
214
215 toolchain_library {
216 name: "libclang_rt.builtins-i686-android",
217 src: "",
218 vendor_available: true,
219 recovery_available: true,
220 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221 }
222
223 cc_object {
224 name: "crtbegin_so",
225 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900226 vendor_available: true,
227 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900228 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
230
231 cc_object {
232 name: "crtend_so",
233 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900234 vendor_available: true,
235 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900236 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237 }
238
Alex Light3d673592019-01-18 14:37:31 -0800239 cc_object {
240 name: "crtbegin_static",
241 stl: "none",
242 }
243
244 cc_object {
245 name: "crtend_android",
246 stl: "none",
247 }
248
Jiyong Parkda6eb592018-12-19 17:12:36 +0900249 llndk_library {
250 name: "libc",
251 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900252 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900253 }
254
255 llndk_library {
256 name: "libm",
257 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900258 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900259 }
260
261 llndk_library {
262 name: "libdl",
263 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900264 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900265 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900266
267 filegroup {
268 name: "myapex-file_contexts",
269 srcs: [
270 "system/sepolicy/apex/myapex-file_contexts",
271 ],
272 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 `
Dario Frenicde2a032019-10-27 00:29:22 +0100274 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900275
Jooyung Han344d5432019-08-23 11:17:39 +0900276 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900277 "Android.bp": []byte(bp),
278 "a.java": nil,
279 "PrebuiltAppFoo.apk": nil,
280 "PrebuiltAppFooPriv.apk": nil,
281 "build/make/target/product/security": nil,
282 "apex_manifest.json": nil,
283 "AndroidManifest.xml": nil,
284 "system/sepolicy/apex/myapex-file_contexts": nil,
285 "system/sepolicy/apex/otherapex-file_contexts": nil,
286 "system/sepolicy/apex/commonapex-file_contexts": nil,
287 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
288 "mylib.cpp": nil,
289 "mylib_common.cpp": nil,
290 "mytest.cpp": nil,
291 "mytest1.cpp": nil,
292 "mytest2.cpp": nil,
293 "mytest3.cpp": nil,
294 "myprebuilt": nil,
295 "my_include": nil,
296 "foo/bar/MyClass.java": nil,
297 "prebuilt.jar": nil,
298 "vendor/foo/devkeys/test.x509.pem": nil,
299 "vendor/foo/devkeys/test.pk8": nil,
300 "testkey.x509.pem": nil,
301 "testkey.pk8": nil,
302 "testkey.override.x509.pem": nil,
303 "testkey.override.pk8": nil,
304 "vendor/foo/devkeys/testkey.avbpubkey": nil,
305 "vendor/foo/devkeys/testkey.pem": nil,
306 "NOTICE": nil,
307 "custom_notice": nil,
308 "testkey2.avbpubkey": nil,
309 "testkey2.pem": nil,
310 "myapex-arm64.apex": nil,
311 "myapex-arm.apex": nil,
312 "frameworks/base/api/current.txt": nil,
313 "framework/aidl/a.aidl": nil,
314 "build/make/core/proguard.flags": nil,
315 "build/make/core/proguard_basic_keeps.flags": nil,
316 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900317 }
318
319 for _, handler := range handlers {
320 handler(fs, config)
321 }
322
323 ctx.MockFileSystem(fs)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324
Jooyung Han5c998b92019-06-27 11:30:33 +0900325 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900326}
327
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700328func setUp() {
329 var err error
330 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900331 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700332 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334}
335
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700336func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 os.RemoveAll(buildDir)
338}
339
340// ensure that 'result' contains 'expected'
341func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900342 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 if !strings.Contains(result, expected) {
344 t.Errorf("%q is not found in %q", expected, result)
345 }
346}
347
348// ensures that 'result' does not contain 'notExpected'
349func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900350 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900351 if strings.Contains(result, notExpected) {
352 t.Errorf("%q is found in %q", notExpected, result)
353 }
354}
355
356func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900357 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900358 if !android.InList(expected, result) {
359 t.Errorf("%q is not found in %v", expected, result)
360 }
361}
362
363func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900364 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900365 if android.InList(notExpected, result) {
366 t.Errorf("%q is found in %v", notExpected, result)
367 }
368}
369
Jooyung Hane1633032019-08-01 17:41:43 +0900370func ensureListEmpty(t *testing.T, result []string) {
371 t.Helper()
372 if len(result) > 0 {
373 t.Errorf("%q is expected to be empty", result)
374 }
375}
376
Jiyong Park25fc6a92018-11-18 18:02:45 +0900377// Minimal test
378func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700379 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900380 apex_defaults {
381 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900382 manifest: ":myapex.manifest",
383 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900384 key: "myapex.key",
385 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800386 multilib: {
387 both: {
388 binaries: ["foo",],
389 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900390 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900391 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900392 }
393
Jiyong Park30ca9372019-02-07 16:27:23 +0900394 apex {
395 name: "myapex",
396 defaults: ["myapex-defaults"],
397 }
398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 apex_key {
400 name: "myapex.key",
401 public_key: "testkey.avbpubkey",
402 private_key: "testkey.pem",
403 }
404
Jiyong Park809bb722019-02-13 21:33:49 +0900405 filegroup {
406 name: "myapex.manifest",
407 srcs: ["apex_manifest.json"],
408 }
409
410 filegroup {
411 name: "myapex.androidmanifest",
412 srcs: ["AndroidManifest.xml"],
413 }
414
Jiyong Park25fc6a92018-11-18 18:02:45 +0900415 cc_library {
416 name: "mylib",
417 srcs: ["mylib.cpp"],
418 shared_libs: ["mylib2"],
419 system_shared_libs: [],
420 stl: "none",
421 }
422
Alex Light3d673592019-01-18 14:37:31 -0800423 cc_binary {
424 name: "foo",
425 srcs: ["mylib.cpp"],
426 compile_multilib: "both",
427 multilib: {
428 lib32: {
429 suffix: "32",
430 },
431 lib64: {
432 suffix: "64",
433 },
434 },
435 symlinks: ["foo_link_"],
436 symlink_preferred_arch: true,
437 system_shared_libs: [],
438 static_executable: true,
439 stl: "none",
440 }
441
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 cc_library {
443 name: "mylib2",
444 srcs: ["mylib.cpp"],
445 system_shared_libs: [],
446 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900447 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900448 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900449
450 java_library {
451 name: "myjar",
452 srcs: ["foo/bar/MyClass.java"],
453 sdk_version: "none",
454 system_modules: "none",
455 compile_dex: true,
456 static_libs: ["myotherjar"],
457 }
458
459 java_library {
460 name: "myotherjar",
461 srcs: ["foo/bar/MyClass.java"],
462 sdk_version: "none",
463 system_modules: "none",
464 compile_dex: true,
465 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900466
467 java_import {
468 name: "myprebuiltjar",
469 jars: ["prebuilt.jar"],
470 installable: true,
471 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900472 `)
473
Sundong Ahnabb64432019-10-22 13:58:29 +0900474 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900475
476 optFlags := apexRule.Args["opt_flags"]
477 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700478 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900479 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900480
Jiyong Park25fc6a92018-11-18 18:02:45 +0900481 copyCmds := apexRule.Args["copy_commands"]
482
483 // Ensure that main rule creates an output
484 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
485
486 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900487 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900488 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900489 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900490
491 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900493 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900494
495 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800496 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
497 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900498 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900499 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 // .. but not for java libs
501 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800502
Jiyong Park7f7766d2019-07-25 22:02:35 +0900503 // Ensure that the platform variant ends with _core_shared or _common
Logan Chien3aeedc92018-12-26 15:32:21 +0800504 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
505 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900506 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
507 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900508 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800509
510 // Ensure that all symlinks are present.
511 found_foo_link_64 := false
512 found_foo := false
513 for _, cmd := range strings.Split(copyCmds, " && ") {
514 if strings.HasPrefix(cmd, "ln -s foo64") {
515 if strings.HasSuffix(cmd, "bin/foo") {
516 found_foo = true
517 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
518 found_foo_link_64 = true
519 }
520 }
521 }
522 good := found_foo && found_foo_link_64
523 if !good {
524 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
525 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900526
Sundong Ahnabb64432019-10-22 13:58:29 +0900527 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700528 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700529 if len(noticeInputs) != 2 {
530 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900531 }
532 ensureListContains(t, noticeInputs, "NOTICE")
533 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800534}
535
Jooyung Han01a3ee22019-11-02 02:52:25 +0900536func TestApexManifest(t *testing.T) {
537 ctx, _ := testApex(t, `
538 apex {
539 name: "myapex",
540 key: "myapex.key",
541 }
542
543 apex_key {
544 name: "myapex.key",
545 public_key: "testkey.avbpubkey",
546 private_key: "testkey.pem",
547 }
548 `)
549
550 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
551 module.Output("apex_manifest.pb")
552 module.Output("apex_manifest.json")
553 module.Output("apex_manifest_full.json")
554}
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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900594 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800595
596 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900597 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900668 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900671 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900673 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_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)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900676 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900677 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900678 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900679
680 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900681 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900685 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jiyong Park25fc6a92018-11-18 18:02:45 +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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900742 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900745 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900746 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900747 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900748
Jiyong Parkda6eb592018-12-19 17:12:36 +0900749 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900955 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
956 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
957 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900961 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900963 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900970 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900972 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_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
Jiyong Parkda6eb592018-12-19 17:12:36 +0900979 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900981 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
982 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900983 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900984 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_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
990 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
991 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
992 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
993 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_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
1104 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
1105 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
1106}
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
1198 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
1199
1200 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +08001201 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_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
Jiyong Park58e364a2019-01-19 19:24:06 +09001288 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_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
Jiyong Park58e364a2019-01-19 19:24:06 +09001294 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_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
Jiyong Park58e364a2019-01-19 19:24:06 +09001300 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_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
1348 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1349
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
1974 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1975
1976 // Ensure that both direct and indirect deps are copied into apex
1977 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1978
1979 // Ensure that the platform variant ends with _core_shared
1980 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1981
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
2025 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
2026
2027 // Ensure that both direct and indirect deps are copied into apex
2028 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2029
2030 // Ensure that the platform variant ends with _core_shared
2031 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
2032
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
2104 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2105 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
2106 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
2107
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
2113 // Ensure that the platform variant ends with _core_shared
2114 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
2115 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
2116 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
2117}
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 Parkb0a012c2019-11-14 17:17:03 +09002384 actual := android.AndroidMkEntriesForTest(t, config, "", p).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")
2456 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.json.myapex\n")
2457 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 Han5c998b92019-06-27 11:30:33 +09002461func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002462 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002463 apex {
2464 name: "myapex",
2465 key: "myapex.key",
2466 native_shared_libs: ["mylib"],
2467 uses: ["commonapex"],
2468 }
2469
2470 apex {
2471 name: "commonapex",
2472 key: "myapex.key",
2473 native_shared_libs: ["libcommon"],
2474 provide_cpp_shared_libs: true,
2475 }
2476
2477 apex_key {
2478 name: "myapex.key",
2479 public_key: "testkey.avbpubkey",
2480 private_key: "testkey.pem",
2481 }
2482
2483 cc_library {
2484 name: "mylib",
2485 srcs: ["mylib.cpp"],
2486 shared_libs: ["libcommon"],
2487 system_shared_libs: [],
2488 stl: "none",
2489 }
2490
2491 cc_library {
2492 name: "libcommon",
2493 srcs: ["mylib_common.cpp"],
2494 system_shared_libs: [],
2495 stl: "none",
2496 }
2497 `)
2498
Sundong Ahnabb64432019-10-22 13:58:29 +09002499 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002500 apexRule1 := module1.Rule("apexRule")
2501 copyCmds1 := apexRule1.Args["copy_commands"]
2502
Sundong Ahnabb64432019-10-22 13:58:29 +09002503 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002504 apexRule2 := module2.Rule("apexRule")
2505 copyCmds2 := apexRule2.Args["copy_commands"]
2506
2507 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
2508 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
2509 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2510 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2511 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2512}
2513
2514func TestApexUsesFailsIfNotProvided(t *testing.T) {
2515 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2516 apex {
2517 name: "myapex",
2518 key: "myapex.key",
2519 uses: ["commonapex"],
2520 }
2521
2522 apex {
2523 name: "commonapex",
2524 key: "myapex.key",
2525 }
2526
2527 apex_key {
2528 name: "myapex.key",
2529 public_key: "testkey.avbpubkey",
2530 private_key: "testkey.pem",
2531 }
2532 `)
2533 testApexError(t, `uses: "commonapex" is not a provider`, `
2534 apex {
2535 name: "myapex",
2536 key: "myapex.key",
2537 uses: ["commonapex"],
2538 }
2539
2540 cc_library {
2541 name: "commonapex",
2542 system_shared_libs: [],
2543 stl: "none",
2544 }
2545
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
2551 `)
2552}
2553
2554func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2555 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2556 apex {
2557 name: "myapex",
2558 key: "myapex.key",
2559 use_vendor: true,
2560 uses: ["commonapex"],
2561 }
2562
2563 apex {
2564 name: "commonapex",
2565 key: "myapex.key",
2566 provide_cpp_shared_libs: true,
2567 }
2568
2569 apex_key {
2570 name: "myapex.key",
2571 public_key: "testkey.avbpubkey",
2572 private_key: "testkey.pem",
2573 }
Jooyung Handc782442019-11-01 03:14:38 +09002574 `, func(fs map[string][]byte, config android.Config) {
2575 setUseVendorWhitelistForTest(config, []string{"myapex"})
2576 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002577}
2578
Jooyung Hand48f3c32019-08-23 11:18:57 +09002579func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2580 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2581 apex {
2582 name: "myapex",
2583 key: "myapex.key",
2584 native_shared_libs: ["libfoo"],
2585 }
2586
2587 apex_key {
2588 name: "myapex.key",
2589 public_key: "testkey.avbpubkey",
2590 private_key: "testkey.pem",
2591 }
2592
2593 cc_library {
2594 name: "libfoo",
2595 stl: "none",
2596 system_shared_libs: [],
2597 enabled: false,
2598 }
2599 `)
2600 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2601 apex {
2602 name: "myapex",
2603 key: "myapex.key",
2604 java_libs: ["myjar"],
2605 }
2606
2607 apex_key {
2608 name: "myapex.key",
2609 public_key: "testkey.avbpubkey",
2610 private_key: "testkey.pem",
2611 }
2612
2613 java_library {
2614 name: "myjar",
2615 srcs: ["foo/bar/MyClass.java"],
2616 sdk_version: "none",
2617 system_modules: "none",
2618 compile_dex: true,
2619 enabled: false,
2620 }
2621 `)
2622}
2623
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002624func TestApexWithApps(t *testing.T) {
2625 ctx, _ := testApex(t, `
2626 apex {
2627 name: "myapex",
2628 key: "myapex.key",
2629 apps: [
2630 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002631 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002632 ],
2633 }
2634
2635 apex_key {
2636 name: "myapex.key",
2637 public_key: "testkey.avbpubkey",
2638 private_key: "testkey.pem",
2639 }
2640
2641 android_app {
2642 name: "AppFoo",
2643 srcs: ["foo/bar/MyClass.java"],
2644 sdk_version: "none",
2645 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002646 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002647 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002648
2649 android_app {
2650 name: "AppFooPriv",
2651 srcs: ["foo/bar/MyClass.java"],
2652 sdk_version: "none",
2653 system_modules: "none",
2654 privileged: true,
2655 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002656
2657 cc_library_shared {
2658 name: "libjni",
2659 srcs: ["mylib.cpp"],
2660 stl: "none",
2661 system_shared_libs: [],
2662 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002663 `)
2664
Sundong Ahnabb64432019-10-22 13:58:29 +09002665 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002666 apexRule := module.Rule("apexRule")
2667 copyCmds := apexRule.Args["copy_commands"]
2668
2669 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002670 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002671
2672 // JNI libraries are embedded inside APK
2673 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
2674 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_core_shared_myapex").Module().(*cc.Module).OutputFile()
2675 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2676 // ... uncompressed
2677 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2678 t.Errorf("jni lib is not uncompressed for AppFoo")
2679 }
2680 // ... and not directly inside the APEX
2681 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002682}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002683
Dario Frenicde2a032019-10-27 00:29:22 +01002684func TestApexWithAppImports(t *testing.T) {
2685 ctx, _ := testApex(t, `
2686 apex {
2687 name: "myapex",
2688 key: "myapex.key",
2689 apps: [
2690 "AppFooPrebuilt",
2691 "AppFooPrivPrebuilt",
2692 ],
2693 }
2694
2695 apex_key {
2696 name: "myapex.key",
2697 public_key: "testkey.avbpubkey",
2698 private_key: "testkey.pem",
2699 }
2700
2701 android_app_import {
2702 name: "AppFooPrebuilt",
2703 apk: "PrebuiltAppFoo.apk",
2704 presigned: true,
2705 dex_preopt: {
2706 enabled: false,
2707 },
2708 }
2709
2710 android_app_import {
2711 name: "AppFooPrivPrebuilt",
2712 apk: "PrebuiltAppFooPriv.apk",
2713 privileged: true,
2714 presigned: true,
2715 dex_preopt: {
2716 enabled: false,
2717 },
2718 }
2719 `)
2720
Sundong Ahnabb64432019-10-22 13:58:29 +09002721 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002722 apexRule := module.Rule("apexRule")
2723 copyCmds := apexRule.Args["copy_commands"]
2724
2725 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2726 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002727}
2728
Jooyung Han18020ea2019-11-13 10:50:48 +09002729func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2730 // libfoo's apex_available comes from cc_defaults
2731 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2732 apex {
2733 name: "myapex",
2734 key: "myapex.key",
2735 native_shared_libs: ["libfoo"],
2736 }
2737
2738 apex_key {
2739 name: "myapex.key",
2740 public_key: "testkey.avbpubkey",
2741 private_key: "testkey.pem",
2742 }
2743
2744 apex {
2745 name: "otherapex",
2746 key: "myapex.key",
2747 native_shared_libs: ["libfoo"],
2748 }
2749
2750 cc_defaults {
2751 name: "libfoo-defaults",
2752 apex_available: ["otherapex"],
2753 }
2754
2755 cc_library {
2756 name: "libfoo",
2757 defaults: ["libfoo-defaults"],
2758 stl: "none",
2759 system_shared_libs: [],
2760 }`)
2761}
2762
Jiyong Park127b40b2019-09-30 16:04:35 +09002763func TestApexAvailable(t *testing.T) {
2764 // libfoo is not available to myapex, but only to otherapex
2765 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2766 apex {
2767 name: "myapex",
2768 key: "myapex.key",
2769 native_shared_libs: ["libfoo"],
2770 }
2771
2772 apex_key {
2773 name: "myapex.key",
2774 public_key: "testkey.avbpubkey",
2775 private_key: "testkey.pem",
2776 }
2777
2778 apex {
2779 name: "otherapex",
2780 key: "otherapex.key",
2781 native_shared_libs: ["libfoo"],
2782 }
2783
2784 apex_key {
2785 name: "otherapex.key",
2786 public_key: "testkey.avbpubkey",
2787 private_key: "testkey.pem",
2788 }
2789
2790 cc_library {
2791 name: "libfoo",
2792 stl: "none",
2793 system_shared_libs: [],
2794 apex_available: ["otherapex"],
2795 }`)
2796
2797 // libbar is an indirect dep
2798 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2799 apex {
2800 name: "myapex",
2801 key: "myapex.key",
2802 native_shared_libs: ["libfoo"],
2803 }
2804
2805 apex_key {
2806 name: "myapex.key",
2807 public_key: "testkey.avbpubkey",
2808 private_key: "testkey.pem",
2809 }
2810
2811 apex {
2812 name: "otherapex",
2813 key: "otherapex.key",
2814 native_shared_libs: ["libfoo"],
2815 }
2816
2817 apex_key {
2818 name: "otherapex.key",
2819 public_key: "testkey.avbpubkey",
2820 private_key: "testkey.pem",
2821 }
2822
2823 cc_library {
2824 name: "libfoo",
2825 stl: "none",
2826 shared_libs: ["libbar"],
2827 system_shared_libs: [],
2828 apex_available: ["myapex", "otherapex"],
2829 }
2830
2831 cc_library {
2832 name: "libbar",
2833 stl: "none",
2834 system_shared_libs: [],
2835 apex_available: ["otherapex"],
2836 }`)
2837
2838 testApexError(t, "\"otherapex\" is not a valid module name", `
2839 apex {
2840 name: "myapex",
2841 key: "myapex.key",
2842 native_shared_libs: ["libfoo"],
2843 }
2844
2845 apex_key {
2846 name: "myapex.key",
2847 public_key: "testkey.avbpubkey",
2848 private_key: "testkey.pem",
2849 }
2850
2851 cc_library {
2852 name: "libfoo",
2853 stl: "none",
2854 system_shared_libs: [],
2855 apex_available: ["otherapex"],
2856 }`)
2857
2858 ctx, _ := testApex(t, `
2859 apex {
2860 name: "myapex",
2861 key: "myapex.key",
2862 native_shared_libs: ["libfoo", "libbar"],
2863 }
2864
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870
2871 cc_library {
2872 name: "libfoo",
2873 stl: "none",
2874 system_shared_libs: [],
2875 apex_available: ["myapex"],
2876 }
2877
2878 cc_library {
2879 name: "libbar",
2880 stl: "none",
2881 system_shared_libs: [],
2882 apex_available: ["//apex_available:anyapex"],
2883 }`)
2884
2885 // check that libfoo and libbar are created only for myapex, but not for the platform
2886 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2887 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2888 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared_myapex")
2889 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_core_shared")
2890
2891 ctx, _ = testApex(t, `
2892 apex {
2893 name: "myapex",
2894 key: "myapex.key",
2895 }
2896
2897 apex_key {
2898 name: "myapex.key",
2899 public_key: "testkey.avbpubkey",
2900 private_key: "testkey.pem",
2901 }
2902
2903 cc_library {
2904 name: "libfoo",
2905 stl: "none",
2906 system_shared_libs: [],
2907 apex_available: ["//apex_available:platform"],
2908 }`)
2909
2910 // check that libfoo is created only for the platform
2911 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2912 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002913
2914 ctx, _ = testApex(t, `
2915 apex {
2916 name: "myapex",
2917 key: "myapex.key",
2918 native_shared_libs: ["libfoo"],
2919 }
2920
2921 apex_key {
2922 name: "myapex.key",
2923 public_key: "testkey.avbpubkey",
2924 private_key: "testkey.pem",
2925 }
2926
2927 cc_library {
2928 name: "libfoo",
2929 stl: "none",
2930 system_shared_libs: [],
2931 apex_available: ["myapex"],
2932 static: {
2933 apex_available: ["//apex_available:platform"],
2934 },
2935 }`)
2936
2937 // shared variant of libfoo is only available to myapex
2938 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared_myapex")
2939 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_shared")
2940 // but the static variant is available to both myapex and the platform
2941 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static_myapex")
2942 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_core_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09002943}
2944
Jiyong Park5d790c32019-11-15 18:40:32 +09002945func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002946 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09002947 apex {
2948 name: "myapex",
2949 key: "myapex.key",
2950 apps: ["app"],
2951 }
2952
2953 override_apex {
2954 name: "override_myapex",
2955 base: "myapex",
2956 apps: ["override_app"],
2957 }
2958
2959 apex_key {
2960 name: "myapex.key",
2961 public_key: "testkey.avbpubkey",
2962 private_key: "testkey.pem",
2963 }
2964
2965 android_app {
2966 name: "app",
2967 srcs: ["foo/bar/MyClass.java"],
2968 package_name: "foo",
2969 sdk_version: "none",
2970 system_modules: "none",
2971 }
2972
2973 override_android_app {
2974 name: "override_app",
2975 base: "app",
2976 package_name: "bar",
2977 }
2978 `)
2979
2980 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
2981 apexRule := module.Rule("apexRule")
2982 copyCmds := apexRule.Args["copy_commands"]
2983
2984 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
2985 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002986
2987 apexBundle := module.Module().(*apexBundle)
2988 name := apexBundle.Name()
2989 if name != "override_myapex" {
2990 t.Errorf("name should be \"override_myapex\", but was %q", name)
2991 }
2992
2993 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2994 var builder strings.Builder
2995 data.Custom(&builder, name, "TARGET_", "", data)
2996 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09002997 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002998 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
2999 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3000 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003001 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003002 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3003 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003004}
3005
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003006func TestMain(m *testing.M) {
3007 run := func() int {
3008 setUp()
3009 defer tearDown()
3010
3011 return m.Run()
3012 }
3013
3014 os.Exit(run())
3015}