blob: 4236ca6adc20effb7e974087c757c03878a628ac [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()
Jiyong Park25fc6a92018-11-18 18:02:45 +090096
97 bp = bp + `
98 toolchain_library {
99 name: "libcompiler_rt-extras",
100 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900101 vendor_available: true,
102 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900103 }
104
105 toolchain_library {
106 name: "libatomic",
107 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900108 vendor_available: true,
109 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900110 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900111 }
112
113 toolchain_library {
114 name: "libgcc",
115 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900116 vendor_available: true,
117 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900118 }
119
120 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700121 name: "libgcc_stripped",
122 src: "",
123 vendor_available: true,
124 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900125 native_bridge_supported: true,
Yi Kongacee27c2019-03-29 20:05:14 -0700126 }
127
128 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900129 name: "libclang_rt.builtins-aarch64-android",
130 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900131 vendor_available: true,
132 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900133 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900134 }
135
136 toolchain_library {
137 name: "libclang_rt.builtins-arm-android",
138 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900139 vendor_available: true,
140 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900141 native_bridge_supported: true,
142 }
143
144 toolchain_library {
145 name: "libclang_rt.builtins-x86_64-android",
146 src: "",
147 vendor_available: true,
148 recovery_available: true,
149 native_bridge_supported: true,
150 }
151
152 toolchain_library {
153 name: "libclang_rt.builtins-i686-android",
154 src: "",
155 vendor_available: true,
156 recovery_available: true,
157 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900158 }
159
160 cc_object {
161 name: "crtbegin_so",
162 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900163 vendor_available: true,
164 recovery_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +0900165 native_bridge_supported: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 }
167
168 cc_object {
169 name: "crtend_so",
170 stl: "none",
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
Alex Light3d673592019-01-18 14:37:31 -0800176 cc_object {
177 name: "crtbegin_static",
178 stl: "none",
179 }
180
181 cc_object {
182 name: "crtend_android",
183 stl: "none",
184 }
185
Jiyong Parkda6eb592018-12-19 17:12:36 +0900186 llndk_library {
187 name: "libc",
188 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900189 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 }
191
192 llndk_library {
193 name: "libm",
194 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900195 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900196 }
197
198 llndk_library {
199 name: "libdl",
200 symbol_file: "",
Jooyung Han344d5432019-08-23 11:17:39 +0900201 native_bridge_supported: true,
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202 }
Jooyung Han54aca7b2019-11-20 02:26:02 +0900203
204 filegroup {
205 name: "myapex-file_contexts",
206 srcs: [
207 "system/sepolicy/apex/myapex-file_contexts",
208 ],
209 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800211
Dario Frenicde2a032019-10-27 00:29:22 +0100212 bp = bp + java.GatherRequiredDepsForTest()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213
Jooyung Han344d5432019-08-23 11:17:39 +0900214 fs := map[string][]byte{
Jooyung Han54aca7b2019-11-20 02:26:02 +0900215 "a.java": nil,
216 "PrebuiltAppFoo.apk": nil,
217 "PrebuiltAppFooPriv.apk": nil,
218 "build/make/target/product/security": nil,
219 "apex_manifest.json": nil,
220 "AndroidManifest.xml": nil,
221 "system/sepolicy/apex/myapex-file_contexts": nil,
222 "system/sepolicy/apex/otherapex-file_contexts": nil,
223 "system/sepolicy/apex/commonapex-file_contexts": nil,
224 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
Colin Cross98be1bb2019-12-13 20:41:13 -0800225 "mylib.cpp": nil,
226 "mylib_common.cpp": nil,
227 "mytest.cpp": nil,
228 "mytest1.cpp": nil,
229 "mytest2.cpp": nil,
230 "mytest3.cpp": nil,
231 "myprebuilt": nil,
232 "my_include": nil,
233 "foo/bar/MyClass.java": nil,
234 "prebuilt.jar": nil,
235 "vendor/foo/devkeys/test.x509.pem": nil,
236 "vendor/foo/devkeys/test.pk8": nil,
237 "testkey.x509.pem": nil,
238 "testkey.pk8": nil,
239 "testkey.override.x509.pem": nil,
240 "testkey.override.pk8": nil,
241 "vendor/foo/devkeys/testkey.avbpubkey": nil,
242 "vendor/foo/devkeys/testkey.pem": nil,
243 "NOTICE": nil,
244 "custom_notice": nil,
245 "testkey2.avbpubkey": nil,
246 "testkey2.pem": nil,
247 "myapex-arm64.apex": nil,
248 "myapex-arm.apex": nil,
249 "frameworks/base/api/current.txt": nil,
250 "framework/aidl/a.aidl": nil,
251 "build/make/core/proguard.flags": nil,
252 "build/make/core/proguard_basic_keeps.flags": nil,
253 "dummy.txt": nil,
Jooyung Han344d5432019-08-23 11:17:39 +0900254 }
255
256 for _, handler := range handlers {
Colin Cross98be1bb2019-12-13 20:41:13 -0800257 // The fs now needs to be populated before creating the config, call handlers twice
258 // for now, once to get any fs changes, and later after the config was created to
259 // set product variables or targets.
260 tempConfig := android.TestArchConfig(buildDir, nil, bp, fs)
261 handler(fs, tempConfig)
Jooyung Han344d5432019-08-23 11:17:39 +0900262 }
263
Colin Cross98be1bb2019-12-13 20:41:13 -0800264 config := android.TestArchConfig(buildDir, nil, bp, fs)
265 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
266 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
267 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
268 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
269 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
270 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
271
272 for _, handler := range handlers {
273 // The fs now needs to be populated before creating the config, call handlers twice
274 // for now, earlier to get any fs changes, and now after the config was created to
275 // set product variables or targets.
276 tempFS := map[string][]byte{}
277 handler(tempFS, config)
278 }
279
280 ctx := android.NewTestArchContext()
281 ctx.RegisterModuleType("apex", BundleFactory)
282 ctx.RegisterModuleType("apex_test", testApexBundleFactory)
283 ctx.RegisterModuleType("apex_vndk", vndkApexBundleFactory)
284 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
285 ctx.RegisterModuleType("apex_defaults", defaultsFactory)
286 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
287 ctx.RegisterModuleType("override_apex", overrideApexFactory)
288
Paul Duffin77980a82019-12-19 16:01:36 +0000289 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross98be1bb2019-12-13 20:41:13 -0800290 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800291 ctx.RegisterModuleType("cc_test", cc.TestFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800292 ctx.RegisterModuleType("vndk_prebuilt_shared", cc.VndkPrebuiltSharedFactory)
293 ctx.RegisterModuleType("vndk_libraries_txt", cc.VndkLibrariesTxtFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800294 ctx.RegisterModuleType("prebuilt_etc", android.PrebuiltEtcFactory)
295 ctx.RegisterModuleType("sh_binary", android.ShBinaryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800296 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000297 java.RegisterJavaBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +0000298 java.RegisterSystemModulesBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +0000299 java.RegisterAppBuildComponents(ctx)
Jooyung Han58f26ab2019-12-18 15:34:32 +0900300 ctx.RegisterModuleType("java_sdk_library", java.SdkLibraryFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -0800301
302 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800303 ctx.PreDepsMutators(RegisterPreDepsMutators)
304 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
305 ctx.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross98be1bb2019-12-13 20:41:13 -0800306
307 ctx.Register(config)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308
Jooyung Han5c998b92019-06-27 11:30:33 +0900309 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310}
311
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700312func setUp() {
313 var err error
314 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700316 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900318}
319
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700320func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321 os.RemoveAll(buildDir)
322}
323
324// ensure that 'result' contains 'expected'
325func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900326 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 if !strings.Contains(result, expected) {
328 t.Errorf("%q is not found in %q", expected, result)
329 }
330}
331
332// ensures that 'result' does not contain 'notExpected'
333func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900334 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 if strings.Contains(result, notExpected) {
336 t.Errorf("%q is found in %q", notExpected, result)
337 }
338}
339
340func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900341 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900342 if !android.InList(expected, result) {
343 t.Errorf("%q is not found in %v", expected, result)
344 }
345}
346
347func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900348 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900349 if android.InList(notExpected, result) {
350 t.Errorf("%q is found in %v", notExpected, result)
351 }
352}
353
Jooyung Hane1633032019-08-01 17:41:43 +0900354func ensureListEmpty(t *testing.T, result []string) {
355 t.Helper()
356 if len(result) > 0 {
357 t.Errorf("%q is expected to be empty", result)
358 }
359}
360
Jiyong Park25fc6a92018-11-18 18:02:45 +0900361// Minimal test
362func TestBasicApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700363 ctx, _ := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900364 apex_defaults {
365 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900366 manifest: ":myapex.manifest",
367 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 key: "myapex.key",
369 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800370 multilib: {
371 both: {
372 binaries: ["foo",],
373 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 },
Jiyong Park9e6c2422019-08-09 20:39:45 +0900375 java_libs: ["myjar", "myprebuiltjar"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900376 }
377
Jiyong Park30ca9372019-02-07 16:27:23 +0900378 apex {
379 name: "myapex",
380 defaults: ["myapex-defaults"],
381 }
382
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383 apex_key {
384 name: "myapex.key",
385 public_key: "testkey.avbpubkey",
386 private_key: "testkey.pem",
387 }
388
Jiyong Park809bb722019-02-13 21:33:49 +0900389 filegroup {
390 name: "myapex.manifest",
391 srcs: ["apex_manifest.json"],
392 }
393
394 filegroup {
395 name: "myapex.androidmanifest",
396 srcs: ["AndroidManifest.xml"],
397 }
398
Jiyong Park25fc6a92018-11-18 18:02:45 +0900399 cc_library {
400 name: "mylib",
401 srcs: ["mylib.cpp"],
402 shared_libs: ["mylib2"],
403 system_shared_libs: [],
404 stl: "none",
405 }
406
Alex Light3d673592019-01-18 14:37:31 -0800407 cc_binary {
408 name: "foo",
409 srcs: ["mylib.cpp"],
410 compile_multilib: "both",
411 multilib: {
412 lib32: {
413 suffix: "32",
414 },
415 lib64: {
416 suffix: "64",
417 },
418 },
419 symlinks: ["foo_link_"],
420 symlink_preferred_arch: true,
421 system_shared_libs: [],
422 static_executable: true,
423 stl: "none",
424 }
425
Jiyong Park25fc6a92018-11-18 18:02:45 +0900426 cc_library {
427 name: "mylib2",
428 srcs: ["mylib.cpp"],
429 system_shared_libs: [],
430 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900431 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900433
434 java_library {
435 name: "myjar",
436 srcs: ["foo/bar/MyClass.java"],
437 sdk_version: "none",
438 system_modules: "none",
439 compile_dex: true,
440 static_libs: ["myotherjar"],
441 }
442
443 java_library {
444 name: "myotherjar",
445 srcs: ["foo/bar/MyClass.java"],
446 sdk_version: "none",
447 system_modules: "none",
448 compile_dex: true,
449 }
Jiyong Park9e6c2422019-08-09 20:39:45 +0900450
451 java_import {
452 name: "myprebuiltjar",
453 jars: ["prebuilt.jar"],
454 installable: true,
455 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900456 `)
457
Sundong Ahnabb64432019-10-22 13:58:29 +0900458 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900459
460 optFlags := apexRule.Args["opt_flags"]
461 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700462 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900463 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900464
Jiyong Park25fc6a92018-11-18 18:02:45 +0900465 copyCmds := apexRule.Args["copy_commands"]
466
467 // Ensure that main rule creates an output
468 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
469
470 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800471 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900472 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900473 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474
475 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800476 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900478
479 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800480 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
481 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900483 ensureContains(t, copyCmds, "image.apex/javalib/myprebuiltjar.jar")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 // .. but not for java libs
485 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800486
Colin Cross7113d202019-11-20 16:39:12 -0800487 // Ensure that the platform variant ends with _shared or _common
488 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
489 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
491 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park9e6c2422019-08-09 20:39:45 +0900492 ensureListContains(t, ctx.ModuleVariantsForTests("myprebuiltjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800493
494 // Ensure that all symlinks are present.
495 found_foo_link_64 := false
496 found_foo := false
497 for _, cmd := range strings.Split(copyCmds, " && ") {
498 if strings.HasPrefix(cmd, "ln -s foo64") {
499 if strings.HasSuffix(cmd, "bin/foo") {
500 found_foo = true
501 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
502 found_foo_link_64 = true
503 }
504 }
505 }
506 good := found_foo && found_foo_link_64
507 if !good {
508 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
509 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900510
Sundong Ahnabb64432019-10-22 13:58:29 +0900511 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700512 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700513 if len(noticeInputs) != 2 {
514 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900515 }
516 ensureListContains(t, noticeInputs, "NOTICE")
517 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800518}
519
Jooyung Hanf21c7972019-12-16 22:32:06 +0900520func TestDefaults(t *testing.T) {
521 ctx, _ := testApex(t, `
522 apex_defaults {
523 name: "myapex-defaults",
524 key: "myapex.key",
525 prebuilts: ["myetc"],
526 native_shared_libs: ["mylib"],
527 java_libs: ["myjar"],
528 apps: ["AppFoo"],
529 }
530
531 prebuilt_etc {
532 name: "myetc",
533 src: "myprebuilt",
534 }
535
536 apex {
537 name: "myapex",
538 defaults: ["myapex-defaults"],
539 }
540
541 apex_key {
542 name: "myapex.key",
543 public_key: "testkey.avbpubkey",
544 private_key: "testkey.pem",
545 }
546
547 cc_library {
548 name: "mylib",
549 system_shared_libs: [],
550 stl: "none",
551 }
552
553 java_library {
554 name: "myjar",
555 srcs: ["foo/bar/MyClass.java"],
556 sdk_version: "none",
557 system_modules: "none",
558 compile_dex: true,
559 }
560
561 android_app {
562 name: "AppFoo",
563 srcs: ["foo/bar/MyClass.java"],
564 sdk_version: "none",
565 system_modules: "none",
566 }
567 `)
568 ensureExactContents(t, ctx, "myapex", []string{
569 "etc/myetc",
570 "javalib/myjar.jar",
571 "lib64/mylib.so",
572 "app/AppFoo/AppFoo.apk",
573 })
574}
575
Jooyung Han01a3ee22019-11-02 02:52:25 +0900576func TestApexManifest(t *testing.T) {
577 ctx, _ := testApex(t, `
578 apex {
579 name: "myapex",
580 key: "myapex.key",
581 }
582
583 apex_key {
584 name: "myapex.key",
585 public_key: "testkey.avbpubkey",
586 private_key: "testkey.pem",
587 }
588 `)
589
590 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900591 args := module.Rule("apexRule").Args
592 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
593 t.Error("manifest should be apex_manifest.pb, but " + manifest)
594 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900595}
596
Alex Light5098a612018-11-29 17:12:15 -0800597func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700598 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800599 apex {
600 name: "myapex",
601 key: "myapex.key",
602 payload_type: "zip",
603 native_shared_libs: ["mylib"],
604 }
605
606 apex_key {
607 name: "myapex.key",
608 public_key: "testkey.avbpubkey",
609 private_key: "testkey.pem",
610 }
611
612 cc_library {
613 name: "mylib",
614 srcs: ["mylib.cpp"],
615 shared_libs: ["mylib2"],
616 system_shared_libs: [],
617 stl: "none",
618 }
619
620 cc_library {
621 name: "mylib2",
622 srcs: ["mylib.cpp"],
623 system_shared_libs: [],
624 stl: "none",
625 }
626 `)
627
Sundong Ahnabb64432019-10-22 13:58:29 +0900628 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800629 copyCmds := zipApexRule.Args["copy_commands"]
630
631 // Ensure that main rule creates an output
632 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
633
634 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800635 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800636
637 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800638 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800639
640 // Ensure that both direct and indirect deps are copied into apex
641 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
642 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900643}
644
645func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700646 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900647 apex {
648 name: "myapex",
649 key: "myapex.key",
650 native_shared_libs: ["mylib", "mylib3"],
651 }
652
653 apex_key {
654 name: "myapex.key",
655 public_key: "testkey.avbpubkey",
656 private_key: "testkey.pem",
657 }
658
659 cc_library {
660 name: "mylib",
661 srcs: ["mylib.cpp"],
662 shared_libs: ["mylib2", "mylib3"],
663 system_shared_libs: [],
664 stl: "none",
665 }
666
667 cc_library {
668 name: "mylib2",
669 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900670 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671 system_shared_libs: [],
672 stl: "none",
673 stubs: {
674 versions: ["1", "2", "3"],
675 },
676 }
677
678 cc_library {
679 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900680 srcs: ["mylib.cpp"],
681 shared_libs: ["mylib4"],
682 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900683 stl: "none",
684 stubs: {
685 versions: ["10", "11", "12"],
686 },
687 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900688
689 cc_library {
690 name: "mylib4",
691 srcs: ["mylib.cpp"],
692 system_shared_libs: [],
693 stl: "none",
694 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900695 `)
696
Sundong Ahnabb64432019-10-22 13:58:29 +0900697 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900698 copyCmds := apexRule.Args["copy_commands"]
699
700 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800701 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900702
703 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800704 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900705
706 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800707 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900708
Colin Cross7113d202019-11-20 16:39:12 -0800709 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900710
711 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800712 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900713 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800714 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900715
716 // 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 -0800717 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900718 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800719 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900720
721 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800722 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900723 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900724
725 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800726 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900727
728 ensureExactContents(t, ctx, "myapex", []string{
729 "lib64/mylib.so",
730 "lib64/mylib3.so",
731 "lib64/mylib4.so",
732 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900733}
734
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900735func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700736 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900737 apex {
738 name: "myapex",
739 key: "myapex.key",
740 native_shared_libs: ["mylib"],
741 }
742
743 apex_key {
744 name: "myapex.key",
745 public_key: "testkey.avbpubkey",
746 private_key: "testkey.pem",
747 }
748
749 cc_library {
750 name: "mylib",
751 srcs: ["mylib.cpp"],
752 shared_libs: ["libfoo#10"],
753 system_shared_libs: [],
754 stl: "none",
755 }
756
757 cc_library {
758 name: "libfoo",
759 srcs: ["mylib.cpp"],
760 shared_libs: ["libbar"],
761 system_shared_libs: [],
762 stl: "none",
763 stubs: {
764 versions: ["10", "20", "30"],
765 },
766 }
767
768 cc_library {
769 name: "libbar",
770 srcs: ["mylib.cpp"],
771 system_shared_libs: [],
772 stl: "none",
773 }
774
775 `)
776
Sundong Ahnabb64432019-10-22 13:58:29 +0900777 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900778 copyCmds := apexRule.Args["copy_commands"]
779
780 // Ensure that direct non-stubs dep is always included
781 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
782
783 // Ensure that indirect stubs dep is not included
784 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
785
786 // Ensure that dependency of stubs is not included
787 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
788
Colin Cross7113d202019-11-20 16:39:12 -0800789 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900790
791 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800792 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900793 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800794 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900795
Colin Cross7113d202019-11-20 16:39:12 -0800796 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900797
798 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
799 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
800}
801
Jooyung Hand3639552019-08-09 12:57:43 +0900802func TestApexWithRuntimeLibsDependency(t *testing.T) {
803 /*
804 myapex
805 |
806 v (runtime_libs)
807 mylib ------+------> libfoo [provides stub]
808 |
809 `------> libbar
810 */
811 ctx, _ := testApex(t, `
812 apex {
813 name: "myapex",
814 key: "myapex.key",
815 native_shared_libs: ["mylib"],
816 }
817
818 apex_key {
819 name: "myapex.key",
820 public_key: "testkey.avbpubkey",
821 private_key: "testkey.pem",
822 }
823
824 cc_library {
825 name: "mylib",
826 srcs: ["mylib.cpp"],
827 runtime_libs: ["libfoo", "libbar"],
828 system_shared_libs: [],
829 stl: "none",
830 }
831
832 cc_library {
833 name: "libfoo",
834 srcs: ["mylib.cpp"],
835 system_shared_libs: [],
836 stl: "none",
837 stubs: {
838 versions: ["10", "20", "30"],
839 },
840 }
841
842 cc_library {
843 name: "libbar",
844 srcs: ["mylib.cpp"],
845 system_shared_libs: [],
846 stl: "none",
847 }
848
849 `)
850
Sundong Ahnabb64432019-10-22 13:58:29 +0900851 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900852 copyCmds := apexRule.Args["copy_commands"]
853
854 // Ensure that direct non-stubs dep is always included
855 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
856
857 // Ensure that indirect stubs dep is not included
858 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
859
860 // Ensure that runtime_libs dep in included
861 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
862
Sundong Ahnabb64432019-10-22 13:58:29 +0900863 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900864 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
865 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900866
867}
868
Jooyung Han9c80bae2019-08-20 17:30:57 +0900869func TestApexDependencyToLLNDK(t *testing.T) {
870 ctx, _ := testApex(t, `
871 apex {
872 name: "myapex",
873 key: "myapex.key",
874 use_vendor: true,
875 native_shared_libs: ["mylib"],
876 }
877
878 apex_key {
879 name: "myapex.key",
880 public_key: "testkey.avbpubkey",
881 private_key: "testkey.pem",
882 }
883
884 cc_library {
885 name: "mylib",
886 srcs: ["mylib.cpp"],
887 vendor_available: true,
888 shared_libs: ["libbar"],
889 system_shared_libs: [],
890 stl: "none",
891 }
892
893 cc_library {
894 name: "libbar",
895 srcs: ["mylib.cpp"],
896 system_shared_libs: [],
897 stl: "none",
898 }
899
900 llndk_library {
901 name: "libbar",
902 symbol_file: "",
903 }
Jooyung Handc782442019-11-01 03:14:38 +0900904 `, func(fs map[string][]byte, config android.Config) {
905 setUseVendorWhitelistForTest(config, []string{"myapex"})
906 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900907
Sundong Ahnabb64432019-10-22 13:58:29 +0900908 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900909 copyCmds := apexRule.Args["copy_commands"]
910
911 // Ensure that LLNDK dep is not included
912 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
913
Sundong Ahnabb64432019-10-22 13:58:29 +0900914 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900915 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900916
917 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900918 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900919
920}
921
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700923 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924 apex {
925 name: "myapex",
926 key: "myapex.key",
927 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
928 }
929
930 apex_key {
931 name: "myapex.key",
932 public_key: "testkey.avbpubkey",
933 private_key: "testkey.pem",
934 }
935
936 cc_library {
937 name: "mylib",
938 srcs: ["mylib.cpp"],
939 shared_libs: ["libdl#27"],
940 stl: "none",
941 }
942
943 cc_library_shared {
944 name: "mylib_shared",
945 srcs: ["mylib.cpp"],
946 shared_libs: ["libdl#27"],
947 stl: "none",
948 }
949
950 cc_library {
951 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700952 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900953 nocrt: true,
954 system_shared_libs: [],
955 stl: "none",
956 stubs: {
957 versions: ["27", "28", "29"],
958 },
959 }
960
961 cc_library {
962 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700963 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 nocrt: true,
965 system_shared_libs: [],
966 stl: "none",
967 stubs: {
968 versions: ["27", "28", "29"],
969 },
970 }
971
972 cc_library {
973 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700974 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 nocrt: true,
976 system_shared_libs: [],
977 stl: "none",
978 stubs: {
979 versions: ["27", "28", "29"],
980 },
981 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900982
983 cc_library {
984 name: "libBootstrap",
985 srcs: ["mylib.cpp"],
986 stl: "none",
987 bootstrap: true,
988 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989 `)
990
Sundong Ahnabb64432019-10-22 13:58:29 +0900991 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 copyCmds := apexRule.Args["copy_commands"]
993
994 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800995 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900996 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
997 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998
999 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001000 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001001
Colin Cross7113d202019-11-20 16:39:12 -08001002 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
1003 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
1004 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005
1006 // For dependency to libc
1007 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001008 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001009 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001010 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... Cflags from stub is correctly exported to mylib
1012 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1013 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1014
1015 // For dependency to libm
1016 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001017 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001018 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001019 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001020 // ... and is not compiling with the stub
1021 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1022 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1023
1024 // For dependency to libdl
1025 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001026 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001027 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001028 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1029 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001030 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001031 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032 // ... Cflags from stub is correctly exported to mylib
1033 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1034 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001035
1036 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001037 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1038 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1039 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1040 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001041}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001042
1043func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001044 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001045 apex {
1046 name: "myapex",
1047 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001048 native_shared_libs: ["mylib"],
1049 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001050 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001051 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001052 }
1053
1054 apex_key {
1055 name: "myapex.key",
1056 public_key: "testkey.avbpubkey",
1057 private_key: "testkey.pem",
1058 }
1059
1060 prebuilt_etc {
1061 name: "myetc",
1062 src: "myprebuilt",
1063 sub_dir: "foo/bar",
1064 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001065
1066 cc_library {
1067 name: "mylib",
1068 srcs: ["mylib.cpp"],
1069 relative_install_path: "foo/bar",
1070 system_shared_libs: [],
1071 stl: "none",
1072 }
1073
1074 cc_binary {
1075 name: "mybin",
1076 srcs: ["mylib.cpp"],
1077 relative_install_path: "foo/bar",
1078 system_shared_libs: [],
1079 static_executable: true,
1080 stl: "none",
1081 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001082 `)
1083
Sundong Ahnabb64432019-10-22 13:58:29 +09001084 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001085 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1086
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001087 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001088 ensureListContains(t, dirs, "etc")
1089 ensureListContains(t, dirs, "etc/foo")
1090 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001091 ensureListContains(t, dirs, "lib64")
1092 ensureListContains(t, dirs, "lib64/foo")
1093 ensureListContains(t, dirs, "lib64/foo/bar")
1094 ensureListContains(t, dirs, "lib")
1095 ensureListContains(t, dirs, "lib/foo")
1096 ensureListContains(t, dirs, "lib/foo/bar")
1097
Jiyong Parkbd13e442019-03-15 18:10:35 +09001098 ensureListContains(t, dirs, "bin")
1099 ensureListContains(t, dirs, "bin/foo")
1100 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001101}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001102
1103func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001104 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001105 apex {
1106 name: "myapex",
1107 key: "myapex.key",
1108 native_shared_libs: ["mylib"],
1109 use_vendor: true,
1110 }
1111
1112 apex_key {
1113 name: "myapex.key",
1114 public_key: "testkey.avbpubkey",
1115 private_key: "testkey.pem",
1116 }
1117
1118 cc_library {
1119 name: "mylib",
1120 srcs: ["mylib.cpp"],
1121 shared_libs: ["mylib2"],
1122 system_shared_libs: [],
1123 vendor_available: true,
1124 stl: "none",
1125 }
1126
1127 cc_library {
1128 name: "mylib2",
1129 srcs: ["mylib.cpp"],
1130 system_shared_libs: [],
1131 vendor_available: true,
1132 stl: "none",
1133 }
Jooyung Handc782442019-11-01 03:14:38 +09001134 `, func(fs map[string][]byte, config android.Config) {
1135 setUseVendorWhitelistForTest(config, []string{"myapex"})
1136 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001137
1138 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001139 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001140 for _, implicit := range i.Implicits {
1141 inputsList = append(inputsList, implicit.String())
1142 }
1143 }
1144 inputsString := strings.Join(inputsList, " ")
1145
1146 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001147 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1148 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001149
1150 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001151 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1152 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001153}
Jiyong Park16e91a02018-12-20 18:18:08 +09001154
Jooyung Handc782442019-11-01 03:14:38 +09001155func TestUseVendorRestriction(t *testing.T) {
1156 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1157 apex {
1158 name: "myapex",
1159 key: "myapex.key",
1160 use_vendor: true,
1161 }
1162 apex_key {
1163 name: "myapex.key",
1164 public_key: "testkey.avbpubkey",
1165 private_key: "testkey.pem",
1166 }
1167 `, func(fs map[string][]byte, config android.Config) {
1168 setUseVendorWhitelistForTest(config, []string{""})
1169 })
1170 // no error with whitelist
1171 testApex(t, `
1172 apex {
1173 name: "myapex",
1174 key: "myapex.key",
1175 use_vendor: true,
1176 }
1177 apex_key {
1178 name: "myapex.key",
1179 public_key: "testkey.avbpubkey",
1180 private_key: "testkey.pem",
1181 }
1182 `, func(fs map[string][]byte, config android.Config) {
1183 setUseVendorWhitelistForTest(config, []string{"myapex"})
1184 })
1185}
1186
Jooyung Han5c998b92019-06-27 11:30:33 +09001187func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1188 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1189 apex {
1190 name: "myapex",
1191 key: "myapex.key",
1192 native_shared_libs: ["mylib"],
1193 use_vendor: true,
1194 }
1195
1196 apex_key {
1197 name: "myapex.key",
1198 public_key: "testkey.avbpubkey",
1199 private_key: "testkey.pem",
1200 }
1201
1202 cc_library {
1203 name: "mylib",
1204 srcs: ["mylib.cpp"],
1205 system_shared_libs: [],
1206 stl: "none",
1207 }
1208 `)
1209}
1210
Jiyong Park16e91a02018-12-20 18:18:08 +09001211func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001212 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001213 apex {
1214 name: "myapex",
1215 key: "myapex.key",
1216 native_shared_libs: ["mylib"],
1217 }
1218
1219 apex_key {
1220 name: "myapex.key",
1221 public_key: "testkey.avbpubkey",
1222 private_key: "testkey.pem",
1223 }
1224
1225 cc_library {
1226 name: "mylib",
1227 srcs: ["mylib.cpp"],
1228 system_shared_libs: [],
1229 stl: "none",
1230 stubs: {
1231 versions: ["1", "2", "3"],
1232 },
1233 }
1234
1235 cc_binary {
1236 name: "not_in_apex",
1237 srcs: ["mylib.cpp"],
1238 static_libs: ["mylib"],
1239 static_executable: true,
1240 system_shared_libs: [],
1241 stl: "none",
1242 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001243 `)
1244
Colin Cross7113d202019-11-20 16:39:12 -08001245 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001246
1247 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001248 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001249}
Jiyong Park9335a262018-12-24 11:31:58 +09001250
1251func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001252 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001253 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001254 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001255 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001256 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001257 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001258 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001259 }
1260
1261 cc_library {
1262 name: "mylib",
1263 srcs: ["mylib.cpp"],
1264 system_shared_libs: [],
1265 stl: "none",
1266 }
1267
1268 apex_key {
1269 name: "myapex.key",
1270 public_key: "testkey.avbpubkey",
1271 private_key: "testkey.pem",
1272 }
1273
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001274 android_app_certificate {
1275 name: "myapex.certificate",
1276 certificate: "testkey",
1277 }
1278
1279 android_app_certificate {
1280 name: "myapex.certificate.override",
1281 certificate: "testkey.override",
1282 }
1283
Jiyong Park9335a262018-12-24 11:31:58 +09001284 `)
1285
1286 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001287 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001288
1289 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1290 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1291 "vendor/foo/devkeys/testkey.avbpubkey")
1292 }
1293 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1294 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1295 "vendor/foo/devkeys/testkey.pem")
1296 }
1297
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001298 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001299 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001300 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001301 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001302 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001303 }
1304}
Jiyong Park58e364a2019-01-19 19:24:06 +09001305
Jooyung Hanf121a652019-12-17 14:30:11 +09001306func TestCertificate(t *testing.T) {
1307 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1308 ctx, _ := testApex(t, `
1309 apex {
1310 name: "myapex",
1311 key: "myapex.key",
1312 }
1313 apex_key {
1314 name: "myapex.key",
1315 public_key: "testkey.avbpubkey",
1316 private_key: "testkey.pem",
1317 }`)
1318 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1319 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1320 if actual := rule.Args["certificates"]; actual != expected {
1321 t.Errorf("certificates should be %q, not %q", expected, actual)
1322 }
1323 })
1324 t.Run("override when unspecified", func(t *testing.T) {
1325 ctx, _ := testApex(t, `
1326 apex {
1327 name: "myapex_keytest",
1328 key: "myapex.key",
1329 file_contexts: ":myapex-file_contexts",
1330 }
1331 apex_key {
1332 name: "myapex.key",
1333 public_key: "testkey.avbpubkey",
1334 private_key: "testkey.pem",
1335 }
1336 android_app_certificate {
1337 name: "myapex.certificate.override",
1338 certificate: "testkey.override",
1339 }`)
1340 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1341 expected := "testkey.override.x509.pem testkey.override.pk8"
1342 if actual := rule.Args["certificates"]; actual != expected {
1343 t.Errorf("certificates should be %q, not %q", expected, actual)
1344 }
1345 })
1346 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1347 ctx, _ := testApex(t, `
1348 apex {
1349 name: "myapex",
1350 key: "myapex.key",
1351 certificate: ":myapex.certificate",
1352 }
1353 apex_key {
1354 name: "myapex.key",
1355 public_key: "testkey.avbpubkey",
1356 private_key: "testkey.pem",
1357 }
1358 android_app_certificate {
1359 name: "myapex.certificate",
1360 certificate: "testkey",
1361 }`)
1362 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1363 expected := "testkey.x509.pem testkey.pk8"
1364 if actual := rule.Args["certificates"]; actual != expected {
1365 t.Errorf("certificates should be %q, not %q", expected, actual)
1366 }
1367 })
1368 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1369 ctx, _ := testApex(t, `
1370 apex {
1371 name: "myapex_keytest",
1372 key: "myapex.key",
1373 file_contexts: ":myapex-file_contexts",
1374 certificate: ":myapex.certificate",
1375 }
1376 apex_key {
1377 name: "myapex.key",
1378 public_key: "testkey.avbpubkey",
1379 private_key: "testkey.pem",
1380 }
1381 android_app_certificate {
1382 name: "myapex.certificate.override",
1383 certificate: "testkey.override",
1384 }`)
1385 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1386 expected := "testkey.override.x509.pem testkey.override.pk8"
1387 if actual := rule.Args["certificates"]; actual != expected {
1388 t.Errorf("certificates should be %q, not %q", expected, actual)
1389 }
1390 })
1391 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1392 ctx, _ := testApex(t, `
1393 apex {
1394 name: "myapex",
1395 key: "myapex.key",
1396 certificate: "testkey",
1397 }
1398 apex_key {
1399 name: "myapex.key",
1400 public_key: "testkey.avbpubkey",
1401 private_key: "testkey.pem",
1402 }`)
1403 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1404 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1405 if actual := rule.Args["certificates"]; actual != expected {
1406 t.Errorf("certificates should be %q, not %q", expected, actual)
1407 }
1408 })
1409 t.Run("override when specified as <name>", func(t *testing.T) {
1410 ctx, _ := testApex(t, `
1411 apex {
1412 name: "myapex_keytest",
1413 key: "myapex.key",
1414 file_contexts: ":myapex-file_contexts",
1415 certificate: "testkey",
1416 }
1417 apex_key {
1418 name: "myapex.key",
1419 public_key: "testkey.avbpubkey",
1420 private_key: "testkey.pem",
1421 }
1422 android_app_certificate {
1423 name: "myapex.certificate.override",
1424 certificate: "testkey.override",
1425 }`)
1426 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1427 expected := "testkey.override.x509.pem testkey.override.pk8"
1428 if actual := rule.Args["certificates"]; actual != expected {
1429 t.Errorf("certificates should be %q, not %q", expected, actual)
1430 }
1431 })
1432}
1433
Jiyong Park58e364a2019-01-19 19:24:06 +09001434func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001435 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001436 apex {
1437 name: "myapex",
1438 key: "myapex.key",
1439 native_shared_libs: ["mylib"],
1440 }
1441
1442 apex {
1443 name: "otherapex",
1444 key: "myapex.key",
1445 native_shared_libs: ["mylib"],
1446 }
1447
1448 apex_key {
1449 name: "myapex.key",
1450 public_key: "testkey.avbpubkey",
1451 private_key: "testkey.pem",
1452 }
1453
1454 cc_library {
1455 name: "mylib",
1456 srcs: ["mylib.cpp"],
1457 system_shared_libs: [],
1458 stl: "none",
1459 }
1460 `)
1461
Jooyung Han6b8459b2019-10-30 08:29:25 +09001462 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001463 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001464 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001465 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1466 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001467
Jooyung Han6b8459b2019-10-30 08:29:25 +09001468 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001469 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001470 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001471 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1472 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001473
Jooyung Han6b8459b2019-10-30 08:29:25 +09001474 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001475 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001476 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001477 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1478 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001479}
Jiyong Park7e636d02019-01-28 16:16:54 +09001480
1481func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001482 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001483 apex {
1484 name: "myapex",
1485 key: "myapex.key",
1486 native_shared_libs: ["mylib"],
1487 }
1488
1489 apex_key {
1490 name: "myapex.key",
1491 public_key: "testkey.avbpubkey",
1492 private_key: "testkey.pem",
1493 }
1494
1495 cc_library_headers {
1496 name: "mylib_headers",
1497 export_include_dirs: ["my_include"],
1498 system_shared_libs: [],
1499 stl: "none",
1500 }
1501
1502 cc_library {
1503 name: "mylib",
1504 srcs: ["mylib.cpp"],
1505 system_shared_libs: [],
1506 stl: "none",
1507 header_libs: ["mylib_headers"],
1508 export_header_lib_headers: ["mylib_headers"],
1509 stubs: {
1510 versions: ["1", "2", "3"],
1511 },
1512 }
1513
1514 cc_library {
1515 name: "otherlib",
1516 srcs: ["mylib.cpp"],
1517 system_shared_libs: [],
1518 stl: "none",
1519 shared_libs: ["mylib"],
1520 }
1521 `)
1522
Colin Cross7113d202019-11-20 16:39:12 -08001523 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001524
1525 // Ensure that the include path of the header lib is exported to 'otherlib'
1526 ensureContains(t, cFlags, "-Imy_include")
1527}
Alex Light9670d332019-01-29 18:07:33 -08001528
Jooyung Han31c470b2019-10-18 16:26:59 +09001529func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1530 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001531 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001532 copyCmds := apexRule.Args["copy_commands"]
1533 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001534 var failed bool
1535 var surplus []string
1536 filesMatched := make(map[string]bool)
1537 addContent := func(content string) {
1538 for _, expected := range files {
1539 if matched, _ := path.Match(expected, content); matched {
1540 filesMatched[expected] = true
1541 return
1542 }
1543 }
1544 surplus = append(surplus, content)
1545 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001546 for _, cmd := range strings.Split(copyCmds, "&&") {
1547 cmd = strings.TrimSpace(cmd)
1548 if cmd == "" {
1549 continue
1550 }
1551 terms := strings.Split(cmd, " ")
1552 switch terms[0] {
1553 case "mkdir":
1554 case "cp":
1555 if len(terms) != 3 {
1556 t.Fatal("copyCmds contains invalid cp command", cmd)
1557 }
1558 dst := terms[2]
1559 index := strings.Index(dst, imageApexDir)
1560 if index == -1 {
1561 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1562 }
1563 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001564 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001565 default:
1566 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1567 }
1568 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001569
Jooyung Han31c470b2019-10-18 16:26:59 +09001570 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001571 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001572 t.Log("surplus files", surplus)
1573 failed = true
1574 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001575
1576 if len(files) > len(filesMatched) {
1577 var missing []string
1578 for _, expected := range files {
1579 if !filesMatched[expected] {
1580 missing = append(missing, expected)
1581 }
1582 }
1583 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001584 t.Log("missing files", missing)
1585 failed = true
1586 }
1587 if failed {
1588 t.Fail()
1589 }
1590}
1591
Jooyung Han344d5432019-08-23 11:17:39 +09001592func TestVndkApexCurrent(t *testing.T) {
1593 ctx, _ := testApex(t, `
1594 apex_vndk {
1595 name: "myapex",
1596 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001597 }
1598
1599 apex_key {
1600 name: "myapex.key",
1601 public_key: "testkey.avbpubkey",
1602 private_key: "testkey.pem",
1603 }
1604
1605 cc_library {
1606 name: "libvndk",
1607 srcs: ["mylib.cpp"],
1608 vendor_available: true,
1609 vndk: {
1610 enabled: true,
1611 },
1612 system_shared_libs: [],
1613 stl: "none",
1614 }
1615
1616 cc_library {
1617 name: "libvndksp",
1618 srcs: ["mylib.cpp"],
1619 vendor_available: true,
1620 vndk: {
1621 enabled: true,
1622 support_system_process: true,
1623 },
1624 system_shared_libs: [],
1625 stl: "none",
1626 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001627 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001628
Jooyung Han31c470b2019-10-18 16:26:59 +09001629 ensureExactContents(t, ctx, "myapex", []string{
1630 "lib/libvndk.so",
1631 "lib/libvndksp.so",
1632 "lib64/libvndk.so",
1633 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001634 "etc/llndk.libraries.VER.txt",
1635 "etc/vndkcore.libraries.VER.txt",
1636 "etc/vndksp.libraries.VER.txt",
1637 "etc/vndkprivate.libraries.VER.txt",
1638 "etc/vndkcorevariant.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001639 })
Jooyung Han344d5432019-08-23 11:17:39 +09001640}
1641
1642func TestVndkApexWithPrebuilt(t *testing.T) {
1643 ctx, _ := testApex(t, `
1644 apex_vndk {
1645 name: "myapex",
1646 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001647 }
1648
1649 apex_key {
1650 name: "myapex.key",
1651 public_key: "testkey.avbpubkey",
1652 private_key: "testkey.pem",
1653 }
1654
1655 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001656 name: "libvndk",
1657 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001658 vendor_available: true,
1659 vndk: {
1660 enabled: true,
1661 },
1662 system_shared_libs: [],
1663 stl: "none",
1664 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001665
1666 cc_prebuilt_library_shared {
1667 name: "libvndk.arm",
1668 srcs: ["libvndk.arm.so"],
1669 vendor_available: true,
1670 vndk: {
1671 enabled: true,
1672 },
1673 enabled: false,
1674 arch: {
1675 arm: {
1676 enabled: true,
1677 },
1678 },
1679 system_shared_libs: [],
1680 stl: "none",
1681 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001682 `+vndkLibrariesTxtFiles("current"),
1683 withFiles(map[string][]byte{
1684 "libvndk.so": nil,
1685 "libvndk.arm.so": nil,
1686 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001687
Jooyung Han31c470b2019-10-18 16:26:59 +09001688 ensureExactContents(t, ctx, "myapex", []string{
1689 "lib/libvndk.so",
1690 "lib/libvndk.arm.so",
1691 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001692 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001693 })
Jooyung Han344d5432019-08-23 11:17:39 +09001694}
1695
Jooyung Han39edb6c2019-11-06 16:53:07 +09001696func vndkLibrariesTxtFiles(vers ...string) (result string) {
1697 for _, v := range vers {
1698 if v == "current" {
1699 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkcorevariant"} {
1700 result += `
1701 vndk_libraries_txt {
1702 name: "` + txt + `.libraries.txt",
1703 }
1704 `
1705 }
1706 } else {
1707 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1708 result += `
1709 prebuilt_etc {
1710 name: "` + txt + `.libraries.` + v + `.txt",
1711 src: "dummy.txt",
1712 }
1713 `
1714 }
1715 }
1716 }
1717 return
1718}
1719
Jooyung Han344d5432019-08-23 11:17:39 +09001720func TestVndkApexVersion(t *testing.T) {
1721 ctx, _ := testApex(t, `
1722 apex_vndk {
1723 name: "myapex_v27",
1724 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001725 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001726 vndk_version: "27",
1727 }
1728
1729 apex_key {
1730 name: "myapex.key",
1731 public_key: "testkey.avbpubkey",
1732 private_key: "testkey.pem",
1733 }
1734
Jooyung Han31c470b2019-10-18 16:26:59 +09001735 vndk_prebuilt_shared {
1736 name: "libvndk27",
1737 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001738 vendor_available: true,
1739 vndk: {
1740 enabled: true,
1741 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001742 target_arch: "arm64",
1743 arch: {
1744 arm: {
1745 srcs: ["libvndk27_arm.so"],
1746 },
1747 arm64: {
1748 srcs: ["libvndk27_arm64.so"],
1749 },
1750 },
Jooyung Han344d5432019-08-23 11:17:39 +09001751 }
1752
1753 vndk_prebuilt_shared {
1754 name: "libvndk27",
1755 version: "27",
1756 vendor_available: true,
1757 vndk: {
1758 enabled: true,
1759 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001760 target_arch: "x86_64",
1761 arch: {
1762 x86: {
1763 srcs: ["libvndk27_x86.so"],
1764 },
1765 x86_64: {
1766 srcs: ["libvndk27_x86_64.so"],
1767 },
1768 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001769 }
1770 `+vndkLibrariesTxtFiles("27"),
1771 withFiles(map[string][]byte{
1772 "libvndk27_arm.so": nil,
1773 "libvndk27_arm64.so": nil,
1774 "libvndk27_x86.so": nil,
1775 "libvndk27_x86_64.so": nil,
1776 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001777
Jooyung Han31c470b2019-10-18 16:26:59 +09001778 ensureExactContents(t, ctx, "myapex_v27", []string{
1779 "lib/libvndk27_arm.so",
1780 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001781 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001782 })
Jooyung Han344d5432019-08-23 11:17:39 +09001783}
1784
1785func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1786 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1787 apex_vndk {
1788 name: "myapex_v27",
1789 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001790 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001791 vndk_version: "27",
1792 }
1793 apex_vndk {
1794 name: "myapex_v27_other",
1795 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001796 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001797 vndk_version: "27",
1798 }
1799
1800 apex_key {
1801 name: "myapex.key",
1802 public_key: "testkey.avbpubkey",
1803 private_key: "testkey.pem",
1804 }
1805
1806 cc_library {
1807 name: "libvndk",
1808 srcs: ["mylib.cpp"],
1809 vendor_available: true,
1810 vndk: {
1811 enabled: true,
1812 },
1813 system_shared_libs: [],
1814 stl: "none",
1815 }
1816
1817 vndk_prebuilt_shared {
1818 name: "libvndk",
1819 version: "27",
1820 vendor_available: true,
1821 vndk: {
1822 enabled: true,
1823 },
1824 srcs: ["libvndk.so"],
1825 }
1826 `, withFiles(map[string][]byte{
1827 "libvndk.so": nil,
1828 }))
1829}
1830
Jooyung Han90eee022019-10-01 20:02:42 +09001831func TestVndkApexNameRule(t *testing.T) {
1832 ctx, _ := testApex(t, `
1833 apex_vndk {
1834 name: "myapex",
1835 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001836 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001837 }
1838 apex_vndk {
1839 name: "myapex_v28",
1840 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001841 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001842 vndk_version: "28",
1843 }
1844 apex_key {
1845 name: "myapex.key",
1846 public_key: "testkey.avbpubkey",
1847 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001848 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001849
1850 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001851 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001852 actual := proptools.String(bundle.properties.Apex_name)
1853 if !reflect.DeepEqual(actual, expected) {
1854 t.Errorf("Got '%v', expected '%v'", actual, expected)
1855 }
1856 }
1857
1858 assertApexName("com.android.vndk.vVER", "myapex")
1859 assertApexName("com.android.vndk.v28", "myapex_v28")
1860}
1861
Jooyung Han344d5432019-08-23 11:17:39 +09001862func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1863 ctx, _ := testApex(t, `
1864 apex_vndk {
1865 name: "myapex",
1866 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001867 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001868 }
1869
1870 apex_key {
1871 name: "myapex.key",
1872 public_key: "testkey.avbpubkey",
1873 private_key: "testkey.pem",
1874 }
1875
1876 cc_library {
1877 name: "libvndk",
1878 srcs: ["mylib.cpp"],
1879 vendor_available: true,
1880 native_bridge_supported: true,
1881 host_supported: true,
1882 vndk: {
1883 enabled: true,
1884 },
1885 system_shared_libs: [],
1886 stl: "none",
1887 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001888 `+vndkLibrariesTxtFiles("current"),
1889 withTargets(map[android.OsType][]android.Target{
1890 android.Android: []android.Target{
1891 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1892 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1893 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1894 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1895 },
1896 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001897
Jooyung Han31c470b2019-10-18 16:26:59 +09001898 ensureExactContents(t, ctx, "myapex", []string{
1899 "lib/libvndk.so",
1900 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001901 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001902 })
Jooyung Han344d5432019-08-23 11:17:39 +09001903}
1904
1905func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1906 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1907 apex_vndk {
1908 name: "myapex",
1909 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001910 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001911 native_bridge_supported: true,
1912 }
1913
1914 apex_key {
1915 name: "myapex.key",
1916 public_key: "testkey.avbpubkey",
1917 private_key: "testkey.pem",
1918 }
1919
1920 cc_library {
1921 name: "libvndk",
1922 srcs: ["mylib.cpp"],
1923 vendor_available: true,
1924 native_bridge_supported: true,
1925 host_supported: true,
1926 vndk: {
1927 enabled: true,
1928 },
1929 system_shared_libs: [],
1930 stl: "none",
1931 }
1932 `)
1933}
1934
Jooyung Han31c470b2019-10-18 16:26:59 +09001935func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001936 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001937 apex_vndk {
1938 name: "myapex_v27",
1939 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001940 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001941 vndk_version: "27",
1942 }
1943
1944 apex_key {
1945 name: "myapex.key",
1946 public_key: "testkey.avbpubkey",
1947 private_key: "testkey.pem",
1948 }
1949
1950 vndk_prebuilt_shared {
1951 name: "libvndk27",
1952 version: "27",
1953 target_arch: "arm",
1954 vendor_available: true,
1955 vndk: {
1956 enabled: true,
1957 },
1958 arch: {
1959 arm: {
1960 srcs: ["libvndk27.so"],
1961 }
1962 },
1963 }
1964
1965 vndk_prebuilt_shared {
1966 name: "libvndk27",
1967 version: "27",
1968 target_arch: "arm",
1969 binder32bit: true,
1970 vendor_available: true,
1971 vndk: {
1972 enabled: true,
1973 },
1974 arch: {
1975 arm: {
1976 srcs: ["libvndk27binder32.so"],
1977 }
1978 },
1979 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001980 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001981 withFiles(map[string][]byte{
1982 "libvndk27.so": nil,
1983 "libvndk27binder32.so": nil,
1984 }),
1985 withBinder32bit,
1986 withTargets(map[android.OsType][]android.Target{
1987 android.Android: []android.Target{
1988 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1989 },
1990 }),
1991 )
1992
1993 ensureExactContents(t, ctx, "myapex_v27", []string{
1994 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001995 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001996 })
1997}
1998
Jooyung Hane1633032019-08-01 17:41:43 +09001999func TestDependenciesInApexManifest(t *testing.T) {
2000 ctx, _ := testApex(t, `
2001 apex {
2002 name: "myapex_nodep",
2003 key: "myapex.key",
2004 native_shared_libs: ["lib_nodep"],
2005 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002006 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002007 }
2008
2009 apex {
2010 name: "myapex_dep",
2011 key: "myapex.key",
2012 native_shared_libs: ["lib_dep"],
2013 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002014 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002015 }
2016
2017 apex {
2018 name: "myapex_provider",
2019 key: "myapex.key",
2020 native_shared_libs: ["libfoo"],
2021 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002022 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002023 }
2024
2025 apex {
2026 name: "myapex_selfcontained",
2027 key: "myapex.key",
2028 native_shared_libs: ["lib_dep", "libfoo"],
2029 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002030 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002031 }
2032
2033 apex_key {
2034 name: "myapex.key",
2035 public_key: "testkey.avbpubkey",
2036 private_key: "testkey.pem",
2037 }
2038
2039 cc_library {
2040 name: "lib_nodep",
2041 srcs: ["mylib.cpp"],
2042 system_shared_libs: [],
2043 stl: "none",
2044 }
2045
2046 cc_library {
2047 name: "lib_dep",
2048 srcs: ["mylib.cpp"],
2049 shared_libs: ["libfoo"],
2050 system_shared_libs: [],
2051 stl: "none",
2052 }
2053
2054 cc_library {
2055 name: "libfoo",
2056 srcs: ["mytest.cpp"],
2057 stubs: {
2058 versions: ["1"],
2059 },
2060 system_shared_libs: [],
2061 stl: "none",
2062 }
2063 `)
2064
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002065 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002066 var provideNativeLibs, requireNativeLibs []string
2067
Sundong Ahnabb64432019-10-22 13:58:29 +09002068 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002069 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2070 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002071 ensureListEmpty(t, provideNativeLibs)
2072 ensureListEmpty(t, requireNativeLibs)
2073
Sundong Ahnabb64432019-10-22 13:58:29 +09002074 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002075 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2076 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002077 ensureListEmpty(t, provideNativeLibs)
2078 ensureListContains(t, requireNativeLibs, "libfoo.so")
2079
Sundong Ahnabb64432019-10-22 13:58:29 +09002080 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002081 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2082 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002083 ensureListContains(t, provideNativeLibs, "libfoo.so")
2084 ensureListEmpty(t, requireNativeLibs)
2085
Sundong Ahnabb64432019-10-22 13:58:29 +09002086 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002087 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2088 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002089 ensureListContains(t, provideNativeLibs, "libfoo.so")
2090 ensureListEmpty(t, requireNativeLibs)
2091}
2092
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002093func TestApexName(t *testing.T) {
2094 ctx, _ := testApex(t, `
2095 apex {
2096 name: "myapex",
2097 key: "myapex.key",
2098 apex_name: "com.android.myapex",
2099 }
2100
2101 apex_key {
2102 name: "myapex.key",
2103 public_key: "testkey.avbpubkey",
2104 private_key: "testkey.pem",
2105 }
2106 `)
2107
Sundong Ahnabb64432019-10-22 13:58:29 +09002108 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002109 apexManifestRule := module.Rule("apexManifestRule")
2110 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2111 apexRule := module.Rule("apexRule")
2112 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2113}
2114
Alex Light0851b882019-02-07 13:20:53 -08002115func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002116 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002117 apex {
2118 name: "myapex",
2119 key: "myapex.key",
2120 native_shared_libs: ["mylib_common"],
2121 }
2122
2123 apex_key {
2124 name: "myapex.key",
2125 public_key: "testkey.avbpubkey",
2126 private_key: "testkey.pem",
2127 }
2128
2129 cc_library {
2130 name: "mylib_common",
2131 srcs: ["mylib.cpp"],
2132 system_shared_libs: [],
2133 stl: "none",
2134 }
2135 `)
2136
Sundong Ahnabb64432019-10-22 13:58:29 +09002137 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002138 apexRule := module.Rule("apexRule")
2139 copyCmds := apexRule.Args["copy_commands"]
2140
2141 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2142 t.Log("Apex was a test apex!")
2143 t.Fail()
2144 }
2145 // Ensure that main rule creates an output
2146 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2147
2148 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002149 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002150
2151 // Ensure that both direct and indirect deps are copied into apex
2152 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2153
Colin Cross7113d202019-11-20 16:39:12 -08002154 // Ensure that the platform variant ends with _shared
2155 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002156
2157 if !android.InAnyApex("mylib_common") {
2158 t.Log("Found mylib_common not in any apex!")
2159 t.Fail()
2160 }
2161}
2162
2163func TestTestApex(t *testing.T) {
2164 if android.InAnyApex("mylib_common_test") {
2165 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!")
2166 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002167 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002168 apex_test {
2169 name: "myapex",
2170 key: "myapex.key",
2171 native_shared_libs: ["mylib_common_test"],
2172 }
2173
2174 apex_key {
2175 name: "myapex.key",
2176 public_key: "testkey.avbpubkey",
2177 private_key: "testkey.pem",
2178 }
2179
2180 cc_library {
2181 name: "mylib_common_test",
2182 srcs: ["mylib.cpp"],
2183 system_shared_libs: [],
2184 stl: "none",
2185 }
2186 `)
2187
Sundong Ahnabb64432019-10-22 13:58:29 +09002188 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002189 apexRule := module.Rule("apexRule")
2190 copyCmds := apexRule.Args["copy_commands"]
2191
2192 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2193 t.Log("Apex was not a test apex!")
2194 t.Fail()
2195 }
2196 // Ensure that main rule creates an output
2197 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2198
2199 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002200 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002201
2202 // Ensure that both direct and indirect deps are copied into apex
2203 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2204
Colin Cross7113d202019-11-20 16:39:12 -08002205 // Ensure that the platform variant ends with _shared
2206 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002207
2208 if android.InAnyApex("mylib_common_test") {
2209 t.Log("Found mylib_common_test in some apex!")
2210 t.Fail()
2211 }
2212}
2213
Alex Light9670d332019-01-29 18:07:33 -08002214func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002215 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002216 apex {
2217 name: "myapex",
2218 key: "myapex.key",
2219 multilib: {
2220 first: {
2221 native_shared_libs: ["mylib_common"],
2222 }
2223 },
2224 target: {
2225 android: {
2226 multilib: {
2227 first: {
2228 native_shared_libs: ["mylib"],
2229 }
2230 }
2231 },
2232 host: {
2233 multilib: {
2234 first: {
2235 native_shared_libs: ["mylib2"],
2236 }
2237 }
2238 }
2239 }
2240 }
2241
2242 apex_key {
2243 name: "myapex.key",
2244 public_key: "testkey.avbpubkey",
2245 private_key: "testkey.pem",
2246 }
2247
2248 cc_library {
2249 name: "mylib",
2250 srcs: ["mylib.cpp"],
2251 system_shared_libs: [],
2252 stl: "none",
2253 }
2254
2255 cc_library {
2256 name: "mylib_common",
2257 srcs: ["mylib.cpp"],
2258 system_shared_libs: [],
2259 stl: "none",
2260 compile_multilib: "first",
2261 }
2262
2263 cc_library {
2264 name: "mylib2",
2265 srcs: ["mylib.cpp"],
2266 system_shared_libs: [],
2267 stl: "none",
2268 compile_multilib: "first",
2269 }
2270 `)
2271
Sundong Ahnabb64432019-10-22 13:58:29 +09002272 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002273 copyCmds := apexRule.Args["copy_commands"]
2274
2275 // Ensure that main rule creates an output
2276 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2277
2278 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002279 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2280 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2281 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002282
2283 // Ensure that both direct and indirect deps are copied into apex
2284 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2285 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2286 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2287
Colin Cross7113d202019-11-20 16:39:12 -08002288 // Ensure that the platform variant ends with _shared
2289 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2290 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2291 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002292}
Jiyong Park04480cf2019-02-06 00:16:29 +09002293
2294func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002295 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002296 apex {
2297 name: "myapex",
2298 key: "myapex.key",
2299 binaries: ["myscript"],
2300 }
2301
2302 apex_key {
2303 name: "myapex.key",
2304 public_key: "testkey.avbpubkey",
2305 private_key: "testkey.pem",
2306 }
2307
2308 sh_binary {
2309 name: "myscript",
2310 src: "mylib.cpp",
2311 filename: "myscript.sh",
2312 sub_dir: "script",
2313 }
2314 `)
2315
Sundong Ahnabb64432019-10-22 13:58:29 +09002316 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002317 copyCmds := apexRule.Args["copy_commands"]
2318
2319 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2320}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002321
Jooyung Han91df2082019-11-20 01:49:42 +09002322func TestApexInVariousPartition(t *testing.T) {
2323 testcases := []struct {
2324 propName, parition, flattenedPartition string
2325 }{
2326 {"", "system", "system_ext"},
2327 {"product_specific: true", "product", "product"},
2328 {"soc_specific: true", "vendor", "vendor"},
2329 {"proprietary: true", "vendor", "vendor"},
2330 {"vendor: true", "vendor", "vendor"},
2331 {"system_ext_specific: true", "system_ext", "system_ext"},
2332 }
2333 for _, tc := range testcases {
2334 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2335 ctx, _ := testApex(t, `
2336 apex {
2337 name: "myapex",
2338 key: "myapex.key",
2339 `+tc.propName+`
2340 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002341
Jooyung Han91df2082019-11-20 01:49:42 +09002342 apex_key {
2343 name: "myapex.key",
2344 public_key: "testkey.avbpubkey",
2345 private_key: "testkey.pem",
2346 }
2347 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002348
Jooyung Han91df2082019-11-20 01:49:42 +09002349 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2350 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2351 actual := apex.installDir.String()
2352 if actual != expected {
2353 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2354 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002355
Jooyung Han91df2082019-11-20 01:49:42 +09002356 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2357 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2358 actual = flattened.installDir.String()
2359 if actual != expected {
2360 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2361 }
2362 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002363 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002364}
Jiyong Park67882562019-03-21 01:11:21 +09002365
Jooyung Han54aca7b2019-11-20 02:26:02 +09002366func TestFileContexts(t *testing.T) {
2367 ctx, _ := testApex(t, `
2368 apex {
2369 name: "myapex",
2370 key: "myapex.key",
2371 }
2372
2373 apex_key {
2374 name: "myapex.key",
2375 public_key: "testkey.avbpubkey",
2376 private_key: "testkey.pem",
2377 }
2378 `)
2379 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2380 apexRule := module.Rule("apexRule")
2381 actual := apexRule.Args["file_contexts"]
2382 expected := "system/sepolicy/apex/myapex-file_contexts"
2383 if actual != expected {
2384 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2385 }
2386
2387 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2388 apex {
2389 name: "myapex",
2390 key: "myapex.key",
2391 file_contexts: "my_own_file_contexts",
2392 }
2393
2394 apex_key {
2395 name: "myapex.key",
2396 public_key: "testkey.avbpubkey",
2397 private_key: "testkey.pem",
2398 }
2399 `, withFiles(map[string][]byte{
2400 "my_own_file_contexts": nil,
2401 }))
2402
2403 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2404 apex {
2405 name: "myapex",
2406 key: "myapex.key",
2407 product_specific: true,
2408 file_contexts: "product_specific_file_contexts",
2409 }
2410
2411 apex_key {
2412 name: "myapex.key",
2413 public_key: "testkey.avbpubkey",
2414 private_key: "testkey.pem",
2415 }
2416 `)
2417
2418 ctx, _ = testApex(t, `
2419 apex {
2420 name: "myapex",
2421 key: "myapex.key",
2422 product_specific: true,
2423 file_contexts: "product_specific_file_contexts",
2424 }
2425
2426 apex_key {
2427 name: "myapex.key",
2428 public_key: "testkey.avbpubkey",
2429 private_key: "testkey.pem",
2430 }
2431 `, withFiles(map[string][]byte{
2432 "product_specific_file_contexts": nil,
2433 }))
2434 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2435 apexRule = module.Rule("apexRule")
2436 actual = apexRule.Args["file_contexts"]
2437 expected = "product_specific_file_contexts"
2438 if actual != expected {
2439 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2440 }
2441
2442 ctx, _ = testApex(t, `
2443 apex {
2444 name: "myapex",
2445 key: "myapex.key",
2446 product_specific: true,
2447 file_contexts: ":my-file-contexts",
2448 }
2449
2450 apex_key {
2451 name: "myapex.key",
2452 public_key: "testkey.avbpubkey",
2453 private_key: "testkey.pem",
2454 }
2455
2456 filegroup {
2457 name: "my-file-contexts",
2458 srcs: ["product_specific_file_contexts"],
2459 }
2460 `, withFiles(map[string][]byte{
2461 "product_specific_file_contexts": nil,
2462 }))
2463 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2464 apexRule = module.Rule("apexRule")
2465 actual = apexRule.Args["file_contexts"]
2466 expected = "product_specific_file_contexts"
2467 if actual != expected {
2468 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2469 }
2470}
2471
Jiyong Park67882562019-03-21 01:11:21 +09002472func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002473 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002474 apex_key {
2475 name: "myapex.key",
2476 public_key: ":my.avbpubkey",
2477 private_key: ":my.pem",
2478 product_specific: true,
2479 }
2480
2481 filegroup {
2482 name: "my.avbpubkey",
2483 srcs: ["testkey2.avbpubkey"],
2484 }
2485
2486 filegroup {
2487 name: "my.pem",
2488 srcs: ["testkey2.pem"],
2489 }
2490 `)
2491
2492 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2493 expected_pubkey := "testkey2.avbpubkey"
2494 actual_pubkey := apex_key.public_key_file.String()
2495 if actual_pubkey != expected_pubkey {
2496 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2497 }
2498 expected_privkey := "testkey2.pem"
2499 actual_privkey := apex_key.private_key_file.String()
2500 if actual_privkey != expected_privkey {
2501 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2502 }
2503}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002504
2505func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002506 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002507 prebuilt_apex {
2508 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002509 arch: {
2510 arm64: {
2511 src: "myapex-arm64.apex",
2512 },
2513 arm: {
2514 src: "myapex-arm.apex",
2515 },
2516 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002517 }
2518 `)
2519
2520 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2521
Jiyong Parkc95714e2019-03-29 14:23:10 +09002522 expectedInput := "myapex-arm64.apex"
2523 if prebuilt.inputApex.String() != expectedInput {
2524 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2525 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002526}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002527
2528func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002529 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002530 prebuilt_apex {
2531 name: "myapex",
2532 src: "myapex-arm.apex",
2533 filename: "notmyapex.apex",
2534 }
2535 `)
2536
2537 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2538
2539 expected := "notmyapex.apex"
2540 if p.installFilename != expected {
2541 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2542 }
2543}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002544
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002545func TestPrebuiltOverrides(t *testing.T) {
2546 ctx, config := testApex(t, `
2547 prebuilt_apex {
2548 name: "myapex.prebuilt",
2549 src: "myapex-arm.apex",
2550 overrides: [
2551 "myapex",
2552 ],
2553 }
2554 `)
2555
2556 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2557
2558 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002559 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002560 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002561 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002562 }
2563}
2564
Roland Levillain630846d2019-06-26 12:48:34 +01002565func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002566 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002567 apex_test {
2568 name: "myapex",
2569 key: "myapex.key",
2570 tests: [
2571 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002572 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002573 ],
2574 }
2575
2576 apex_key {
2577 name: "myapex.key",
2578 public_key: "testkey.avbpubkey",
2579 private_key: "testkey.pem",
2580 }
2581
2582 cc_test {
2583 name: "mytest",
2584 gtest: false,
2585 srcs: ["mytest.cpp"],
2586 relative_install_path: "test",
2587 system_shared_libs: [],
2588 static_executable: true,
2589 stl: "none",
2590 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002591
2592 cc_test {
2593 name: "mytests",
2594 gtest: false,
2595 srcs: [
2596 "mytest1.cpp",
2597 "mytest2.cpp",
2598 "mytest3.cpp",
2599 ],
2600 test_per_src: true,
2601 relative_install_path: "test",
2602 system_shared_libs: [],
2603 static_executable: true,
2604 stl: "none",
2605 }
Roland Levillain630846d2019-06-26 12:48:34 +01002606 `)
2607
Sundong Ahnabb64432019-10-22 13:58:29 +09002608 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002609 copyCmds := apexRule.Args["copy_commands"]
2610
2611 // Ensure that test dep is copied into apex.
2612 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002613
2614 // Ensure that test deps built with `test_per_src` are copied into apex.
2615 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2616 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2617 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002618
2619 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002620 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002621 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2622 name := apexBundle.BaseModuleName()
2623 prefix := "TARGET_"
2624 var builder strings.Builder
2625 data.Custom(&builder, name, prefix, "", data)
2626 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002627 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2628 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2629 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2630 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002631 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002632 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002633 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002634}
2635
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002636func TestInstallExtraFlattenedApexes(t *testing.T) {
2637 ctx, config := testApex(t, `
2638 apex {
2639 name: "myapex",
2640 key: "myapex.key",
2641 }
2642 apex_key {
2643 name: "myapex.key",
2644 public_key: "testkey.avbpubkey",
2645 private_key: "testkey.pem",
2646 }
2647 `, func(fs map[string][]byte, config android.Config) {
2648 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2649 })
2650 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2651 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2652 mk := android.AndroidMkDataForTest(t, config, "", ab)
2653 var builder strings.Builder
2654 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2655 androidMk := builder.String()
2656 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2657}
2658
Jooyung Han5c998b92019-06-27 11:30:33 +09002659func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002660 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002661 apex {
2662 name: "myapex",
2663 key: "myapex.key",
2664 native_shared_libs: ["mylib"],
2665 uses: ["commonapex"],
2666 }
2667
2668 apex {
2669 name: "commonapex",
2670 key: "myapex.key",
2671 native_shared_libs: ["libcommon"],
2672 provide_cpp_shared_libs: true,
2673 }
2674
2675 apex_key {
2676 name: "myapex.key",
2677 public_key: "testkey.avbpubkey",
2678 private_key: "testkey.pem",
2679 }
2680
2681 cc_library {
2682 name: "mylib",
2683 srcs: ["mylib.cpp"],
2684 shared_libs: ["libcommon"],
2685 system_shared_libs: [],
2686 stl: "none",
2687 }
2688
2689 cc_library {
2690 name: "libcommon",
2691 srcs: ["mylib_common.cpp"],
2692 system_shared_libs: [],
2693 stl: "none",
2694 }
2695 `)
2696
Sundong Ahnabb64432019-10-22 13:58:29 +09002697 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002698 apexRule1 := module1.Rule("apexRule")
2699 copyCmds1 := apexRule1.Args["copy_commands"]
2700
Sundong Ahnabb64432019-10-22 13:58:29 +09002701 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002702 apexRule2 := module2.Rule("apexRule")
2703 copyCmds2 := apexRule2.Args["copy_commands"]
2704
Colin Cross7113d202019-11-20 16:39:12 -08002705 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2706 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002707 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2708 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2709 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2710}
2711
2712func TestApexUsesFailsIfNotProvided(t *testing.T) {
2713 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2714 apex {
2715 name: "myapex",
2716 key: "myapex.key",
2717 uses: ["commonapex"],
2718 }
2719
2720 apex {
2721 name: "commonapex",
2722 key: "myapex.key",
2723 }
2724
2725 apex_key {
2726 name: "myapex.key",
2727 public_key: "testkey.avbpubkey",
2728 private_key: "testkey.pem",
2729 }
2730 `)
2731 testApexError(t, `uses: "commonapex" is not a provider`, `
2732 apex {
2733 name: "myapex",
2734 key: "myapex.key",
2735 uses: ["commonapex"],
2736 }
2737
2738 cc_library {
2739 name: "commonapex",
2740 system_shared_libs: [],
2741 stl: "none",
2742 }
2743
2744 apex_key {
2745 name: "myapex.key",
2746 public_key: "testkey.avbpubkey",
2747 private_key: "testkey.pem",
2748 }
2749 `)
2750}
2751
2752func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2753 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2754 apex {
2755 name: "myapex",
2756 key: "myapex.key",
2757 use_vendor: true,
2758 uses: ["commonapex"],
2759 }
2760
2761 apex {
2762 name: "commonapex",
2763 key: "myapex.key",
2764 provide_cpp_shared_libs: true,
2765 }
2766
2767 apex_key {
2768 name: "myapex.key",
2769 public_key: "testkey.avbpubkey",
2770 private_key: "testkey.pem",
2771 }
Jooyung Handc782442019-11-01 03:14:38 +09002772 `, func(fs map[string][]byte, config android.Config) {
2773 setUseVendorWhitelistForTest(config, []string{"myapex"})
2774 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002775}
2776
Jooyung Hand48f3c32019-08-23 11:18:57 +09002777func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2778 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2779 apex {
2780 name: "myapex",
2781 key: "myapex.key",
2782 native_shared_libs: ["libfoo"],
2783 }
2784
2785 apex_key {
2786 name: "myapex.key",
2787 public_key: "testkey.avbpubkey",
2788 private_key: "testkey.pem",
2789 }
2790
2791 cc_library {
2792 name: "libfoo",
2793 stl: "none",
2794 system_shared_libs: [],
2795 enabled: false,
2796 }
2797 `)
2798 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2799 apex {
2800 name: "myapex",
2801 key: "myapex.key",
2802 java_libs: ["myjar"],
2803 }
2804
2805 apex_key {
2806 name: "myapex.key",
2807 public_key: "testkey.avbpubkey",
2808 private_key: "testkey.pem",
2809 }
2810
2811 java_library {
2812 name: "myjar",
2813 srcs: ["foo/bar/MyClass.java"],
2814 sdk_version: "none",
2815 system_modules: "none",
2816 compile_dex: true,
2817 enabled: false,
2818 }
2819 `)
2820}
2821
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002822func TestApexWithApps(t *testing.T) {
2823 ctx, _ := testApex(t, `
2824 apex {
2825 name: "myapex",
2826 key: "myapex.key",
2827 apps: [
2828 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002829 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002830 ],
2831 }
2832
2833 apex_key {
2834 name: "myapex.key",
2835 public_key: "testkey.avbpubkey",
2836 private_key: "testkey.pem",
2837 }
2838
2839 android_app {
2840 name: "AppFoo",
2841 srcs: ["foo/bar/MyClass.java"],
2842 sdk_version: "none",
2843 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002844 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002845 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002846
2847 android_app {
2848 name: "AppFooPriv",
2849 srcs: ["foo/bar/MyClass.java"],
2850 sdk_version: "none",
2851 system_modules: "none",
2852 privileged: true,
2853 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002854
2855 cc_library_shared {
2856 name: "libjni",
2857 srcs: ["mylib.cpp"],
2858 stl: "none",
2859 system_shared_libs: [],
2860 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002861 `)
2862
Sundong Ahnabb64432019-10-22 13:58:29 +09002863 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002864 apexRule := module.Rule("apexRule")
2865 copyCmds := apexRule.Args["copy_commands"]
2866
2867 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002868 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002869
2870 // JNI libraries are embedded inside APK
2871 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002872 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002873 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2874 // ... uncompressed
2875 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2876 t.Errorf("jni lib is not uncompressed for AppFoo")
2877 }
2878 // ... and not directly inside the APEX
2879 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002880}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002881
Dario Frenicde2a032019-10-27 00:29:22 +01002882func TestApexWithAppImports(t *testing.T) {
2883 ctx, _ := testApex(t, `
2884 apex {
2885 name: "myapex",
2886 key: "myapex.key",
2887 apps: [
2888 "AppFooPrebuilt",
2889 "AppFooPrivPrebuilt",
2890 ],
2891 }
2892
2893 apex_key {
2894 name: "myapex.key",
2895 public_key: "testkey.avbpubkey",
2896 private_key: "testkey.pem",
2897 }
2898
2899 android_app_import {
2900 name: "AppFooPrebuilt",
2901 apk: "PrebuiltAppFoo.apk",
2902 presigned: true,
2903 dex_preopt: {
2904 enabled: false,
2905 },
2906 }
2907
2908 android_app_import {
2909 name: "AppFooPrivPrebuilt",
2910 apk: "PrebuiltAppFooPriv.apk",
2911 privileged: true,
2912 presigned: true,
2913 dex_preopt: {
2914 enabled: false,
2915 },
2916 }
2917 `)
2918
Sundong Ahnabb64432019-10-22 13:58:29 +09002919 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002920 apexRule := module.Rule("apexRule")
2921 copyCmds := apexRule.Args["copy_commands"]
2922
2923 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2924 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002925}
2926
Dario Freni6f3937c2019-12-20 22:58:03 +00002927func TestApexWithTestHelperApp(t *testing.T) {
2928 ctx, _ := testApex(t, `
2929 apex {
2930 name: "myapex",
2931 key: "myapex.key",
2932 apps: [
2933 "TesterHelpAppFoo",
2934 ],
2935 }
2936
2937 apex_key {
2938 name: "myapex.key",
2939 public_key: "testkey.avbpubkey",
2940 private_key: "testkey.pem",
2941 }
2942
2943 android_test_helper_app {
2944 name: "TesterHelpAppFoo",
2945 srcs: ["foo/bar/MyClass.java"],
2946 }
2947
2948 `)
2949
2950 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2951 apexRule := module.Rule("apexRule")
2952 copyCmds := apexRule.Args["copy_commands"]
2953
2954 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2955}
2956
Jooyung Han18020ea2019-11-13 10:50:48 +09002957func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2958 // libfoo's apex_available comes from cc_defaults
2959 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2960 apex {
2961 name: "myapex",
2962 key: "myapex.key",
2963 native_shared_libs: ["libfoo"],
2964 }
2965
2966 apex_key {
2967 name: "myapex.key",
2968 public_key: "testkey.avbpubkey",
2969 private_key: "testkey.pem",
2970 }
2971
2972 apex {
2973 name: "otherapex",
2974 key: "myapex.key",
2975 native_shared_libs: ["libfoo"],
2976 }
2977
2978 cc_defaults {
2979 name: "libfoo-defaults",
2980 apex_available: ["otherapex"],
2981 }
2982
2983 cc_library {
2984 name: "libfoo",
2985 defaults: ["libfoo-defaults"],
2986 stl: "none",
2987 system_shared_libs: [],
2988 }`)
2989}
2990
Jiyong Park127b40b2019-09-30 16:04:35 +09002991func TestApexAvailable(t *testing.T) {
2992 // libfoo is not available to myapex, but only to otherapex
2993 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2994 apex {
2995 name: "myapex",
2996 key: "myapex.key",
2997 native_shared_libs: ["libfoo"],
2998 }
2999
3000 apex_key {
3001 name: "myapex.key",
3002 public_key: "testkey.avbpubkey",
3003 private_key: "testkey.pem",
3004 }
3005
3006 apex {
3007 name: "otherapex",
3008 key: "otherapex.key",
3009 native_shared_libs: ["libfoo"],
3010 }
3011
3012 apex_key {
3013 name: "otherapex.key",
3014 public_key: "testkey.avbpubkey",
3015 private_key: "testkey.pem",
3016 }
3017
3018 cc_library {
3019 name: "libfoo",
3020 stl: "none",
3021 system_shared_libs: [],
3022 apex_available: ["otherapex"],
3023 }`)
3024
3025 // libbar is an indirect dep
3026 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3027 apex {
3028 name: "myapex",
3029 key: "myapex.key",
3030 native_shared_libs: ["libfoo"],
3031 }
3032
3033 apex_key {
3034 name: "myapex.key",
3035 public_key: "testkey.avbpubkey",
3036 private_key: "testkey.pem",
3037 }
3038
3039 apex {
3040 name: "otherapex",
3041 key: "otherapex.key",
3042 native_shared_libs: ["libfoo"],
3043 }
3044
3045 apex_key {
3046 name: "otherapex.key",
3047 public_key: "testkey.avbpubkey",
3048 private_key: "testkey.pem",
3049 }
3050
3051 cc_library {
3052 name: "libfoo",
3053 stl: "none",
3054 shared_libs: ["libbar"],
3055 system_shared_libs: [],
3056 apex_available: ["myapex", "otherapex"],
3057 }
3058
3059 cc_library {
3060 name: "libbar",
3061 stl: "none",
3062 system_shared_libs: [],
3063 apex_available: ["otherapex"],
3064 }`)
3065
3066 testApexError(t, "\"otherapex\" is not a valid module name", `
3067 apex {
3068 name: "myapex",
3069 key: "myapex.key",
3070 native_shared_libs: ["libfoo"],
3071 }
3072
3073 apex_key {
3074 name: "myapex.key",
3075 public_key: "testkey.avbpubkey",
3076 private_key: "testkey.pem",
3077 }
3078
3079 cc_library {
3080 name: "libfoo",
3081 stl: "none",
3082 system_shared_libs: [],
3083 apex_available: ["otherapex"],
3084 }`)
3085
3086 ctx, _ := testApex(t, `
3087 apex {
3088 name: "myapex",
3089 key: "myapex.key",
3090 native_shared_libs: ["libfoo", "libbar"],
3091 }
3092
3093 apex_key {
3094 name: "myapex.key",
3095 public_key: "testkey.avbpubkey",
3096 private_key: "testkey.pem",
3097 }
3098
3099 cc_library {
3100 name: "libfoo",
3101 stl: "none",
3102 system_shared_libs: [],
3103 apex_available: ["myapex"],
3104 }
3105
3106 cc_library {
3107 name: "libbar",
3108 stl: "none",
3109 system_shared_libs: [],
3110 apex_available: ["//apex_available:anyapex"],
3111 }`)
3112
3113 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003114 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3115 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3116 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3117 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003118
3119 ctx, _ = testApex(t, `
3120 apex {
3121 name: "myapex",
3122 key: "myapex.key",
3123 }
3124
3125 apex_key {
3126 name: "myapex.key",
3127 public_key: "testkey.avbpubkey",
3128 private_key: "testkey.pem",
3129 }
3130
3131 cc_library {
3132 name: "libfoo",
3133 stl: "none",
3134 system_shared_libs: [],
3135 apex_available: ["//apex_available:platform"],
3136 }`)
3137
3138 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003139 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3140 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003141
3142 ctx, _ = testApex(t, `
3143 apex {
3144 name: "myapex",
3145 key: "myapex.key",
3146 native_shared_libs: ["libfoo"],
3147 }
3148
3149 apex_key {
3150 name: "myapex.key",
3151 public_key: "testkey.avbpubkey",
3152 private_key: "testkey.pem",
3153 }
3154
3155 cc_library {
3156 name: "libfoo",
3157 stl: "none",
3158 system_shared_libs: [],
3159 apex_available: ["myapex"],
3160 static: {
3161 apex_available: ["//apex_available:platform"],
3162 },
3163 }`)
3164
3165 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003166 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3167 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003168 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003169 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3170 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003171}
3172
Jiyong Park5d790c32019-11-15 18:40:32 +09003173func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003174 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003175 apex {
3176 name: "myapex",
3177 key: "myapex.key",
3178 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003179 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003180 }
3181
3182 override_apex {
3183 name: "override_myapex",
3184 base: "myapex",
3185 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003186 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003187 }
3188
3189 apex_key {
3190 name: "myapex.key",
3191 public_key: "testkey.avbpubkey",
3192 private_key: "testkey.pem",
3193 }
3194
3195 android_app {
3196 name: "app",
3197 srcs: ["foo/bar/MyClass.java"],
3198 package_name: "foo",
3199 sdk_version: "none",
3200 system_modules: "none",
3201 }
3202
3203 override_android_app {
3204 name: "override_app",
3205 base: "app",
3206 package_name: "bar",
3207 }
3208 `)
3209
Jiyong Park317645e2019-12-05 13:20:58 +09003210 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3211 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3212 if originalVariant.GetOverriddenBy() != "" {
3213 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3214 }
3215 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3216 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3217 }
3218
Jiyong Park5d790c32019-11-15 18:40:32 +09003219 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3220 apexRule := module.Rule("apexRule")
3221 copyCmds := apexRule.Args["copy_commands"]
3222
3223 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3224 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003225
3226 apexBundle := module.Module().(*apexBundle)
3227 name := apexBundle.Name()
3228 if name != "override_myapex" {
3229 t.Errorf("name should be \"override_myapex\", but was %q", name)
3230 }
3231
3232 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3233 var builder strings.Builder
3234 data.Custom(&builder, name, "TARGET_", "", data)
3235 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003236 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003237 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3238 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003239 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003240 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003241 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003242 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3243 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003244}
3245
Jooyung Han214bf372019-11-12 13:03:50 +09003246func TestLegacyAndroid10Support(t *testing.T) {
3247 ctx, _ := testApex(t, `
3248 apex {
3249 name: "myapex",
3250 key: "myapex.key",
3251 legacy_android10_support: true,
3252 }
3253
3254 apex_key {
3255 name: "myapex.key",
3256 public_key: "testkey.avbpubkey",
3257 private_key: "testkey.pem",
3258 }
3259 `)
3260
3261 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3262 args := module.Rule("apexRule").Args
3263 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3264}
3265
Jooyung Han58f26ab2019-12-18 15:34:32 +09003266func TestJavaSDKLibrary(t *testing.T) {
3267 ctx, _ := testApex(t, `
3268 apex {
3269 name: "myapex",
3270 key: "myapex.key",
3271 java_libs: ["foo"],
3272 }
3273
3274 apex_key {
3275 name: "myapex.key",
3276 public_key: "testkey.avbpubkey",
3277 private_key: "testkey.pem",
3278 }
3279
3280 java_sdk_library {
3281 name: "foo",
3282 srcs: ["a.java"],
3283 api_packages: ["foo"],
3284 }
3285 `, withFiles(map[string][]byte{
3286 "api/current.txt": nil,
3287 "api/removed.txt": nil,
3288 "api/system-current.txt": nil,
3289 "api/system-removed.txt": nil,
3290 "api/test-current.txt": nil,
3291 "api/test-removed.txt": nil,
3292 }))
3293
3294 // java_sdk_library installs both impl jar and permission XML
3295 ensureExactContents(t, ctx, "myapex", []string{
3296 "javalib/foo.jar",
3297 "etc/permissions/foo.xml",
3298 })
3299 // Permission XML should point to the activated path of impl jar of java_sdk_library
3300 genXMLCommand := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml").RuleParams.Command
3301 ensureContains(t, genXMLCommand, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
3302}
3303
Jiyong Park479321d2019-12-16 11:47:12 +09003304func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3305 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3306 apex {
3307 name: "myapex",
3308 key: "myapex.key",
3309 java_libs: ["myjar"],
3310 }
3311
3312 apex_key {
3313 name: "myapex.key",
3314 public_key: "testkey.avbpubkey",
3315 private_key: "testkey.pem",
3316 }
3317
3318 java_library {
3319 name: "myjar",
3320 srcs: ["foo/bar/MyClass.java"],
3321 sdk_version: "none",
3322 system_modules: "none",
3323 }
3324 `)
3325}
3326
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003327func TestMain(m *testing.M) {
3328 run := func() int {
3329 setUp()
3330 defer tearDown()
3331
3332 return m.Run()
3333 }
3334
3335 os.Exit(run())
3336}