blob: add07a6bac8b80907edab66e18986543a14d51bb [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 },
Jooyung Han5a80d9f2019-12-23 15:38:34 +0900375 java_libs: ["myjar"],
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 Park25fc6a92018-11-18 18:02:45 +0900450 `)
451
Sundong Ahnabb64432019-10-22 13:58:29 +0900452 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900453
454 optFlags := apexRule.Args["opt_flags"]
455 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700456 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900457 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900458
Jiyong Park25fc6a92018-11-18 18:02:45 +0900459 copyCmds := apexRule.Args["copy_commands"]
460
461 // Ensure that main rule creates an output
462 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
463
464 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800465 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900466 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467
468 // Ensure that apex variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800469 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900470 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471
472 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800473 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
474 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 ensureContains(t, copyCmds, "image.apex/javalib/myjar.jar")
476 // .. but not for java libs
477 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800478
Colin Cross7113d202019-11-20 16:39:12 -0800479 // Ensure that the platform variant ends with _shared or _common
480 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
481 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
483 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Alex Light3d673592019-01-18 14:37:31 -0800484
485 // Ensure that all symlinks are present.
486 found_foo_link_64 := false
487 found_foo := false
488 for _, cmd := range strings.Split(copyCmds, " && ") {
489 if strings.HasPrefix(cmd, "ln -s foo64") {
490 if strings.HasSuffix(cmd, "bin/foo") {
491 found_foo = true
492 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
493 found_foo_link_64 = true
494 }
495 }
496 }
497 good := found_foo && found_foo_link_64
498 if !good {
499 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
500 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900501
Sundong Ahnabb64432019-10-22 13:58:29 +0900502 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700503 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700504 if len(noticeInputs) != 2 {
505 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900506 }
507 ensureListContains(t, noticeInputs, "NOTICE")
508 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800509}
510
Jooyung Hanf21c7972019-12-16 22:32:06 +0900511func TestDefaults(t *testing.T) {
512 ctx, _ := testApex(t, `
513 apex_defaults {
514 name: "myapex-defaults",
515 key: "myapex.key",
516 prebuilts: ["myetc"],
517 native_shared_libs: ["mylib"],
518 java_libs: ["myjar"],
519 apps: ["AppFoo"],
520 }
521
522 prebuilt_etc {
523 name: "myetc",
524 src: "myprebuilt",
525 }
526
527 apex {
528 name: "myapex",
529 defaults: ["myapex-defaults"],
530 }
531
532 apex_key {
533 name: "myapex.key",
534 public_key: "testkey.avbpubkey",
535 private_key: "testkey.pem",
536 }
537
538 cc_library {
539 name: "mylib",
540 system_shared_libs: [],
541 stl: "none",
542 }
543
544 java_library {
545 name: "myjar",
546 srcs: ["foo/bar/MyClass.java"],
547 sdk_version: "none",
548 system_modules: "none",
549 compile_dex: true,
550 }
551
552 android_app {
553 name: "AppFoo",
554 srcs: ["foo/bar/MyClass.java"],
555 sdk_version: "none",
556 system_modules: "none",
557 }
558 `)
559 ensureExactContents(t, ctx, "myapex", []string{
560 "etc/myetc",
561 "javalib/myjar.jar",
562 "lib64/mylib.so",
563 "app/AppFoo/AppFoo.apk",
564 })
565}
566
Jooyung Han01a3ee22019-11-02 02:52:25 +0900567func TestApexManifest(t *testing.T) {
568 ctx, _ := testApex(t, `
569 apex {
570 name: "myapex",
571 key: "myapex.key",
572 }
573
574 apex_key {
575 name: "myapex.key",
576 public_key: "testkey.avbpubkey",
577 private_key: "testkey.pem",
578 }
579 `)
580
581 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900582 args := module.Rule("apexRule").Args
583 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
584 t.Error("manifest should be apex_manifest.pb, but " + manifest)
585 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900586}
587
Alex Light5098a612018-11-29 17:12:15 -0800588func TestBasicZipApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700589 ctx, _ := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800590 apex {
591 name: "myapex",
592 key: "myapex.key",
593 payload_type: "zip",
594 native_shared_libs: ["mylib"],
595 }
596
597 apex_key {
598 name: "myapex.key",
599 public_key: "testkey.avbpubkey",
600 private_key: "testkey.pem",
601 }
602
603 cc_library {
604 name: "mylib",
605 srcs: ["mylib.cpp"],
606 shared_libs: ["mylib2"],
607 system_shared_libs: [],
608 stl: "none",
609 }
610
611 cc_library {
612 name: "mylib2",
613 srcs: ["mylib.cpp"],
614 system_shared_libs: [],
615 stl: "none",
616 }
617 `)
618
Sundong Ahnabb64432019-10-22 13:58:29 +0900619 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800620 copyCmds := zipApexRule.Args["copy_commands"]
621
622 // Ensure that main rule creates an output
623 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
624
625 // Ensure that APEX variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -0800626 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800627
628 // Ensure that APEX variant is created for the indirect dep
Colin Cross7113d202019-11-20 16:39:12 -0800629 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800630
631 // Ensure that both direct and indirect deps are copied into apex
632 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
633 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634}
635
636func TestApexWithStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700637 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900638 apex {
639 name: "myapex",
640 key: "myapex.key",
641 native_shared_libs: ["mylib", "mylib3"],
642 }
643
644 apex_key {
645 name: "myapex.key",
646 public_key: "testkey.avbpubkey",
647 private_key: "testkey.pem",
648 }
649
650 cc_library {
651 name: "mylib",
652 srcs: ["mylib.cpp"],
653 shared_libs: ["mylib2", "mylib3"],
654 system_shared_libs: [],
655 stl: "none",
656 }
657
658 cc_library {
659 name: "mylib2",
660 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900661 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900662 system_shared_libs: [],
663 stl: "none",
664 stubs: {
665 versions: ["1", "2", "3"],
666 },
667 }
668
669 cc_library {
670 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900671 srcs: ["mylib.cpp"],
672 shared_libs: ["mylib4"],
673 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900674 stl: "none",
675 stubs: {
676 versions: ["10", "11", "12"],
677 },
678 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900679
680 cc_library {
681 name: "mylib4",
682 srcs: ["mylib.cpp"],
683 system_shared_libs: [],
684 stl: "none",
685 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900686 `)
687
Sundong Ahnabb64432019-10-22 13:58:29 +0900688 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900689 copyCmds := apexRule.Args["copy_commands"]
690
691 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800692 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900693
694 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800695 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900696
697 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800698 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699
Colin Cross7113d202019-11-20 16:39:12 -0800700 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900701
702 // Ensure that mylib is linking with the latest version of stubs for mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800703 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900704 // ... and not linking to the non-stub (impl) variant of mylib2
Colin Cross7113d202019-11-20 16:39:12 -0800705 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900706
707 // 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 -0800708 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900709 // .. and not linking to the stubs variant of mylib3
Colin Cross7113d202019-11-20 16:39:12 -0800710 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900711
712 // Ensure that stubs libs are built without -include flags
Colin Cross7113d202019-11-20 16:39:12 -0800713 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900714 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900715
716 // Ensure that genstub is invoked with --apex
Colin Cross7113d202019-11-20 16:39:12 -0800717 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900718
719 ensureExactContents(t, ctx, "myapex", []string{
720 "lib64/mylib.so",
721 "lib64/mylib3.so",
722 "lib64/mylib4.so",
723 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900724}
725
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900726func TestApexWithExplicitStubsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700727 ctx, _ := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900728 apex {
729 name: "myapex",
730 key: "myapex.key",
731 native_shared_libs: ["mylib"],
732 }
733
734 apex_key {
735 name: "myapex.key",
736 public_key: "testkey.avbpubkey",
737 private_key: "testkey.pem",
738 }
739
740 cc_library {
741 name: "mylib",
742 srcs: ["mylib.cpp"],
743 shared_libs: ["libfoo#10"],
744 system_shared_libs: [],
745 stl: "none",
746 }
747
748 cc_library {
749 name: "libfoo",
750 srcs: ["mylib.cpp"],
751 shared_libs: ["libbar"],
752 system_shared_libs: [],
753 stl: "none",
754 stubs: {
755 versions: ["10", "20", "30"],
756 },
757 }
758
759 cc_library {
760 name: "libbar",
761 srcs: ["mylib.cpp"],
762 system_shared_libs: [],
763 stl: "none",
764 }
765
766 `)
767
Sundong Ahnabb64432019-10-22 13:58:29 +0900768 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900769 copyCmds := apexRule.Args["copy_commands"]
770
771 // Ensure that direct non-stubs dep is always included
772 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
773
774 // Ensure that indirect stubs dep is not included
775 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
776
777 // Ensure that dependency of stubs is not included
778 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
779
Colin Cross7113d202019-11-20 16:39:12 -0800780 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900781
782 // Ensure that mylib is linking with version 10 of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800783 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900784 // ... and not linking to the non-stub (impl) variant of libfoo
Colin Cross7113d202019-11-20 16:39:12 -0800785 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900786
Colin Cross7113d202019-11-20 16:39:12 -0800787 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900788
789 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
790 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
791}
792
Jooyung Hand3639552019-08-09 12:57:43 +0900793func TestApexWithRuntimeLibsDependency(t *testing.T) {
794 /*
795 myapex
796 |
797 v (runtime_libs)
798 mylib ------+------> libfoo [provides stub]
799 |
800 `------> libbar
801 */
802 ctx, _ := testApex(t, `
803 apex {
804 name: "myapex",
805 key: "myapex.key",
806 native_shared_libs: ["mylib"],
807 }
808
809 apex_key {
810 name: "myapex.key",
811 public_key: "testkey.avbpubkey",
812 private_key: "testkey.pem",
813 }
814
815 cc_library {
816 name: "mylib",
817 srcs: ["mylib.cpp"],
818 runtime_libs: ["libfoo", "libbar"],
819 system_shared_libs: [],
820 stl: "none",
821 }
822
823 cc_library {
824 name: "libfoo",
825 srcs: ["mylib.cpp"],
826 system_shared_libs: [],
827 stl: "none",
828 stubs: {
829 versions: ["10", "20", "30"],
830 },
831 }
832
833 cc_library {
834 name: "libbar",
835 srcs: ["mylib.cpp"],
836 system_shared_libs: [],
837 stl: "none",
838 }
839
840 `)
841
Sundong Ahnabb64432019-10-22 13:58:29 +0900842 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +0900843 copyCmds := apexRule.Args["copy_commands"]
844
845 // Ensure that direct non-stubs dep is always included
846 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
847
848 // Ensure that indirect stubs dep is not included
849 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
850
851 // Ensure that runtime_libs dep in included
852 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
853
Sundong Ahnabb64432019-10-22 13:58:29 +0900854 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900855 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
856 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +0900857
858}
859
Jooyung Han9c80bae2019-08-20 17:30:57 +0900860func TestApexDependencyToLLNDK(t *testing.T) {
861 ctx, _ := testApex(t, `
862 apex {
863 name: "myapex",
864 key: "myapex.key",
865 use_vendor: true,
866 native_shared_libs: ["mylib"],
867 }
868
869 apex_key {
870 name: "myapex.key",
871 public_key: "testkey.avbpubkey",
872 private_key: "testkey.pem",
873 }
874
875 cc_library {
876 name: "mylib",
877 srcs: ["mylib.cpp"],
878 vendor_available: true,
879 shared_libs: ["libbar"],
880 system_shared_libs: [],
881 stl: "none",
882 }
883
884 cc_library {
885 name: "libbar",
886 srcs: ["mylib.cpp"],
887 system_shared_libs: [],
888 stl: "none",
889 }
890
891 llndk_library {
892 name: "libbar",
893 symbol_file: "",
894 }
Jooyung Handc782442019-11-01 03:14:38 +0900895 `, func(fs map[string][]byte, config android.Config) {
896 setUseVendorWhitelistForTest(config, []string{"myapex"})
897 })
Jooyung Han9c80bae2019-08-20 17:30:57 +0900898
Sundong Ahnabb64432019-10-22 13:58:29 +0900899 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900900 copyCmds := apexRule.Args["copy_commands"]
901
902 // Ensure that LLNDK dep is not included
903 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
904
Sundong Ahnabb64432019-10-22 13:58:29 +0900905 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900906 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
Jooyung Han9c80bae2019-08-20 17:30:57 +0900907
908 // Ensure that LLNDK dep is required
Jooyung Hand15aa1f2019-09-27 00:38:03 +0900909 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +0900910
911}
912
Jiyong Park25fc6a92018-11-18 18:02:45 +0900913func TestApexWithSystemLibsStubs(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -0700914 ctx, _ := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915 apex {
916 name: "myapex",
917 key: "myapex.key",
918 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
919 }
920
921 apex_key {
922 name: "myapex.key",
923 public_key: "testkey.avbpubkey",
924 private_key: "testkey.pem",
925 }
926
927 cc_library {
928 name: "mylib",
929 srcs: ["mylib.cpp"],
930 shared_libs: ["libdl#27"],
931 stl: "none",
932 }
933
934 cc_library_shared {
935 name: "mylib_shared",
936 srcs: ["mylib.cpp"],
937 shared_libs: ["libdl#27"],
938 stl: "none",
939 }
940
941 cc_library {
942 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700943 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 nocrt: true,
945 system_shared_libs: [],
946 stl: "none",
947 stubs: {
948 versions: ["27", "28", "29"],
949 },
950 }
951
952 cc_library {
953 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700954 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900955 nocrt: true,
956 system_shared_libs: [],
957 stl: "none",
958 stubs: {
959 versions: ["27", "28", "29"],
960 },
961 }
962
963 cc_library {
964 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700965 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900966 nocrt: true,
967 system_shared_libs: [],
968 stl: "none",
969 stubs: {
970 versions: ["27", "28", "29"],
971 },
972 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900973
974 cc_library {
975 name: "libBootstrap",
976 srcs: ["mylib.cpp"],
977 stl: "none",
978 bootstrap: true,
979 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900980 `)
981
Sundong Ahnabb64432019-10-22 13:58:29 +0900982 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900983 copyCmds := apexRule.Args["copy_commands"]
984
985 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800986 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900987 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
988 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900989
990 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900991 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992
Colin Cross7113d202019-11-20 16:39:12 -0800993 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_myapex").Rule("ld").Args["libFlags"]
994 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
995 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996
997 // For dependency to libc
998 // Ensure that mylib is linking with the latest version of stubs
Colin Cross7113d202019-11-20 16:39:12 -0800999 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001001 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002 // ... Cflags from stub is correctly exported to mylib
1003 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1004 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1005
1006 // For dependency to libm
1007 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001008 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001009 // ... and not linking to the stub variant
Colin Cross7113d202019-11-20 16:39:12 -08001010 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... and is not compiling with the stub
1012 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1013 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1014
1015 // For dependency to libdl
1016 // Ensure that mylib is linking with the specified version of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001017 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001018 // ... and not linking to the other versions of stubs
Colin Cross7113d202019-11-20 16:39:12 -08001019 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28_myapex/libdl.so")
1020 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021 // ... and not linking to the non-stub (impl) variant
Colin Cross7113d202019-11-20 16:39:12 -08001022 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001023 // ... Cflags from stub is correctly exported to mylib
1024 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1025 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001026
1027 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001028 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1029 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1030 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1031 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001032}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001033
1034func TestFilesInSubDir(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001035 ctx, _ := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09001036 apex {
1037 name: "myapex",
1038 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001039 native_shared_libs: ["mylib"],
1040 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09001041 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001042 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +09001043 }
1044
1045 apex_key {
1046 name: "myapex.key",
1047 public_key: "testkey.avbpubkey",
1048 private_key: "testkey.pem",
1049 }
1050
1051 prebuilt_etc {
1052 name: "myetc",
1053 src: "myprebuilt",
1054 sub_dir: "foo/bar",
1055 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001056
1057 cc_library {
1058 name: "mylib",
1059 srcs: ["mylib.cpp"],
1060 relative_install_path: "foo/bar",
1061 system_shared_libs: [],
1062 stl: "none",
1063 }
1064
1065 cc_binary {
1066 name: "mybin",
1067 srcs: ["mylib.cpp"],
1068 relative_install_path: "foo/bar",
1069 system_shared_libs: [],
1070 static_executable: true,
1071 stl: "none",
1072 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09001073 `)
1074
Sundong Ahnabb64432019-10-22 13:58:29 +09001075 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001076 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
1077
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001078 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09001079 ensureListContains(t, dirs, "etc")
1080 ensureListContains(t, dirs, "etc/foo")
1081 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09001082 ensureListContains(t, dirs, "lib64")
1083 ensureListContains(t, dirs, "lib64/foo")
1084 ensureListContains(t, dirs, "lib64/foo/bar")
1085 ensureListContains(t, dirs, "lib")
1086 ensureListContains(t, dirs, "lib/foo")
1087 ensureListContains(t, dirs, "lib/foo/bar")
1088
Jiyong Parkbd13e442019-03-15 18:10:35 +09001089 ensureListContains(t, dirs, "bin")
1090 ensureListContains(t, dirs, "bin/foo")
1091 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09001092}
Jiyong Parkda6eb592018-12-19 17:12:36 +09001093
1094func TestUseVendor(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001095 ctx, _ := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09001096 apex {
1097 name: "myapex",
1098 key: "myapex.key",
1099 native_shared_libs: ["mylib"],
1100 use_vendor: true,
1101 }
1102
1103 apex_key {
1104 name: "myapex.key",
1105 public_key: "testkey.avbpubkey",
1106 private_key: "testkey.pem",
1107 }
1108
1109 cc_library {
1110 name: "mylib",
1111 srcs: ["mylib.cpp"],
1112 shared_libs: ["mylib2"],
1113 system_shared_libs: [],
1114 vendor_available: true,
1115 stl: "none",
1116 }
1117
1118 cc_library {
1119 name: "mylib2",
1120 srcs: ["mylib.cpp"],
1121 system_shared_libs: [],
1122 vendor_available: true,
1123 stl: "none",
1124 }
Jooyung Handc782442019-11-01 03:14:38 +09001125 `, func(fs map[string][]byte, config android.Config) {
1126 setUseVendorWhitelistForTest(config, []string{"myapex"})
1127 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09001128
1129 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09001130 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09001131 for _, implicit := range i.Implicits {
1132 inputsList = append(inputsList, implicit.String())
1133 }
1134 }
1135 inputsString := strings.Join(inputsList, " ")
1136
1137 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossfb0c16e2019-11-20 17:12:35 -08001138 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib.so")
1139 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001140
1141 // ensure that the apex does not include core variants
Colin Cross7113d202019-11-20 16:39:12 -08001142 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib.so")
1143 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_myapex/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09001144}
Jiyong Park16e91a02018-12-20 18:18:08 +09001145
Jooyung Handc782442019-11-01 03:14:38 +09001146func TestUseVendorRestriction(t *testing.T) {
1147 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
1148 apex {
1149 name: "myapex",
1150 key: "myapex.key",
1151 use_vendor: true,
1152 }
1153 apex_key {
1154 name: "myapex.key",
1155 public_key: "testkey.avbpubkey",
1156 private_key: "testkey.pem",
1157 }
1158 `, func(fs map[string][]byte, config android.Config) {
1159 setUseVendorWhitelistForTest(config, []string{""})
1160 })
1161 // no error with whitelist
1162 testApex(t, `
1163 apex {
1164 name: "myapex",
1165 key: "myapex.key",
1166 use_vendor: true,
1167 }
1168 apex_key {
1169 name: "myapex.key",
1170 public_key: "testkey.avbpubkey",
1171 private_key: "testkey.pem",
1172 }
1173 `, func(fs map[string][]byte, config android.Config) {
1174 setUseVendorWhitelistForTest(config, []string{"myapex"})
1175 })
1176}
1177
Jooyung Han5c998b92019-06-27 11:30:33 +09001178func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
1179 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
1180 apex {
1181 name: "myapex",
1182 key: "myapex.key",
1183 native_shared_libs: ["mylib"],
1184 use_vendor: true,
1185 }
1186
1187 apex_key {
1188 name: "myapex.key",
1189 public_key: "testkey.avbpubkey",
1190 private_key: "testkey.pem",
1191 }
1192
1193 cc_library {
1194 name: "mylib",
1195 srcs: ["mylib.cpp"],
1196 system_shared_libs: [],
1197 stl: "none",
1198 }
1199 `)
1200}
1201
Jiyong Park16e91a02018-12-20 18:18:08 +09001202func TestStaticLinking(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001203 ctx, _ := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09001204 apex {
1205 name: "myapex",
1206 key: "myapex.key",
1207 native_shared_libs: ["mylib"],
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 cc_library {
1217 name: "mylib",
1218 srcs: ["mylib.cpp"],
1219 system_shared_libs: [],
1220 stl: "none",
1221 stubs: {
1222 versions: ["1", "2", "3"],
1223 },
1224 }
1225
1226 cc_binary {
1227 name: "not_in_apex",
1228 srcs: ["mylib.cpp"],
1229 static_libs: ["mylib"],
1230 static_executable: true,
1231 system_shared_libs: [],
1232 stl: "none",
1233 }
Jiyong Park16e91a02018-12-20 18:18:08 +09001234 `)
1235
Colin Cross7113d202019-11-20 16:39:12 -08001236 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09001237
1238 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08001239 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09001240}
Jiyong Park9335a262018-12-24 11:31:58 +09001241
1242func TestKeys(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001243 ctx, _ := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09001244 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001245 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09001246 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001247 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09001248 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09001249 file_contexts: ":myapex-file_contexts",
Jiyong Park9335a262018-12-24 11:31:58 +09001250 }
1251
1252 cc_library {
1253 name: "mylib",
1254 srcs: ["mylib.cpp"],
1255 system_shared_libs: [],
1256 stl: "none",
1257 }
1258
1259 apex_key {
1260 name: "myapex.key",
1261 public_key: "testkey.avbpubkey",
1262 private_key: "testkey.pem",
1263 }
1264
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001265 android_app_certificate {
1266 name: "myapex.certificate",
1267 certificate: "testkey",
1268 }
1269
1270 android_app_certificate {
1271 name: "myapex.certificate.override",
1272 certificate: "testkey.override",
1273 }
1274
Jiyong Park9335a262018-12-24 11:31:58 +09001275 `)
1276
1277 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001278 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09001279
1280 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
1281 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
1282 "vendor/foo/devkeys/testkey.avbpubkey")
1283 }
1284 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
1285 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
1286 "vendor/foo/devkeys/testkey.pem")
1287 }
1288
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001289 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09001290 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001291 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09001292 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09001293 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09001294 }
1295}
Jiyong Park58e364a2019-01-19 19:24:06 +09001296
Jooyung Hanf121a652019-12-17 14:30:11 +09001297func TestCertificate(t *testing.T) {
1298 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
1299 ctx, _ := testApex(t, `
1300 apex {
1301 name: "myapex",
1302 key: "myapex.key",
1303 }
1304 apex_key {
1305 name: "myapex.key",
1306 public_key: "testkey.avbpubkey",
1307 private_key: "testkey.pem",
1308 }`)
1309 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1310 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
1311 if actual := rule.Args["certificates"]; actual != expected {
1312 t.Errorf("certificates should be %q, not %q", expected, actual)
1313 }
1314 })
1315 t.Run("override when unspecified", func(t *testing.T) {
1316 ctx, _ := testApex(t, `
1317 apex {
1318 name: "myapex_keytest",
1319 key: "myapex.key",
1320 file_contexts: ":myapex-file_contexts",
1321 }
1322 apex_key {
1323 name: "myapex.key",
1324 public_key: "testkey.avbpubkey",
1325 private_key: "testkey.pem",
1326 }
1327 android_app_certificate {
1328 name: "myapex.certificate.override",
1329 certificate: "testkey.override",
1330 }`)
1331 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1332 expected := "testkey.override.x509.pem testkey.override.pk8"
1333 if actual := rule.Args["certificates"]; actual != expected {
1334 t.Errorf("certificates should be %q, not %q", expected, actual)
1335 }
1336 })
1337 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
1338 ctx, _ := testApex(t, `
1339 apex {
1340 name: "myapex",
1341 key: "myapex.key",
1342 certificate: ":myapex.certificate",
1343 }
1344 apex_key {
1345 name: "myapex.key",
1346 public_key: "testkey.avbpubkey",
1347 private_key: "testkey.pem",
1348 }
1349 android_app_certificate {
1350 name: "myapex.certificate",
1351 certificate: "testkey",
1352 }`)
1353 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1354 expected := "testkey.x509.pem testkey.pk8"
1355 if actual := rule.Args["certificates"]; actual != expected {
1356 t.Errorf("certificates should be %q, not %q", expected, actual)
1357 }
1358 })
1359 t.Run("override when specifiec as <:module>", func(t *testing.T) {
1360 ctx, _ := testApex(t, `
1361 apex {
1362 name: "myapex_keytest",
1363 key: "myapex.key",
1364 file_contexts: ":myapex-file_contexts",
1365 certificate: ":myapex.certificate",
1366 }
1367 apex_key {
1368 name: "myapex.key",
1369 public_key: "testkey.avbpubkey",
1370 private_key: "testkey.pem",
1371 }
1372 android_app_certificate {
1373 name: "myapex.certificate.override",
1374 certificate: "testkey.override",
1375 }`)
1376 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1377 expected := "testkey.override.x509.pem testkey.override.pk8"
1378 if actual := rule.Args["certificates"]; actual != expected {
1379 t.Errorf("certificates should be %q, not %q", expected, actual)
1380 }
1381 })
1382 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
1383 ctx, _ := testApex(t, `
1384 apex {
1385 name: "myapex",
1386 key: "myapex.key",
1387 certificate: "testkey",
1388 }
1389 apex_key {
1390 name: "myapex.key",
1391 public_key: "testkey.avbpubkey",
1392 private_key: "testkey.pem",
1393 }`)
1394 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
1395 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
1396 if actual := rule.Args["certificates"]; actual != expected {
1397 t.Errorf("certificates should be %q, not %q", expected, actual)
1398 }
1399 })
1400 t.Run("override when specified as <name>", func(t *testing.T) {
1401 ctx, _ := testApex(t, `
1402 apex {
1403 name: "myapex_keytest",
1404 key: "myapex.key",
1405 file_contexts: ":myapex-file_contexts",
1406 certificate: "testkey",
1407 }
1408 apex_key {
1409 name: "myapex.key",
1410 public_key: "testkey.avbpubkey",
1411 private_key: "testkey.pem",
1412 }
1413 android_app_certificate {
1414 name: "myapex.certificate.override",
1415 certificate: "testkey.override",
1416 }`)
1417 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
1418 expected := "testkey.override.x509.pem testkey.override.pk8"
1419 if actual := rule.Args["certificates"]; actual != expected {
1420 t.Errorf("certificates should be %q, not %q", expected, actual)
1421 }
1422 })
1423}
1424
Jiyong Park58e364a2019-01-19 19:24:06 +09001425func TestMacro(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001426 ctx, _ := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09001427 apex {
1428 name: "myapex",
1429 key: "myapex.key",
1430 native_shared_libs: ["mylib"],
1431 }
1432
1433 apex {
1434 name: "otherapex",
1435 key: "myapex.key",
1436 native_shared_libs: ["mylib"],
1437 }
1438
1439 apex_key {
1440 name: "myapex.key",
1441 public_key: "testkey.avbpubkey",
1442 private_key: "testkey.pem",
1443 }
1444
1445 cc_library {
1446 name: "mylib",
1447 srcs: ["mylib.cpp"],
1448 system_shared_libs: [],
1449 stl: "none",
1450 }
1451 `)
1452
Jooyung Han6b8459b2019-10-30 08:29:25 +09001453 // non-APEX variant does not have __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001454 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001455 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001456 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1457 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001458
Jooyung Han6b8459b2019-10-30 08:29:25 +09001459 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001460 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001461 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001462 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1463 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001464
Jooyung Han6b8459b2019-10-30 08:29:25 +09001465 // APEX variant has __ANDROID_APEX(_NAME)__ defined
Colin Cross7113d202019-11-20 16:39:12 -08001466 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09001467 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09001468 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
1469 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09001470}
Jiyong Park7e636d02019-01-28 16:16:54 +09001471
1472func TestHeaderLibsDependency(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07001473 ctx, _ := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09001474 apex {
1475 name: "myapex",
1476 key: "myapex.key",
1477 native_shared_libs: ["mylib"],
1478 }
1479
1480 apex_key {
1481 name: "myapex.key",
1482 public_key: "testkey.avbpubkey",
1483 private_key: "testkey.pem",
1484 }
1485
1486 cc_library_headers {
1487 name: "mylib_headers",
1488 export_include_dirs: ["my_include"],
1489 system_shared_libs: [],
1490 stl: "none",
1491 }
1492
1493 cc_library {
1494 name: "mylib",
1495 srcs: ["mylib.cpp"],
1496 system_shared_libs: [],
1497 stl: "none",
1498 header_libs: ["mylib_headers"],
1499 export_header_lib_headers: ["mylib_headers"],
1500 stubs: {
1501 versions: ["1", "2", "3"],
1502 },
1503 }
1504
1505 cc_library {
1506 name: "otherlib",
1507 srcs: ["mylib.cpp"],
1508 system_shared_libs: [],
1509 stl: "none",
1510 shared_libs: ["mylib"],
1511 }
1512 `)
1513
Colin Cross7113d202019-11-20 16:39:12 -08001514 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09001515
1516 // Ensure that the include path of the header lib is exported to 'otherlib'
1517 ensureContains(t, cFlags, "-Imy_include")
1518}
Alex Light9670d332019-01-29 18:07:33 -08001519
Jooyung Han31c470b2019-10-18 16:26:59 +09001520func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName string, files []string) {
1521 t.Helper()
Sundong Ahnabb64432019-10-22 13:58:29 +09001522 apexRule := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09001523 copyCmds := apexRule.Args["copy_commands"]
1524 imageApexDir := "/image.apex/"
Jooyung Han39edb6c2019-11-06 16:53:07 +09001525 var failed bool
1526 var surplus []string
1527 filesMatched := make(map[string]bool)
1528 addContent := func(content string) {
1529 for _, expected := range files {
1530 if matched, _ := path.Match(expected, content); matched {
1531 filesMatched[expected] = true
1532 return
1533 }
1534 }
1535 surplus = append(surplus, content)
1536 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001537 for _, cmd := range strings.Split(copyCmds, "&&") {
1538 cmd = strings.TrimSpace(cmd)
1539 if cmd == "" {
1540 continue
1541 }
1542 terms := strings.Split(cmd, " ")
1543 switch terms[0] {
1544 case "mkdir":
1545 case "cp":
1546 if len(terms) != 3 {
1547 t.Fatal("copyCmds contains invalid cp command", cmd)
1548 }
1549 dst := terms[2]
1550 index := strings.Index(dst, imageApexDir)
1551 if index == -1 {
1552 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
1553 }
1554 dstFile := dst[index+len(imageApexDir):]
Jooyung Han39edb6c2019-11-06 16:53:07 +09001555 addContent(dstFile)
Jooyung Han31c470b2019-10-18 16:26:59 +09001556 default:
1557 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
1558 }
1559 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001560
Jooyung Han31c470b2019-10-18 16:26:59 +09001561 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001562 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09001563 t.Log("surplus files", surplus)
1564 failed = true
1565 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001566
1567 if len(files) > len(filesMatched) {
1568 var missing []string
1569 for _, expected := range files {
1570 if !filesMatched[expected] {
1571 missing = append(missing, expected)
1572 }
1573 }
1574 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09001575 t.Log("missing files", missing)
1576 failed = true
1577 }
1578 if failed {
1579 t.Fail()
1580 }
1581}
1582
Jooyung Han344d5432019-08-23 11:17:39 +09001583func TestVndkApexCurrent(t *testing.T) {
1584 ctx, _ := testApex(t, `
1585 apex_vndk {
1586 name: "myapex",
1587 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001588 }
1589
1590 apex_key {
1591 name: "myapex.key",
1592 public_key: "testkey.avbpubkey",
1593 private_key: "testkey.pem",
1594 }
1595
1596 cc_library {
1597 name: "libvndk",
1598 srcs: ["mylib.cpp"],
1599 vendor_available: true,
1600 vndk: {
1601 enabled: true,
1602 },
1603 system_shared_libs: [],
1604 stl: "none",
1605 }
1606
1607 cc_library {
1608 name: "libvndksp",
1609 srcs: ["mylib.cpp"],
1610 vendor_available: true,
1611 vndk: {
1612 enabled: true,
1613 support_system_process: true,
1614 },
1615 system_shared_libs: [],
1616 stl: "none",
1617 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001618 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09001619
Jooyung Han31c470b2019-10-18 16:26:59 +09001620 ensureExactContents(t, ctx, "myapex", []string{
1621 "lib/libvndk.so",
1622 "lib/libvndksp.so",
1623 "lib64/libvndk.so",
1624 "lib64/libvndksp.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001625 "etc/llndk.libraries.VER.txt",
1626 "etc/vndkcore.libraries.VER.txt",
1627 "etc/vndksp.libraries.VER.txt",
1628 "etc/vndkprivate.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09001629 })
Jooyung Han344d5432019-08-23 11:17:39 +09001630}
1631
1632func TestVndkApexWithPrebuilt(t *testing.T) {
1633 ctx, _ := testApex(t, `
1634 apex_vndk {
1635 name: "myapex",
1636 key: "myapex.key",
Jooyung Han344d5432019-08-23 11:17:39 +09001637 }
1638
1639 apex_key {
1640 name: "myapex.key",
1641 public_key: "testkey.avbpubkey",
1642 private_key: "testkey.pem",
1643 }
1644
1645 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09001646 name: "libvndk",
1647 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09001648 vendor_available: true,
1649 vndk: {
1650 enabled: true,
1651 },
1652 system_shared_libs: [],
1653 stl: "none",
1654 }
Jooyung Han31c470b2019-10-18 16:26:59 +09001655
1656 cc_prebuilt_library_shared {
1657 name: "libvndk.arm",
1658 srcs: ["libvndk.arm.so"],
1659 vendor_available: true,
1660 vndk: {
1661 enabled: true,
1662 },
1663 enabled: false,
1664 arch: {
1665 arm: {
1666 enabled: true,
1667 },
1668 },
1669 system_shared_libs: [],
1670 stl: "none",
1671 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001672 `+vndkLibrariesTxtFiles("current"),
1673 withFiles(map[string][]byte{
1674 "libvndk.so": nil,
1675 "libvndk.arm.so": nil,
1676 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001677
Jooyung Han31c470b2019-10-18 16:26:59 +09001678 ensureExactContents(t, ctx, "myapex", []string{
1679 "lib/libvndk.so",
1680 "lib/libvndk.arm.so",
1681 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001682 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001683 })
Jooyung Han344d5432019-08-23 11:17:39 +09001684}
1685
Jooyung Han39edb6c2019-11-06 16:53:07 +09001686func vndkLibrariesTxtFiles(vers ...string) (result string) {
1687 for _, v := range vers {
1688 if v == "current" {
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +09001689 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001690 result += `
1691 vndk_libraries_txt {
1692 name: "` + txt + `.libraries.txt",
1693 }
1694 `
1695 }
1696 } else {
1697 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate"} {
1698 result += `
1699 prebuilt_etc {
1700 name: "` + txt + `.libraries.` + v + `.txt",
1701 src: "dummy.txt",
1702 }
1703 `
1704 }
1705 }
1706 }
1707 return
1708}
1709
Jooyung Han344d5432019-08-23 11:17:39 +09001710func TestVndkApexVersion(t *testing.T) {
1711 ctx, _ := testApex(t, `
1712 apex_vndk {
1713 name: "myapex_v27",
1714 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001715 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001716 vndk_version: "27",
1717 }
1718
1719 apex_key {
1720 name: "myapex.key",
1721 public_key: "testkey.avbpubkey",
1722 private_key: "testkey.pem",
1723 }
1724
Jooyung Han31c470b2019-10-18 16:26:59 +09001725 vndk_prebuilt_shared {
1726 name: "libvndk27",
1727 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09001728 vendor_available: true,
1729 vndk: {
1730 enabled: true,
1731 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001732 target_arch: "arm64",
1733 arch: {
1734 arm: {
1735 srcs: ["libvndk27_arm.so"],
1736 },
1737 arm64: {
1738 srcs: ["libvndk27_arm64.so"],
1739 },
1740 },
Jooyung Han344d5432019-08-23 11:17:39 +09001741 }
1742
1743 vndk_prebuilt_shared {
1744 name: "libvndk27",
1745 version: "27",
1746 vendor_available: true,
1747 vndk: {
1748 enabled: true,
1749 },
Jooyung Han31c470b2019-10-18 16:26:59 +09001750 target_arch: "x86_64",
1751 arch: {
1752 x86: {
1753 srcs: ["libvndk27_x86.so"],
1754 },
1755 x86_64: {
1756 srcs: ["libvndk27_x86_64.so"],
1757 },
1758 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09001759 }
1760 `+vndkLibrariesTxtFiles("27"),
1761 withFiles(map[string][]byte{
1762 "libvndk27_arm.so": nil,
1763 "libvndk27_arm64.so": nil,
1764 "libvndk27_x86.so": nil,
1765 "libvndk27_x86_64.so": nil,
1766 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001767
Jooyung Han31c470b2019-10-18 16:26:59 +09001768 ensureExactContents(t, ctx, "myapex_v27", []string{
1769 "lib/libvndk27_arm.so",
1770 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001771 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001772 })
Jooyung Han344d5432019-08-23 11:17:39 +09001773}
1774
1775func TestVndkApexErrorWithDuplicateVersion(t *testing.T) {
1776 testApexError(t, `module "myapex_v27.*" .*: vndk_version: 27 is already defined in "myapex_v27.*"`, `
1777 apex_vndk {
1778 name: "myapex_v27",
1779 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001780 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001781 vndk_version: "27",
1782 }
1783 apex_vndk {
1784 name: "myapex_v27_other",
1785 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001786 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001787 vndk_version: "27",
1788 }
1789
1790 apex_key {
1791 name: "myapex.key",
1792 public_key: "testkey.avbpubkey",
1793 private_key: "testkey.pem",
1794 }
1795
1796 cc_library {
1797 name: "libvndk",
1798 srcs: ["mylib.cpp"],
1799 vendor_available: true,
1800 vndk: {
1801 enabled: true,
1802 },
1803 system_shared_libs: [],
1804 stl: "none",
1805 }
1806
1807 vndk_prebuilt_shared {
1808 name: "libvndk",
1809 version: "27",
1810 vendor_available: true,
1811 vndk: {
1812 enabled: true,
1813 },
1814 srcs: ["libvndk.so"],
1815 }
1816 `, withFiles(map[string][]byte{
1817 "libvndk.so": nil,
1818 }))
1819}
1820
Jooyung Han90eee022019-10-01 20:02:42 +09001821func TestVndkApexNameRule(t *testing.T) {
1822 ctx, _ := testApex(t, `
1823 apex_vndk {
1824 name: "myapex",
1825 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001826 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001827 }
1828 apex_vndk {
1829 name: "myapex_v28",
1830 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001831 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09001832 vndk_version: "28",
1833 }
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001838 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09001839
1840 assertApexName := func(expected, moduleName string) {
Sundong Ahnabb64432019-10-22 13:58:29 +09001841 bundle := ctx.ModuleForTests(moduleName, "android_common_"+moduleName+"_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09001842 actual := proptools.String(bundle.properties.Apex_name)
1843 if !reflect.DeepEqual(actual, expected) {
1844 t.Errorf("Got '%v', expected '%v'", actual, expected)
1845 }
1846 }
1847
1848 assertApexName("com.android.vndk.vVER", "myapex")
1849 assertApexName("com.android.vndk.v28", "myapex_v28")
1850}
1851
Jooyung Han344d5432019-08-23 11:17:39 +09001852func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
1853 ctx, _ := testApex(t, `
1854 apex_vndk {
1855 name: "myapex",
1856 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001857 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001858 }
1859
1860 apex_key {
1861 name: "myapex.key",
1862 public_key: "testkey.avbpubkey",
1863 private_key: "testkey.pem",
1864 }
1865
1866 cc_library {
1867 name: "libvndk",
1868 srcs: ["mylib.cpp"],
1869 vendor_available: true,
1870 native_bridge_supported: true,
1871 host_supported: true,
1872 vndk: {
1873 enabled: true,
1874 },
1875 system_shared_libs: [],
1876 stl: "none",
1877 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001878 `+vndkLibrariesTxtFiles("current"),
1879 withTargets(map[android.OsType][]android.Target{
1880 android.Android: []android.Target{
1881 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1882 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1883 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm64", NativeBridgeRelativePath: "x86_64"},
1884 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "arm", NativeBridgeRelativePath: "x86"},
1885 },
1886 }))
Jooyung Han344d5432019-08-23 11:17:39 +09001887
Jooyung Han31c470b2019-10-18 16:26:59 +09001888 ensureExactContents(t, ctx, "myapex", []string{
1889 "lib/libvndk.so",
1890 "lib64/libvndk.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001891 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001892 })
Jooyung Han344d5432019-08-23 11:17:39 +09001893}
1894
1895func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
1896 testApexError(t, `module "myapex" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
1897 apex_vndk {
1898 name: "myapex",
1899 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001900 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09001901 native_bridge_supported: true,
1902 }
1903
1904 apex_key {
1905 name: "myapex.key",
1906 public_key: "testkey.avbpubkey",
1907 private_key: "testkey.pem",
1908 }
1909
1910 cc_library {
1911 name: "libvndk",
1912 srcs: ["mylib.cpp"],
1913 vendor_available: true,
1914 native_bridge_supported: true,
1915 host_supported: true,
1916 vndk: {
1917 enabled: true,
1918 },
1919 system_shared_libs: [],
1920 stl: "none",
1921 }
1922 `)
1923}
1924
Jooyung Han31c470b2019-10-18 16:26:59 +09001925func TestVndkApexWithBinder32(t *testing.T) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09001926 ctx, _ := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09001927 apex_vndk {
1928 name: "myapex_v27",
1929 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001930 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09001931 vndk_version: "27",
1932 }
1933
1934 apex_key {
1935 name: "myapex.key",
1936 public_key: "testkey.avbpubkey",
1937 private_key: "testkey.pem",
1938 }
1939
1940 vndk_prebuilt_shared {
1941 name: "libvndk27",
1942 version: "27",
1943 target_arch: "arm",
1944 vendor_available: true,
1945 vndk: {
1946 enabled: true,
1947 },
1948 arch: {
1949 arm: {
1950 srcs: ["libvndk27.so"],
1951 }
1952 },
1953 }
1954
1955 vndk_prebuilt_shared {
1956 name: "libvndk27",
1957 version: "27",
1958 target_arch: "arm",
1959 binder32bit: true,
1960 vendor_available: true,
1961 vndk: {
1962 enabled: true,
1963 },
1964 arch: {
1965 arm: {
1966 srcs: ["libvndk27binder32.so"],
1967 }
1968 },
1969 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09001970 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09001971 withFiles(map[string][]byte{
1972 "libvndk27.so": nil,
1973 "libvndk27binder32.so": nil,
1974 }),
1975 withBinder32bit,
1976 withTargets(map[android.OsType][]android.Target{
1977 android.Android: []android.Target{
1978 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
1979 },
1980 }),
1981 )
1982
1983 ensureExactContents(t, ctx, "myapex_v27", []string{
1984 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09001985 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09001986 })
1987}
1988
Jooyung Hane1633032019-08-01 17:41:43 +09001989func TestDependenciesInApexManifest(t *testing.T) {
1990 ctx, _ := testApex(t, `
1991 apex {
1992 name: "myapex_nodep",
1993 key: "myapex.key",
1994 native_shared_libs: ["lib_nodep"],
1995 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09001996 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09001997 }
1998
1999 apex {
2000 name: "myapex_dep",
2001 key: "myapex.key",
2002 native_shared_libs: ["lib_dep"],
2003 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002004 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002005 }
2006
2007 apex {
2008 name: "myapex_provider",
2009 key: "myapex.key",
2010 native_shared_libs: ["libfoo"],
2011 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002012 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002013 }
2014
2015 apex {
2016 name: "myapex_selfcontained",
2017 key: "myapex.key",
2018 native_shared_libs: ["lib_dep", "libfoo"],
2019 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09002020 file_contexts: ":myapex-file_contexts",
Jooyung Hane1633032019-08-01 17:41:43 +09002021 }
2022
2023 apex_key {
2024 name: "myapex.key",
2025 public_key: "testkey.avbpubkey",
2026 private_key: "testkey.pem",
2027 }
2028
2029 cc_library {
2030 name: "lib_nodep",
2031 srcs: ["mylib.cpp"],
2032 system_shared_libs: [],
2033 stl: "none",
2034 }
2035
2036 cc_library {
2037 name: "lib_dep",
2038 srcs: ["mylib.cpp"],
2039 shared_libs: ["libfoo"],
2040 system_shared_libs: [],
2041 stl: "none",
2042 }
2043
2044 cc_library {
2045 name: "libfoo",
2046 srcs: ["mytest.cpp"],
2047 stubs: {
2048 versions: ["1"],
2049 },
2050 system_shared_libs: [],
2051 stl: "none",
2052 }
2053 `)
2054
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002055 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09002056 var provideNativeLibs, requireNativeLibs []string
2057
Sundong Ahnabb64432019-10-22 13:58:29 +09002058 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002059 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2060 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002061 ensureListEmpty(t, provideNativeLibs)
2062 ensureListEmpty(t, requireNativeLibs)
2063
Sundong Ahnabb64432019-10-22 13:58:29 +09002064 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002065 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2066 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002067 ensureListEmpty(t, provideNativeLibs)
2068 ensureListContains(t, requireNativeLibs, "libfoo.so")
2069
Sundong Ahnabb64432019-10-22 13:58:29 +09002070 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002071 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2072 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002073 ensureListContains(t, provideNativeLibs, "libfoo.so")
2074 ensureListEmpty(t, requireNativeLibs)
2075
Sundong Ahnabb64432019-10-22 13:58:29 +09002076 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002077 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
2078 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09002079 ensureListContains(t, provideNativeLibs, "libfoo.so")
2080 ensureListEmpty(t, requireNativeLibs)
2081}
2082
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002083func TestApexName(t *testing.T) {
2084 ctx, _ := testApex(t, `
2085 apex {
2086 name: "myapex",
2087 key: "myapex.key",
2088 apex_name: "com.android.myapex",
2089 }
2090
2091 apex_key {
2092 name: "myapex.key",
2093 public_key: "testkey.avbpubkey",
2094 private_key: "testkey.pem",
2095 }
2096 `)
2097
Sundong Ahnabb64432019-10-22 13:58:29 +09002098 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09002099 apexManifestRule := module.Rule("apexManifestRule")
2100 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
2101 apexRule := module.Rule("apexRule")
2102 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
2103}
2104
Alex Light0851b882019-02-07 13:20:53 -08002105func TestNonTestApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002106 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002107 apex {
2108 name: "myapex",
2109 key: "myapex.key",
2110 native_shared_libs: ["mylib_common"],
2111 }
2112
2113 apex_key {
2114 name: "myapex.key",
2115 public_key: "testkey.avbpubkey",
2116 private_key: "testkey.pem",
2117 }
2118
2119 cc_library {
2120 name: "mylib_common",
2121 srcs: ["mylib.cpp"],
2122 system_shared_libs: [],
2123 stl: "none",
2124 }
2125 `)
2126
Sundong Ahnabb64432019-10-22 13:58:29 +09002127 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002128 apexRule := module.Rule("apexRule")
2129 copyCmds := apexRule.Args["copy_commands"]
2130
2131 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
2132 t.Log("Apex was a test apex!")
2133 t.Fail()
2134 }
2135 // Ensure that main rule creates an output
2136 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2137
2138 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002139 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002140
2141 // Ensure that both direct and indirect deps are copied into apex
2142 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2143
Colin Cross7113d202019-11-20 16:39:12 -08002144 // Ensure that the platform variant ends with _shared
2145 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002146
2147 if !android.InAnyApex("mylib_common") {
2148 t.Log("Found mylib_common not in any apex!")
2149 t.Fail()
2150 }
2151}
2152
2153func TestTestApex(t *testing.T) {
2154 if android.InAnyApex("mylib_common_test") {
2155 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!")
2156 }
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002157 ctx, _ := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08002158 apex_test {
2159 name: "myapex",
2160 key: "myapex.key",
2161 native_shared_libs: ["mylib_common_test"],
2162 }
2163
2164 apex_key {
2165 name: "myapex.key",
2166 public_key: "testkey.avbpubkey",
2167 private_key: "testkey.pem",
2168 }
2169
2170 cc_library {
2171 name: "mylib_common_test",
2172 srcs: ["mylib.cpp"],
2173 system_shared_libs: [],
2174 stl: "none",
2175 }
2176 `)
2177
Sundong Ahnabb64432019-10-22 13:58:29 +09002178 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08002179 apexRule := module.Rule("apexRule")
2180 copyCmds := apexRule.Args["copy_commands"]
2181
2182 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
2183 t.Log("Apex was not a test apex!")
2184 t.Fail()
2185 }
2186 // Ensure that main rule creates an output
2187 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2188
2189 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002190 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_myapex")
Alex Light0851b882019-02-07 13:20:53 -08002191
2192 // Ensure that both direct and indirect deps are copied into apex
2193 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
2194
Colin Cross7113d202019-11-20 16:39:12 -08002195 // Ensure that the platform variant ends with _shared
2196 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08002197
2198 if android.InAnyApex("mylib_common_test") {
2199 t.Log("Found mylib_common_test in some apex!")
2200 t.Fail()
2201 }
2202}
2203
Alex Light9670d332019-01-29 18:07:33 -08002204func TestApexWithTarget(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002205 ctx, _ := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08002206 apex {
2207 name: "myapex",
2208 key: "myapex.key",
2209 multilib: {
2210 first: {
2211 native_shared_libs: ["mylib_common"],
2212 }
2213 },
2214 target: {
2215 android: {
2216 multilib: {
2217 first: {
2218 native_shared_libs: ["mylib"],
2219 }
2220 }
2221 },
2222 host: {
2223 multilib: {
2224 first: {
2225 native_shared_libs: ["mylib2"],
2226 }
2227 }
2228 }
2229 }
2230 }
2231
2232 apex_key {
2233 name: "myapex.key",
2234 public_key: "testkey.avbpubkey",
2235 private_key: "testkey.pem",
2236 }
2237
2238 cc_library {
2239 name: "mylib",
2240 srcs: ["mylib.cpp"],
2241 system_shared_libs: [],
2242 stl: "none",
2243 }
2244
2245 cc_library {
2246 name: "mylib_common",
2247 srcs: ["mylib.cpp"],
2248 system_shared_libs: [],
2249 stl: "none",
2250 compile_multilib: "first",
2251 }
2252
2253 cc_library {
2254 name: "mylib2",
2255 srcs: ["mylib.cpp"],
2256 system_shared_libs: [],
2257 stl: "none",
2258 compile_multilib: "first",
2259 }
2260 `)
2261
Sundong Ahnabb64432019-10-22 13:58:29 +09002262 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08002263 copyCmds := apexRule.Args["copy_commands"]
2264
2265 // Ensure that main rule creates an output
2266 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
2267
2268 // Ensure that apex variant is created for the direct dep
Colin Cross7113d202019-11-20 16:39:12 -08002269 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2270 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_myapex")
2271 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_myapex")
Alex Light9670d332019-01-29 18:07:33 -08002272
2273 // Ensure that both direct and indirect deps are copied into apex
2274 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
2275 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
2276 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
2277
Colin Cross7113d202019-11-20 16:39:12 -08002278 // Ensure that the platform variant ends with _shared
2279 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
2280 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
2281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08002282}
Jiyong Park04480cf2019-02-06 00:16:29 +09002283
2284func TestApexWithShBinary(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002285 ctx, _ := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09002286 apex {
2287 name: "myapex",
2288 key: "myapex.key",
2289 binaries: ["myscript"],
2290 }
2291
2292 apex_key {
2293 name: "myapex.key",
2294 public_key: "testkey.avbpubkey",
2295 private_key: "testkey.pem",
2296 }
2297
2298 sh_binary {
2299 name: "myscript",
2300 src: "mylib.cpp",
2301 filename: "myscript.sh",
2302 sub_dir: "script",
2303 }
2304 `)
2305
Sundong Ahnabb64432019-10-22 13:58:29 +09002306 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09002307 copyCmds := apexRule.Args["copy_commands"]
2308
2309 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
2310}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002311
Jooyung Han91df2082019-11-20 01:49:42 +09002312func TestApexInVariousPartition(t *testing.T) {
2313 testcases := []struct {
2314 propName, parition, flattenedPartition string
2315 }{
2316 {"", "system", "system_ext"},
2317 {"product_specific: true", "product", "product"},
2318 {"soc_specific: true", "vendor", "vendor"},
2319 {"proprietary: true", "vendor", "vendor"},
2320 {"vendor: true", "vendor", "vendor"},
2321 {"system_ext_specific: true", "system_ext", "system_ext"},
2322 }
2323 for _, tc := range testcases {
2324 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
2325 ctx, _ := testApex(t, `
2326 apex {
2327 name: "myapex",
2328 key: "myapex.key",
2329 `+tc.propName+`
2330 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002331
Jooyung Han91df2082019-11-20 01:49:42 +09002332 apex_key {
2333 name: "myapex.key",
2334 public_key: "testkey.avbpubkey",
2335 private_key: "testkey.pem",
2336 }
2337 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002338
Jooyung Han91df2082019-11-20 01:49:42 +09002339 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2340 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
2341 actual := apex.installDir.String()
2342 if actual != expected {
2343 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2344 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002345
Jooyung Han91df2082019-11-20 01:49:42 +09002346 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
2347 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
2348 actual = flattened.installDir.String()
2349 if actual != expected {
2350 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
2351 }
2352 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002353 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002354}
Jiyong Park67882562019-03-21 01:11:21 +09002355
Jooyung Han54aca7b2019-11-20 02:26:02 +09002356func TestFileContexts(t *testing.T) {
2357 ctx, _ := testApex(t, `
2358 apex {
2359 name: "myapex",
2360 key: "myapex.key",
2361 }
2362
2363 apex_key {
2364 name: "myapex.key",
2365 public_key: "testkey.avbpubkey",
2366 private_key: "testkey.pem",
2367 }
2368 `)
2369 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2370 apexRule := module.Rule("apexRule")
2371 actual := apexRule.Args["file_contexts"]
2372 expected := "system/sepolicy/apex/myapex-file_contexts"
2373 if actual != expected {
2374 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2375 }
2376
2377 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
2378 apex {
2379 name: "myapex",
2380 key: "myapex.key",
2381 file_contexts: "my_own_file_contexts",
2382 }
2383
2384 apex_key {
2385 name: "myapex.key",
2386 public_key: "testkey.avbpubkey",
2387 private_key: "testkey.pem",
2388 }
2389 `, withFiles(map[string][]byte{
2390 "my_own_file_contexts": nil,
2391 }))
2392
2393 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
2394 apex {
2395 name: "myapex",
2396 key: "myapex.key",
2397 product_specific: true,
2398 file_contexts: "product_specific_file_contexts",
2399 }
2400
2401 apex_key {
2402 name: "myapex.key",
2403 public_key: "testkey.avbpubkey",
2404 private_key: "testkey.pem",
2405 }
2406 `)
2407
2408 ctx, _ = testApex(t, `
2409 apex {
2410 name: "myapex",
2411 key: "myapex.key",
2412 product_specific: true,
2413 file_contexts: "product_specific_file_contexts",
2414 }
2415
2416 apex_key {
2417 name: "myapex.key",
2418 public_key: "testkey.avbpubkey",
2419 private_key: "testkey.pem",
2420 }
2421 `, withFiles(map[string][]byte{
2422 "product_specific_file_contexts": nil,
2423 }))
2424 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2425 apexRule = module.Rule("apexRule")
2426 actual = apexRule.Args["file_contexts"]
2427 expected = "product_specific_file_contexts"
2428 if actual != expected {
2429 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2430 }
2431
2432 ctx, _ = testApex(t, `
2433 apex {
2434 name: "myapex",
2435 key: "myapex.key",
2436 product_specific: true,
2437 file_contexts: ":my-file-contexts",
2438 }
2439
2440 apex_key {
2441 name: "myapex.key",
2442 public_key: "testkey.avbpubkey",
2443 private_key: "testkey.pem",
2444 }
2445
2446 filegroup {
2447 name: "my-file-contexts",
2448 srcs: ["product_specific_file_contexts"],
2449 }
2450 `, withFiles(map[string][]byte{
2451 "product_specific_file_contexts": nil,
2452 }))
2453 module = ctx.ModuleForTests("myapex", "android_common_myapex_image")
2454 apexRule = module.Rule("apexRule")
2455 actual = apexRule.Args["file_contexts"]
2456 expected = "product_specific_file_contexts"
2457 if actual != expected {
2458 t.Errorf("wrong file_contexts. expected %q. actual %q", expected, actual)
2459 }
2460}
2461
Jiyong Park67882562019-03-21 01:11:21 +09002462func TestApexKeyFromOtherModule(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002463 ctx, _ := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09002464 apex_key {
2465 name: "myapex.key",
2466 public_key: ":my.avbpubkey",
2467 private_key: ":my.pem",
2468 product_specific: true,
2469 }
2470
2471 filegroup {
2472 name: "my.avbpubkey",
2473 srcs: ["testkey2.avbpubkey"],
2474 }
2475
2476 filegroup {
2477 name: "my.pem",
2478 srcs: ["testkey2.pem"],
2479 }
2480 `)
2481
2482 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
2483 expected_pubkey := "testkey2.avbpubkey"
2484 actual_pubkey := apex_key.public_key_file.String()
2485 if actual_pubkey != expected_pubkey {
2486 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
2487 }
2488 expected_privkey := "testkey2.pem"
2489 actual_privkey := apex_key.private_key_file.String()
2490 if actual_privkey != expected_privkey {
2491 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
2492 }
2493}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002494
2495func TestPrebuilt(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002496 ctx, _ := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002497 prebuilt_apex {
2498 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09002499 arch: {
2500 arm64: {
2501 src: "myapex-arm64.apex",
2502 },
2503 arm: {
2504 src: "myapex-arm.apex",
2505 },
2506 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002507 }
2508 `)
2509
2510 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2511
Jiyong Parkc95714e2019-03-29 14:23:10 +09002512 expectedInput := "myapex-arm64.apex"
2513 if prebuilt.inputApex.String() != expectedInput {
2514 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
2515 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07002516}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002517
2518func TestPrebuiltFilenameOverride(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002519 ctx, _ := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01002520 prebuilt_apex {
2521 name: "myapex",
2522 src: "myapex-arm.apex",
2523 filename: "notmyapex.apex",
2524 }
2525 `)
2526
2527 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
2528
2529 expected := "notmyapex.apex"
2530 if p.installFilename != expected {
2531 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
2532 }
2533}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07002534
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002535func TestPrebuiltOverrides(t *testing.T) {
2536 ctx, config := testApex(t, `
2537 prebuilt_apex {
2538 name: "myapex.prebuilt",
2539 src: "myapex-arm.apex",
2540 overrides: [
2541 "myapex",
2542 ],
2543 }
2544 `)
2545
2546 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
2547
2548 expected := []string{"myapex"}
Jiyong Park0b0e1b92019-12-03 13:24:29 +09002549 actual := android.AndroidMkEntriesForTest(t, config, "", p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002550 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09002551 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002552 }
2553}
2554
Roland Levillain630846d2019-06-26 12:48:34 +01002555func TestApexWithTests(t *testing.T) {
Roland Levillainf89cd092019-07-29 16:22:59 +01002556 ctx, config := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01002557 apex_test {
2558 name: "myapex",
2559 key: "myapex.key",
2560 tests: [
2561 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01002562 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01002563 ],
2564 }
2565
2566 apex_key {
2567 name: "myapex.key",
2568 public_key: "testkey.avbpubkey",
2569 private_key: "testkey.pem",
2570 }
2571
2572 cc_test {
2573 name: "mytest",
2574 gtest: false,
2575 srcs: ["mytest.cpp"],
2576 relative_install_path: "test",
2577 system_shared_libs: [],
2578 static_executable: true,
2579 stl: "none",
2580 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01002581
2582 cc_test {
2583 name: "mytests",
2584 gtest: false,
2585 srcs: [
2586 "mytest1.cpp",
2587 "mytest2.cpp",
2588 "mytest3.cpp",
2589 ],
2590 test_per_src: true,
2591 relative_install_path: "test",
2592 system_shared_libs: [],
2593 static_executable: true,
2594 stl: "none",
2595 }
Roland Levillain630846d2019-06-26 12:48:34 +01002596 `)
2597
Sundong Ahnabb64432019-10-22 13:58:29 +09002598 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01002599 copyCmds := apexRule.Args["copy_commands"]
2600
2601 // Ensure that test dep is copied into apex.
2602 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Roland Levillain9b5fde92019-06-28 15:41:19 +01002603
2604 // Ensure that test deps built with `test_per_src` are copied into apex.
2605 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
2606 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
2607 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01002608
2609 // Ensure the module is correctly translated.
Sundong Ahnabb64432019-10-22 13:58:29 +09002610 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Roland Levillainf89cd092019-07-29 16:22:59 +01002611 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
2612 name := apexBundle.BaseModuleName()
2613 prefix := "TARGET_"
2614 var builder strings.Builder
2615 data.Custom(&builder, name, prefix, "", data)
2616 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09002617 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
2618 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
2619 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
2620 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09002621 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09002622 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01002623 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01002624}
2625
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09002626func TestInstallExtraFlattenedApexes(t *testing.T) {
2627 ctx, config := testApex(t, `
2628 apex {
2629 name: "myapex",
2630 key: "myapex.key",
2631 }
2632 apex_key {
2633 name: "myapex.key",
2634 public_key: "testkey.avbpubkey",
2635 private_key: "testkey.pem",
2636 }
2637 `, func(fs map[string][]byte, config android.Config) {
2638 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
2639 })
2640 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2641 ensureListContains(t, ab.externalDeps, "myapex.flattened")
2642 mk := android.AndroidMkDataForTest(t, config, "", ab)
2643 var builder strings.Builder
2644 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
2645 androidMk := builder.String()
2646 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
2647}
2648
Jooyung Han5c998b92019-06-27 11:30:33 +09002649func TestApexUsesOtherApex(t *testing.T) {
Jaewoong Jung22f7d182019-07-16 18:25:41 -07002650 ctx, _ := testApex(t, `
Jooyung Han5c998b92019-06-27 11:30:33 +09002651 apex {
2652 name: "myapex",
2653 key: "myapex.key",
2654 native_shared_libs: ["mylib"],
2655 uses: ["commonapex"],
2656 }
2657
2658 apex {
2659 name: "commonapex",
2660 key: "myapex.key",
2661 native_shared_libs: ["libcommon"],
2662 provide_cpp_shared_libs: true,
2663 }
2664
2665 apex_key {
2666 name: "myapex.key",
2667 public_key: "testkey.avbpubkey",
2668 private_key: "testkey.pem",
2669 }
2670
2671 cc_library {
2672 name: "mylib",
2673 srcs: ["mylib.cpp"],
2674 shared_libs: ["libcommon"],
2675 system_shared_libs: [],
2676 stl: "none",
2677 }
2678
2679 cc_library {
2680 name: "libcommon",
2681 srcs: ["mylib_common.cpp"],
2682 system_shared_libs: [],
2683 stl: "none",
2684 }
2685 `)
2686
Sundong Ahnabb64432019-10-22 13:58:29 +09002687 module1 := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002688 apexRule1 := module1.Rule("apexRule")
2689 copyCmds1 := apexRule1.Args["copy_commands"]
2690
Sundong Ahnabb64432019-10-22 13:58:29 +09002691 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex_image")
Jooyung Han5c998b92019-06-27 11:30:33 +09002692 apexRule2 := module2.Rule("apexRule")
2693 copyCmds2 := apexRule2.Args["copy_commands"]
2694
Colin Cross7113d202019-11-20 16:39:12 -08002695 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_myapex")
2696 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_shared_commonapex")
Jooyung Han5c998b92019-06-27 11:30:33 +09002697 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
2698 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
2699 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
2700}
2701
2702func TestApexUsesFailsIfNotProvided(t *testing.T) {
2703 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
2704 apex {
2705 name: "myapex",
2706 key: "myapex.key",
2707 uses: ["commonapex"],
2708 }
2709
2710 apex {
2711 name: "commonapex",
2712 key: "myapex.key",
2713 }
2714
2715 apex_key {
2716 name: "myapex.key",
2717 public_key: "testkey.avbpubkey",
2718 private_key: "testkey.pem",
2719 }
2720 `)
2721 testApexError(t, `uses: "commonapex" is not a provider`, `
2722 apex {
2723 name: "myapex",
2724 key: "myapex.key",
2725 uses: ["commonapex"],
2726 }
2727
2728 cc_library {
2729 name: "commonapex",
2730 system_shared_libs: [],
2731 stl: "none",
2732 }
2733
2734 apex_key {
2735 name: "myapex.key",
2736 public_key: "testkey.avbpubkey",
2737 private_key: "testkey.pem",
2738 }
2739 `)
2740}
2741
2742func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
2743 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
2744 apex {
2745 name: "myapex",
2746 key: "myapex.key",
2747 use_vendor: true,
2748 uses: ["commonapex"],
2749 }
2750
2751 apex {
2752 name: "commonapex",
2753 key: "myapex.key",
2754 provide_cpp_shared_libs: true,
2755 }
2756
2757 apex_key {
2758 name: "myapex.key",
2759 public_key: "testkey.avbpubkey",
2760 private_key: "testkey.pem",
2761 }
Jooyung Handc782442019-11-01 03:14:38 +09002762 `, func(fs map[string][]byte, config android.Config) {
2763 setUseVendorWhitelistForTest(config, []string{"myapex"})
2764 })
Jooyung Han5c998b92019-06-27 11:30:33 +09002765}
2766
Jooyung Hand48f3c32019-08-23 11:18:57 +09002767func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
2768 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
2769 apex {
2770 name: "myapex",
2771 key: "myapex.key",
2772 native_shared_libs: ["libfoo"],
2773 }
2774
2775 apex_key {
2776 name: "myapex.key",
2777 public_key: "testkey.avbpubkey",
2778 private_key: "testkey.pem",
2779 }
2780
2781 cc_library {
2782 name: "libfoo",
2783 stl: "none",
2784 system_shared_libs: [],
2785 enabled: false,
2786 }
2787 `)
2788 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
2789 apex {
2790 name: "myapex",
2791 key: "myapex.key",
2792 java_libs: ["myjar"],
2793 }
2794
2795 apex_key {
2796 name: "myapex.key",
2797 public_key: "testkey.avbpubkey",
2798 private_key: "testkey.pem",
2799 }
2800
2801 java_library {
2802 name: "myjar",
2803 srcs: ["foo/bar/MyClass.java"],
2804 sdk_version: "none",
2805 system_modules: "none",
2806 compile_dex: true,
2807 enabled: false,
2808 }
2809 `)
2810}
2811
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002812func TestApexWithApps(t *testing.T) {
2813 ctx, _ := testApex(t, `
2814 apex {
2815 name: "myapex",
2816 key: "myapex.key",
2817 apps: [
2818 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09002819 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002820 ],
2821 }
2822
2823 apex_key {
2824 name: "myapex.key",
2825 public_key: "testkey.avbpubkey",
2826 private_key: "testkey.pem",
2827 }
2828
2829 android_app {
2830 name: "AppFoo",
2831 srcs: ["foo/bar/MyClass.java"],
2832 sdk_version: "none",
2833 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09002834 jni_libs: ["libjni"],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002835 }
Jiyong Parkf7487312019-10-17 12:54:30 +09002836
2837 android_app {
2838 name: "AppFooPriv",
2839 srcs: ["foo/bar/MyClass.java"],
2840 sdk_version: "none",
2841 system_modules: "none",
2842 privileged: true,
2843 }
Jiyong Park8be103b2019-11-08 15:53:48 +09002844
2845 cc_library_shared {
2846 name: "libjni",
2847 srcs: ["mylib.cpp"],
2848 stl: "none",
2849 system_shared_libs: [],
2850 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002851 `)
2852
Sundong Ahnabb64432019-10-22 13:58:29 +09002853 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002854 apexRule := module.Rule("apexRule")
2855 copyCmds := apexRule.Args["copy_commands"]
2856
2857 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09002858 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09002859
2860 // JNI libraries are embedded inside APK
2861 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
Colin Cross7113d202019-11-20 16:39:12 -08002862 libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared_myapex").Module().(*cc.Module).OutputFile()
Jiyong Park52cd06f2019-11-11 10:14:32 +09002863 ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
2864 // ... uncompressed
2865 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
2866 t.Errorf("jni lib is not uncompressed for AppFoo")
2867 }
2868 // ... and not directly inside the APEX
2869 ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
Dario Frenicde2a032019-10-27 00:29:22 +01002870}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002871
Dario Frenicde2a032019-10-27 00:29:22 +01002872func TestApexWithAppImports(t *testing.T) {
2873 ctx, _ := testApex(t, `
2874 apex {
2875 name: "myapex",
2876 key: "myapex.key",
2877 apps: [
2878 "AppFooPrebuilt",
2879 "AppFooPrivPrebuilt",
2880 ],
2881 }
2882
2883 apex_key {
2884 name: "myapex.key",
2885 public_key: "testkey.avbpubkey",
2886 private_key: "testkey.pem",
2887 }
2888
2889 android_app_import {
2890 name: "AppFooPrebuilt",
2891 apk: "PrebuiltAppFoo.apk",
2892 presigned: true,
2893 dex_preopt: {
2894 enabled: false,
2895 },
2896 }
2897
2898 android_app_import {
2899 name: "AppFooPrivPrebuilt",
2900 apk: "PrebuiltAppFooPriv.apk",
2901 privileged: true,
2902 presigned: true,
2903 dex_preopt: {
2904 enabled: false,
2905 },
2906 }
2907 `)
2908
Sundong Ahnabb64432019-10-22 13:58:29 +09002909 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01002910 apexRule := module.Rule("apexRule")
2911 copyCmds := apexRule.Args["copy_commands"]
2912
2913 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
2914 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AppFooPrivPrebuilt.apk")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09002915}
2916
Dario Freni6f3937c2019-12-20 22:58:03 +00002917func TestApexWithTestHelperApp(t *testing.T) {
2918 ctx, _ := testApex(t, `
2919 apex {
2920 name: "myapex",
2921 key: "myapex.key",
2922 apps: [
2923 "TesterHelpAppFoo",
2924 ],
2925 }
2926
2927 apex_key {
2928 name: "myapex.key",
2929 public_key: "testkey.avbpubkey",
2930 private_key: "testkey.pem",
2931 }
2932
2933 android_test_helper_app {
2934 name: "TesterHelpAppFoo",
2935 srcs: ["foo/bar/MyClass.java"],
2936 }
2937
2938 `)
2939
2940 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2941 apexRule := module.Rule("apexRule")
2942 copyCmds := apexRule.Args["copy_commands"]
2943
2944 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
2945}
2946
Jooyung Han18020ea2019-11-13 10:50:48 +09002947func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
2948 // libfoo's apex_available comes from cc_defaults
2949 testApexError(t, `"myapex" .*: requires "libfoo" that is not available for the APEX`, `
2950 apex {
2951 name: "myapex",
2952 key: "myapex.key",
2953 native_shared_libs: ["libfoo"],
2954 }
2955
2956 apex_key {
2957 name: "myapex.key",
2958 public_key: "testkey.avbpubkey",
2959 private_key: "testkey.pem",
2960 }
2961
2962 apex {
2963 name: "otherapex",
2964 key: "myapex.key",
2965 native_shared_libs: ["libfoo"],
2966 }
2967
2968 cc_defaults {
2969 name: "libfoo-defaults",
2970 apex_available: ["otherapex"],
2971 }
2972
2973 cc_library {
2974 name: "libfoo",
2975 defaults: ["libfoo-defaults"],
2976 stl: "none",
2977 system_shared_libs: [],
2978 }`)
2979}
2980
Jiyong Park127b40b2019-09-30 16:04:35 +09002981func TestApexAvailable(t *testing.T) {
2982 // libfoo is not available to myapex, but only to otherapex
2983 testApexError(t, "requires \"libfoo\" that is not available for the APEX", `
2984 apex {
2985 name: "myapex",
2986 key: "myapex.key",
2987 native_shared_libs: ["libfoo"],
2988 }
2989
2990 apex_key {
2991 name: "myapex.key",
2992 public_key: "testkey.avbpubkey",
2993 private_key: "testkey.pem",
2994 }
2995
2996 apex {
2997 name: "otherapex",
2998 key: "otherapex.key",
2999 native_shared_libs: ["libfoo"],
3000 }
3001
3002 apex_key {
3003 name: "otherapex.key",
3004 public_key: "testkey.avbpubkey",
3005 private_key: "testkey.pem",
3006 }
3007
3008 cc_library {
3009 name: "libfoo",
3010 stl: "none",
3011 system_shared_libs: [],
3012 apex_available: ["otherapex"],
3013 }`)
3014
3015 // libbar is an indirect dep
3016 testApexError(t, "requires \"libbar\" that is not available for the APEX", `
3017 apex {
3018 name: "myapex",
3019 key: "myapex.key",
3020 native_shared_libs: ["libfoo"],
3021 }
3022
3023 apex_key {
3024 name: "myapex.key",
3025 public_key: "testkey.avbpubkey",
3026 private_key: "testkey.pem",
3027 }
3028
3029 apex {
3030 name: "otherapex",
3031 key: "otherapex.key",
3032 native_shared_libs: ["libfoo"],
3033 }
3034
3035 apex_key {
3036 name: "otherapex.key",
3037 public_key: "testkey.avbpubkey",
3038 private_key: "testkey.pem",
3039 }
3040
3041 cc_library {
3042 name: "libfoo",
3043 stl: "none",
3044 shared_libs: ["libbar"],
3045 system_shared_libs: [],
3046 apex_available: ["myapex", "otherapex"],
3047 }
3048
3049 cc_library {
3050 name: "libbar",
3051 stl: "none",
3052 system_shared_libs: [],
3053 apex_available: ["otherapex"],
3054 }`)
3055
3056 testApexError(t, "\"otherapex\" is not a valid module name", `
3057 apex {
3058 name: "myapex",
3059 key: "myapex.key",
3060 native_shared_libs: ["libfoo"],
3061 }
3062
3063 apex_key {
3064 name: "myapex.key",
3065 public_key: "testkey.avbpubkey",
3066 private_key: "testkey.pem",
3067 }
3068
3069 cc_library {
3070 name: "libfoo",
3071 stl: "none",
3072 system_shared_libs: [],
3073 apex_available: ["otherapex"],
3074 }`)
3075
3076 ctx, _ := testApex(t, `
3077 apex {
3078 name: "myapex",
3079 key: "myapex.key",
3080 native_shared_libs: ["libfoo", "libbar"],
3081 }
3082
3083 apex_key {
3084 name: "myapex.key",
3085 public_key: "testkey.avbpubkey",
3086 private_key: "testkey.pem",
3087 }
3088
3089 cc_library {
3090 name: "libfoo",
3091 stl: "none",
3092 system_shared_libs: [],
3093 apex_available: ["myapex"],
3094 }
3095
3096 cc_library {
3097 name: "libbar",
3098 stl: "none",
3099 system_shared_libs: [],
3100 apex_available: ["//apex_available:anyapex"],
3101 }`)
3102
3103 // check that libfoo and libbar are created only for myapex, but not for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003104 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3105 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
3106 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_myapex")
3107 ensureListNotContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared")
Jiyong Park127b40b2019-09-30 16:04:35 +09003108
3109 ctx, _ = testApex(t, `
3110 apex {
3111 name: "myapex",
3112 key: "myapex.key",
3113 }
3114
3115 apex_key {
3116 name: "myapex.key",
3117 public_key: "testkey.avbpubkey",
3118 private_key: "testkey.pem",
3119 }
3120
3121 cc_library {
3122 name: "libfoo",
3123 stl: "none",
3124 system_shared_libs: [],
3125 apex_available: ["//apex_available:platform"],
3126 }`)
3127
3128 // check that libfoo is created only for the platform
Colin Cross7113d202019-11-20 16:39:12 -08003129 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3130 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003131
3132 ctx, _ = testApex(t, `
3133 apex {
3134 name: "myapex",
3135 key: "myapex.key",
3136 native_shared_libs: ["libfoo"],
3137 }
3138
3139 apex_key {
3140 name: "myapex.key",
3141 public_key: "testkey.avbpubkey",
3142 private_key: "testkey.pem",
3143 }
3144
3145 cc_library {
3146 name: "libfoo",
3147 stl: "none",
3148 system_shared_libs: [],
3149 apex_available: ["myapex"],
3150 static: {
3151 apex_available: ["//apex_available:platform"],
3152 },
3153 }`)
3154
3155 // shared variant of libfoo is only available to myapex
Colin Cross7113d202019-11-20 16:39:12 -08003156 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared_myapex")
3157 ensureListNotContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_shared")
Jiyong Parka90ca002019-10-07 15:47:24 +09003158 // but the static variant is available to both myapex and the platform
Colin Cross7113d202019-11-20 16:39:12 -08003159 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static_myapex")
3160 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo"), "android_arm64_armv8-a_static")
Jiyong Park127b40b2019-09-30 16:04:35 +09003161}
3162
Jiyong Park5d790c32019-11-15 18:40:32 +09003163func TestOverrideApex(t *testing.T) {
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003164 ctx, config := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09003165 apex {
3166 name: "myapex",
3167 key: "myapex.key",
3168 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003169 overrides: ["oldapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003170 }
3171
3172 override_apex {
3173 name: "override_myapex",
3174 base: "myapex",
3175 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003176 overrides: ["unknownapex"],
Jiyong Park5d790c32019-11-15 18:40:32 +09003177 }
3178
3179 apex_key {
3180 name: "myapex.key",
3181 public_key: "testkey.avbpubkey",
3182 private_key: "testkey.pem",
3183 }
3184
3185 android_app {
3186 name: "app",
3187 srcs: ["foo/bar/MyClass.java"],
3188 package_name: "foo",
3189 sdk_version: "none",
3190 system_modules: "none",
3191 }
3192
3193 override_android_app {
3194 name: "override_app",
3195 base: "app",
3196 package_name: "bar",
3197 }
3198 `)
3199
Jiyong Park317645e2019-12-05 13:20:58 +09003200 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
3201 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
3202 if originalVariant.GetOverriddenBy() != "" {
3203 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
3204 }
3205 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
3206 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
3207 }
3208
Jiyong Park5d790c32019-11-15 18:40:32 +09003209 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
3210 apexRule := module.Rule("apexRule")
3211 copyCmds := apexRule.Args["copy_commands"]
3212
3213 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
3214 ensureContains(t, copyCmds, "image.apex/app/app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003215
3216 apexBundle := module.Module().(*apexBundle)
3217 name := apexBundle.Name()
3218 if name != "override_myapex" {
3219 t.Errorf("name should be \"override_myapex\", but was %q", name)
3220 }
3221
3222 data := android.AndroidMkDataForTest(t, config, "", apexBundle)
3223 var builder strings.Builder
3224 data.Custom(&builder, name, "TARGET_", "", data)
3225 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09003226 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003227 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
3228 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08003229 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003230 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09003231 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08003232 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
3233 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09003234}
3235
Jooyung Han214bf372019-11-12 13:03:50 +09003236func TestLegacyAndroid10Support(t *testing.T) {
3237 ctx, _ := testApex(t, `
3238 apex {
3239 name: "myapex",
3240 key: "myapex.key",
3241 legacy_android10_support: true,
3242 }
3243
3244 apex_key {
3245 name: "myapex.key",
3246 public_key: "testkey.avbpubkey",
3247 private_key: "testkey.pem",
3248 }
3249 `)
3250
3251 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
3252 args := module.Rule("apexRule").Args
3253 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
3254}
3255
Jooyung Han58f26ab2019-12-18 15:34:32 +09003256func TestJavaSDKLibrary(t *testing.T) {
3257 ctx, _ := testApex(t, `
3258 apex {
3259 name: "myapex",
3260 key: "myapex.key",
3261 java_libs: ["foo"],
3262 }
3263
3264 apex_key {
3265 name: "myapex.key",
3266 public_key: "testkey.avbpubkey",
3267 private_key: "testkey.pem",
3268 }
3269
3270 java_sdk_library {
3271 name: "foo",
3272 srcs: ["a.java"],
3273 api_packages: ["foo"],
3274 }
3275 `, withFiles(map[string][]byte{
3276 "api/current.txt": nil,
3277 "api/removed.txt": nil,
3278 "api/system-current.txt": nil,
3279 "api/system-removed.txt": nil,
3280 "api/test-current.txt": nil,
3281 "api/test-removed.txt": nil,
3282 }))
3283
3284 // java_sdk_library installs both impl jar and permission XML
3285 ensureExactContents(t, ctx, "myapex", []string{
3286 "javalib/foo.jar",
3287 "etc/permissions/foo.xml",
3288 })
3289 // Permission XML should point to the activated path of impl jar of java_sdk_library
3290 genXMLCommand := ctx.ModuleForTests("foo", "android_common_myapex").Output("foo.xml").RuleParams.Command
3291 ensureContains(t, genXMLCommand, `<library name="foo" file="/apex/myapex/javalib/foo.jar"`)
3292}
3293
Jiyong Park479321d2019-12-16 11:47:12 +09003294func TestRejectNonInstallableJavaLibrary(t *testing.T) {
3295 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
3296 apex {
3297 name: "myapex",
3298 key: "myapex.key",
3299 java_libs: ["myjar"],
3300 }
3301
3302 apex_key {
3303 name: "myapex.key",
3304 public_key: "testkey.avbpubkey",
3305 private_key: "testkey.pem",
3306 }
3307
3308 java_library {
3309 name: "myjar",
3310 srcs: ["foo/bar/MyClass.java"],
3311 sdk_version: "none",
3312 system_modules: "none",
3313 }
3314 `)
3315}
3316
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07003317func TestMain(m *testing.M) {
3318 run := func() int {
3319 setUp()
3320 defer tearDown()
3321
3322 return m.Run()
3323 }
3324
3325 os.Exit(run())
3326}