blob: aa70b501792356958253225d094cb4ccb0217ccc [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Park25fc6a92018-11-18 18:02:45 +090018 "io/ioutil"
19 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Jooyung Han31c470b2019-10-18 16:26:59 +090022 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090023 "strings"
24 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090030 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090031)
32
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070033var buildDir string
34
Jooyung Hand3639552019-08-09 12:57:43 +090035// names returns name list from white space separated string
36func names(s string) (ns []string) {
37 for _, n := range strings.Split(s, " ") {
38 if len(n) > 0 {
39 ns = append(ns, n)
40 }
41 }
42 return
43}
44
Jooyung Han344d5432019-08-23 11:17:39 +090045func testApexError(t *testing.T, pattern, bp string, handlers ...testCustomizer) {
46 t.Helper()
47 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090048 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
49 if len(errs) > 0 {
50 android.FailIfNoMatchingErrors(t, pattern, errs)
51 return
52 }
53 _, errs = ctx.PrepareBuildActions(config)
54 if len(errs) > 0 {
55 android.FailIfNoMatchingErrors(t, pattern, errs)
56 return
57 }
58
59 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
60}
61
Jooyung Han344d5432019-08-23 11:17:39 +090062func testApex(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
63 t.Helper()
64 ctx, config := testApexContext(t, bp, handlers...)
Jooyung Han5c998b92019-06-27 11:30:33 +090065 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
66 android.FailIfErrored(t, errs)
67 _, errs = ctx.PrepareBuildActions(config)
68 android.FailIfErrored(t, errs)
Jaewoong Jung22f7d182019-07-16 18:25:41 -070069 return ctx, config
Jooyung Han5c998b92019-06-27 11:30:33 +090070}
71
Jooyung Han344d5432019-08-23 11:17:39 +090072type testCustomizer func(fs map[string][]byte, config android.Config)
73
74func withFiles(files map[string][]byte) testCustomizer {
75 return func(fs map[string][]byte, config android.Config) {
76 for k, v := range files {
77 fs[k] = v
78 }
79 }
80}
81
82func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
83 return func(fs map[string][]byte, config android.Config) {
84 for k, v := range targets {
85 config.Targets[k] = v
86 }
87 }
88}
89
Jooyung Han31c470b2019-10-18 16:26:59 +090090func withBinder32bit(fs map[string][]byte, config android.Config) {
91 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
92}
93
Jooyung Han344d5432019-08-23 11:17:39 +090094func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*android.TestContext, android.Config) {
Jooyung Han671f1ce2019-12-17 12:47:13 +090095 android.ClearApexDependency()
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070096 config := android.TestArchConfig(buildDir, nil)
97 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
98 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
99 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
100 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
101 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jooyung Han344d5432019-08-23 11:17:39 +0900102 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103
104 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -0800105 ctx.RegisterModuleType("apex", BundleFactory)
106 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
107 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
108 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
109 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
110 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
111 ctx.RegisterModuleType("override_apex", overrideApexFactory)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900112
Colin Cross4b49b762019-11-22 15:25:03 -0800113 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
114 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
115 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
116 ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory)
117 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
118 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
119 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
120 ctx.RegisterModuleType("cc_defaults", func() android.Module {
Jooyung Han18020ea2019-11-13 10:50:48 +0900121 return cc.DefaultsFactory()
Colin Cross4b49b762019-11-22 15:25:03 -0800122 })
123 ctx.RegisterModuleType("cc_test", cc.TestFactory)
124 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
125 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
126 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
127 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
128 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
129 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
130 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
131 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
132 ctx.RegisterModuleType("java_library", java.LibraryFactory)
133 ctx.RegisterModuleType("java_import", java.ImportFactory)
134 ctx.RegisterModuleType("java_system_modules", java.SystemModulesFactory)
135 ctx.RegisterModuleType("android_app", java.AndroidAppFactory)
136 ctx.RegisterModuleType("android_app_import", java.AndroidAppImportFactory)
137 ctx.RegisterModuleType("override_android_app", java.OverrideAndroidAppModuleFactory)
Jiyong Park7f7766d2019-07-25 22:02:35 +0900138
Jooyung Han344d5432019-08-23 11:17:39 +0900139 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jaewoong Jung939ebd52019-03-26 15:07:36 -0700140 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
141 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
142 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900143 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +0900144 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jooyung Han214bf372019-11-12 13:03:50 +0900145 ctx.BottomUp("link", cc.LinkageMutator).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
Colin Cross7113d202019-11-20 16:39:12 -0800487 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_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
Colin Cross7113d202019-11-20 16:39:12 -0800492 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_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
Colin Cross7113d202019-11-20 16:39:12 -0800503 // Ensure that the platform variant ends with _shared or _common
504 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
505 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_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")
Jooyung Han214bf372019-11-12 13:03:50 +0900551 args := module.Rule("apexRule").Args
552 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
553 t.Error("manifest should be apex_manifest.pb, but " + manifest)
554 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900555}
556
Alex Light5098a612018-11-29 17:12:15 -0800557func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700558 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800559 apex {
560 name: "myapex",
561 key: "myapex.key",
562 payload_type: "zip",
563 native_shared_libs: ["mylib"],
564 }
565
566 apex_key {
567 name: "myapex.key",
568 public_key: "testkey.avbpubkey",
569 private_key: "testkey.pem",
570 }
571
572 cc_library {
573 name: "mylib",
574 srcs: ["mylib.cpp"],
575 shared_libs: ["mylib2"],
576 system_shared_libs: [],
577 stl: "none",
578 }
579
580 cc_library {
581 name: "mylib2",
582 srcs: ["mylib.cpp"],
583 system_shared_libs: [],
584 stl: "none",
585 }
586 `)
587
Sundong Ahnabb64432019-10-22 13:58:29 +0900588 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800589 copyCmds := zipApexRule.Args["copy_commands"]
590
591 // Ensure that main rule creates an output
592 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
593
594 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800595 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800596
597 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800598 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800599
600 // Ensure that both direct and indirect deps are copied into apex
601 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
602 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900603}
604
605func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700606 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900607 apex {
608 name: "myapex",
609 key: "myapex.key",
610 native_shared_libs: ["mylib", "mylib3"],
611 }
612
613 apex_key {
614 name: "myapex.key",
615 public_key: "testkey.avbpubkey",
616 private_key: "testkey.pem",
617 }
618
619 cc_library {
620 name: "mylib",
621 srcs: ["mylib.cpp"],
622 shared_libs: ["mylib2", "mylib3"],
623 system_shared_libs: [],
624 stl: "none",
625 }
626
627 cc_library {
628 name: "mylib2",
629 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900630 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631 system_shared_libs: [],
632 stl: "none",
633 stubs: {
634 versions: ["1", "2", "3"],
635 },
636 }
637
638 cc_library {
639 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900640 srcs: ["mylib.cpp"],
641 shared_libs: ["mylib4"],
642 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643 stl: "none",
644 stubs: {
645 versions: ["10", "11", "12"],
646 },
647 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900648
649 cc_library {
650 name: "mylib4",
651 srcs: ["mylib.cpp"],
652 system_shared_libs: [],
653 stl: "none",
654 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655 `)
656
Sundong Ahnabb64432019-10-22 13:58:29 +0900657 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658 copyCmds := apexRule.Args["copy_commands"]
659
660 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800661 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900662
663 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800664 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900665
666 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800667 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900668
Colin Cross7113d202019-11-20 16:39:12 -0800669 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900670
671 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800672 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900673 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800674 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900675
676 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Cross7113d202019-11-20 16:39:12 -0800677 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900678 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800679 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900680
681 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800682 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900683 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900684
685 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800686 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900687
688 ensureExactContents(t, ctx, "myapex", []string{
689 "lib64/mylib.so",
690 "lib64/mylib3.so",
691 "lib64/mylib4.so",
692 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693}
694
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900695func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700696 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900697 apex {
698 name: "myapex",
699 key: "myapex.key",
700 native_shared_libs: ["mylib"],
701 }
702
703 apex_key {
704 name: "myapex.key",
705 public_key: "testkey.avbpubkey",
706 private_key: "testkey.pem",
707 }
708
709 cc_library {
710 name: "mylib",
711 srcs: ["mylib.cpp"],
712 shared_libs: ["libfoo#10"],
713 system_shared_libs: [],
714 stl: "none",
715 }
716
717 cc_library {
718 name: "libfoo",
719 srcs: ["mylib.cpp"],
720 shared_libs: ["libbar"],
721 system_shared_libs: [],
722 stl: "none",
723 stubs: {
724 versions: ["10", "20", "30"],
725 },
726 }
727
728 cc_library {
729 name: "libbar",
730 srcs: ["mylib.cpp"],
731 system_shared_libs: [],
732 stl: "none",
733 }
734
735 `)
736
Sundong Ahnabb64432019-10-22 13:58:29 +0900737 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900738 copyCmds := apexRule.Args["copy_commands"]
739
740 // Ensure that direct non-stubs dep is always included
741 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
742
743 // Ensure that indirect stubs dep is not included
744 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
745
746 // Ensure that dependency of stubs is not included
747 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
748
Colin Cross7113d202019-11-20 16:39:12 -0800749 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900750
751 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800752 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900753 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800754 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900755
Colin Cross7113d202019-11-20 16:39:12 -0800756 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900757
758 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
759 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
760}
761
Jooyung Hand3639552019-08-09 12:57:43 +0900762func TestApexWithRuntimeLibsDependency(t *testing.T) {
763 /*
764 myapex
765 |
766 v (runtime_libs)
767 mylib ------+------> libfoo [provides stub]
768 |
769 `------> libbar
770 */
771 ctx, _ := testApex(t, `
772 apex {
773 name: "myapex",
774 key: "myapex.key",
775 native_shared_libs: ["mylib"],
776 }
777
778 apex_key {
779 name: "myapex.key",
780 public_key: "testkey.avbpubkey",
781 private_key: "testkey.pem",
782 }
783
784 cc_library {
785 name: "mylib",
786 srcs: ["mylib.cpp"],
787 runtime_libs: ["libfoo", "libbar"],
788 system_shared_libs: [],
789 stl: "none",
790 }
791
792 cc_library {
793 name: "libfoo",
794 srcs: ["mylib.cpp"],
795 system_shared_libs: [],
796 stl: "none",
797 stubs: {
798 versions: ["10", "20", "30"],
799 },
800 }
801
802 cc_library {
803 name: "libbar",
804 srcs: ["mylib.cpp"],
805 system_shared_libs: [],
806 stl: "none",
807 }
808
809 `)
810
Sundong Ahnabb64432019-10-22 13:58:29 +0900811 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900812 copyCmds := apexRule.Args["copy_commands"]
813
814 // Ensure that direct non-stubs dep is always included
815 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
816
817 // Ensure that indirect stubs dep is not included
818 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
819
820 // Ensure that runtime_libs dep in included
821 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
822
Sundong Ahnabb64432019-10-22 13:58:29 +0900823 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900824 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
825 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900826
827}
828
Jooyung Han9c80bae2019-08-20 17:30:57 +0900829func TestApexDependencyToLLNDK(t *testing.T) {
830 ctx, _ := testApex(t, `
831 apex {
832 name: "myapex",
833 key: "myapex.key",
834 use_vendor: true,
835 native_shared_libs: ["mylib"],
836 }
837
838 apex_key {
839 name: "myapex.key",
840 public_key: "testkey.avbpubkey",
841 private_key: "testkey.pem",
842 }
843
844 cc_library {
845 name: "mylib",
846 srcs: ["mylib.cpp"],
847 vendor_available: true,
848 shared_libs: ["libbar"],
849 system_shared_libs: [],
850 stl: "none",
851 }
852
853 cc_library {
854 name: "libbar",
855 srcs: ["mylib.cpp"],
856 system_shared_libs: [],
857 stl: "none",
858 }
859
860 llndk_library {
861 name: "libbar",
862 symbol_file: "",
863 }
Jooyung Handc782442019-11-01 03:14:38 +0900864 `, func(fs map[string][]byte, config android.Config) {
865 setUseVendorWhitelistForTest(config, []string{"myapex"})
866 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900867
Sundong Ahnabb64432019-10-22 13:58:29 +0900868 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900869 copyCmds := apexRule.Args["copy_commands"]
870
871 // Ensure that LLNDK dep is not included
872 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
873
Sundong Ahnabb64432019-10-22 13:58:29 +0900874 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900875 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900876
877 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900878 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900879
880}
881
Jiyong Park25fc6a92018-11-18 18:02:45 +0900882func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700883 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900884 apex {
885 name: "myapex",
886 key: "myapex.key",
887 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
888 }
889
890 apex_key {
891 name: "myapex.key",
892 public_key: "testkey.avbpubkey",
893 private_key: "testkey.pem",
894 }
895
896 cc_library {
897 name: "mylib",
898 srcs: ["mylib.cpp"],
899 shared_libs: ["libdl#27"],
900 stl: "none",
901 }
902
903 cc_library_shared {
904 name: "mylib_shared",
905 srcs: ["mylib.cpp"],
906 shared_libs: ["libdl#27"],
907 stl: "none",
908 }
909
910 cc_library {
911 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700912 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900913 nocrt: true,
914 system_shared_libs: [],
915 stl: "none",
916 stubs: {
917 versions: ["27", "28", "29"],
918 },
919 }
920
921 cc_library {
922 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700923 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924 nocrt: true,
925 system_shared_libs: [],
926 stl: "none",
927 stubs: {
928 versions: ["27", "28", "29"],
929 },
930 }
931
932 cc_library {
933 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700934 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935 nocrt: true,
936 system_shared_libs: [],
937 stl: "none",
938 stubs: {
939 versions: ["27", "28", "29"],
940 },
941 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900942
943 cc_library {
944 name: "libBootstrap",
945 srcs: ["mylib.cpp"],
946 stl: "none",
947 bootstrap: true,
948 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 `)
950
Sundong Ahnabb64432019-10-22 13:58:29 +0900951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 copyCmds := apexRule.Args["copy_commands"]
953
954 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900956 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
957 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900958
959 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900960 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900961
Colin Cross7113d202019-11-20 16:39:12 -0800962 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
963 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
964 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900965
966 // For dependency to libc
967 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800968 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800970 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971 // ... Cflags from stub is correctly exported to mylib
972 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
973 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
974
975 // For dependency to libm
976 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800977 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900978 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -0800979 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 // ... and is not compiling with the stub
981 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
982 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
983
984 // For dependency to libdl
985 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800986 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800988 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
989 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -0800991 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 // ... Cflags from stub is correctly exported to mylib
993 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
994 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900995
996 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -0800997 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
998 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
999 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1000 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001002
1003func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001004 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001005 apex {
1006 name: "myapex",
1007 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001008 native_shared_libs: ["mylib"],
1009 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001010 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001011 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001012 }
1013
1014 apex_key {
1015 name: "myapex.key",
1016 public_key: "testkey.avbpubkey",
1017 private_key: "testkey.pem",
1018 }
1019
1020 prebuilt_etc {
1021 name: "myetc",
1022 src: "myprebuilt",
1023 sub_dir: "foo/bar",
1024 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001025
1026 cc_library {
1027 name: "mylib",
1028 srcs: ["mylib.cpp"],
1029 relative_install_path: "foo/bar",
1030 system_shared_libs: [],
1031 stl: "none",
1032 }
1033
1034 cc_binary {
1035 name: "mybin",
1036 srcs: ["mylib.cpp"],
1037 relative_install_path: "foo/bar",
1038 system_shared_libs: [],
1039 static_executable: true,
1040 stl: "none",
1041 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001042 `)
1043
Sundong Ahnabb64432019-10-22 13:58:29 +09001044 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001045 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1046
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001047 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001048 ensureListContains(t, dirs, "etc")
1049 ensureListContains(t, dirs, "etc/foo")
1050 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001051 ensureListContains(t, dirs, "lib64")
1052 ensureListContains(t, dirs, "lib64/foo")
1053 ensureListContains(t, dirs, "lib64/foo/bar")
1054 ensureListContains(t, dirs, "lib")
1055 ensureListContains(t, dirs, "lib/foo")
1056 ensureListContains(t, dirs, "lib/foo/bar")
1057
Jiyong Parkbd13e442019-03-15 18:10:35 +09001058 ensureListContains(t, dirs, "bin")
1059 ensureListContains(t, dirs, "bin/foo")
1060 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001061}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001062
1063func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001064 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001065 apex {
1066 name: "myapex",
1067 key: "myapex.key",
1068 native_shared_libs: ["mylib"],
1069 use_vendor: true,
1070 }
1071
1072 apex_key {
1073 name: "myapex.key",
1074 public_key: "testkey.avbpubkey",
1075 private_key: "testkey.pem",
1076 }
1077
1078 cc_library {
1079 name: "mylib",
1080 srcs: ["mylib.cpp"],
1081 shared_libs: ["mylib2"],
1082 system_shared_libs: [],
1083 vendor_available: true,
1084 stl: "none",
1085 }
1086
1087 cc_library {
1088 name: "mylib2",
1089 srcs: ["mylib.cpp"],
1090 system_shared_libs: [],
1091 vendor_available: true,
1092 stl: "none",
1093 }
Jooyung Handc782442019-11-01 03:14:38 +09001094 `, func(fs map[string][]byte, config android.Config) {
1095 setUseVendorWhitelistForTest(config, []string{"myapex"})
1096 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001097
1098 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001099 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001100 for _, implicit := range i.Implicits {
1101 inputsList = append(inputsList, implicit.String())
1102 }
1103 }
1104 inputsString := strings.Join(inputsList, " ")
1105
1106 // ensure that the apex includes vendor variants of the direct and indirect deps
Inseob Kim64c43952019-08-26 16:52:35 +09001107 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib.so")
1108 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor.VER_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001109
1110 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001111 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1112 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001113}
Jiyong Park16e91a02018-12-20 18:18:08 +09001114
Jooyung Handc782442019-11-01 03:14:38 +09001115func TestUseVendorRestriction(t *testing.T) {
1116 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1117 apex {
1118 name: "myapex",
1119 key: "myapex.key",
1120 use_vendor: true,
1121 }
1122 apex_key {
1123 name: "myapex.key",
1124 public_key: "testkey.avbpubkey",
1125 private_key: "testkey.pem",
1126 }
1127 `, func(fs map[string][]byte, config android.Config) {
1128 setUseVendorWhitelistForTest(config, []string{""})
1129 })
1130 // no error with whitelist
1131 testApex(t, `
1132 apex {
1133 name: "myapex",
1134 key: "myapex.key",
1135 use_vendor: true,
1136 }
1137 apex_key {
1138 name: "myapex.key",
1139 public_key: "testkey.avbpubkey",
1140 private_key: "testkey.pem",
1141 }
1142 `, func(fs map[string][]byte, config android.Config) {
1143 setUseVendorWhitelistForTest(config, []string{"myapex"})
1144 })
1145}
1146
Jooyung Han5c998b92019-06-27 11:30:33 +09001147func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1148 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1149 apex {
1150 name: "myapex",
1151 key: "myapex.key",
1152 native_shared_libs: ["mylib"],
1153 use_vendor: true,
1154 }
1155
1156 apex_key {
1157 name: "myapex.key",
1158 public_key: "testkey.avbpubkey",
1159 private_key: "testkey.pem",
1160 }
1161
1162 cc_library {
1163 name: "mylib",
1164 srcs: ["mylib.cpp"],
1165 system_shared_libs: [],
1166 stl: "none",
1167 }
1168 `)
1169}
1170
Jiyong Park16e91a02018-12-20 18:18:08 +09001171func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001172 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001173 apex {
1174 name: "myapex",
1175 key: "myapex.key",
1176 native_shared_libs: ["mylib"],
1177 }
1178
1179 apex_key {
1180 name: "myapex.key",
1181 public_key: "testkey.avbpubkey",
1182 private_key: "testkey.pem",
1183 }
1184
1185 cc_library {
1186 name: "mylib",
1187 srcs: ["mylib.cpp"],
1188 system_shared_libs: [],
1189 stl: "none",
1190 stubs: {
1191 versions: ["1", "2", "3"],
1192 },
1193 }
1194
1195 cc_binary {
1196 name: "not_in_apex",
1197 srcs: ["mylib.cpp"],
1198 static_libs: ["mylib"],
1199 static_executable: true,
1200 system_shared_libs: [],
1201 stl: "none",
1202 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001203 `)
1204
Colin Cross7113d202019-11-20 16:39:12 -08001205 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001206
1207 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001208 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001209}
Jiyong Park9335a262018-12-24 11:31:58 +09001210
1211func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001212 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001213 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001214 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001215 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001216 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001217 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001218 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001219 }
1220
1221 cc_library {
1222 name: "mylib",
1223 srcs: ["mylib.cpp"],
1224 system_shared_libs: [],
1225 stl: "none",
1226 }
1227
1228 apex_key {
1229 name: "myapex.key",
1230 public_key: "testkey.avbpubkey",
1231 private_key: "testkey.pem",
1232 }
1233
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001234 android_app_certificate {
1235 name: "myapex.certificate",
1236 certificate: "testkey",
1237 }
1238
1239 android_app_certificate {
1240 name: "myapex.certificate.override",
1241 certificate: "testkey.override",
1242 }
1243
Jiyong Park9335a262018-12-24 11:31:58 +09001244 `)
1245
1246 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001247 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001248
1249 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1250 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1251 "vendor/foo/devkeys/testkey.avbpubkey")
1252 }
1253 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1254 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1255 "vendor/foo/devkeys/testkey.pem")
1256 }
1257
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001258 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001259 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001260 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001261 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001262 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001263 }
1264}
Jiyong Park58e364a2019-01-19 19:24:06 +09001265
1266func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001267 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001268 apex {
1269 name: "myapex",
1270 key: "myapex.key",
1271 native_shared_libs: ["mylib"],
1272 }
1273
1274 apex {
1275 name: "otherapex",
1276 key: "myapex.key",
1277 native_shared_libs: ["mylib"],
1278 }
1279
1280 apex_key {
1281 name: "myapex.key",
1282 public_key: "testkey.avbpubkey",
1283 private_key: "testkey.pem",
1284 }
1285
1286 cc_library {
1287 name: "mylib",
1288 srcs: ["mylib.cpp"],
1289 system_shared_libs: [],
1290 stl: "none",
1291 }
1292 `)
1293
Jooyung Han6b8459b2019-10-30 08:29:25 +09001294 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001295 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001296 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001297 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1298 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001299
Jooyung Han6b8459b2019-10-30 08:29:25 +09001300 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001301 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001302 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001303 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1304 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001305
Jooyung Han6b8459b2019-10-30 08:29:25 +09001306 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001307 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001308 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001309 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1310 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001311}
Jiyong Park7e636d02019-01-28 16:16:54 +09001312
1313func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001314 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001315 apex {
1316 name: "myapex",
1317 key: "myapex.key",
1318 native_shared_libs: ["mylib"],
1319 }
1320
1321 apex_key {
1322 name: "myapex.key",
1323 public_key: "testkey.avbpubkey",
1324 private_key: "testkey.pem",
1325 }
1326
1327 cc_library_headers {
1328 name: "mylib_headers",
1329 export_include_dirs: ["my_include"],
1330 system_shared_libs: [],
1331 stl: "none",
1332 }
1333
1334 cc_library {
1335 name: "mylib",
1336 srcs: ["mylib.cpp"],
1337 system_shared_libs: [],
1338 stl: "none",
1339 header_libs: ["mylib_headers"],
1340 export_header_lib_headers: ["mylib_headers"],
1341 stubs: {
1342 versions: ["1", "2", "3"],
1343 },
1344 }
1345
1346 cc_library {
1347 name: "otherlib",
1348 srcs: ["mylib.cpp"],
1349 system_shared_libs: [],
1350 stl: "none",
1351 shared_libs: ["mylib"],
1352 }
1353 `)
1354
Colin Cross7113d202019-11-20 16:39:12 -08001355 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001356
1357 // Ensure that the include path of the header lib is exported to 'otherlib'
1358 ensureContains(t, cFlags, "-Imy_include")
1359}
Alex Light9670d332019-01-29 18:07:33 -08001360
Jooyung Han31c470b2019-10-18 16:26:59 +09001361func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1362 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001363 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001364 copyCmds := apexRule.Args["copy_commands"]
1365 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001366 var failed bool
1367 var surplus []string
1368 filesMatched := make(map[string]bool)
1369 addContent := func(content string) {
1370 for _, expected := range files {
1371 if matched, _ := path.Match(expected, content); matched {
1372 filesMatched[expected] = true
1373 return
1374 }
1375 }
1376 surplus = append(surplus, content)
1377 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001378 for _, cmd := range strings.Split(copyCmds, "&&") {
1379 cmd = strings.TrimSpace(cmd)
1380 if cmd == "" {
1381 continue
1382 }
1383 terms := strings.Split(cmd, " ")
1384 switch terms[0] {
1385 case "mkdir":
1386 case "cp":
1387 if len(terms) != 3 {
1388 t.Fatal("copyCmds contains invalid cp command", cmd)
1389 }
1390 dst := terms[2]
1391 index := strings.Index(dst, imageApexDir)
1392 if index == -1 {
1393 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1394 }
1395 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001396 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001397 default:
1398 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1399 }
1400 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001401
Jooyung Han31c470b2019-10-18 16:26:59 +09001402 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001403 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001404 t.Log("surplus files", surplus)
1405 failed = true
1406 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001407
1408 if len(files) > len(filesMatched) {
1409 var missing []string
1410 for _, expected := range files {
1411 if !filesMatched[expected] {
1412 missing = append(missing, expected)
1413 }
1414 }
1415 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001416 t.Log("missing files", missing)
1417 failed = true
1418 }
1419 if failed {
1420 t.Fail()
1421 }
1422}
1423
Jooyung Han344d5432019-08-23 11:17:39 +09001424func TestVndkApexCurrent(t *testing.T) {
1425 ctx, _ := testApex(t, `
1426 apex_vndk {
1427 name: "myapex",
1428 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001429 }
1430
1431 apex_key {
1432 name: "myapex.key",
1433 public_key: "testkey.avbpubkey",
1434 private_key: "testkey.pem",
1435 }
1436
1437 cc_library {
1438 name: "libvndk",
1439 srcs: ["mylib.cpp"],
1440 vendor_available: true,
1441 vndk: {
1442 enabled: true,
1443 },
1444 system_shared_libs: [],
1445 stl: "none",
1446 }
1447
1448 cc_library {
1449 name: "libvndksp",
1450 srcs: ["mylib.cpp"],
1451 vendor_available: true,
1452 vndk: {
1453 enabled: true,
1454 support_system_process: true,
1455 },
1456 system_shared_libs: [],
1457 stl: "none",
1458 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001459 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001460
Jooyung Han31c470b2019-10-18 16:26:59 +09001461 ensureExactContents(t, ctx, "myapex", []string{
1462 "lib/libvndk.so",
1463 "lib/libvndksp.so",
1464 "lib64/libvndk.so",
1465 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001466 "etc/llndk.libraries.VER.txt",
1467 "etc/vndkcore.libraries.VER.txt",
1468 "etc/vndksp.libraries.VER.txt",
1469 "etc/vndkprivate.libraries.VER.txt",
1470 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001471 })
Jooyung Han344d5432019-08-23 11:17:39 +09001472}
1473
1474func TestVndkApexWithPrebuilt(t *testing.T) {
1475 ctx, _ := testApex(t, `
1476 apex_vndk {
1477 name: "myapex",
1478 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001479 }
1480
1481 apex_key {
1482 name: "myapex.key",
1483 public_key: "testkey.avbpubkey",
1484 private_key: "testkey.pem",
1485 }
1486
1487 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001488 name: "libvndk",
1489 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001490 vendor_available: true,
1491 vndk: {
1492 enabled: true,
1493 },
1494 system_shared_libs: [],
1495 stl: "none",
1496 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001497
1498 cc_prebuilt_library_shared {
1499 name: "libvndk.arm",
1500 srcs: ["libvndk.arm.so"],
1501 vendor_available: true,
1502 vndk: {
1503 enabled: true,
1504 },
1505 enabled: false,
1506 arch: {
1507 arm: {
1508 enabled: true,
1509 },
1510 },
1511 system_shared_libs: [],
1512 stl: "none",
1513 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001514 `+vndkLibrariesTxtFiles("current"),
1515 withFiles(map[string][]byte{
1516 "libvndk.so": nil,
1517 "libvndk.arm.so": nil,
1518 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001519
Jooyung Han31c470b2019-10-18 16:26:59 +09001520 ensureExactContents(t, ctx, "myapex", []string{
1521 "lib/libvndk.so",
1522 "lib/libvndk.arm.so",
1523 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001524 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001525 })
Jooyung Han344d5432019-08-23 11:17:39 +09001526}
1527
Jooyung Han39edb6c2019-11-06 16:53:07 +09001528func vndkLibrariesTxtFiles(vers ...string) (result string) {
1529 for _, v := range vers {
1530 if v == "current" {
1531 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1532 result += `
1533 vndk_libraries_txt {
1534 name: "` + txt + `.libraries.txt",
1535 }
1536 `
1537 }
1538 } else {
1539 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1540 result += `
1541 prebuilt_etc {
1542 name: "` + txt + `.libraries.` + v + `.txt",
1543 src: "dummy.txt",
1544 }
1545 `
1546 }
1547 }
1548 }
1549 return
1550}
1551
Jooyung Han344d5432019-08-23 11:17:39 +09001552func TestVndkApexVersion(t *testing.T) {
1553 ctx, _ := testApex(t, `
1554 apex_vndk {
1555 name: "myapex_v27",
1556 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001557 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001558 vndk_version: "27",
1559 }
1560
1561 apex_key {
1562 name: "myapex.key",
1563 public_key: "testkey.avbpubkey",
1564 private_key: "testkey.pem",
1565 }
1566
Jooyung Han31c470b2019-10-18 16:26:59 +09001567 vndk_prebuilt_shared {
1568 name: "libvndk27",
1569 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001570 vendor_available: true,
1571 vndk: {
1572 enabled: true,
1573 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001574 target_arch: "arm64",
1575 arch: {
1576 arm: {
1577 srcs: ["libvndk27_arm.so"],
1578 },
1579 arm64: {
1580 srcs: ["libvndk27_arm64.so"],
1581 },
1582 },
Jooyung Han344d5432019-08-23 11:17:39 +09001583 }
1584
1585 vndk_prebuilt_shared {
1586 name: "libvndk27",
1587 version: "27",
1588 vendor_available: true,
1589 vndk: {
1590 enabled: true,
1591 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001592 target_arch: "x86_64",
1593 arch: {
1594 x86: {
1595 srcs: ["libvndk27_x86.so"],
1596 },
1597 x86_64: {
1598 srcs: ["libvndk27_x86_64.so"],
1599 },
1600 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001601 }
1602 `+vndkLibrariesTxtFiles("27"),
1603 withFiles(map[string][]byte{
1604 "libvndk27_arm.so": nil,
1605 "libvndk27_arm64.so": nil,
1606 "libvndk27_x86.so": nil,
1607 "libvndk27_x86_64.so": nil,
1608 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001609
Jooyung Han31c470b2019-10-18 16:26:59 +09001610 ensureExactContents(t, ctx, "myapex_v27", []string{
1611 "lib/libvndk27_arm.so",
1612 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001613 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001614 })
Jooyung Han344d5432019-08-23 11:17:39 +09001615}
1616
1617func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1618 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1619 apex_vndk {
1620 name: "myapex_v27",
1621 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001622 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001623 vndk_version: "27",
1624 }
1625 apex_vndk {
1626 name: "myapex_v27_other",
1627 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001628 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001629 vndk_version: "27",
1630 }
1631
1632 apex_key {
1633 name: "myapex.key",
1634 public_key: "testkey.avbpubkey",
1635 private_key: "testkey.pem",
1636 }
1637
1638 cc_library {
1639 name: "libvndk",
1640 srcs: ["mylib.cpp"],
1641 vendor_available: true,
1642 vndk: {
1643 enabled: true,
1644 },
1645 system_shared_libs: [],
1646 stl: "none",
1647 }
1648
1649 vndk_prebuilt_shared {
1650 name: "libvndk",
1651 version: "27",
1652 vendor_available: true,
1653 vndk: {
1654 enabled: true,
1655 },
1656 srcs: ["libvndk.so"],
1657 }
1658 `, withFiles(map[string][]byte{
1659 "libvndk.so": nil,
1660 }))
1661}
1662
Jooyung Han90eee022019-10-01 20:02:42 +09001663func TestVndkApexNameRule(t *testing.T) {
1664 ctx, _ := testApex(t, `
1665 apex_vndk {
1666 name: "myapex",
1667 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001668 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001669 }
1670 apex_vndk {
1671 name: "myapex_v28",
1672 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001673 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001674 vndk_version: "28",
1675 }
1676 apex_key {
1677 name: "myapex.key",
1678 public_key: "testkey.avbpubkey",
1679 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001680 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001681
1682 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001683 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001684 actual := proptools.String(bundle.properties.Apex_name)
1685 if !reflect.DeepEqual(actual, expected) {
1686 t.Errorf("Got '%v', expected '%v'", actual, expected)
1687 }
1688 }
1689
1690 assertApexName("com.android.vndk.vVER", "myapex")
1691 assertApexName("com.android.vndk.v28", "myapex_v28")
1692}
1693
Jooyung Han344d5432019-08-23 11:17:39 +09001694func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1695 ctx, _ := testApex(t, `
1696 apex_vndk {
1697 name: "myapex",
1698 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001699 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001700 }
1701
1702 apex_key {
1703 name: "myapex.key",
1704 public_key: "testkey.avbpubkey",
1705 private_key: "testkey.pem",
1706 }
1707
1708 cc_library {
1709 name: "libvndk",
1710 srcs: ["mylib.cpp"],
1711 vendor_available: true,
1712 native_bridge_supported: true,
1713 host_supported: true,
1714 vndk: {
1715 enabled: true,
1716 },
1717 system_shared_libs: [],
1718 stl: "none",
1719 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001720 `+vndkLibrariesTxtFiles("current"),
1721 withTargets(map[android.OsType][]android.Target{
1722 android.Android: []android.Target{
1723 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1724 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1725 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1726 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1727 },
1728 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001729
Jooyung Han31c470b2019-10-18 16:26:59 +09001730 ensureExactContents(t, ctx, "myapex", []string{
1731 "lib/libvndk.so",
1732 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001733 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001734 })
Jooyung Han344d5432019-08-23 11:17:39 +09001735}
1736
1737func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1738 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1739 apex_vndk {
1740 name: "myapex",
1741 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001742 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001743 native_bridge_supported: true,
1744 }
1745
1746 apex_key {
1747 name: "myapex.key",
1748 public_key: "testkey.avbpubkey",
1749 private_key: "testkey.pem",
1750 }
1751
1752 cc_library {
1753 name: "libvndk",
1754 srcs: ["mylib.cpp"],
1755 vendor_available: true,
1756 native_bridge_supported: true,
1757 host_supported: true,
1758 vndk: {
1759 enabled: true,
1760 },
1761 system_shared_libs: [],
1762 stl: "none",
1763 }
1764 `)
1765}
1766
Jooyung Han31c470b2019-10-18 16:26:59 +09001767func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001768 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001769 apex_vndk {
1770 name: "myapex_v27",
1771 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001772 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001773 vndk_version: "27",
1774 }
1775
1776 apex_key {
1777 name: "myapex.key",
1778 public_key: "testkey.avbpubkey",
1779 private_key: "testkey.pem",
1780 }
1781
1782 vndk_prebuilt_shared {
1783 name: "libvndk27",
1784 version: "27",
1785 target_arch: "arm",
1786 vendor_available: true,
1787 vndk: {
1788 enabled: true,
1789 },
1790 arch: {
1791 arm: {
1792 srcs: ["libvndk27.so"],
1793 }
1794 },
1795 }
1796
1797 vndk_prebuilt_shared {
1798 name: "libvndk27",
1799 version: "27",
1800 target_arch: "arm",
1801 binder32bit: true,
1802 vendor_available: true,
1803 vndk: {
1804 enabled: true,
1805 },
1806 arch: {
1807 arm: {
1808 srcs: ["libvndk27binder32.so"],
1809 }
1810 },
1811 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001812 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001813 withFiles(map[string][]byte{
1814 "libvndk27.so": nil,
1815 "libvndk27binder32.so": nil,
1816 }),
1817 withBinder32bit,
1818 withTargets(map[android.OsType][]android.Target{
1819 android.Android: []android.Target{
1820 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1821 },
1822 }),
1823 )
1824
1825 ensureExactContents(t, ctx, "myapex_v27", []string{
1826 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001827 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001828 })
1829}
1830
Jooyung Hane1633032019-08-01 17:41:43 +09001831func TestDependenciesInApexManifest(t *testing.T) {
1832 ctx, _ := testApex(t, `
1833 apex {
1834 name: "myapex_nodep",
1835 key: "myapex.key",
1836 native_shared_libs: ["lib_nodep"],
1837 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001838 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001839 }
1840
1841 apex {
1842 name: "myapex_dep",
1843 key: "myapex.key",
1844 native_shared_libs: ["lib_dep"],
1845 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001846 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001847 }
1848
1849 apex {
1850 name: "myapex_provider",
1851 key: "myapex.key",
1852 native_shared_libs: ["libfoo"],
1853 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001854 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001855 }
1856
1857 apex {
1858 name: "myapex_selfcontained",
1859 key: "myapex.key",
1860 native_shared_libs: ["lib_dep", "libfoo"],
1861 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001862 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001863 }
1864
1865 apex_key {
1866 name: "myapex.key",
1867 public_key: "testkey.avbpubkey",
1868 private_key: "testkey.pem",
1869 }
1870
1871 cc_library {
1872 name: "lib_nodep",
1873 srcs: ["mylib.cpp"],
1874 system_shared_libs: [],
1875 stl: "none",
1876 }
1877
1878 cc_library {
1879 name: "lib_dep",
1880 srcs: ["mylib.cpp"],
1881 shared_libs: ["libfoo"],
1882 system_shared_libs: [],
1883 stl: "none",
1884 }
1885
1886 cc_library {
1887 name: "libfoo",
1888 srcs: ["mytest.cpp"],
1889 stubs: {
1890 versions: ["1"],
1891 },
1892 system_shared_libs: [],
1893 stl: "none",
1894 }
1895 `)
1896
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001897 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09001898 var provideNativeLibs, requireNativeLibs []string
1899
Sundong Ahnabb64432019-10-22 13:58:29 +09001900 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001901 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1902 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001903 ensureListEmpty(t, provideNativeLibs)
1904 ensureListEmpty(t, requireNativeLibs)
1905
Sundong Ahnabb64432019-10-22 13:58:29 +09001906 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001907 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1908 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001909 ensureListEmpty(t, provideNativeLibs)
1910 ensureListContains(t, requireNativeLibs, "libfoo.so")
1911
Sundong Ahnabb64432019-10-22 13:58:29 +09001912 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001913 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1914 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001915 ensureListContains(t, provideNativeLibs, "libfoo.so")
1916 ensureListEmpty(t, requireNativeLibs)
1917
Sundong Ahnabb64432019-10-22 13:58:29 +09001918 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001919 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
1920 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09001921 ensureListContains(t, provideNativeLibs, "libfoo.so")
1922 ensureListEmpty(t, requireNativeLibs)
1923}
1924
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001925func TestApexName(t *testing.T) {
1926 ctx, _ := testApex(t, `
1927 apex {
1928 name: "myapex",
1929 key: "myapex.key",
1930 apex_name: "com.android.myapex",
1931 }
1932
1933 apex_key {
1934 name: "myapex.key",
1935 public_key: "testkey.avbpubkey",
1936 private_key: "testkey.pem",
1937 }
1938 `)
1939
Sundong Ahnabb64432019-10-22 13:58:29 +09001940 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001941 apexManifestRule := module.Rule("apexManifestRule")
1942 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
1943 apexRule := module.Rule("apexRule")
1944 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
1945}
1946
Alex Light0851b882019-02-07 13:20:53 -08001947func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001948 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08001949 apex {
1950 name: "myapex",
1951 key: "myapex.key",
1952 native_shared_libs: ["mylib_common"],
1953 }
1954
1955 apex_key {
1956 name: "myapex.key",
1957 public_key: "testkey.avbpubkey",
1958 private_key: "testkey.pem",
1959 }
1960
1961 cc_library {
1962 name: "mylib_common",
1963 srcs: ["mylib.cpp"],
1964 system_shared_libs: [],
1965 stl: "none",
1966 }
1967 `)
1968
Sundong Ahnabb64432019-10-22 13:58:29 +09001969 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08001970 apexRule := module.Rule("apexRule")
1971 copyCmds := apexRule.Args["copy_commands"]
1972
1973 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1974 t.Log("Apex was a test apex!")
1975 t.Fail()
1976 }
1977 // Ensure that main rule creates an output
1978 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1979
1980 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08001981 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08001982
1983 // Ensure that both direct and indirect deps are copied into apex
1984 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1985
Colin Cross7113d202019-11-20 16:39:12 -08001986 // Ensure that the platform variant ends with _shared
1987 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08001988
1989 if !android.InAnyApex("mylib_common") {
1990 t.Log("Found mylib_common not in any apex!")
1991 t.Fail()
1992 }
1993}
1994
1995func TestTestApex(t *testing.T) {
1996 if android.InAnyApex("mylib_common_test") {
1997 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!")
1998 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001999 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002000 apex_test {
2001 name: "myapex",
2002 key: "myapex.key",
2003 native_shared_libs: ["mylib_common_test"],
2004 }
2005
2006 apex_key {
2007 name: "myapex.key",
2008 public_key: "testkey.avbpubkey",
2009 private_key: "testkey.pem",
2010 }
2011
2012 cc_library {
2013 name: "mylib_common_test",
2014 srcs: ["mylib.cpp"],
2015 system_shared_libs: [],
2016 stl: "none",
2017 }
2018 `)
2019
Sundong Ahnabb64432019-10-22 13:58:29 +09002020 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002021 apexRule := module.Rule("apexRule")
2022 copyCmds := apexRule.Args["copy_commands"]
2023
2024 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2025 t.Log("Apex was not a test apex!")
2026 t.Fail()
2027 }
2028 // Ensure that main rule creates an output
2029 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2030
2031 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002032 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002033
2034 // Ensure that both direct and indirect deps are copied into apex
2035 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2036
Colin Cross7113d202019-11-20 16:39:12 -08002037 // Ensure that the platform variant ends with _shared
2038 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002039
2040 if android.InAnyApex("mylib_common_test") {
2041 t.Log("Found mylib_common_test in some apex!")
2042 t.Fail()
2043 }
2044}
2045
Alex Light9670d332019-01-29 18:07:33 -08002046func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002047 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002048 apex {
2049 name: "myapex",
2050 key: "myapex.key",
2051 multilib: {
2052 first: {
2053 native_shared_libs: ["mylib_common"],
2054 }
2055 },
2056 target: {
2057 android: {
2058 multilib: {
2059 first: {
2060 native_shared_libs: ["mylib"],
2061 }
2062 }
2063 },
2064 host: {
2065 multilib: {
2066 first: {
2067 native_shared_libs: ["mylib2"],
2068 }
2069 }
2070 }
2071 }
2072 }
2073
2074 apex_key {
2075 name: "myapex.key",
2076 public_key: "testkey.avbpubkey",
2077 private_key: "testkey.pem",
2078 }
2079
2080 cc_library {
2081 name: "mylib",
2082 srcs: ["mylib.cpp"],
2083 system_shared_libs: [],
2084 stl: "none",
2085 }
2086
2087 cc_library {
2088 name: "mylib_common",
2089 srcs: ["mylib.cpp"],
2090 system_shared_libs: [],
2091 stl: "none",
2092 compile_multilib: "first",
2093 }
2094
2095 cc_library {
2096 name: "mylib2",
2097 srcs: ["mylib.cpp"],
2098 system_shared_libs: [],
2099 stl: "none",
2100 compile_multilib: "first",
2101 }
2102 `)
2103
Sundong Ahnabb64432019-10-22 13:58:29 +09002104 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002105 copyCmds := apexRule.Args["copy_commands"]
2106
2107 // Ensure that main rule creates an output
2108 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2109
2110 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002111 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2112 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2113 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002114
2115 // Ensure that both direct and indirect deps are copied into apex
2116 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2117 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2118 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2119
Colin Cross7113d202019-11-20 16:39:12 -08002120 // Ensure that the platform variant ends with _shared
2121 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2122 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2123 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002124}
Jiyong Park04480cf2019-02-06 00:16:29 +09002125
2126func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002127 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002128 apex {
2129 name: "myapex",
2130 key: "myapex.key",
2131 binaries: ["myscript"],
2132 }
2133
2134 apex_key {
2135 name: "myapex.key",
2136 public_key: "testkey.avbpubkey",
2137 private_key: "testkey.pem",
2138 }
2139
2140 sh_binary {
2141 name: "myscript",
2142 src: "mylib.cpp",
2143 filename: "myscript.sh",
2144 sub_dir: "script",
2145 }
2146 `)
2147
Sundong Ahnabb64432019-10-22 13:58:29 +09002148 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002149 copyCmds := apexRule.Args["copy_commands"]
2150
2151 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2152}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002153
Jooyung Han91df2082019-11-20 01:49:42 +09002154func TestApexInVariousPartition(t *testing.T) {
2155 testcases := []struct {
2156 propName, parition, flattenedPartition string
2157 }{
2158 {"", "system", "system_ext"},
2159 {"product_specific: true", "product", "product"},
2160 {"soc_specific: true", "vendor", "vendor"},
2161 {"proprietary: true", "vendor", "vendor"},
2162 {"vendor: true", "vendor", "vendor"},
2163 {"system_ext_specific: true", "system_ext", "system_ext"},
2164 }
2165 for _, tc := range testcases {
2166 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2167 ctx, _ := testApex(t, `
2168 apex {
2169 name: "myapex",
2170 key: "myapex.key",
2171 `+tc.propName+`
2172 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002173
Jooyung Han91df2082019-11-20 01:49:42 +09002174 apex_key {
2175 name: "myapex.key",
2176 public_key: "testkey.avbpubkey",
2177 private_key: "testkey.pem",
2178 }
2179 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002180
Jooyung Han91df2082019-11-20 01:49:42 +09002181 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2182 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2183 actual := apex.installDir.String()
2184 if actual != expected {
2185 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2186 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002187
Jooyung Han91df2082019-11-20 01:49:42 +09002188 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2189 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2190 actual = flattened.installDir.String()
2191 if actual != expected {
2192 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2193 }
2194 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002195 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002196}
Jiyong Park67882562019-03-21 01:11:21 +09002197
Jooyung Han54aca7b2019-11-20 02:26:02 +09002198func TestFileContexts(t *testing.T) {
2199 ctx, _ := testApex(t, `
2200 apex {
2201 name: "myapex",
2202 key: "myapex.key",
2203 }
2204
2205 apex_key {
2206 name: "myapex.key",
2207 public_key: "testkey.avbpubkey",
2208 private_key: "testkey.pem",
2209 }
2210 `)
2211 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2212 apexRule := module.Rule("apexRule")
2213 actual := apexRule.Args["file_contexts"]
2214 expected := "system/sepolicy/apex/myapex-file_contexts"
2215 if actual != expected {
2216 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2217 }
2218
2219 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2220 apex {
2221 name: "myapex",
2222 key: "myapex.key",
2223 file_contexts: "my_own_file_contexts",
2224 }
2225
2226 apex_key {
2227 name: "myapex.key",
2228 public_key: "testkey.avbpubkey",
2229 private_key: "testkey.pem",
2230 }
2231 `, withFiles(map[string][]byte{
2232 "my_own_file_contexts": nil,
2233 }))
2234
2235 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2236 apex {
2237 name: "myapex",
2238 key: "myapex.key",
2239 product_specific: true,
2240 file_contexts: "product_specific_file_contexts",
2241 }
2242
2243 apex_key {
2244 name: "myapex.key",
2245 public_key: "testkey.avbpubkey",
2246 private_key: "testkey.pem",
2247 }
2248 `)
2249
2250 ctx, _ = testApex(t, `
2251 apex {
2252 name: "myapex",
2253 key: "myapex.key",
2254 product_specific: true,
2255 file_contexts: "product_specific_file_contexts",
2256 }
2257
2258 apex_key {
2259 name: "myapex.key",
2260 public_key: "testkey.avbpubkey",
2261 private_key: "testkey.pem",
2262 }
2263 `, withFiles(map[string][]byte{
2264 "product_specific_file_contexts": nil,
2265 }))
2266 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2267 apexRule = module.Rule("apexRule")
2268 actual = apexRule.Args["file_contexts"]
2269 expected = "product_specific_file_contexts"
2270 if actual != expected {
2271 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2272 }
2273
2274 ctx, _ = testApex(t, `
2275 apex {
2276 name: "myapex",
2277 key: "myapex.key",
2278 product_specific: true,
2279 file_contexts: ":my-file-contexts",
2280 }
2281
2282 apex_key {
2283 name: "myapex.key",
2284 public_key: "testkey.avbpubkey",
2285 private_key: "testkey.pem",
2286 }
2287
2288 filegroup {
2289 name: "my-file-contexts",
2290 srcs: ["product_specific_file_contexts"],
2291 }
2292 `, withFiles(map[string][]byte{
2293 "product_specific_file_contexts": nil,
2294 }))
2295 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2296 apexRule = module.Rule("apexRule")
2297 actual = apexRule.Args["file_contexts"]
2298 expected = "product_specific_file_contexts"
2299 if actual != expected {
2300 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2301 }
2302}
2303
Jiyong Park67882562019-03-21 01:11:21 +09002304func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002305 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002306 apex_key {
2307 name: "myapex.key",
2308 public_key: ":my.avbpubkey",
2309 private_key: ":my.pem",
2310 product_specific: true,
2311 }
2312
2313 filegroup {
2314 name: "my.avbpubkey",
2315 srcs: ["testkey2.avbpubkey"],
2316 }
2317
2318 filegroup {
2319 name: "my.pem",
2320 srcs: ["testkey2.pem"],
2321 }
2322 `)
2323
2324 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2325 expected_pubkey := "testkey2.avbpubkey"
2326 actual_pubkey := apex_key.public_key_file.String()
2327 if actual_pubkey != expected_pubkey {
2328 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2329 }
2330 expected_privkey := "testkey2.pem"
2331 actual_privkey := apex_key.private_key_file.String()
2332 if actual_privkey != expected_privkey {
2333 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2334 }
2335}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002336
2337func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002338 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002339 prebuilt_apex {
2340 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002341 arch: {
2342 arm64: {
2343 src: "myapex-arm64.apex",
2344 },
2345 arm: {
2346 src: "myapex-arm.apex",
2347 },
2348 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002349 }
2350 `)
2351
2352 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2353
Jiyong Parkc95714e2019-03-29 14:23:10 +09002354 expectedInput := "myapex-arm64.apex"
2355 if prebuilt.inputApex.String() != expectedInput {
2356 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2357 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002358}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002359
2360func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002361 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002362 prebuilt_apex {
2363 name: "myapex",
2364 src: "myapex-arm.apex",
2365 filename: "notmyapex.apex",
2366 }
2367 `)
2368
2369 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2370
2371 expected := "notmyapex.apex"
2372 if p.installFilename != expected {
2373 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2374 }
2375}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002376
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002377func TestPrebuiltOverrides(t *testing.T) {
2378 ctx, config := testApex(t, `
2379 prebuilt_apex {
2380 name: "myapex.prebuilt",
2381 src: "myapex-arm.apex",
2382 overrides: [
2383 "myapex",
2384 ],
2385 }
2386 `)
2387
2388 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2389
2390 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002391 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002392 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002393 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002394 }
2395}
2396
Roland Levillain630846d2019-06-26 12:48:34 +01002397func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002398 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002399 apex_test {
2400 name: "myapex",
2401 key: "myapex.key",
2402 tests: [
2403 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002404 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002405 ],
2406 }
2407
2408 apex_key {
2409 name: "myapex.key",
2410 public_key: "testkey.avbpubkey",
2411 private_key: "testkey.pem",
2412 }
2413
2414 cc_test {
2415 name: "mytest",
2416 gtest: false,
2417 srcs: ["mytest.cpp"],
2418 relative_install_path: "test",
2419 system_shared_libs: [],
2420 static_executable: true,
2421 stl: "none",
2422 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002423
2424 cc_test {
2425 name: "mytests",
2426 gtest: false,
2427 srcs: [
2428 "mytest1.cpp",
2429 "mytest2.cpp",
2430 "mytest3.cpp",
2431 ],
2432 test_per_src: true,
2433 relative_install_path: "test",
2434 system_shared_libs: [],
2435 static_executable: true,
2436 stl: "none",
2437 }
Roland Levillain630846d2019-06-26 12:48:34 +01002438 `)
2439
Sundong Ahnabb64432019-10-22 13:58:29 +09002440 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002441 copyCmds := apexRule.Args["copy_commands"]
2442
2443 // Ensure that test dep is copied into apex.
2444 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002445
2446 // Ensure that test deps built with `test_per_src` are copied into apex.
2447 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2448 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2449 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002450
2451 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002452 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002453 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2454 name := apexBundle.BaseModuleName()
2455 prefix := "TARGET_"
2456 var builder strings.Builder
2457 data.Custom(&builder, name, prefix, "", data)
2458 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002459 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2460 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2461 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2462 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002463 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002464 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002465 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002466}
2467
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002468func TestInstallExtraFlattenedApexes(t *testing.T) {
2469 ctx, config := testApex(t, `
2470 apex {
2471 name: "myapex",
2472 key: "myapex.key",
2473 }
2474 apex_key {
2475 name: "myapex.key",
2476 public_key: "testkey.avbpubkey",
2477 private_key: "testkey.pem",
2478 }
2479 `, func(fs map[string][]byte, config android.Config) {
2480 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2481 })
2482 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2483 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2484 mk := android.AndroidMkDataForTest(t, config, "", ab)
2485 var builder strings.Builder
2486 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2487 androidMk := builder.String()
2488 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2489}
2490
Jooyung Han5c998b92019-06-27 11:30:33 +09002491func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002492 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002493 apex {
2494 name: "myapex",
2495 key: "myapex.key",
2496 native_shared_libs: ["mylib"],
2497 uses: ["commonapex"],
2498 }
2499
2500 apex {
2501 name: "commonapex",
2502 key: "myapex.key",
2503 native_shared_libs: ["libcommon"],
2504 provide_cpp_shared_libs: true,
2505 }
2506
2507 apex_key {
2508 name: "myapex.key",
2509 public_key: "testkey.avbpubkey",
2510 private_key: "testkey.pem",
2511 }
2512
2513 cc_library {
2514 name: "mylib",
2515 srcs: ["mylib.cpp"],
2516 shared_libs: ["libcommon"],
2517 system_shared_libs: [],
2518 stl: "none",
2519 }
2520
2521 cc_library {
2522 name: "libcommon",
2523 srcs: ["mylib_common.cpp"],
2524 system_shared_libs: [],
2525 stl: "none",
2526 }
2527 `)
2528
Sundong Ahnabb64432019-10-22 13:58:29 +09002529 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002530 apexRule1 := module1.Rule("apexRule")
2531 copyCmds1 := apexRule1.Args["copy_commands"]
2532
Sundong Ahnabb64432019-10-22 13:58:29 +09002533 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002534 apexRule2 := module2.Rule("apexRule")
2535 copyCmds2 := apexRule2.Args["copy_commands"]
2536
Colin Cross7113d202019-11-20 16:39:12 -08002537 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2538 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002539 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2540 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2541 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2542}
2543
2544func TestApexUsesFailsIfNotProvided(t *testing.T) {
2545 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2546 apex {
2547 name: "myapex",
2548 key: "myapex.key",
2549 uses: ["commonapex"],
2550 }
2551
2552 apex {
2553 name: "commonapex",
2554 key: "myapex.key",
2555 }
2556
2557 apex_key {
2558 name: "myapex.key",
2559 public_key: "testkey.avbpubkey",
2560 private_key: "testkey.pem",
2561 }
2562 `)
2563 testApexError(t, `uses: "commonapex" is not a provider`, `
2564 apex {
2565 name: "myapex",
2566 key: "myapex.key",
2567 uses: ["commonapex"],
2568 }
2569
2570 cc_library {
2571 name: "commonapex",
2572 system_shared_libs: [],
2573 stl: "none",
2574 }
2575
2576 apex_key {
2577 name: "myapex.key",
2578 public_key: "testkey.avbpubkey",
2579 private_key: "testkey.pem",
2580 }
2581 `)
2582}
2583
2584func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2585 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2586 apex {
2587 name: "myapex",
2588 key: "myapex.key",
2589 use_vendor: true,
2590 uses: ["commonapex"],
2591 }
2592
2593 apex {
2594 name: "commonapex",
2595 key: "myapex.key",
2596 provide_cpp_shared_libs: true,
2597 }
2598
2599 apex_key {
2600 name: "myapex.key",
2601 public_key: "testkey.avbpubkey",
2602 private_key: "testkey.pem",
2603 }
Jooyung Handc782442019-11-01 03:14:38 +09002604 `, func(fs map[string][]byte, config android.Config) {
2605 setUseVendorWhitelistForTest(config, []string{"myapex"})
2606 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002607}
2608
Jooyung Hand48f3c32019-08-23 11:18:57 +09002609func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2610 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2611 apex {
2612 name: "myapex",
2613 key: "myapex.key",
2614 native_shared_libs: ["libfoo"],
2615 }
2616
2617 apex_key {
2618 name: "myapex.key",
2619 public_key: "testkey.avbpubkey",
2620 private_key: "testkey.pem",
2621 }
2622
2623 cc_library {
2624 name: "libfoo",
2625 stl: "none",
2626 system_shared_libs: [],
2627 enabled: false,
2628 }
2629 `)
2630 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2631 apex {
2632 name: "myapex",
2633 key: "myapex.key",
2634 java_libs: ["myjar"],
2635 }
2636
2637 apex_key {
2638 name: "myapex.key",
2639 public_key: "testkey.avbpubkey",
2640 private_key: "testkey.pem",
2641 }
2642
2643 java_library {
2644 name: "myjar",
2645 srcs: ["foo/bar/MyClass.java"],
2646 sdk_version: "none",
2647 system_modules: "none",
2648 compile_dex: true,
2649 enabled: false,
2650 }
2651 `)
2652}
2653
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002654func TestApexWithApps(t *testing.T) {
2655 ctx, _ := testApex(t, `
2656 apex {
2657 name: "myapex",
2658 key: "myapex.key",
2659 apps: [
2660 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002661 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002662 ],
2663 }
2664
2665 apex_key {
2666 name: "myapex.key",
2667 public_key: "testkey.avbpubkey",
2668 private_key: "testkey.pem",
2669 }
2670
2671 android_app {
2672 name: "AppFoo",
2673 srcs: ["foo/bar/MyClass.java"],
2674 sdk_version: "none",
2675 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002676 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002677 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002678
2679 android_app {
2680 name: "AppFooPriv",
2681 srcs: ["foo/bar/MyClass.java"],
2682 sdk_version: "none",
2683 system_modules: "none",
2684 privileged: true,
2685 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002686
2687 cc_library_shared {
2688 name: "libjni",
2689 srcs: ["mylib.cpp"],
2690 stl: "none",
2691 system_shared_libs: [],
2692 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002693 `)
2694
Sundong Ahnabb64432019-10-22 13:58:29 +09002695 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002696 apexRule := module.Rule("apexRule")
2697 copyCmds := apexRule.Args["copy_commands"]
2698
2699 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002700 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002701
2702 // JNI libraries are embedded inside APK
2703 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002704 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002705 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2706 // ... uncompressed
2707 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2708 t.Errorf("jni lib is not uncompressed for AppFoo")
2709 }
2710 // ... and not directly inside the APEX
2711 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002712}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002713
Dario Frenicde2a032019-10-27 00:29:22 +01002714func TestApexWithAppImports(t *testing.T) {
2715 ctx, _ := testApex(t, `
2716 apex {
2717 name: "myapex",
2718 key: "myapex.key",
2719 apps: [
2720 "AppFooPrebuilt",
2721 "AppFooPrivPrebuilt",
2722 ],
2723 }
2724
2725 apex_key {
2726 name: "myapex.key",
2727 public_key: "testkey.avbpubkey",
2728 private_key: "testkey.pem",
2729 }
2730
2731 android_app_import {
2732 name: "AppFooPrebuilt",
2733 apk: "PrebuiltAppFoo.apk",
2734 presigned: true,
2735 dex_preopt: {
2736 enabled: false,
2737 },
2738 }
2739
2740 android_app_import {
2741 name: "AppFooPrivPrebuilt",
2742 apk: "PrebuiltAppFooPriv.apk",
2743 privileged: true,
2744 presigned: true,
2745 dex_preopt: {
2746 enabled: false,
2747 },
2748 }
2749 `)
2750
Sundong Ahnabb64432019-10-22 13:58:29 +09002751 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002752 apexRule := module.Rule("apexRule")
2753 copyCmds := apexRule.Args["copy_commands"]
2754
2755 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2756 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002757}
2758
Jooyung Han18020ea2019-11-13 10:50:48 +09002759func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2760 // libfoo's apex_available comes from cc_defaults
2761 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2762 apex {
2763 name: "myapex",
2764 key: "myapex.key",
2765 native_shared_libs: ["libfoo"],
2766 }
2767
2768 apex_key {
2769 name: "myapex.key",
2770 public_key: "testkey.avbpubkey",
2771 private_key: "testkey.pem",
2772 }
2773
2774 apex {
2775 name: "otherapex",
2776 key: "myapex.key",
2777 native_shared_libs: ["libfoo"],
2778 }
2779
2780 cc_defaults {
2781 name: "libfoo-defaults",
2782 apex_available: ["otherapex"],
2783 }
2784
2785 cc_library {
2786 name: "libfoo",
2787 defaults: ["libfoo-defaults"],
2788 stl: "none",
2789 system_shared_libs: [],
2790 }`)
2791}
2792
Jiyong Park127b40b2019-09-30 16:04:35 +09002793func TestApexAvailable(t *testing.T) {
2794 // libfoo is not available to myapex, but only to otherapex
2795 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2796 apex {
2797 name: "myapex",
2798 key: "myapex.key",
2799 native_shared_libs: ["libfoo"],
2800 }
2801
2802 apex_key {
2803 name: "myapex.key",
2804 public_key: "testkey.avbpubkey",
2805 private_key: "testkey.pem",
2806 }
2807
2808 apex {
2809 name: "otherapex",
2810 key: "otherapex.key",
2811 native_shared_libs: ["libfoo"],
2812 }
2813
2814 apex_key {
2815 name: "otherapex.key",
2816 public_key: "testkey.avbpubkey",
2817 private_key: "testkey.pem",
2818 }
2819
2820 cc_library {
2821 name: "libfoo",
2822 stl: "none",
2823 system_shared_libs: [],
2824 apex_available: ["otherapex"],
2825 }`)
2826
2827 // libbar is an indirect dep
2828 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2829 apex {
2830 name: "myapex",
2831 key: "myapex.key",
2832 native_shared_libs: ["libfoo"],
2833 }
2834
2835 apex_key {
2836 name: "myapex.key",
2837 public_key: "testkey.avbpubkey",
2838 private_key: "testkey.pem",
2839 }
2840
2841 apex {
2842 name: "otherapex",
2843 key: "otherapex.key",
2844 native_shared_libs: ["libfoo"],
2845 }
2846
2847 apex_key {
2848 name: "otherapex.key",
2849 public_key: "testkey.avbpubkey",
2850 private_key: "testkey.pem",
2851 }
2852
2853 cc_library {
2854 name: "libfoo",
2855 stl: "none",
2856 shared_libs: ["libbar"],
2857 system_shared_libs: [],
2858 apex_available: ["myapex", "otherapex"],
2859 }
2860
2861 cc_library {
2862 name: "libbar",
2863 stl: "none",
2864 system_shared_libs: [],
2865 apex_available: ["otherapex"],
2866 }`)
2867
2868 testApexError(t, "\"otherapex\" is not a valid module name", `
2869 apex {
2870 name: "myapex",
2871 key: "myapex.key",
2872 native_shared_libs: ["libfoo"],
2873 }
2874
2875 apex_key {
2876 name: "myapex.key",
2877 public_key: "testkey.avbpubkey",
2878 private_key: "testkey.pem",
2879 }
2880
2881 cc_library {
2882 name: "libfoo",
2883 stl: "none",
2884 system_shared_libs: [],
2885 apex_available: ["otherapex"],
2886 }`)
2887
2888 ctx, _ := testApex(t, `
2889 apex {
2890 name: "myapex",
2891 key: "myapex.key",
2892 native_shared_libs: ["libfoo", "libbar"],
2893 }
2894
2895 apex_key {
2896 name: "myapex.key",
2897 public_key: "testkey.avbpubkey",
2898 private_key: "testkey.pem",
2899 }
2900
2901 cc_library {
2902 name: "libfoo",
2903 stl: "none",
2904 system_shared_libs: [],
2905 apex_available: ["myapex"],
2906 }
2907
2908 cc_library {
2909 name: "libbar",
2910 stl: "none",
2911 system_shared_libs: [],
2912 apex_available: ["//apex_available:anyapex"],
2913 }`)
2914
2915 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08002916 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
2917 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
2918 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
2919 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09002920
2921 ctx, _ = testApex(t, `
2922 apex {
2923 name: "myapex",
2924 key: "myapex.key",
2925 }
2926
2927 apex_key {
2928 name: "myapex.key",
2929 public_key: "testkey.avbpubkey",
2930 private_key: "testkey.pem",
2931 }
2932
2933 cc_library {
2934 name: "libfoo",
2935 stl: "none",
2936 system_shared_libs: [],
2937 apex_available: ["//apex_available:platform"],
2938 }`)
2939
2940 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08002941 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
2942 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002943
2944 ctx, _ = testApex(t, `
2945 apex {
2946 name: "myapex",
2947 key: "myapex.key",
2948 native_shared_libs: ["libfoo"],
2949 }
2950
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956
2957 cc_library {
2958 name: "libfoo",
2959 stl: "none",
2960 system_shared_libs: [],
2961 apex_available: ["myapex"],
2962 static: {
2963 apex_available: ["//apex_available:platform"],
2964 },
2965 }`)
2966
2967 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08002968 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
2969 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09002970 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08002971 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
2972 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09002973}
2974
Jiyong Park5d790c32019-11-15 18:40:32 +09002975func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08002976 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09002977 apex {
2978 name: "myapex",
2979 key: "myapex.key",
2980 apps: ["app"],
2981 }
2982
2983 override_apex {
2984 name: "override_myapex",
2985 base: "myapex",
2986 apps: ["override_app"],
2987 }
2988
2989 apex_key {
2990 name: "myapex.key",
2991 public_key: "testkey.avbpubkey",
2992 private_key: "testkey.pem",
2993 }
2994
2995 android_app {
2996 name: "app",
2997 srcs: ["foo/bar/MyClass.java"],
2998 package_name: "foo",
2999 sdk_version: "none",
3000 system_modules: "none",
3001 }
3002
3003 override_android_app {
3004 name: "override_app",
3005 base: "app",
3006 package_name: "bar",
3007 }
3008 `)
3009
Jiyong Park317645e2019-12-05 13:20:58 +09003010 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3011 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3012 if originalVariant.GetOverriddenBy() != "" {
3013 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3014 }
3015 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3016 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3017 }
3018
Jiyong Park5d790c32019-11-15 18:40:32 +09003019 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3020 apexRule := module.Rule("apexRule")
3021 copyCmds := apexRule.Args["copy_commands"]
3022
3023 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3024 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003025
3026 apexBundle := module.Module().(*apexBundle)
3027 name := apexBundle.Name()
3028 if name != "override_myapex" {
3029 t.Errorf("name should be \"override_myapex\", but was %q", name)
3030 }
3031
3032 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3033 var builder strings.Builder
3034 data.Custom(&builder, name, "TARGET_", "", data)
3035 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003036 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003037 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3038 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3039 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003040 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003041 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3042 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003043}
3044
Jooyung Han214bf372019-11-12 13:03:50 +09003045func TestLegacyAndroid10Support(t *testing.T) {
3046 ctx, _ := testApex(t, `
3047 apex {
3048 name: "myapex",
3049 key: "myapex.key",
3050 legacy_android10_support: true,
3051 }
3052
3053 apex_key {
3054 name: "myapex.key",
3055 public_key: "testkey.avbpubkey",
3056 private_key: "testkey.pem",
3057 }
3058 `)
3059
3060 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3061 args := module.Rule("apexRule").Args
3062 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3063}
3064
Jiyong Park479321d2019-12-16 11:47:12 +09003065func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3066 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3067 apex {
3068 name: "myapex",
3069 key: "myapex.key",
3070 java_libs: ["myjar"],
3071 }
3072
3073 apex_key {
3074 name: "myapex.key",
3075 public_key: "testkey.avbpubkey",
3076 private_key: "testkey.pem",
3077 }
3078
3079 java_library {
3080 name: "myjar",
3081 srcs: ["foo/bar/MyClass.java"],
3082 sdk_version: "none",
3083 system_modules: "none",
3084 }
3085 `)
3086}
3087
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003088func TestMain(m *testing.M) {
3089 run := func() int {
3090 setUp()
3091 defer tearDown()
3092
3093 return m.Run()
3094 }
3095
3096 os.Exit(run())
3097}