blob: f6b9e888c23e82d741111dc11d83d441d5010fb0 [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
Jooyung Hanf121a652019-12-17 14:30:11 +09001266func TestCertificate(t *testing.T) {
1267 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1268 ctx, _ := testApex(t, `
1269 apex {
1270 name: "myapex",
1271 key: "myapex.key",
1272 }
1273 apex_key {
1274 name: "myapex.key",
1275 public_key: "testkey.avbpubkey",
1276 private_key: "testkey.pem",
1277 }`)
1278 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1279 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1280 if actual := rule.Args["certificates"]; actual != expected {
1281 t.Errorf("certificates should be %q, not %q", expected, actual)
1282 }
1283 })
1284 t.Run("override when unspecified", func(t *testing.T) {
1285 ctx, _ := testApex(t, `
1286 apex {
1287 name: "myapex_keytest",
1288 key: "myapex.key",
1289 file_contexts: ":myapex-file_contexts",
1290 }
1291 apex_key {
1292 name: "myapex.key",
1293 public_key: "testkey.avbpubkey",
1294 private_key: "testkey.pem",
1295 }
1296 android_app_certificate {
1297 name: "myapex.certificate.override",
1298 certificate: "testkey.override",
1299 }`)
1300 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1301 expected := "testkey.override.x509.pem testkey.override.pk8"
1302 if actual := rule.Args["certificates"]; actual != expected {
1303 t.Errorf("certificates should be %q, not %q", expected, actual)
1304 }
1305 })
1306 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1307 ctx, _ := testApex(t, `
1308 apex {
1309 name: "myapex",
1310 key: "myapex.key",
1311 certificate: ":myapex.certificate",
1312 }
1313 apex_key {
1314 name: "myapex.key",
1315 public_key: "testkey.avbpubkey",
1316 private_key: "testkey.pem",
1317 }
1318 android_app_certificate {
1319 name: "myapex.certificate",
1320 certificate: "testkey",
1321 }`)
1322 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1323 expected := "testkey.x509.pem testkey.pk8"
1324 if actual := rule.Args["certificates"]; actual != expected {
1325 t.Errorf("certificates should be %q, not %q", expected, actual)
1326 }
1327 })
1328 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1329 ctx, _ := testApex(t, `
1330 apex {
1331 name: "myapex_keytest",
1332 key: "myapex.key",
1333 file_contexts: ":myapex-file_contexts",
1334 certificate: ":myapex.certificate",
1335 }
1336 apex_key {
1337 name: "myapex.key",
1338 public_key: "testkey.avbpubkey",
1339 private_key: "testkey.pem",
1340 }
1341 android_app_certificate {
1342 name: "myapex.certificate.override",
1343 certificate: "testkey.override",
1344 }`)
1345 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1346 expected := "testkey.override.x509.pem testkey.override.pk8"
1347 if actual := rule.Args["certificates"]; actual != expected {
1348 t.Errorf("certificates should be %q, not %q", expected, actual)
1349 }
1350 })
1351 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1352 ctx, _ := testApex(t, `
1353 apex {
1354 name: "myapex",
1355 key: "myapex.key",
1356 certificate: "testkey",
1357 }
1358 apex_key {
1359 name: "myapex.key",
1360 public_key: "testkey.avbpubkey",
1361 private_key: "testkey.pem",
1362 }`)
1363 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1364 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1365 if actual := rule.Args["certificates"]; actual != expected {
1366 t.Errorf("certificates should be %q, not %q", expected, actual)
1367 }
1368 })
1369 t.Run("override when specified as <name>", func(t *testing.T) {
1370 ctx, _ := testApex(t, `
1371 apex {
1372 name: "myapex_keytest",
1373 key: "myapex.key",
1374 file_contexts: ":myapex-file_contexts",
1375 certificate: "testkey",
1376 }
1377 apex_key {
1378 name: "myapex.key",
1379 public_key: "testkey.avbpubkey",
1380 private_key: "testkey.pem",
1381 }
1382 android_app_certificate {
1383 name: "myapex.certificate.override",
1384 certificate: "testkey.override",
1385 }`)
1386 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1387 expected := "testkey.override.x509.pem testkey.override.pk8"
1388 if actual := rule.Args["certificates"]; actual != expected {
1389 t.Errorf("certificates should be %q, not %q", expected, actual)
1390 }
1391 })
1392}
1393
Jiyong Park58e364a2019-01-19 19:24:06 +09001394func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001395 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001396 apex {
1397 name: "myapex",
1398 key: "myapex.key",
1399 native_shared_libs: ["mylib"],
1400 }
1401
1402 apex {
1403 name: "otherapex",
1404 key: "myapex.key",
1405 native_shared_libs: ["mylib"],
1406 }
1407
1408 apex_key {
1409 name: "myapex.key",
1410 public_key: "testkey.avbpubkey",
1411 private_key: "testkey.pem",
1412 }
1413
1414 cc_library {
1415 name: "mylib",
1416 srcs: ["mylib.cpp"],
1417 system_shared_libs: [],
1418 stl: "none",
1419 }
1420 `)
1421
Jooyung Han6b8459b2019-10-30 08:29:25 +09001422 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001423 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001424 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001425 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1426 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001427
Jooyung Han6b8459b2019-10-30 08:29:25 +09001428 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001429 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001430 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001431 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1432 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001433
Jooyung Han6b8459b2019-10-30 08:29:25 +09001434 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001435 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001436 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001437 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1438 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001439}
Jiyong Park7e636d02019-01-28 16:16:54 +09001440
1441func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001442 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001443 apex {
1444 name: "myapex",
1445 key: "myapex.key",
1446 native_shared_libs: ["mylib"],
1447 }
1448
1449 apex_key {
1450 name: "myapex.key",
1451 public_key: "testkey.avbpubkey",
1452 private_key: "testkey.pem",
1453 }
1454
1455 cc_library_headers {
1456 name: "mylib_headers",
1457 export_include_dirs: ["my_include"],
1458 system_shared_libs: [],
1459 stl: "none",
1460 }
1461
1462 cc_library {
1463 name: "mylib",
1464 srcs: ["mylib.cpp"],
1465 system_shared_libs: [],
1466 stl: "none",
1467 header_libs: ["mylib_headers"],
1468 export_header_lib_headers: ["mylib_headers"],
1469 stubs: {
1470 versions: ["1", "2", "3"],
1471 },
1472 }
1473
1474 cc_library {
1475 name: "otherlib",
1476 srcs: ["mylib.cpp"],
1477 system_shared_libs: [],
1478 stl: "none",
1479 shared_libs: ["mylib"],
1480 }
1481 `)
1482
Colin Cross7113d202019-11-20 16:39:12 -08001483 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001484
1485 // Ensure that the include path of the header lib is exported to 'otherlib'
1486 ensureContains(t, cFlags, "-Imy_include")
1487}
Alex Light9670d332019-01-29 18:07:33 -08001488
Jooyung Han31c470b2019-10-18 16:26:59 +09001489func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1490 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001491 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001492 copyCmds := apexRule.Args["copy_commands"]
1493 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001494 var failed bool
1495 var surplus []string
1496 filesMatched := make(map[string]bool)
1497 addContent := func(content string) {
1498 for _, expected := range files {
1499 if matched, _ := path.Match(expected, content); matched {
1500 filesMatched[expected] = true
1501 return
1502 }
1503 }
1504 surplus = append(surplus, content)
1505 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001506 for _, cmd := range strings.Split(copyCmds, "&&") {
1507 cmd = strings.TrimSpace(cmd)
1508 if cmd == "" {
1509 continue
1510 }
1511 terms := strings.Split(cmd, " ")
1512 switch terms[0] {
1513 case "mkdir":
1514 case "cp":
1515 if len(terms) != 3 {
1516 t.Fatal("copyCmds contains invalid cp command", cmd)
1517 }
1518 dst := terms[2]
1519 index := strings.Index(dst, imageApexDir)
1520 if index == -1 {
1521 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1522 }
1523 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001524 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001525 default:
1526 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1527 }
1528 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001529
Jooyung Han31c470b2019-10-18 16:26:59 +09001530 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001531 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001532 t.Log("surplus files", surplus)
1533 failed = true
1534 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001535
1536 if len(files) > len(filesMatched) {
1537 var missing []string
1538 for _, expected := range files {
1539 if !filesMatched[expected] {
1540 missing = append(missing, expected)
1541 }
1542 }
1543 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001544 t.Log("missing files", missing)
1545 failed = true
1546 }
1547 if failed {
1548 t.Fail()
1549 }
1550}
1551
Jooyung Han344d5432019-08-23 11:17:39 +09001552func TestVndkApexCurrent(t *testing.T) {
1553 ctx, _ := testApex(t, `
1554 apex_vndk {
1555 name: "myapex",
1556 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001557 }
1558
1559 apex_key {
1560 name: "myapex.key",
1561 public_key: "testkey.avbpubkey",
1562 private_key: "testkey.pem",
1563 }
1564
1565 cc_library {
1566 name: "libvndk",
1567 srcs: ["mylib.cpp"],
1568 vendor_available: true,
1569 vndk: {
1570 enabled: true,
1571 },
1572 system_shared_libs: [],
1573 stl: "none",
1574 }
1575
1576 cc_library {
1577 name: "libvndksp",
1578 srcs: ["mylib.cpp"],
1579 vendor_available: true,
1580 vndk: {
1581 enabled: true,
1582 support_system_process: true,
1583 },
1584 system_shared_libs: [],
1585 stl: "none",
1586 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001587 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001588
Jooyung Han31c470b2019-10-18 16:26:59 +09001589 ensureExactContents(t, ctx, "myapex", []string{
1590 "lib/libvndk.so",
1591 "lib/libvndksp.so",
1592 "lib64/libvndk.so",
1593 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001594 "etc/llndk.libraries.VER.txt",
1595 "etc/vndkcore.libraries.VER.txt",
1596 "etc/vndksp.libraries.VER.txt",
1597 "etc/vndkprivate.libraries.VER.txt",
1598 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001599 })
Jooyung Han344d5432019-08-23 11:17:39 +09001600}
1601
1602func TestVndkApexWithPrebuilt(t *testing.T) {
1603 ctx, _ := testApex(t, `
1604 apex_vndk {
1605 name: "myapex",
1606 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001607 }
1608
1609 apex_key {
1610 name: "myapex.key",
1611 public_key: "testkey.avbpubkey",
1612 private_key: "testkey.pem",
1613 }
1614
1615 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001616 name: "libvndk",
1617 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001618 vendor_available: true,
1619 vndk: {
1620 enabled: true,
1621 },
1622 system_shared_libs: [],
1623 stl: "none",
1624 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001625
1626 cc_prebuilt_library_shared {
1627 name: "libvndk.arm",
1628 srcs: ["libvndk.arm.so"],
1629 vendor_available: true,
1630 vndk: {
1631 enabled: true,
1632 },
1633 enabled: false,
1634 arch: {
1635 arm: {
1636 enabled: true,
1637 },
1638 },
1639 system_shared_libs: [],
1640 stl: "none",
1641 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001642 `+vndkLibrariesTxtFiles("current"),
1643 withFiles(map[string][]byte{
1644 "libvndk.so": nil,
1645 "libvndk.arm.so": nil,
1646 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001647
Jooyung Han31c470b2019-10-18 16:26:59 +09001648 ensureExactContents(t, ctx, "myapex", []string{
1649 "lib/libvndk.so",
1650 "lib/libvndk.arm.so",
1651 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001652 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001653 })
Jooyung Han344d5432019-08-23 11:17:39 +09001654}
1655
Jooyung Han39edb6c2019-11-06 16:53:07 +09001656func vndkLibrariesTxtFiles(vers ...string) (result string) {
1657 for _, v := range vers {
1658 if v == "current" {
1659 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1660 result += `
1661 vndk_libraries_txt {
1662 name: "` + txt + `.libraries.txt",
1663 }
1664 `
1665 }
1666 } else {
1667 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1668 result += `
1669 prebuilt_etc {
1670 name: "` + txt + `.libraries.` + v + `.txt",
1671 src: "dummy.txt",
1672 }
1673 `
1674 }
1675 }
1676 }
1677 return
1678}
1679
Jooyung Han344d5432019-08-23 11:17:39 +09001680func TestVndkApexVersion(t *testing.T) {
1681 ctx, _ := testApex(t, `
1682 apex_vndk {
1683 name: "myapex_v27",
1684 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001685 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001686 vndk_version: "27",
1687 }
1688
1689 apex_key {
1690 name: "myapex.key",
1691 public_key: "testkey.avbpubkey",
1692 private_key: "testkey.pem",
1693 }
1694
Jooyung Han31c470b2019-10-18 16:26:59 +09001695 vndk_prebuilt_shared {
1696 name: "libvndk27",
1697 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001698 vendor_available: true,
1699 vndk: {
1700 enabled: true,
1701 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001702 target_arch: "arm64",
1703 arch: {
1704 arm: {
1705 srcs: ["libvndk27_arm.so"],
1706 },
1707 arm64: {
1708 srcs: ["libvndk27_arm64.so"],
1709 },
1710 },
Jooyung Han344d5432019-08-23 11:17:39 +09001711 }
1712
1713 vndk_prebuilt_shared {
1714 name: "libvndk27",
1715 version: "27",
1716 vendor_available: true,
1717 vndk: {
1718 enabled: true,
1719 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001720 target_arch: "x86_64",
1721 arch: {
1722 x86: {
1723 srcs: ["libvndk27_x86.so"],
1724 },
1725 x86_64: {
1726 srcs: ["libvndk27_x86_64.so"],
1727 },
1728 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001729 }
1730 `+vndkLibrariesTxtFiles("27"),
1731 withFiles(map[string][]byte{
1732 "libvndk27_arm.so": nil,
1733 "libvndk27_arm64.so": nil,
1734 "libvndk27_x86.so": nil,
1735 "libvndk27_x86_64.so": nil,
1736 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001737
Jooyung Han31c470b2019-10-18 16:26:59 +09001738 ensureExactContents(t, ctx, "myapex_v27", []string{
1739 "lib/libvndk27_arm.so",
1740 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001741 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001742 })
Jooyung Han344d5432019-08-23 11:17:39 +09001743}
1744
1745func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1746 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1747 apex_vndk {
1748 name: "myapex_v27",
1749 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001750 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001751 vndk_version: "27",
1752 }
1753 apex_vndk {
1754 name: "myapex_v27_other",
1755 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001756 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001757 vndk_version: "27",
1758 }
1759
1760 apex_key {
1761 name: "myapex.key",
1762 public_key: "testkey.avbpubkey",
1763 private_key: "testkey.pem",
1764 }
1765
1766 cc_library {
1767 name: "libvndk",
1768 srcs: ["mylib.cpp"],
1769 vendor_available: true,
1770 vndk: {
1771 enabled: true,
1772 },
1773 system_shared_libs: [],
1774 stl: "none",
1775 }
1776
1777 vndk_prebuilt_shared {
1778 name: "libvndk",
1779 version: "27",
1780 vendor_available: true,
1781 vndk: {
1782 enabled: true,
1783 },
1784 srcs: ["libvndk.so"],
1785 }
1786 `, withFiles(map[string][]byte{
1787 "libvndk.so": nil,
1788 }))
1789}
1790
Jooyung Han90eee022019-10-01 20:02:42 +09001791func TestVndkApexNameRule(t *testing.T) {
1792 ctx, _ := testApex(t, `
1793 apex_vndk {
1794 name: "myapex",
1795 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001796 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001797 }
1798 apex_vndk {
1799 name: "myapex_v28",
1800 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001801 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001802 vndk_version: "28",
1803 }
1804 apex_key {
1805 name: "myapex.key",
1806 public_key: "testkey.avbpubkey",
1807 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001808 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001809
1810 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001811 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001812 actual := proptools.String(bundle.properties.Apex_name)
1813 if !reflect.DeepEqual(actual, expected) {
1814 t.Errorf("Got '%v', expected '%v'", actual, expected)
1815 }
1816 }
1817
1818 assertApexName("com.android.vndk.vVER", "myapex")
1819 assertApexName("com.android.vndk.v28", "myapex_v28")
1820}
1821
Jooyung Han344d5432019-08-23 11:17:39 +09001822func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1823 ctx, _ := testApex(t, `
1824 apex_vndk {
1825 name: "myapex",
1826 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001827 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001828 }
1829
1830 apex_key {
1831 name: "myapex.key",
1832 public_key: "testkey.avbpubkey",
1833 private_key: "testkey.pem",
1834 }
1835
1836 cc_library {
1837 name: "libvndk",
1838 srcs: ["mylib.cpp"],
1839 vendor_available: true,
1840 native_bridge_supported: true,
1841 host_supported: true,
1842 vndk: {
1843 enabled: true,
1844 },
1845 system_shared_libs: [],
1846 stl: "none",
1847 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001848 `+vndkLibrariesTxtFiles("current"),
1849 withTargets(map[android.OsType][]android.Target{
1850 android.Android: []android.Target{
1851 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1852 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1853 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1854 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1855 },
1856 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001857
Jooyung Han31c470b2019-10-18 16:26:59 +09001858 ensureExactContents(t, ctx, "myapex", []string{
1859 "lib/libvndk.so",
1860 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001861 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001862 })
Jooyung Han344d5432019-08-23 11:17:39 +09001863}
1864
1865func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1866 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1867 apex_vndk {
1868 name: "myapex",
1869 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001870 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001871 native_bridge_supported: true,
1872 }
1873
1874 apex_key {
1875 name: "myapex.key",
1876 public_key: "testkey.avbpubkey",
1877 private_key: "testkey.pem",
1878 }
1879
1880 cc_library {
1881 name: "libvndk",
1882 srcs: ["mylib.cpp"],
1883 vendor_available: true,
1884 native_bridge_supported: true,
1885 host_supported: true,
1886 vndk: {
1887 enabled: true,
1888 },
1889 system_shared_libs: [],
1890 stl: "none",
1891 }
1892 `)
1893}
1894
Jooyung Han31c470b2019-10-18 16:26:59 +09001895func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001896 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001897 apex_vndk {
1898 name: "myapex_v27",
1899 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001900 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001901 vndk_version: "27",
1902 }
1903
1904 apex_key {
1905 name: "myapex.key",
1906 public_key: "testkey.avbpubkey",
1907 private_key: "testkey.pem",
1908 }
1909
1910 vndk_prebuilt_shared {
1911 name: "libvndk27",
1912 version: "27",
1913 target_arch: "arm",
1914 vendor_available: true,
1915 vndk: {
1916 enabled: true,
1917 },
1918 arch: {
1919 arm: {
1920 srcs: ["libvndk27.so"],
1921 }
1922 },
1923 }
1924
1925 vndk_prebuilt_shared {
1926 name: "libvndk27",
1927 version: "27",
1928 target_arch: "arm",
1929 binder32bit: true,
1930 vendor_available: true,
1931 vndk: {
1932 enabled: true,
1933 },
1934 arch: {
1935 arm: {
1936 srcs: ["libvndk27binder32.so"],
1937 }
1938 },
1939 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001940 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001941 withFiles(map[string][]byte{
1942 "libvndk27.so": nil,
1943 "libvndk27binder32.so": nil,
1944 }),
1945 withBinder32bit,
1946 withTargets(map[android.OsType][]android.Target{
1947 android.Android: []android.Target{
1948 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1949 },
1950 }),
1951 )
1952
1953 ensureExactContents(t, ctx, "myapex_v27", []string{
1954 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001955 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001956 })
1957}
1958
Jooyung Hane1633032019-08-01 17:41:43 +09001959func TestDependenciesInApexManifest(t *testing.T) {
1960 ctx, _ := testApex(t, `
1961 apex {
1962 name: "myapex_nodep",
1963 key: "myapex.key",
1964 native_shared_libs: ["lib_nodep"],
1965 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001966 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001967 }
1968
1969 apex {
1970 name: "myapex_dep",
1971 key: "myapex.key",
1972 native_shared_libs: ["lib_dep"],
1973 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001974 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001975 }
1976
1977 apex {
1978 name: "myapex_provider",
1979 key: "myapex.key",
1980 native_shared_libs: ["libfoo"],
1981 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001982 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001983 }
1984
1985 apex {
1986 name: "myapex_selfcontained",
1987 key: "myapex.key",
1988 native_shared_libs: ["lib_dep", "libfoo"],
1989 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001990 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001991 }
1992
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998
1999 cc_library {
2000 name: "lib_nodep",
2001 srcs: ["mylib.cpp"],
2002 system_shared_libs: [],
2003 stl: "none",
2004 }
2005
2006 cc_library {
2007 name: "lib_dep",
2008 srcs: ["mylib.cpp"],
2009 shared_libs: ["libfoo"],
2010 system_shared_libs: [],
2011 stl: "none",
2012 }
2013
2014 cc_library {
2015 name: "libfoo",
2016 srcs: ["mytest.cpp"],
2017 stubs: {
2018 versions: ["1"],
2019 },
2020 system_shared_libs: [],
2021 stl: "none",
2022 }
2023 `)
2024
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002025 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002026 var provideNativeLibs, requireNativeLibs []string
2027
Sundong Ahnabb64432019-10-22 13:58:29 +09002028 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002029 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2030 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002031 ensureListEmpty(t, provideNativeLibs)
2032 ensureListEmpty(t, requireNativeLibs)
2033
Sundong Ahnabb64432019-10-22 13:58:29 +09002034 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002035 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2036 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002037 ensureListEmpty(t, provideNativeLibs)
2038 ensureListContains(t, requireNativeLibs, "libfoo.so")
2039
Sundong Ahnabb64432019-10-22 13:58:29 +09002040 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002041 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2042 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002043 ensureListContains(t, provideNativeLibs, "libfoo.so")
2044 ensureListEmpty(t, requireNativeLibs)
2045
Sundong Ahnabb64432019-10-22 13:58:29 +09002046 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002047 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2048 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002049 ensureListContains(t, provideNativeLibs, "libfoo.so")
2050 ensureListEmpty(t, requireNativeLibs)
2051}
2052
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002053func TestApexName(t *testing.T) {
2054 ctx, _ := testApex(t, `
2055 apex {
2056 name: "myapex",
2057 key: "myapex.key",
2058 apex_name: "com.android.myapex",
2059 }
2060
2061 apex_key {
2062 name: "myapex.key",
2063 public_key: "testkey.avbpubkey",
2064 private_key: "testkey.pem",
2065 }
2066 `)
2067
Sundong Ahnabb64432019-10-22 13:58:29 +09002068 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002069 apexManifestRule := module.Rule("apexManifestRule")
2070 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2071 apexRule := module.Rule("apexRule")
2072 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2073}
2074
Alex Light0851b882019-02-07 13:20:53 -08002075func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002076 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002077 apex {
2078 name: "myapex",
2079 key: "myapex.key",
2080 native_shared_libs: ["mylib_common"],
2081 }
2082
2083 apex_key {
2084 name: "myapex.key",
2085 public_key: "testkey.avbpubkey",
2086 private_key: "testkey.pem",
2087 }
2088
2089 cc_library {
2090 name: "mylib_common",
2091 srcs: ["mylib.cpp"],
2092 system_shared_libs: [],
2093 stl: "none",
2094 }
2095 `)
2096
Sundong Ahnabb64432019-10-22 13:58:29 +09002097 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002098 apexRule := module.Rule("apexRule")
2099 copyCmds := apexRule.Args["copy_commands"]
2100
2101 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2102 t.Log("Apex was a test apex!")
2103 t.Fail()
2104 }
2105 // Ensure that main rule creates an output
2106 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2107
2108 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002109 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002110
2111 // Ensure that both direct and indirect deps are copied into apex
2112 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2113
Colin Cross7113d202019-11-20 16:39:12 -08002114 // Ensure that the platform variant ends with _shared
2115 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002116
2117 if !android.InAnyApex("mylib_common") {
2118 t.Log("Found mylib_common not in any apex!")
2119 t.Fail()
2120 }
2121}
2122
2123func TestTestApex(t *testing.T) {
2124 if android.InAnyApex("mylib_common_test") {
2125 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!")
2126 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002127 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002128 apex_test {
2129 name: "myapex",
2130 key: "myapex.key",
2131 native_shared_libs: ["mylib_common_test"],
2132 }
2133
2134 apex_key {
2135 name: "myapex.key",
2136 public_key: "testkey.avbpubkey",
2137 private_key: "testkey.pem",
2138 }
2139
2140 cc_library {
2141 name: "mylib_common_test",
2142 srcs: ["mylib.cpp"],
2143 system_shared_libs: [],
2144 stl: "none",
2145 }
2146 `)
2147
Sundong Ahnabb64432019-10-22 13:58:29 +09002148 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002149 apexRule := module.Rule("apexRule")
2150 copyCmds := apexRule.Args["copy_commands"]
2151
2152 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2153 t.Log("Apex was not a test apex!")
2154 t.Fail()
2155 }
2156 // Ensure that main rule creates an output
2157 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2158
2159 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002160 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002161
2162 // Ensure that both direct and indirect deps are copied into apex
2163 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2164
Colin Cross7113d202019-11-20 16:39:12 -08002165 // Ensure that the platform variant ends with _shared
2166 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002167
2168 if android.InAnyApex("mylib_common_test") {
2169 t.Log("Found mylib_common_test in some apex!")
2170 t.Fail()
2171 }
2172}
2173
Alex Light9670d332019-01-29 18:07:33 -08002174func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002175 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002176 apex {
2177 name: "myapex",
2178 key: "myapex.key",
2179 multilib: {
2180 first: {
2181 native_shared_libs: ["mylib_common"],
2182 }
2183 },
2184 target: {
2185 android: {
2186 multilib: {
2187 first: {
2188 native_shared_libs: ["mylib"],
2189 }
2190 }
2191 },
2192 host: {
2193 multilib: {
2194 first: {
2195 native_shared_libs: ["mylib2"],
2196 }
2197 }
2198 }
2199 }
2200 }
2201
2202 apex_key {
2203 name: "myapex.key",
2204 public_key: "testkey.avbpubkey",
2205 private_key: "testkey.pem",
2206 }
2207
2208 cc_library {
2209 name: "mylib",
2210 srcs: ["mylib.cpp"],
2211 system_shared_libs: [],
2212 stl: "none",
2213 }
2214
2215 cc_library {
2216 name: "mylib_common",
2217 srcs: ["mylib.cpp"],
2218 system_shared_libs: [],
2219 stl: "none",
2220 compile_multilib: "first",
2221 }
2222
2223 cc_library {
2224 name: "mylib2",
2225 srcs: ["mylib.cpp"],
2226 system_shared_libs: [],
2227 stl: "none",
2228 compile_multilib: "first",
2229 }
2230 `)
2231
Sundong Ahnabb64432019-10-22 13:58:29 +09002232 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002233 copyCmds := apexRule.Args["copy_commands"]
2234
2235 // Ensure that main rule creates an output
2236 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2237
2238 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002239 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2240 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2241 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002242
2243 // Ensure that both direct and indirect deps are copied into apex
2244 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2245 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2246 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2247
Colin Cross7113d202019-11-20 16:39:12 -08002248 // Ensure that the platform variant ends with _shared
2249 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2250 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2251 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002252}
Jiyong Park04480cf2019-02-06 00:16:29 +09002253
2254func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002255 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002256 apex {
2257 name: "myapex",
2258 key: "myapex.key",
2259 binaries: ["myscript"],
2260 }
2261
2262 apex_key {
2263 name: "myapex.key",
2264 public_key: "testkey.avbpubkey",
2265 private_key: "testkey.pem",
2266 }
2267
2268 sh_binary {
2269 name: "myscript",
2270 src: "mylib.cpp",
2271 filename: "myscript.sh",
2272 sub_dir: "script",
2273 }
2274 `)
2275
Sundong Ahnabb64432019-10-22 13:58:29 +09002276 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002277 copyCmds := apexRule.Args["copy_commands"]
2278
2279 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2280}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002281
Jooyung Han91df2082019-11-20 01:49:42 +09002282func TestApexInVariousPartition(t *testing.T) {
2283 testcases := []struct {
2284 propName, parition, flattenedPartition string
2285 }{
2286 {"", "system", "system_ext"},
2287 {"product_specific: true", "product", "product"},
2288 {"soc_specific: true", "vendor", "vendor"},
2289 {"proprietary: true", "vendor", "vendor"},
2290 {"vendor: true", "vendor", "vendor"},
2291 {"system_ext_specific: true", "system_ext", "system_ext"},
2292 }
2293 for _, tc := range testcases {
2294 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2295 ctx, _ := testApex(t, `
2296 apex {
2297 name: "myapex",
2298 key: "myapex.key",
2299 `+tc.propName+`
2300 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002301
Jooyung Han91df2082019-11-20 01:49:42 +09002302 apex_key {
2303 name: "myapex.key",
2304 public_key: "testkey.avbpubkey",
2305 private_key: "testkey.pem",
2306 }
2307 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002308
Jooyung Han91df2082019-11-20 01:49:42 +09002309 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2310 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2311 actual := apex.installDir.String()
2312 if actual != expected {
2313 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2314 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002315
Jooyung Han91df2082019-11-20 01:49:42 +09002316 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2317 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2318 actual = flattened.installDir.String()
2319 if actual != expected {
2320 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2321 }
2322 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002323 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002324}
Jiyong Park67882562019-03-21 01:11:21 +09002325
Jooyung Han54aca7b2019-11-20 02:26:02 +09002326func TestFileContexts(t *testing.T) {
2327 ctx, _ := testApex(t, `
2328 apex {
2329 name: "myapex",
2330 key: "myapex.key",
2331 }
2332
2333 apex_key {
2334 name: "myapex.key",
2335 public_key: "testkey.avbpubkey",
2336 private_key: "testkey.pem",
2337 }
2338 `)
2339 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2340 apexRule := module.Rule("apexRule")
2341 actual := apexRule.Args["file_contexts"]
2342 expected := "system/sepolicy/apex/myapex-file_contexts"
2343 if actual != expected {
2344 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2345 }
2346
2347 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2348 apex {
2349 name: "myapex",
2350 key: "myapex.key",
2351 file_contexts: "my_own_file_contexts",
2352 }
2353
2354 apex_key {
2355 name: "myapex.key",
2356 public_key: "testkey.avbpubkey",
2357 private_key: "testkey.pem",
2358 }
2359 `, withFiles(map[string][]byte{
2360 "my_own_file_contexts": nil,
2361 }))
2362
2363 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2364 apex {
2365 name: "myapex",
2366 key: "myapex.key",
2367 product_specific: true,
2368 file_contexts: "product_specific_file_contexts",
2369 }
2370
2371 apex_key {
2372 name: "myapex.key",
2373 public_key: "testkey.avbpubkey",
2374 private_key: "testkey.pem",
2375 }
2376 `)
2377
2378 ctx, _ = testApex(t, `
2379 apex {
2380 name: "myapex",
2381 key: "myapex.key",
2382 product_specific: true,
2383 file_contexts: "product_specific_file_contexts",
2384 }
2385
2386 apex_key {
2387 name: "myapex.key",
2388 public_key: "testkey.avbpubkey",
2389 private_key: "testkey.pem",
2390 }
2391 `, withFiles(map[string][]byte{
2392 "product_specific_file_contexts": nil,
2393 }))
2394 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2395 apexRule = module.Rule("apexRule")
2396 actual = apexRule.Args["file_contexts"]
2397 expected = "product_specific_file_contexts"
2398 if actual != expected {
2399 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2400 }
2401
2402 ctx, _ = testApex(t, `
2403 apex {
2404 name: "myapex",
2405 key: "myapex.key",
2406 product_specific: true,
2407 file_contexts: ":my-file-contexts",
2408 }
2409
2410 apex_key {
2411 name: "myapex.key",
2412 public_key: "testkey.avbpubkey",
2413 private_key: "testkey.pem",
2414 }
2415
2416 filegroup {
2417 name: "my-file-contexts",
2418 srcs: ["product_specific_file_contexts"],
2419 }
2420 `, withFiles(map[string][]byte{
2421 "product_specific_file_contexts": nil,
2422 }))
2423 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2424 apexRule = module.Rule("apexRule")
2425 actual = apexRule.Args["file_contexts"]
2426 expected = "product_specific_file_contexts"
2427 if actual != expected {
2428 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2429 }
2430}
2431
Jiyong Park67882562019-03-21 01:11:21 +09002432func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002433 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002434 apex_key {
2435 name: "myapex.key",
2436 public_key: ":my.avbpubkey",
2437 private_key: ":my.pem",
2438 product_specific: true,
2439 }
2440
2441 filegroup {
2442 name: "my.avbpubkey",
2443 srcs: ["testkey2.avbpubkey"],
2444 }
2445
2446 filegroup {
2447 name: "my.pem",
2448 srcs: ["testkey2.pem"],
2449 }
2450 `)
2451
2452 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2453 expected_pubkey := "testkey2.avbpubkey"
2454 actual_pubkey := apex_key.public_key_file.String()
2455 if actual_pubkey != expected_pubkey {
2456 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2457 }
2458 expected_privkey := "testkey2.pem"
2459 actual_privkey := apex_key.private_key_file.String()
2460 if actual_privkey != expected_privkey {
2461 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2462 }
2463}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002464
2465func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002466 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002467 prebuilt_apex {
2468 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002469 arch: {
2470 arm64: {
2471 src: "myapex-arm64.apex",
2472 },
2473 arm: {
2474 src: "myapex-arm.apex",
2475 },
2476 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002477 }
2478 `)
2479
2480 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2481
Jiyong Parkc95714e2019-03-29 14:23:10 +09002482 expectedInput := "myapex-arm64.apex"
2483 if prebuilt.inputApex.String() != expectedInput {
2484 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2485 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002486}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002487
2488func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002489 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002490 prebuilt_apex {
2491 name: "myapex",
2492 src: "myapex-arm.apex",
2493 filename: "notmyapex.apex",
2494 }
2495 `)
2496
2497 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2498
2499 expected := "notmyapex.apex"
2500 if p.installFilename != expected {
2501 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2502 }
2503}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002504
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002505func TestPrebuiltOverrides(t *testing.T) {
2506 ctx, config := testApex(t, `
2507 prebuilt_apex {
2508 name: "myapex.prebuilt",
2509 src: "myapex-arm.apex",
2510 overrides: [
2511 "myapex",
2512 ],
2513 }
2514 `)
2515
2516 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2517
2518 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002519 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002520 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002521 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002522 }
2523}
2524
Roland Levillain630846d2019-06-26 12:48:34 +01002525func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002526 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002527 apex_test {
2528 name: "myapex",
2529 key: "myapex.key",
2530 tests: [
2531 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002532 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002533 ],
2534 }
2535
2536 apex_key {
2537 name: "myapex.key",
2538 public_key: "testkey.avbpubkey",
2539 private_key: "testkey.pem",
2540 }
2541
2542 cc_test {
2543 name: "mytest",
2544 gtest: false,
2545 srcs: ["mytest.cpp"],
2546 relative_install_path: "test",
2547 system_shared_libs: [],
2548 static_executable: true,
2549 stl: "none",
2550 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002551
2552 cc_test {
2553 name: "mytests",
2554 gtest: false,
2555 srcs: [
2556 "mytest1.cpp",
2557 "mytest2.cpp",
2558 "mytest3.cpp",
2559 ],
2560 test_per_src: true,
2561 relative_install_path: "test",
2562 system_shared_libs: [],
2563 static_executable: true,
2564 stl: "none",
2565 }
Roland Levillain630846d2019-06-26 12:48:34 +01002566 `)
2567
Sundong Ahnabb64432019-10-22 13:58:29 +09002568 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002569 copyCmds := apexRule.Args["copy_commands"]
2570
2571 // Ensure that test dep is copied into apex.
2572 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002573
2574 // Ensure that test deps built with `test_per_src` are copied into apex.
2575 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2576 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2577 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002578
2579 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002580 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002581 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2582 name := apexBundle.BaseModuleName()
2583 prefix := "TARGET_"
2584 var builder strings.Builder
2585 data.Custom(&builder, name, prefix, "", data)
2586 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002587 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2588 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2589 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2590 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002591 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002592 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002593 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002594}
2595
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002596func TestInstallExtraFlattenedApexes(t *testing.T) {
2597 ctx, config := testApex(t, `
2598 apex {
2599 name: "myapex",
2600 key: "myapex.key",
2601 }
2602 apex_key {
2603 name: "myapex.key",
2604 public_key: "testkey.avbpubkey",
2605 private_key: "testkey.pem",
2606 }
2607 `, func(fs map[string][]byte, config android.Config) {
2608 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2609 })
2610 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2611 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2612 mk := android.AndroidMkDataForTest(t, config, "", ab)
2613 var builder strings.Builder
2614 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2615 androidMk := builder.String()
2616 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2617}
2618
Jooyung Han5c998b92019-06-27 11:30:33 +09002619func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002620 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002621 apex {
2622 name: "myapex",
2623 key: "myapex.key",
2624 native_shared_libs: ["mylib"],
2625 uses: ["commonapex"],
2626 }
2627
2628 apex {
2629 name: "commonapex",
2630 key: "myapex.key",
2631 native_shared_libs: ["libcommon"],
2632 provide_cpp_shared_libs: true,
2633 }
2634
2635 apex_key {
2636 name: "myapex.key",
2637 public_key: "testkey.avbpubkey",
2638 private_key: "testkey.pem",
2639 }
2640
2641 cc_library {
2642 name: "mylib",
2643 srcs: ["mylib.cpp"],
2644 shared_libs: ["libcommon"],
2645 system_shared_libs: [],
2646 stl: "none",
2647 }
2648
2649 cc_library {
2650 name: "libcommon",
2651 srcs: ["mylib_common.cpp"],
2652 system_shared_libs: [],
2653 stl: "none",
2654 }
2655 `)
2656
Sundong Ahnabb64432019-10-22 13:58:29 +09002657 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002658 apexRule1 := module1.Rule("apexRule")
2659 copyCmds1 := apexRule1.Args["copy_commands"]
2660
Sundong Ahnabb64432019-10-22 13:58:29 +09002661 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002662 apexRule2 := module2.Rule("apexRule")
2663 copyCmds2 := apexRule2.Args["copy_commands"]
2664
Colin Cross7113d202019-11-20 16:39:12 -08002665 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2666 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002667 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2668 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2669 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2670}
2671
2672func TestApexUsesFailsIfNotProvided(t *testing.T) {
2673 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2674 apex {
2675 name: "myapex",
2676 key: "myapex.key",
2677 uses: ["commonapex"],
2678 }
2679
2680 apex {
2681 name: "commonapex",
2682 key: "myapex.key",
2683 }
2684
2685 apex_key {
2686 name: "myapex.key",
2687 public_key: "testkey.avbpubkey",
2688 private_key: "testkey.pem",
2689 }
2690 `)
2691 testApexError(t, `uses: "commonapex" is not a provider`, `
2692 apex {
2693 name: "myapex",
2694 key: "myapex.key",
2695 uses: ["commonapex"],
2696 }
2697
2698 cc_library {
2699 name: "commonapex",
2700 system_shared_libs: [],
2701 stl: "none",
2702 }
2703
2704 apex_key {
2705 name: "myapex.key",
2706 public_key: "testkey.avbpubkey",
2707 private_key: "testkey.pem",
2708 }
2709 `)
2710}
2711
2712func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2713 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2714 apex {
2715 name: "myapex",
2716 key: "myapex.key",
2717 use_vendor: true,
2718 uses: ["commonapex"],
2719 }
2720
2721 apex {
2722 name: "commonapex",
2723 key: "myapex.key",
2724 provide_cpp_shared_libs: true,
2725 }
2726
2727 apex_key {
2728 name: "myapex.key",
2729 public_key: "testkey.avbpubkey",
2730 private_key: "testkey.pem",
2731 }
Jooyung Handc782442019-11-01 03:14:38 +09002732 `, func(fs map[string][]byte, config android.Config) {
2733 setUseVendorWhitelistForTest(config, []string{"myapex"})
2734 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002735}
2736
Jooyung Hand48f3c32019-08-23 11:18:57 +09002737func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2738 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2739 apex {
2740 name: "myapex",
2741 key: "myapex.key",
2742 native_shared_libs: ["libfoo"],
2743 }
2744
2745 apex_key {
2746 name: "myapex.key",
2747 public_key: "testkey.avbpubkey",
2748 private_key: "testkey.pem",
2749 }
2750
2751 cc_library {
2752 name: "libfoo",
2753 stl: "none",
2754 system_shared_libs: [],
2755 enabled: false,
2756 }
2757 `)
2758 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2759 apex {
2760 name: "myapex",
2761 key: "myapex.key",
2762 java_libs: ["myjar"],
2763 }
2764
2765 apex_key {
2766 name: "myapex.key",
2767 public_key: "testkey.avbpubkey",
2768 private_key: "testkey.pem",
2769 }
2770
2771 java_library {
2772 name: "myjar",
2773 srcs: ["foo/bar/MyClass.java"],
2774 sdk_version: "none",
2775 system_modules: "none",
2776 compile_dex: true,
2777 enabled: false,
2778 }
2779 `)
2780}
2781
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002782func TestApexWithApps(t *testing.T) {
2783 ctx, _ := testApex(t, `
2784 apex {
2785 name: "myapex",
2786 key: "myapex.key",
2787 apps: [
2788 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002789 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002790 ],
2791 }
2792
2793 apex_key {
2794 name: "myapex.key",
2795 public_key: "testkey.avbpubkey",
2796 private_key: "testkey.pem",
2797 }
2798
2799 android_app {
2800 name: "AppFoo",
2801 srcs: ["foo/bar/MyClass.java"],
2802 sdk_version: "none",
2803 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002804 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002805 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002806
2807 android_app {
2808 name: "AppFooPriv",
2809 srcs: ["foo/bar/MyClass.java"],
2810 sdk_version: "none",
2811 system_modules: "none",
2812 privileged: true,
2813 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002814
2815 cc_library_shared {
2816 name: "libjni",
2817 srcs: ["mylib.cpp"],
2818 stl: "none",
2819 system_shared_libs: [],
2820 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002821 `)
2822
Sundong Ahnabb64432019-10-22 13:58:29 +09002823 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002824 apexRule := module.Rule("apexRule")
2825 copyCmds := apexRule.Args["copy_commands"]
2826
2827 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002828 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002829
2830 // JNI libraries are embedded inside APK
2831 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002832 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002833 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2834 // ... uncompressed
2835 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2836 t.Errorf("jni lib is not uncompressed for AppFoo")
2837 }
2838 // ... and not directly inside the APEX
2839 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002840}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002841
Dario Frenicde2a032019-10-27 00:29:22 +01002842func TestApexWithAppImports(t *testing.T) {
2843 ctx, _ := testApex(t, `
2844 apex {
2845 name: "myapex",
2846 key: "myapex.key",
2847 apps: [
2848 "AppFooPrebuilt",
2849 "AppFooPrivPrebuilt",
2850 ],
2851 }
2852
2853 apex_key {
2854 name: "myapex.key",
2855 public_key: "testkey.avbpubkey",
2856 private_key: "testkey.pem",
2857 }
2858
2859 android_app_import {
2860 name: "AppFooPrebuilt",
2861 apk: "PrebuiltAppFoo.apk",
2862 presigned: true,
2863 dex_preopt: {
2864 enabled: false,
2865 },
2866 }
2867
2868 android_app_import {
2869 name: "AppFooPrivPrebuilt",
2870 apk: "PrebuiltAppFooPriv.apk",
2871 privileged: true,
2872 presigned: true,
2873 dex_preopt: {
2874 enabled: false,
2875 },
2876 }
2877 `)
2878
Sundong Ahnabb64432019-10-22 13:58:29 +09002879 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002880 apexRule := module.Rule("apexRule")
2881 copyCmds := apexRule.Args["copy_commands"]
2882
2883 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2884 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002885}
2886
Jooyung Han18020ea2019-11-13 10:50:48 +09002887func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2888 // libfoo's apex_available comes from cc_defaults
2889 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2890 apex {
2891 name: "myapex",
2892 key: "myapex.key",
2893 native_shared_libs: ["libfoo"],
2894 }
2895
2896 apex_key {
2897 name: "myapex.key",
2898 public_key: "testkey.avbpubkey",
2899 private_key: "testkey.pem",
2900 }
2901
2902 apex {
2903 name: "otherapex",
2904 key: "myapex.key",
2905 native_shared_libs: ["libfoo"],
2906 }
2907
2908 cc_defaults {
2909 name: "libfoo-defaults",
2910 apex_available: ["otherapex"],
2911 }
2912
2913 cc_library {
2914 name: "libfoo",
2915 defaults: ["libfoo-defaults"],
2916 stl: "none",
2917 system_shared_libs: [],
2918 }`)
2919}
2920
Jiyong Park127b40b2019-09-30 16:04:35 +09002921func TestApexAvailable(t *testing.T) {
2922 // libfoo is not available to myapex, but only to otherapex
2923 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2924 apex {
2925 name: "myapex",
2926 key: "myapex.key",
2927 native_shared_libs: ["libfoo"],
2928 }
2929
2930 apex_key {
2931 name: "myapex.key",
2932 public_key: "testkey.avbpubkey",
2933 private_key: "testkey.pem",
2934 }
2935
2936 apex {
2937 name: "otherapex",
2938 key: "otherapex.key",
2939 native_shared_libs: ["libfoo"],
2940 }
2941
2942 apex_key {
2943 name: "otherapex.key",
2944 public_key: "testkey.avbpubkey",
2945 private_key: "testkey.pem",
2946 }
2947
2948 cc_library {
2949 name: "libfoo",
2950 stl: "none",
2951 system_shared_libs: [],
2952 apex_available: ["otherapex"],
2953 }`)
2954
2955 // libbar is an indirect dep
2956 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
2957 apex {
2958 name: "myapex",
2959 key: "myapex.key",
2960 native_shared_libs: ["libfoo"],
2961 }
2962
2963 apex_key {
2964 name: "myapex.key",
2965 public_key: "testkey.avbpubkey",
2966 private_key: "testkey.pem",
2967 }
2968
2969 apex {
2970 name: "otherapex",
2971 key: "otherapex.key",
2972 native_shared_libs: ["libfoo"],
2973 }
2974
2975 apex_key {
2976 name: "otherapex.key",
2977 public_key: "testkey.avbpubkey",
2978 private_key: "testkey.pem",
2979 }
2980
2981 cc_library {
2982 name: "libfoo",
2983 stl: "none",
2984 shared_libs: ["libbar"],
2985 system_shared_libs: [],
2986 apex_available: ["myapex", "otherapex"],
2987 }
2988
2989 cc_library {
2990 name: "libbar",
2991 stl: "none",
2992 system_shared_libs: [],
2993 apex_available: ["otherapex"],
2994 }`)
2995
2996 testApexError(t, "\"otherapex\" is not a valid module name", `
2997 apex {
2998 name: "myapex",
2999 key: "myapex.key",
3000 native_shared_libs: ["libfoo"],
3001 }
3002
3003 apex_key {
3004 name: "myapex.key",
3005 public_key: "testkey.avbpubkey",
3006 private_key: "testkey.pem",
3007 }
3008
3009 cc_library {
3010 name: "libfoo",
3011 stl: "none",
3012 system_shared_libs: [],
3013 apex_available: ["otherapex"],
3014 }`)
3015
3016 ctx, _ := testApex(t, `
3017 apex {
3018 name: "myapex",
3019 key: "myapex.key",
3020 native_shared_libs: ["libfoo", "libbar"],
3021 }
3022
3023 apex_key {
3024 name: "myapex.key",
3025 public_key: "testkey.avbpubkey",
3026 private_key: "testkey.pem",
3027 }
3028
3029 cc_library {
3030 name: "libfoo",
3031 stl: "none",
3032 system_shared_libs: [],
3033 apex_available: ["myapex"],
3034 }
3035
3036 cc_library {
3037 name: "libbar",
3038 stl: "none",
3039 system_shared_libs: [],
3040 apex_available: ["//apex_available:anyapex"],
3041 }`)
3042
3043 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003044 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3045 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3046 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3047 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003048
3049 ctx, _ = testApex(t, `
3050 apex {
3051 name: "myapex",
3052 key: "myapex.key",
3053 }
3054
3055 apex_key {
3056 name: "myapex.key",
3057 public_key: "testkey.avbpubkey",
3058 private_key: "testkey.pem",
3059 }
3060
3061 cc_library {
3062 name: "libfoo",
3063 stl: "none",
3064 system_shared_libs: [],
3065 apex_available: ["//apex_available:platform"],
3066 }`)
3067
3068 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003069 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3070 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003071
3072 ctx, _ = testApex(t, `
3073 apex {
3074 name: "myapex",
3075 key: "myapex.key",
3076 native_shared_libs: ["libfoo"],
3077 }
3078
3079 apex_key {
3080 name: "myapex.key",
3081 public_key: "testkey.avbpubkey",
3082 private_key: "testkey.pem",
3083 }
3084
3085 cc_library {
3086 name: "libfoo",
3087 stl: "none",
3088 system_shared_libs: [],
3089 apex_available: ["myapex"],
3090 static: {
3091 apex_available: ["//apex_available:platform"],
3092 },
3093 }`)
3094
3095 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003096 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3097 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003098 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003099 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3100 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003101}
3102
Jiyong Park5d790c32019-11-15 18:40:32 +09003103func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003104 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003105 apex {
3106 name: "myapex",
3107 key: "myapex.key",
3108 apps: ["app"],
3109 }
3110
3111 override_apex {
3112 name: "override_myapex",
3113 base: "myapex",
3114 apps: ["override_app"],
3115 }
3116
3117 apex_key {
3118 name: "myapex.key",
3119 public_key: "testkey.avbpubkey",
3120 private_key: "testkey.pem",
3121 }
3122
3123 android_app {
3124 name: "app",
3125 srcs: ["foo/bar/MyClass.java"],
3126 package_name: "foo",
3127 sdk_version: "none",
3128 system_modules: "none",
3129 }
3130
3131 override_android_app {
3132 name: "override_app",
3133 base: "app",
3134 package_name: "bar",
3135 }
3136 `)
3137
Jiyong Park317645e2019-12-05 13:20:58 +09003138 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3139 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3140 if originalVariant.GetOverriddenBy() != "" {
3141 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3142 }
3143 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3144 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3145 }
3146
Jiyong Park5d790c32019-11-15 18:40:32 +09003147 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3148 apexRule := module.Rule("apexRule")
3149 copyCmds := apexRule.Args["copy_commands"]
3150
3151 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3152 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003153
3154 apexBundle := module.Module().(*apexBundle)
3155 name := apexBundle.Name()
3156 if name != "override_myapex" {
3157 t.Errorf("name should be \"override_myapex\", but was %q", name)
3158 }
3159
3160 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3161 var builder strings.Builder
3162 data.Custom(&builder, name, "TARGET_", "", data)
3163 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003164 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003165 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3166 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
3167 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003168 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003169 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3170 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003171}
3172
Jooyung Han214bf372019-11-12 13:03:50 +09003173func TestLegacyAndroid10Support(t *testing.T) {
3174 ctx, _ := testApex(t, `
3175 apex {
3176 name: "myapex",
3177 key: "myapex.key",
3178 legacy_android10_support: true,
3179 }
3180
3181 apex_key {
3182 name: "myapex.key",
3183 public_key: "testkey.avbpubkey",
3184 private_key: "testkey.pem",
3185 }
3186 `)
3187
3188 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3189 args := module.Rule("apexRule").Args
3190 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3191}
3192
Jiyong Park479321d2019-12-16 11:47:12 +09003193func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3194 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3195 apex {
3196 name: "myapex",
3197 key: "myapex.key",
3198 java_libs: ["myjar"],
3199 }
3200
3201 apex_key {
3202 name: "myapex.key",
3203 public_key: "testkey.avbpubkey",
3204 private_key: "testkey.pem",
3205 }
3206
3207 java_library {
3208 name: "myjar",
3209 srcs: ["foo/bar/MyClass.java"],
3210 sdk_version: "none",
3211 system_modules: "none",
3212 }
3213 `)
3214}
3215
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003216func TestMain(m *testing.M) {
3217 run := func() int {
3218 setUp()
3219 defer tearDown()
3220
3221 return m.Run()
3222 }
3223
3224 os.Exit(run())
3225}