blob: 94cf19d0a18a80afc9e49c8198598957baed51a8 [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"
20 "strings"
21 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090022
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/cc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090027 "android/soong/java"
Jiyong Park25fc6a92018-11-18 18:02:45 +090028)
29
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070030var buildDir string
31
Jooyung Han5c998b92019-06-27 11:30:33 +090032func testApexError(t *testing.T, pattern, bp string) {
33 ctx, config := testApexContext(t, bp)
34 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
35 if len(errs) > 0 {
36 android.FailIfNoMatchingErrors(t, pattern, errs)
37 return
38 }
39 _, errs = ctx.PrepareBuildActions(config)
40 if len(errs) > 0 {
41 android.FailIfNoMatchingErrors(t, pattern, errs)
42 return
43 }
44
45 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
46}
47
Jiyong Park25fc6a92018-11-18 18:02:45 +090048func testApex(t *testing.T, bp string) *android.TestContext {
Jooyung Han5c998b92019-06-27 11:30:33 +090049 ctx, config := testApexContext(t, bp)
50 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
51 android.FailIfErrored(t, errs)
52 _, errs = ctx.PrepareBuildActions(config)
53 android.FailIfErrored(t, errs)
54 return ctx
55}
56
57func testApexContext(t *testing.T, bp string) (*android.TestContext, android.Config) {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070058 config := android.TestArchConfig(buildDir, nil)
59 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
60 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
61 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
62 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
63 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +090064
65 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080066 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
67 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090068 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090069 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070070 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090071 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090072
73 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
74 ctx.TopDown("apex_deps", apexDepsMutator)
75 ctx.BottomUp("apex", apexMutator)
Jooyung Han5c998b92019-06-27 11:30:33 +090076 ctx.BottomUp("apex_uses", apexUsesMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070077 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
78 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090079 })
80
81 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
82 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090083 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090084 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090085 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Roland Levillain630846d2019-06-26 12:48:34 +010086 ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc.TestFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090087 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090088 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090089 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090090 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090091 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090092 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070093 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
94 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
95 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090096 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090097 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090098 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090099 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100 ctx.BottomUp("version", cc.VersionMutator).Parallel()
101 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
102 })
103
104 ctx.Register()
105
106 bp = bp + `
107 toolchain_library {
108 name: "libcompiler_rt-extras",
109 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900110 vendor_available: true,
111 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900112 }
113
114 toolchain_library {
115 name: "libatomic",
116 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900117 vendor_available: true,
118 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900119 }
120
121 toolchain_library {
122 name: "libgcc",
123 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900124 vendor_available: true,
125 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900126 }
127
128 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700129 name: "libgcc_stripped",
130 src: "",
131 vendor_available: true,
132 recovery_available: true,
133 }
134
135 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900136 name: "libclang_rt.builtins-aarch64-android",
137 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900138 vendor_available: true,
139 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900140 }
141
142 toolchain_library {
143 name: "libclang_rt.builtins-arm-android",
144 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900145 vendor_available: true,
146 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900147 }
148
149 cc_object {
150 name: "crtbegin_so",
151 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900152 vendor_available: true,
153 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900154 }
155
156 cc_object {
157 name: "crtend_so",
158 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900159 vendor_available: true,
160 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900161 }
162
Alex Light3d673592019-01-18 14:37:31 -0800163 cc_object {
164 name: "crtbegin_static",
165 stl: "none",
166 }
167
168 cc_object {
169 name: "crtend_android",
170 stl: "none",
171 }
172
Jiyong Parkda6eb592018-12-19 17:12:36 +0900173 llndk_library {
174 name: "libc",
175 symbol_file: "",
176 }
177
178 llndk_library {
179 name: "libm",
180 symbol_file: "",
181 }
182
183 llndk_library {
184 name: "libdl",
185 symbol_file: "",
186 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900187 `
188
189 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900190 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700191 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900192 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900193 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900194 "system/sepolicy/apex/myapex-file_contexts": nil,
195 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
196 "system/sepolicy/apex/otherapex-file_contexts": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900197 "system/sepolicy/apex/commonapex-file_contexts": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900198 "mylib.cpp": nil,
Roland Levillain630846d2019-06-26 12:48:34 +0100199 "mytest.cpp": nil,
Jooyung Han5c998b92019-06-27 11:30:33 +0900200 "mylib_common.cpp": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900201 "myprebuilt": nil,
202 "my_include": nil,
203 "vendor/foo/devkeys/test.x509.pem": nil,
204 "vendor/foo/devkeys/test.pk8": nil,
205 "testkey.x509.pem": nil,
206 "testkey.pk8": nil,
207 "testkey.override.x509.pem": nil,
208 "testkey.override.pk8": nil,
209 "vendor/foo/devkeys/testkey.avbpubkey": nil,
210 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900211 "NOTICE": nil,
212 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900213 "testkey2.avbpubkey": nil,
214 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900215 "myapex-arm64.apex": nil,
216 "myapex-arm.apex": nil,
Jiyong Park71b519d2019-04-18 17:25:49 +0900217 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900218 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219
Jooyung Han5c998b92019-06-27 11:30:33 +0900220 return ctx, config
Jiyong Park25fc6a92018-11-18 18:02:45 +0900221}
222
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700223func setUp() {
224 var err error
225 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700227 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229}
230
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700231func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 os.RemoveAll(buildDir)
233}
234
235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
243// ensures that 'result' does not contain 'notExpected'
244func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900245 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246 if strings.Contains(result, notExpected) {
247 t.Errorf("%q is found in %q", notExpected, result)
248 }
249}
250
251func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900252 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900253 if !android.InList(expected, result) {
254 t.Errorf("%q is not found in %v", expected, result)
255 }
256}
257
258func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900259 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260 if android.InList(notExpected, result) {
261 t.Errorf("%q is found in %v", notExpected, result)
262 }
263}
264
265// Minimal test
266func TestBasicApex(t *testing.T) {
267 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900268 apex_defaults {
269 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900270 manifest: ":myapex.manifest",
271 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 key: "myapex.key",
273 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800274 multilib: {
275 both: {
276 binaries: ["foo",],
277 }
278 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 }
280
Jiyong Park30ca9372019-02-07 16:27:23 +0900281 apex {
282 name: "myapex",
283 defaults: ["myapex-defaults"],
284 }
285
Jiyong Park25fc6a92018-11-18 18:02:45 +0900286 apex_key {
287 name: "myapex.key",
288 public_key: "testkey.avbpubkey",
289 private_key: "testkey.pem",
290 }
291
Jiyong Park809bb722019-02-13 21:33:49 +0900292 filegroup {
293 name: "myapex.manifest",
294 srcs: ["apex_manifest.json"],
295 }
296
297 filegroup {
298 name: "myapex.androidmanifest",
299 srcs: ["AndroidManifest.xml"],
300 }
301
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 cc_library {
303 name: "mylib",
304 srcs: ["mylib.cpp"],
305 shared_libs: ["mylib2"],
306 system_shared_libs: [],
307 stl: "none",
308 }
309
Alex Light3d673592019-01-18 14:37:31 -0800310 cc_binary {
311 name: "foo",
312 srcs: ["mylib.cpp"],
313 compile_multilib: "both",
314 multilib: {
315 lib32: {
316 suffix: "32",
317 },
318 lib64: {
319 suffix: "64",
320 },
321 },
322 symlinks: ["foo_link_"],
323 symlink_preferred_arch: true,
324 system_shared_libs: [],
325 static_executable: true,
326 stl: "none",
327 }
328
Jiyong Park25fc6a92018-11-18 18:02:45 +0900329 cc_library {
330 name: "mylib2",
331 srcs: ["mylib.cpp"],
332 system_shared_libs: [],
333 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900334 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900335 }
336 `)
337
338 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900339
340 optFlags := apexRule.Args["opt_flags"]
341 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700342 // Ensure that the NOTICE output is being packaged as an asset.
343 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900344
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 copyCmds := apexRule.Args["copy_commands"]
346
347 // Ensure that main rule creates an output
348 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
349
350 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900351 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900352
353 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900354 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355
356 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800357 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
358 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800359
360 // Ensure that the platform variant ends with _core_shared
361 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
362 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800363
364 // Ensure that all symlinks are present.
365 found_foo_link_64 := false
366 found_foo := false
367 for _, cmd := range strings.Split(copyCmds, " && ") {
368 if strings.HasPrefix(cmd, "ln -s foo64") {
369 if strings.HasSuffix(cmd, "bin/foo") {
370 found_foo = true
371 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
372 found_foo_link_64 = true
373 }
374 }
375 }
376 good := found_foo && found_foo_link_64
377 if !good {
378 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
379 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900380
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700381 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
382 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700383 if len(noticeInputs) != 2 {
384 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900385 }
386 ensureListContains(t, noticeInputs, "NOTICE")
387 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800388}
389
390func TestBasicZipApex(t *testing.T) {
391 ctx := testApex(t, `
392 apex {
393 name: "myapex",
394 key: "myapex.key",
395 payload_type: "zip",
396 native_shared_libs: ["mylib"],
397 }
398
399 apex_key {
400 name: "myapex.key",
401 public_key: "testkey.avbpubkey",
402 private_key: "testkey.pem",
403 }
404
405 cc_library {
406 name: "mylib",
407 srcs: ["mylib.cpp"],
408 shared_libs: ["mylib2"],
409 system_shared_libs: [],
410 stl: "none",
411 }
412
413 cc_library {
414 name: "mylib2",
415 srcs: ["mylib.cpp"],
416 system_shared_libs: [],
417 stl: "none",
418 }
419 `)
420
421 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
422 copyCmds := zipApexRule.Args["copy_commands"]
423
424 // Ensure that main rule creates an output
425 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
426
427 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900428 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800429
430 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900431 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800432
433 // Ensure that both direct and indirect deps are copied into apex
434 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
435 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436}
437
438func TestApexWithStubs(t *testing.T) {
439 ctx := testApex(t, `
440 apex {
441 name: "myapex",
442 key: "myapex.key",
443 native_shared_libs: ["mylib", "mylib3"],
444 }
445
446 apex_key {
447 name: "myapex.key",
448 public_key: "testkey.avbpubkey",
449 private_key: "testkey.pem",
450 }
451
452 cc_library {
453 name: "mylib",
454 srcs: ["mylib.cpp"],
455 shared_libs: ["mylib2", "mylib3"],
456 system_shared_libs: [],
457 stl: "none",
458 }
459
460 cc_library {
461 name: "mylib2",
462 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900463 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900464 system_shared_libs: [],
465 stl: "none",
466 stubs: {
467 versions: ["1", "2", "3"],
468 },
469 }
470
471 cc_library {
472 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900473 srcs: ["mylib.cpp"],
474 shared_libs: ["mylib4"],
475 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 stl: "none",
477 stubs: {
478 versions: ["10", "11", "12"],
479 },
480 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900481
482 cc_library {
483 name: "mylib4",
484 srcs: ["mylib.cpp"],
485 system_shared_libs: [],
486 stl: "none",
487 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900488 `)
489
490 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
491 copyCmds := apexRule.Args["copy_commands"]
492
493 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800494 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900495
496 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800497 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900498
499 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800500 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900501
Jiyong Parkda6eb592018-12-19 17:12:36 +0900502 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900503
504 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900505 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900506 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900507 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900508
509 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900510 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900511 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900512 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900513
514 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900515 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900516 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900517
518 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900519 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_3_myapex").Rule("genStubSrc").Args["flags"])
Jiyong Park25fc6a92018-11-18 18:02:45 +0900520}
521
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900522func TestApexWithExplicitStubsDependency(t *testing.T) {
523 ctx := testApex(t, `
524 apex {
525 name: "myapex",
526 key: "myapex.key",
527 native_shared_libs: ["mylib"],
528 }
529
530 apex_key {
531 name: "myapex.key",
532 public_key: "testkey.avbpubkey",
533 private_key: "testkey.pem",
534 }
535
536 cc_library {
537 name: "mylib",
538 srcs: ["mylib.cpp"],
539 shared_libs: ["libfoo#10"],
540 system_shared_libs: [],
541 stl: "none",
542 }
543
544 cc_library {
545 name: "libfoo",
546 srcs: ["mylib.cpp"],
547 shared_libs: ["libbar"],
548 system_shared_libs: [],
549 stl: "none",
550 stubs: {
551 versions: ["10", "20", "30"],
552 },
553 }
554
555 cc_library {
556 name: "libbar",
557 srcs: ["mylib.cpp"],
558 system_shared_libs: [],
559 stl: "none",
560 }
561
562 `)
563
564 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
565 copyCmds := apexRule.Args["copy_commands"]
566
567 // Ensure that direct non-stubs dep is always included
568 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
569
570 // Ensure that indirect stubs dep is not included
571 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
572
573 // Ensure that dependency of stubs is not included
574 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
575
Jiyong Parkda6eb592018-12-19 17:12:36 +0900576 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900577
578 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900579 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900580 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900581 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900582
Jiyong Parkda6eb592018-12-19 17:12:36 +0900583 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900584
585 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
586 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
587}
588
Jiyong Park25fc6a92018-11-18 18:02:45 +0900589func TestApexWithSystemLibsStubs(t *testing.T) {
590 ctx := testApex(t, `
591 apex {
592 name: "myapex",
593 key: "myapex.key",
594 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
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: ["libdl#27"],
607 stl: "none",
608 }
609
610 cc_library_shared {
611 name: "mylib_shared",
612 srcs: ["mylib.cpp"],
613 shared_libs: ["libdl#27"],
614 stl: "none",
615 }
616
617 cc_library {
618 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700619 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900620 nocrt: true,
621 system_shared_libs: [],
622 stl: "none",
623 stubs: {
624 versions: ["27", "28", "29"],
625 },
626 }
627
628 cc_library {
629 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700630 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631 nocrt: true,
632 system_shared_libs: [],
633 stl: "none",
634 stubs: {
635 versions: ["27", "28", "29"],
636 },
637 }
638
639 cc_library {
640 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700641 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900642 nocrt: true,
643 system_shared_libs: [],
644 stl: "none",
645 stubs: {
646 versions: ["27", "28", "29"],
647 },
648 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900649
650 cc_library {
651 name: "libBootstrap",
652 srcs: ["mylib.cpp"],
653 stl: "none",
654 bootstrap: true,
655 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656 `)
657
658 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
659 copyCmds := apexRule.Args["copy_commands"]
660
661 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800662 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900663 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
664 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900665
666 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900667 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900668
Jiyong Parkda6eb592018-12-19 17:12:36 +0900669 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
670 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
671 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900672
673 // For dependency to libc
674 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900675 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900676 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900677 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900678 // ... Cflags from stub is correctly exported to mylib
679 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
680 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
681
682 // For dependency to libm
683 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900684 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900685 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900686 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900687 // ... and is not compiling with the stub
688 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
689 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
690
691 // For dependency to libdl
692 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900693 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900694 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900695 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
696 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900697 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900698 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900699 // ... Cflags from stub is correctly exported to mylib
700 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
701 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900702
703 // Ensure that libBootstrap is depending on the platform variant of bionic libs
704 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
705 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
706 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
707 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900708}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900709
710func TestFilesInSubDir(t *testing.T) {
711 ctx := testApex(t, `
712 apex {
713 name: "myapex",
714 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900715 native_shared_libs: ["mylib"],
716 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900717 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900718 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900719 }
720
721 apex_key {
722 name: "myapex.key",
723 public_key: "testkey.avbpubkey",
724 private_key: "testkey.pem",
725 }
726
727 prebuilt_etc {
728 name: "myetc",
729 src: "myprebuilt",
730 sub_dir: "foo/bar",
731 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900732
733 cc_library {
734 name: "mylib",
735 srcs: ["mylib.cpp"],
736 relative_install_path: "foo/bar",
737 system_shared_libs: [],
738 stl: "none",
739 }
740
741 cc_binary {
742 name: "mybin",
743 srcs: ["mylib.cpp"],
744 relative_install_path: "foo/bar",
745 system_shared_libs: [],
746 static_executable: true,
747 stl: "none",
748 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900749 `)
750
751 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
752 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
753
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900754 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900755 ensureListContains(t, dirs, "etc")
756 ensureListContains(t, dirs, "etc/foo")
757 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900758 ensureListContains(t, dirs, "lib64")
759 ensureListContains(t, dirs, "lib64/foo")
760 ensureListContains(t, dirs, "lib64/foo/bar")
761 ensureListContains(t, dirs, "lib")
762 ensureListContains(t, dirs, "lib/foo")
763 ensureListContains(t, dirs, "lib/foo/bar")
764
Jiyong Parkbd13e442019-03-15 18:10:35 +0900765 ensureListContains(t, dirs, "bin")
766 ensureListContains(t, dirs, "bin/foo")
767 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900768}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900769
770func TestUseVendor(t *testing.T) {
771 ctx := testApex(t, `
772 apex {
773 name: "myapex",
774 key: "myapex.key",
775 native_shared_libs: ["mylib"],
776 use_vendor: true,
777 }
778
779 apex_key {
780 name: "myapex.key",
781 public_key: "testkey.avbpubkey",
782 private_key: "testkey.pem",
783 }
784
785 cc_library {
786 name: "mylib",
787 srcs: ["mylib.cpp"],
788 shared_libs: ["mylib2"],
789 system_shared_libs: [],
790 vendor_available: true,
791 stl: "none",
792 }
793
794 cc_library {
795 name: "mylib2",
796 srcs: ["mylib.cpp"],
797 system_shared_libs: [],
798 vendor_available: true,
799 stl: "none",
800 }
801 `)
802
803 inputsList := []string{}
804 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
805 for _, implicit := range i.Implicits {
806 inputsList = append(inputsList, implicit.String())
807 }
808 }
809 inputsString := strings.Join(inputsList, " ")
810
811 // ensure that the apex includes vendor variants of the direct and indirect deps
812 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
813 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
814
815 // ensure that the apex does not include core variants
816 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
817 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
818}
Jiyong Park16e91a02018-12-20 18:18:08 +0900819
Jooyung Han5c998b92019-06-27 11:30:33 +0900820func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
821 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
822 apex {
823 name: "myapex",
824 key: "myapex.key",
825 native_shared_libs: ["mylib"],
826 use_vendor: true,
827 }
828
829 apex_key {
830 name: "myapex.key",
831 public_key: "testkey.avbpubkey",
832 private_key: "testkey.pem",
833 }
834
835 cc_library {
836 name: "mylib",
837 srcs: ["mylib.cpp"],
838 system_shared_libs: [],
839 stl: "none",
840 }
841 `)
842}
843
Jiyong Park16e91a02018-12-20 18:18:08 +0900844func TestStaticLinking(t *testing.T) {
845 ctx := testApex(t, `
846 apex {
847 name: "myapex",
848 key: "myapex.key",
849 native_shared_libs: ["mylib"],
850 }
851
852 apex_key {
853 name: "myapex.key",
854 public_key: "testkey.avbpubkey",
855 private_key: "testkey.pem",
856 }
857
858 cc_library {
859 name: "mylib",
860 srcs: ["mylib.cpp"],
861 system_shared_libs: [],
862 stl: "none",
863 stubs: {
864 versions: ["1", "2", "3"],
865 },
866 }
867
868 cc_binary {
869 name: "not_in_apex",
870 srcs: ["mylib.cpp"],
871 static_libs: ["mylib"],
872 static_executable: true,
873 system_shared_libs: [],
874 stl: "none",
875 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900876 `)
877
878 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
879
880 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800881 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900882}
Jiyong Park9335a262018-12-24 11:31:58 +0900883
884func TestKeys(t *testing.T) {
885 ctx := testApex(t, `
886 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900887 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900888 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900889 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900890 native_shared_libs: ["mylib"],
891 }
892
893 cc_library {
894 name: "mylib",
895 srcs: ["mylib.cpp"],
896 system_shared_libs: [],
897 stl: "none",
898 }
899
900 apex_key {
901 name: "myapex.key",
902 public_key: "testkey.avbpubkey",
903 private_key: "testkey.pem",
904 }
905
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900906 android_app_certificate {
907 name: "myapex.certificate",
908 certificate: "testkey",
909 }
910
911 android_app_certificate {
912 name: "myapex.certificate.override",
913 certificate: "testkey.override",
914 }
915
Jiyong Park9335a262018-12-24 11:31:58 +0900916 `)
917
918 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900919 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900920
921 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
922 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
923 "vendor/foo/devkeys/testkey.avbpubkey")
924 }
925 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
926 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
927 "vendor/foo/devkeys/testkey.pem")
928 }
929
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900930 // check the APK certs. It should be overridden to myapex.certificate.override
931 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
932 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900933 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900934 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900935 }
936}
Jiyong Park58e364a2019-01-19 19:24:06 +0900937
938func TestMacro(t *testing.T) {
939 ctx := testApex(t, `
940 apex {
941 name: "myapex",
942 key: "myapex.key",
943 native_shared_libs: ["mylib"],
944 }
945
946 apex {
947 name: "otherapex",
948 key: "myapex.key",
949 native_shared_libs: ["mylib"],
950 }
951
952 apex_key {
953 name: "myapex.key",
954 public_key: "testkey.avbpubkey",
955 private_key: "testkey.pem",
956 }
957
958 cc_library {
959 name: "mylib",
960 srcs: ["mylib.cpp"],
961 system_shared_libs: [],
962 stl: "none",
963 }
964 `)
965
966 // non-APEX variant does not have __ANDROID__APEX__ defined
967 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
968 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
969 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
970
971 // APEX variant has __ANDROID_APEX__=<apexname> defined
972 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
973 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
974 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
975
976 // APEX variant has __ANDROID_APEX__=<apexname> defined
977 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
978 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
979 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
980}
Jiyong Park7e636d02019-01-28 16:16:54 +0900981
982func TestHeaderLibsDependency(t *testing.T) {
983 ctx := testApex(t, `
984 apex {
985 name: "myapex",
986 key: "myapex.key",
987 native_shared_libs: ["mylib"],
988 }
989
990 apex_key {
991 name: "myapex.key",
992 public_key: "testkey.avbpubkey",
993 private_key: "testkey.pem",
994 }
995
996 cc_library_headers {
997 name: "mylib_headers",
998 export_include_dirs: ["my_include"],
999 system_shared_libs: [],
1000 stl: "none",
1001 }
1002
1003 cc_library {
1004 name: "mylib",
1005 srcs: ["mylib.cpp"],
1006 system_shared_libs: [],
1007 stl: "none",
1008 header_libs: ["mylib_headers"],
1009 export_header_lib_headers: ["mylib_headers"],
1010 stubs: {
1011 versions: ["1", "2", "3"],
1012 },
1013 }
1014
1015 cc_library {
1016 name: "otherlib",
1017 srcs: ["mylib.cpp"],
1018 system_shared_libs: [],
1019 stl: "none",
1020 shared_libs: ["mylib"],
1021 }
1022 `)
1023
1024 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
1025
1026 // Ensure that the include path of the header lib is exported to 'otherlib'
1027 ensureContains(t, cFlags, "-Imy_include")
1028}
Alex Light9670d332019-01-29 18:07:33 -08001029
Alex Light0851b882019-02-07 13:20:53 -08001030func TestNonTestApex(t *testing.T) {
1031 ctx := testApex(t, `
1032 apex {
1033 name: "myapex",
1034 key: "myapex.key",
1035 native_shared_libs: ["mylib_common"],
1036 }
1037
1038 apex_key {
1039 name: "myapex.key",
1040 public_key: "testkey.avbpubkey",
1041 private_key: "testkey.pem",
1042 }
1043
1044 cc_library {
1045 name: "mylib_common",
1046 srcs: ["mylib.cpp"],
1047 system_shared_libs: [],
1048 stl: "none",
1049 }
1050 `)
1051
1052 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1053 apexRule := module.Rule("apexRule")
1054 copyCmds := apexRule.Args["copy_commands"]
1055
1056 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1057 t.Log("Apex was a test apex!")
1058 t.Fail()
1059 }
1060 // Ensure that main rule creates an output
1061 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1062
1063 // Ensure that apex variant is created for the direct dep
1064 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1065
1066 // Ensure that both direct and indirect deps are copied into apex
1067 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1068
1069 // Ensure that the platform variant ends with _core_shared
1070 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1071
1072 if !android.InAnyApex("mylib_common") {
1073 t.Log("Found mylib_common not in any apex!")
1074 t.Fail()
1075 }
1076}
1077
1078func TestTestApex(t *testing.T) {
1079 if android.InAnyApex("mylib_common_test") {
1080 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!")
1081 }
1082 ctx := testApex(t, `
1083 apex_test {
1084 name: "myapex",
1085 key: "myapex.key",
1086 native_shared_libs: ["mylib_common_test"],
1087 }
1088
1089 apex_key {
1090 name: "myapex.key",
1091 public_key: "testkey.avbpubkey",
1092 private_key: "testkey.pem",
1093 }
1094
1095 cc_library {
1096 name: "mylib_common_test",
1097 srcs: ["mylib.cpp"],
1098 system_shared_libs: [],
1099 stl: "none",
1100 }
1101 `)
1102
1103 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1104 apexRule := module.Rule("apexRule")
1105 copyCmds := apexRule.Args["copy_commands"]
1106
1107 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1108 t.Log("Apex was not a test apex!")
1109 t.Fail()
1110 }
1111 // Ensure that main rule creates an output
1112 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1113
1114 // Ensure that apex variant is created for the direct dep
1115 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1116
1117 // Ensure that both direct and indirect deps are copied into apex
1118 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1119
1120 // Ensure that the platform variant ends with _core_shared
1121 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1122
1123 if android.InAnyApex("mylib_common_test") {
1124 t.Log("Found mylib_common_test in some apex!")
1125 t.Fail()
1126 }
1127}
1128
Alex Light9670d332019-01-29 18:07:33 -08001129func TestApexWithTarget(t *testing.T) {
1130 ctx := testApex(t, `
1131 apex {
1132 name: "myapex",
1133 key: "myapex.key",
1134 multilib: {
1135 first: {
1136 native_shared_libs: ["mylib_common"],
1137 }
1138 },
1139 target: {
1140 android: {
1141 multilib: {
1142 first: {
1143 native_shared_libs: ["mylib"],
1144 }
1145 }
1146 },
1147 host: {
1148 multilib: {
1149 first: {
1150 native_shared_libs: ["mylib2"],
1151 }
1152 }
1153 }
1154 }
1155 }
1156
1157 apex_key {
1158 name: "myapex.key",
1159 public_key: "testkey.avbpubkey",
1160 private_key: "testkey.pem",
1161 }
1162
1163 cc_library {
1164 name: "mylib",
1165 srcs: ["mylib.cpp"],
1166 system_shared_libs: [],
1167 stl: "none",
1168 }
1169
1170 cc_library {
1171 name: "mylib_common",
1172 srcs: ["mylib.cpp"],
1173 system_shared_libs: [],
1174 stl: "none",
1175 compile_multilib: "first",
1176 }
1177
1178 cc_library {
1179 name: "mylib2",
1180 srcs: ["mylib.cpp"],
1181 system_shared_libs: [],
1182 stl: "none",
1183 compile_multilib: "first",
1184 }
1185 `)
1186
1187 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1188 copyCmds := apexRule.Args["copy_commands"]
1189
1190 // Ensure that main rule creates an output
1191 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1192
1193 // Ensure that apex variant is created for the direct dep
1194 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1195 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1196 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1197
1198 // Ensure that both direct and indirect deps are copied into apex
1199 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1200 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1201 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1202
1203 // Ensure that the platform variant ends with _core_shared
1204 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1205 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1206 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1207}
Jiyong Park04480cf2019-02-06 00:16:29 +09001208
1209func TestApexWithShBinary(t *testing.T) {
1210 ctx := testApex(t, `
1211 apex {
1212 name: "myapex",
1213 key: "myapex.key",
1214 binaries: ["myscript"],
1215 }
1216
1217 apex_key {
1218 name: "myapex.key",
1219 public_key: "testkey.avbpubkey",
1220 private_key: "testkey.pem",
1221 }
1222
1223 sh_binary {
1224 name: "myscript",
1225 src: "mylib.cpp",
1226 filename: "myscript.sh",
1227 sub_dir: "script",
1228 }
1229 `)
1230
1231 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1232 copyCmds := apexRule.Args["copy_commands"]
1233
1234 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1235}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001236
1237func TestApexInProductPartition(t *testing.T) {
1238 ctx := testApex(t, `
1239 apex {
1240 name: "myapex",
1241 key: "myapex.key",
1242 native_shared_libs: ["mylib"],
1243 product_specific: true,
1244 }
1245
1246 apex_key {
1247 name: "myapex.key",
1248 public_key: "testkey.avbpubkey",
1249 private_key: "testkey.pem",
1250 product_specific: true,
1251 }
1252
1253 cc_library {
1254 name: "mylib",
1255 srcs: ["mylib.cpp"],
1256 system_shared_libs: [],
1257 stl: "none",
1258 }
1259 `)
1260
1261 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1262 expected := "target/product/test_device/product/apex"
1263 actual := apex.installDir.RelPathString()
1264 if actual != expected {
1265 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1266 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001267}
Jiyong Park67882562019-03-21 01:11:21 +09001268
1269func TestApexKeyFromOtherModule(t *testing.T) {
1270 ctx := testApex(t, `
1271 apex_key {
1272 name: "myapex.key",
1273 public_key: ":my.avbpubkey",
1274 private_key: ":my.pem",
1275 product_specific: true,
1276 }
1277
1278 filegroup {
1279 name: "my.avbpubkey",
1280 srcs: ["testkey2.avbpubkey"],
1281 }
1282
1283 filegroup {
1284 name: "my.pem",
1285 srcs: ["testkey2.pem"],
1286 }
1287 `)
1288
1289 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1290 expected_pubkey := "testkey2.avbpubkey"
1291 actual_pubkey := apex_key.public_key_file.String()
1292 if actual_pubkey != expected_pubkey {
1293 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1294 }
1295 expected_privkey := "testkey2.pem"
1296 actual_privkey := apex_key.private_key_file.String()
1297 if actual_privkey != expected_privkey {
1298 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1299 }
1300}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001301
1302func TestPrebuilt(t *testing.T) {
1303 ctx := testApex(t, `
1304 prebuilt_apex {
1305 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001306 arch: {
1307 arm64: {
1308 src: "myapex-arm64.apex",
1309 },
1310 arm: {
1311 src: "myapex-arm.apex",
1312 },
1313 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001314 }
1315 `)
1316
1317 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1318
Jiyong Parkc95714e2019-03-29 14:23:10 +09001319 expectedInput := "myapex-arm64.apex"
1320 if prebuilt.inputApex.String() != expectedInput {
1321 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1322 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001323}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001324
1325func TestPrebuiltFilenameOverride(t *testing.T) {
1326 ctx := testApex(t, `
1327 prebuilt_apex {
1328 name: "myapex",
1329 src: "myapex-arm.apex",
1330 filename: "notmyapex.apex",
1331 }
1332 `)
1333
1334 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1335
1336 expected := "notmyapex.apex"
1337 if p.installFilename != expected {
1338 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1339 }
1340}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001341
Roland Levillain630846d2019-06-26 12:48:34 +01001342func TestApexWithTests(t *testing.T) {
1343 ctx := testApex(t, `
1344 apex_test {
1345 name: "myapex",
1346 key: "myapex.key",
1347 tests: [
1348 "mytest",
1349 ],
1350 }
1351
1352 apex_key {
1353 name: "myapex.key",
1354 public_key: "testkey.avbpubkey",
1355 private_key: "testkey.pem",
1356 }
1357
1358 cc_test {
1359 name: "mytest",
1360 gtest: false,
1361 srcs: ["mytest.cpp"],
1362 relative_install_path: "test",
1363 system_shared_libs: [],
1364 static_executable: true,
1365 stl: "none",
1366 }
1367 `)
1368
1369 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1370 copyCmds := apexRule.Args["copy_commands"]
1371
1372 // Ensure that test dep is copied into apex.
1373 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
1374}
1375
Jooyung Han5c998b92019-06-27 11:30:33 +09001376func TestApexUsesOtherApex(t *testing.T) {
1377 ctx := testApex(t, `
1378 apex {
1379 name: "myapex",
1380 key: "myapex.key",
1381 native_shared_libs: ["mylib"],
1382 uses: ["commonapex"],
1383 }
1384
1385 apex {
1386 name: "commonapex",
1387 key: "myapex.key",
1388 native_shared_libs: ["libcommon"],
1389 provide_cpp_shared_libs: true,
1390 }
1391
1392 apex_key {
1393 name: "myapex.key",
1394 public_key: "testkey.avbpubkey",
1395 private_key: "testkey.pem",
1396 }
1397
1398 cc_library {
1399 name: "mylib",
1400 srcs: ["mylib.cpp"],
1401 shared_libs: ["libcommon"],
1402 system_shared_libs: [],
1403 stl: "none",
1404 }
1405
1406 cc_library {
1407 name: "libcommon",
1408 srcs: ["mylib_common.cpp"],
1409 system_shared_libs: [],
1410 stl: "none",
1411 }
1412 `)
1413
1414 module1 := ctx.ModuleForTests("myapex", "android_common_myapex")
1415 apexRule1 := module1.Rule("apexRule")
1416 copyCmds1 := apexRule1.Args["copy_commands"]
1417
1418 module2 := ctx.ModuleForTests("commonapex", "android_common_commonapex")
1419 apexRule2 := module2.Rule("apexRule")
1420 copyCmds2 := apexRule2.Args["copy_commands"]
1421
1422 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1423 ensureListContains(t, ctx.ModuleVariantsForTests("libcommon"), "android_arm64_armv8-a_core_shared_commonapex")
1424 ensureContains(t, copyCmds1, "image.apex/lib64/mylib.so")
1425 ensureContains(t, copyCmds2, "image.apex/lib64/libcommon.so")
1426 ensureNotContains(t, copyCmds1, "image.apex/lib64/libcommon.so")
1427}
1428
1429func TestApexUsesFailsIfNotProvided(t *testing.T) {
1430 testApexError(t, `uses: "commonapex" does not provide native_shared_libs`, `
1431 apex {
1432 name: "myapex",
1433 key: "myapex.key",
1434 uses: ["commonapex"],
1435 }
1436
1437 apex {
1438 name: "commonapex",
1439 key: "myapex.key",
1440 }
1441
1442 apex_key {
1443 name: "myapex.key",
1444 public_key: "testkey.avbpubkey",
1445 private_key: "testkey.pem",
1446 }
1447 `)
1448 testApexError(t, `uses: "commonapex" is not a provider`, `
1449 apex {
1450 name: "myapex",
1451 key: "myapex.key",
1452 uses: ["commonapex"],
1453 }
1454
1455 cc_library {
1456 name: "commonapex",
1457 system_shared_libs: [],
1458 stl: "none",
1459 }
1460
1461 apex_key {
1462 name: "myapex.key",
1463 public_key: "testkey.avbpubkey",
1464 private_key: "testkey.pem",
1465 }
1466 `)
1467}
1468
1469func TestApexUsesFailsIfUseVenderMismatch(t *testing.T) {
1470 testApexError(t, `use_vendor: "commonapex" has different value of use_vendor`, `
1471 apex {
1472 name: "myapex",
1473 key: "myapex.key",
1474 use_vendor: true,
1475 uses: ["commonapex"],
1476 }
1477
1478 apex {
1479 name: "commonapex",
1480 key: "myapex.key",
1481 provide_cpp_shared_libs: true,
1482 }
1483
1484 apex_key {
1485 name: "myapex.key",
1486 public_key: "testkey.avbpubkey",
1487 private_key: "testkey.pem",
1488 }
1489 `)
1490}
1491
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001492func TestMain(m *testing.M) {
1493 run := func() int {
1494 setUp()
1495 defer tearDown()
1496
1497 return m.Run()
1498 }
1499
1500 os.Exit(run())
1501}