blob: b410425ffdcd3cb848ac7fa879420056ad04bcbf [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
Jiyong Park25fc6a92018-11-18 18:02:45 +090032func testApex(t *testing.T, bp string) *android.TestContext {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -070033 config := android.TestArchConfig(buildDir, nil)
34 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
35 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
36 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
37 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
38 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +090039
40 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080041 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
42 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090043 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090044 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070045 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090046 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090047
48 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
49 ctx.TopDown("apex_deps", apexDepsMutator)
50 ctx.BottomUp("apex", apexMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070051 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
52 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090053 })
54
55 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
56 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090057 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090058 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090059 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Roland Levillain630846d2019-06-26 12:48:34 +010060 ctx.RegisterModuleType("cc_test", android.ModuleFactoryAdaptor(cc.TestFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090061 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090062 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090063 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090064 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090065 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090066 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070067 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
68 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
69 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090070 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090071 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090072 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090073 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090074 ctx.BottomUp("version", cc.VersionMutator).Parallel()
75 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
76 })
77
78 ctx.Register()
79
80 bp = bp + `
81 toolchain_library {
82 name: "libcompiler_rt-extras",
83 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090084 vendor_available: true,
85 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090086 }
87
88 toolchain_library {
89 name: "libatomic",
90 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090091 vendor_available: true,
92 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090093 }
94
95 toolchain_library {
96 name: "libgcc",
97 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090098 vendor_available: true,
99 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900100 }
101
102 toolchain_library {
Yi Kongacee27c2019-03-29 20:05:14 -0700103 name: "libgcc_stripped",
104 src: "",
105 vendor_available: true,
106 recovery_available: true,
107 }
108
109 toolchain_library {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900110 name: "libclang_rt.builtins-aarch64-android",
111 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900112 vendor_available: true,
113 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900114 }
115
116 toolchain_library {
117 name: "libclang_rt.builtins-arm-android",
118 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900119 vendor_available: true,
120 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900121 }
122
123 cc_object {
124 name: "crtbegin_so",
125 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900126 vendor_available: true,
127 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900128 }
129
130 cc_object {
131 name: "crtend_so",
132 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900133 vendor_available: true,
134 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900135 }
136
Alex Light3d673592019-01-18 14:37:31 -0800137 cc_object {
138 name: "crtbegin_static",
139 stl: "none",
140 }
141
142 cc_object {
143 name: "crtend_android",
144 stl: "none",
145 }
146
Jiyong Parkda6eb592018-12-19 17:12:36 +0900147 llndk_library {
148 name: "libc",
149 symbol_file: "",
150 }
151
152 llndk_library {
153 name: "libm",
154 symbol_file: "",
155 }
156
157 llndk_library {
158 name: "libdl",
159 symbol_file: "",
160 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900161 `
162
163 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900164 "Android.bp": []byte(bp),
Dan Willemsen412160e2019-04-09 21:36:26 -0700165 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900166 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900167 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900168 "system/sepolicy/apex/myapex-file_contexts": nil,
169 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
170 "system/sepolicy/apex/otherapex-file_contexts": nil,
171 "mylib.cpp": nil,
Roland Levillain630846d2019-06-26 12:48:34 +0100172 "mytest.cpp": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900173 "myprebuilt": nil,
174 "my_include": nil,
175 "vendor/foo/devkeys/test.x509.pem": nil,
176 "vendor/foo/devkeys/test.pk8": nil,
177 "testkey.x509.pem": nil,
178 "testkey.pk8": nil,
179 "testkey.override.x509.pem": nil,
180 "testkey.override.pk8": nil,
181 "vendor/foo/devkeys/testkey.avbpubkey": nil,
182 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900183 "NOTICE": nil,
184 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900185 "testkey2.avbpubkey": nil,
186 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900187 "myapex-arm64.apex": nil,
188 "myapex-arm.apex": nil,
Jiyong Park71b519d2019-04-18 17:25:49 +0900189 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900190 })
191 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
192 android.FailIfErrored(t, errs)
193 _, errs = ctx.PrepareBuildActions(config)
194 android.FailIfErrored(t, errs)
195
196 return ctx
197}
198
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700199func setUp() {
200 var err error
201 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900202 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700203 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900204 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900205}
206
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700207func tearDown() {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900208 os.RemoveAll(buildDir)
209}
210
211// ensure that 'result' contains 'expected'
212func ensureContains(t *testing.T, result string, expected string) {
213 if !strings.Contains(result, expected) {
214 t.Errorf("%q is not found in %q", expected, result)
215 }
216}
217
218// ensures that 'result' does not contain 'notExpected'
219func ensureNotContains(t *testing.T, result string, notExpected string) {
220 if strings.Contains(result, notExpected) {
221 t.Errorf("%q is found in %q", notExpected, result)
222 }
223}
224
225func ensureListContains(t *testing.T, result []string, expected string) {
226 if !android.InList(expected, result) {
227 t.Errorf("%q is not found in %v", expected, result)
228 }
229}
230
231func ensureListNotContains(t *testing.T, result []string, notExpected string) {
232 if android.InList(notExpected, result) {
233 t.Errorf("%q is found in %v", notExpected, result)
234 }
235}
236
237// Minimal test
238func TestBasicApex(t *testing.T) {
239 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900240 apex_defaults {
241 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900242 manifest: ":myapex.manifest",
243 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 key: "myapex.key",
245 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800246 multilib: {
247 both: {
248 binaries: ["foo",],
249 }
250 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900251 }
252
Jiyong Park30ca9372019-02-07 16:27:23 +0900253 apex {
254 name: "myapex",
255 defaults: ["myapex-defaults"],
256 }
257
Jiyong Park25fc6a92018-11-18 18:02:45 +0900258 apex_key {
259 name: "myapex.key",
260 public_key: "testkey.avbpubkey",
261 private_key: "testkey.pem",
262 }
263
Jiyong Park809bb722019-02-13 21:33:49 +0900264 filegroup {
265 name: "myapex.manifest",
266 srcs: ["apex_manifest.json"],
267 }
268
269 filegroup {
270 name: "myapex.androidmanifest",
271 srcs: ["AndroidManifest.xml"],
272 }
273
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274 cc_library {
275 name: "mylib",
276 srcs: ["mylib.cpp"],
277 shared_libs: ["mylib2"],
278 system_shared_libs: [],
279 stl: "none",
280 }
281
Alex Light3d673592019-01-18 14:37:31 -0800282 cc_binary {
283 name: "foo",
284 srcs: ["mylib.cpp"],
285 compile_multilib: "both",
286 multilib: {
287 lib32: {
288 suffix: "32",
289 },
290 lib64: {
291 suffix: "64",
292 },
293 },
294 symlinks: ["foo_link_"],
295 symlink_preferred_arch: true,
296 system_shared_libs: [],
297 static_executable: true,
298 stl: "none",
299 }
300
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301 cc_library {
302 name: "mylib2",
303 srcs: ["mylib.cpp"],
304 system_shared_libs: [],
305 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900306 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307 }
308 `)
309
310 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900311
312 optFlags := apexRule.Args["opt_flags"]
313 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700314 // Ensure that the NOTICE output is being packaged as an asset.
315 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900316
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 copyCmds := apexRule.Args["copy_commands"]
318
319 // Ensure that main rule creates an output
320 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
321
322 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324
325 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900326 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327
328 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800329 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
330 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800331
332 // Ensure that the platform variant ends with _core_shared
333 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
334 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800335
336 // Ensure that all symlinks are present.
337 found_foo_link_64 := false
338 found_foo := false
339 for _, cmd := range strings.Split(copyCmds, " && ") {
340 if strings.HasPrefix(cmd, "ln -s foo64") {
341 if strings.HasSuffix(cmd, "bin/foo") {
342 found_foo = true
343 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
344 found_foo_link_64 = true
345 }
346 }
347 }
348 good := found_foo && found_foo_link_64
349 if !good {
350 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
351 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900352
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700353 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("mergeNoticesRule")
354 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700355 if len(noticeInputs) != 2 {
356 t.Errorf("number of input notice files: expected = 2, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900357 }
358 ensureListContains(t, noticeInputs, "NOTICE")
359 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800360}
361
362func TestBasicZipApex(t *testing.T) {
363 ctx := testApex(t, `
364 apex {
365 name: "myapex",
366 key: "myapex.key",
367 payload_type: "zip",
368 native_shared_libs: ["mylib"],
369 }
370
371 apex_key {
372 name: "myapex.key",
373 public_key: "testkey.avbpubkey",
374 private_key: "testkey.pem",
375 }
376
377 cc_library {
378 name: "mylib",
379 srcs: ["mylib.cpp"],
380 shared_libs: ["mylib2"],
381 system_shared_libs: [],
382 stl: "none",
383 }
384
385 cc_library {
386 name: "mylib2",
387 srcs: ["mylib.cpp"],
388 system_shared_libs: [],
389 stl: "none",
390 }
391 `)
392
393 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
394 copyCmds := zipApexRule.Args["copy_commands"]
395
396 // Ensure that main rule creates an output
397 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
398
399 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900400 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800401
402 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900403 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800404
405 // Ensure that both direct and indirect deps are copied into apex
406 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
407 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900408}
409
410func TestApexWithStubs(t *testing.T) {
411 ctx := testApex(t, `
412 apex {
413 name: "myapex",
414 key: "myapex.key",
415 native_shared_libs: ["mylib", "mylib3"],
416 }
417
418 apex_key {
419 name: "myapex.key",
420 public_key: "testkey.avbpubkey",
421 private_key: "testkey.pem",
422 }
423
424 cc_library {
425 name: "mylib",
426 srcs: ["mylib.cpp"],
427 shared_libs: ["mylib2", "mylib3"],
428 system_shared_libs: [],
429 stl: "none",
430 }
431
432 cc_library {
433 name: "mylib2",
434 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900435 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 system_shared_libs: [],
437 stl: "none",
438 stubs: {
439 versions: ["1", "2", "3"],
440 },
441 }
442
443 cc_library {
444 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900445 srcs: ["mylib.cpp"],
446 shared_libs: ["mylib4"],
447 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900448 stl: "none",
449 stubs: {
450 versions: ["10", "11", "12"],
451 },
452 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900453
454 cc_library {
455 name: "mylib4",
456 srcs: ["mylib.cpp"],
457 system_shared_libs: [],
458 stl: "none",
459 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900460 `)
461
462 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
463 copyCmds := apexRule.Args["copy_commands"]
464
465 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800466 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467
468 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800469 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470
471 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800472 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900473
Jiyong Parkda6eb592018-12-19 17:12:36 +0900474 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900475
476 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900477 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900478 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900479 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900480
481 // 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 +0900482 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900484 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900485
486 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900487 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900488 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900489
490 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900491 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 +0900492}
493
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900494func TestApexWithExplicitStubsDependency(t *testing.T) {
495 ctx := testApex(t, `
496 apex {
497 name: "myapex",
498 key: "myapex.key",
499 native_shared_libs: ["mylib"],
500 }
501
502 apex_key {
503 name: "myapex.key",
504 public_key: "testkey.avbpubkey",
505 private_key: "testkey.pem",
506 }
507
508 cc_library {
509 name: "mylib",
510 srcs: ["mylib.cpp"],
511 shared_libs: ["libfoo#10"],
512 system_shared_libs: [],
513 stl: "none",
514 }
515
516 cc_library {
517 name: "libfoo",
518 srcs: ["mylib.cpp"],
519 shared_libs: ["libbar"],
520 system_shared_libs: [],
521 stl: "none",
522 stubs: {
523 versions: ["10", "20", "30"],
524 },
525 }
526
527 cc_library {
528 name: "libbar",
529 srcs: ["mylib.cpp"],
530 system_shared_libs: [],
531 stl: "none",
532 }
533
534 `)
535
536 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that direct non-stubs dep is always included
540 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
541
542 // Ensure that indirect stubs dep is not included
543 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
544
545 // Ensure that dependency of stubs is not included
546 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
547
Jiyong Parkda6eb592018-12-19 17:12:36 +0900548 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900549
550 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900551 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900552 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900553 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900554
Jiyong Parkda6eb592018-12-19 17:12:36 +0900555 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900556
557 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
558 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
559}
560
Jiyong Park25fc6a92018-11-18 18:02:45 +0900561func TestApexWithSystemLibsStubs(t *testing.T) {
562 ctx := testApex(t, `
563 apex {
564 name: "myapex",
565 key: "myapex.key",
566 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
567 }
568
569 apex_key {
570 name: "myapex.key",
571 public_key: "testkey.avbpubkey",
572 private_key: "testkey.pem",
573 }
574
575 cc_library {
576 name: "mylib",
577 srcs: ["mylib.cpp"],
578 shared_libs: ["libdl#27"],
579 stl: "none",
580 }
581
582 cc_library_shared {
583 name: "mylib_shared",
584 srcs: ["mylib.cpp"],
585 shared_libs: ["libdl#27"],
586 stl: "none",
587 }
588
589 cc_library {
590 name: "libc",
Yi Konge7fe9912019-06-02 00:53:50 -0700591 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900592 nocrt: true,
593 system_shared_libs: [],
594 stl: "none",
595 stubs: {
596 versions: ["27", "28", "29"],
597 },
598 }
599
600 cc_library {
601 name: "libm",
Yi Konge7fe9912019-06-02 00:53:50 -0700602 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900603 nocrt: true,
604 system_shared_libs: [],
605 stl: "none",
606 stubs: {
607 versions: ["27", "28", "29"],
608 },
609 }
610
611 cc_library {
612 name: "libdl",
Yi Konge7fe9912019-06-02 00:53:50 -0700613 no_libcrt: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900614 nocrt: true,
615 system_shared_libs: [],
616 stl: "none",
617 stubs: {
618 versions: ["27", "28", "29"],
619 },
620 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900621
622 cc_library {
623 name: "libBootstrap",
624 srcs: ["mylib.cpp"],
625 stl: "none",
626 bootstrap: true,
627 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900628 `)
629
630 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
631 copyCmds := apexRule.Args["copy_commands"]
632
633 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800634 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900635 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
636 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637
638 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900639 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900640
Jiyong Parkda6eb592018-12-19 17:12:36 +0900641 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
642 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
643 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900644
645 // For dependency to libc
646 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900647 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900648 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900649 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900650 // ... Cflags from stub is correctly exported to mylib
651 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
652 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
653
654 // For dependency to libm
655 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900656 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900657 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900658 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900659 // ... and is not compiling with the stub
660 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
661 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
662
663 // For dependency to libdl
664 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900665 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900666 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900667 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
668 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900669 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900670 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900671 // ... Cflags from stub is correctly exported to mylib
672 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
673 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900674
675 // Ensure that libBootstrap is depending on the platform variant of bionic libs
676 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
677 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
678 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
679 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900680}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900681
682func TestFilesInSubDir(t *testing.T) {
683 ctx := testApex(t, `
684 apex {
685 name: "myapex",
686 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900687 native_shared_libs: ["mylib"],
688 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900689 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900690 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900691 }
692
693 apex_key {
694 name: "myapex.key",
695 public_key: "testkey.avbpubkey",
696 private_key: "testkey.pem",
697 }
698
699 prebuilt_etc {
700 name: "myetc",
701 src: "myprebuilt",
702 sub_dir: "foo/bar",
703 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900704
705 cc_library {
706 name: "mylib",
707 srcs: ["mylib.cpp"],
708 relative_install_path: "foo/bar",
709 system_shared_libs: [],
710 stl: "none",
711 }
712
713 cc_binary {
714 name: "mybin",
715 srcs: ["mylib.cpp"],
716 relative_install_path: "foo/bar",
717 system_shared_libs: [],
718 static_executable: true,
719 stl: "none",
720 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900721 `)
722
723 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
724 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
725
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900726 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900727 ensureListContains(t, dirs, "etc")
728 ensureListContains(t, dirs, "etc/foo")
729 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900730 ensureListContains(t, dirs, "lib64")
731 ensureListContains(t, dirs, "lib64/foo")
732 ensureListContains(t, dirs, "lib64/foo/bar")
733 ensureListContains(t, dirs, "lib")
734 ensureListContains(t, dirs, "lib/foo")
735 ensureListContains(t, dirs, "lib/foo/bar")
736
Jiyong Parkbd13e442019-03-15 18:10:35 +0900737 ensureListContains(t, dirs, "bin")
738 ensureListContains(t, dirs, "bin/foo")
739 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900740}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900741
742func TestUseVendor(t *testing.T) {
743 ctx := testApex(t, `
744 apex {
745 name: "myapex",
746 key: "myapex.key",
747 native_shared_libs: ["mylib"],
748 use_vendor: true,
749 }
750
751 apex_key {
752 name: "myapex.key",
753 public_key: "testkey.avbpubkey",
754 private_key: "testkey.pem",
755 }
756
757 cc_library {
758 name: "mylib",
759 srcs: ["mylib.cpp"],
760 shared_libs: ["mylib2"],
761 system_shared_libs: [],
762 vendor_available: true,
763 stl: "none",
764 }
765
766 cc_library {
767 name: "mylib2",
768 srcs: ["mylib.cpp"],
769 system_shared_libs: [],
770 vendor_available: true,
771 stl: "none",
772 }
773 `)
774
775 inputsList := []string{}
776 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
777 for _, implicit := range i.Implicits {
778 inputsList = append(inputsList, implicit.String())
779 }
780 }
781 inputsString := strings.Join(inputsList, " ")
782
783 // ensure that the apex includes vendor variants of the direct and indirect deps
784 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
785 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
786
787 // ensure that the apex does not include core variants
788 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
789 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
790}
Jiyong Park16e91a02018-12-20 18:18:08 +0900791
792func TestStaticLinking(t *testing.T) {
793 ctx := testApex(t, `
794 apex {
795 name: "myapex",
796 key: "myapex.key",
797 native_shared_libs: ["mylib"],
798 }
799
800 apex_key {
801 name: "myapex.key",
802 public_key: "testkey.avbpubkey",
803 private_key: "testkey.pem",
804 }
805
806 cc_library {
807 name: "mylib",
808 srcs: ["mylib.cpp"],
809 system_shared_libs: [],
810 stl: "none",
811 stubs: {
812 versions: ["1", "2", "3"],
813 },
814 }
815
816 cc_binary {
817 name: "not_in_apex",
818 srcs: ["mylib.cpp"],
819 static_libs: ["mylib"],
820 static_executable: true,
821 system_shared_libs: [],
822 stl: "none",
823 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900824 `)
825
826 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
827
828 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800829 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900830}
Jiyong Park9335a262018-12-24 11:31:58 +0900831
832func TestKeys(t *testing.T) {
833 ctx := testApex(t, `
834 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900835 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900836 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900837 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900838 native_shared_libs: ["mylib"],
839 }
840
841 cc_library {
842 name: "mylib",
843 srcs: ["mylib.cpp"],
844 system_shared_libs: [],
845 stl: "none",
846 }
847
848 apex_key {
849 name: "myapex.key",
850 public_key: "testkey.avbpubkey",
851 private_key: "testkey.pem",
852 }
853
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900854 android_app_certificate {
855 name: "myapex.certificate",
856 certificate: "testkey",
857 }
858
859 android_app_certificate {
860 name: "myapex.certificate.override",
861 certificate: "testkey.override",
862 }
863
Jiyong Park9335a262018-12-24 11:31:58 +0900864 `)
865
866 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900867 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900868
869 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
870 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
871 "vendor/foo/devkeys/testkey.avbpubkey")
872 }
873 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
874 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
875 "vendor/foo/devkeys/testkey.pem")
876 }
877
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900878 // check the APK certs. It should be overridden to myapex.certificate.override
879 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
880 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900881 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900882 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900883 }
884}
Jiyong Park58e364a2019-01-19 19:24:06 +0900885
886func TestMacro(t *testing.T) {
887 ctx := testApex(t, `
888 apex {
889 name: "myapex",
890 key: "myapex.key",
891 native_shared_libs: ["mylib"],
892 }
893
894 apex {
895 name: "otherapex",
896 key: "myapex.key",
897 native_shared_libs: ["mylib"],
898 }
899
900 apex_key {
901 name: "myapex.key",
902 public_key: "testkey.avbpubkey",
903 private_key: "testkey.pem",
904 }
905
906 cc_library {
907 name: "mylib",
908 srcs: ["mylib.cpp"],
909 system_shared_libs: [],
910 stl: "none",
911 }
912 `)
913
914 // non-APEX variant does not have __ANDROID__APEX__ defined
915 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
916 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
917 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
918
919 // APEX variant has __ANDROID_APEX__=<apexname> defined
920 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
921 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
922 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
923
924 // APEX variant has __ANDROID_APEX__=<apexname> defined
925 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
926 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
927 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
928}
Jiyong Park7e636d02019-01-28 16:16:54 +0900929
930func TestHeaderLibsDependency(t *testing.T) {
931 ctx := testApex(t, `
932 apex {
933 name: "myapex",
934 key: "myapex.key",
935 native_shared_libs: ["mylib"],
936 }
937
938 apex_key {
939 name: "myapex.key",
940 public_key: "testkey.avbpubkey",
941 private_key: "testkey.pem",
942 }
943
944 cc_library_headers {
945 name: "mylib_headers",
946 export_include_dirs: ["my_include"],
947 system_shared_libs: [],
948 stl: "none",
949 }
950
951 cc_library {
952 name: "mylib",
953 srcs: ["mylib.cpp"],
954 system_shared_libs: [],
955 stl: "none",
956 header_libs: ["mylib_headers"],
957 export_header_lib_headers: ["mylib_headers"],
958 stubs: {
959 versions: ["1", "2", "3"],
960 },
961 }
962
963 cc_library {
964 name: "otherlib",
965 srcs: ["mylib.cpp"],
966 system_shared_libs: [],
967 stl: "none",
968 shared_libs: ["mylib"],
969 }
970 `)
971
972 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
973
974 // Ensure that the include path of the header lib is exported to 'otherlib'
975 ensureContains(t, cFlags, "-Imy_include")
976}
Alex Light9670d332019-01-29 18:07:33 -0800977
Alex Light0851b882019-02-07 13:20:53 -0800978func TestNonTestApex(t *testing.T) {
979 ctx := testApex(t, `
980 apex {
981 name: "myapex",
982 key: "myapex.key",
983 native_shared_libs: ["mylib_common"],
984 }
985
986 apex_key {
987 name: "myapex.key",
988 public_key: "testkey.avbpubkey",
989 private_key: "testkey.pem",
990 }
991
992 cc_library {
993 name: "mylib_common",
994 srcs: ["mylib.cpp"],
995 system_shared_libs: [],
996 stl: "none",
997 }
998 `)
999
1000 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1001 apexRule := module.Rule("apexRule")
1002 copyCmds := apexRule.Args["copy_commands"]
1003
1004 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
1005 t.Log("Apex was a test apex!")
1006 t.Fail()
1007 }
1008 // Ensure that main rule creates an output
1009 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1010
1011 // Ensure that apex variant is created for the direct dep
1012 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1013
1014 // Ensure that both direct and indirect deps are copied into apex
1015 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1016
1017 // Ensure that the platform variant ends with _core_shared
1018 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1019
1020 if !android.InAnyApex("mylib_common") {
1021 t.Log("Found mylib_common not in any apex!")
1022 t.Fail()
1023 }
1024}
1025
1026func TestTestApex(t *testing.T) {
1027 if android.InAnyApex("mylib_common_test") {
1028 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!")
1029 }
1030 ctx := testApex(t, `
1031 apex_test {
1032 name: "myapex",
1033 key: "myapex.key",
1034 native_shared_libs: ["mylib_common_test"],
1035 }
1036
1037 apex_key {
1038 name: "myapex.key",
1039 public_key: "testkey.avbpubkey",
1040 private_key: "testkey.pem",
1041 }
1042
1043 cc_library {
1044 name: "mylib_common_test",
1045 srcs: ["mylib.cpp"],
1046 system_shared_libs: [],
1047 stl: "none",
1048 }
1049 `)
1050
1051 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1052 apexRule := module.Rule("apexRule")
1053 copyCmds := apexRule.Args["copy_commands"]
1054
1055 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1056 t.Log("Apex was not a test apex!")
1057 t.Fail()
1058 }
1059 // Ensure that main rule creates an output
1060 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1061
1062 // Ensure that apex variant is created for the direct dep
1063 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1064
1065 // Ensure that both direct and indirect deps are copied into apex
1066 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1067
1068 // Ensure that the platform variant ends with _core_shared
1069 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1070
1071 if android.InAnyApex("mylib_common_test") {
1072 t.Log("Found mylib_common_test in some apex!")
1073 t.Fail()
1074 }
1075}
1076
Alex Light9670d332019-01-29 18:07:33 -08001077func TestApexWithTarget(t *testing.T) {
1078 ctx := testApex(t, `
1079 apex {
1080 name: "myapex",
1081 key: "myapex.key",
1082 multilib: {
1083 first: {
1084 native_shared_libs: ["mylib_common"],
1085 }
1086 },
1087 target: {
1088 android: {
1089 multilib: {
1090 first: {
1091 native_shared_libs: ["mylib"],
1092 }
1093 }
1094 },
1095 host: {
1096 multilib: {
1097 first: {
1098 native_shared_libs: ["mylib2"],
1099 }
1100 }
1101 }
1102 }
1103 }
1104
1105 apex_key {
1106 name: "myapex.key",
1107 public_key: "testkey.avbpubkey",
1108 private_key: "testkey.pem",
1109 }
1110
1111 cc_library {
1112 name: "mylib",
1113 srcs: ["mylib.cpp"],
1114 system_shared_libs: [],
1115 stl: "none",
1116 }
1117
1118 cc_library {
1119 name: "mylib_common",
1120 srcs: ["mylib.cpp"],
1121 system_shared_libs: [],
1122 stl: "none",
1123 compile_multilib: "first",
1124 }
1125
1126 cc_library {
1127 name: "mylib2",
1128 srcs: ["mylib.cpp"],
1129 system_shared_libs: [],
1130 stl: "none",
1131 compile_multilib: "first",
1132 }
1133 `)
1134
1135 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1136 copyCmds := apexRule.Args["copy_commands"]
1137
1138 // Ensure that main rule creates an output
1139 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1140
1141 // Ensure that apex variant is created for the direct dep
1142 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1143 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1144 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1145
1146 // Ensure that both direct and indirect deps are copied into apex
1147 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1148 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1149 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1150
1151 // Ensure that the platform variant ends with _core_shared
1152 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1153 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1154 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1155}
Jiyong Park04480cf2019-02-06 00:16:29 +09001156
1157func TestApexWithShBinary(t *testing.T) {
1158 ctx := testApex(t, `
1159 apex {
1160 name: "myapex",
1161 key: "myapex.key",
1162 binaries: ["myscript"],
1163 }
1164
1165 apex_key {
1166 name: "myapex.key",
1167 public_key: "testkey.avbpubkey",
1168 private_key: "testkey.pem",
1169 }
1170
1171 sh_binary {
1172 name: "myscript",
1173 src: "mylib.cpp",
1174 filename: "myscript.sh",
1175 sub_dir: "script",
1176 }
1177 `)
1178
1179 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1180 copyCmds := apexRule.Args["copy_commands"]
1181
1182 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1183}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001184
1185func TestApexInProductPartition(t *testing.T) {
1186 ctx := testApex(t, `
1187 apex {
1188 name: "myapex",
1189 key: "myapex.key",
1190 native_shared_libs: ["mylib"],
1191 product_specific: true,
1192 }
1193
1194 apex_key {
1195 name: "myapex.key",
1196 public_key: "testkey.avbpubkey",
1197 private_key: "testkey.pem",
1198 product_specific: true,
1199 }
1200
1201 cc_library {
1202 name: "mylib",
1203 srcs: ["mylib.cpp"],
1204 system_shared_libs: [],
1205 stl: "none",
1206 }
1207 `)
1208
1209 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1210 expected := "target/product/test_device/product/apex"
1211 actual := apex.installDir.RelPathString()
1212 if actual != expected {
1213 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1214 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001215}
Jiyong Park67882562019-03-21 01:11:21 +09001216
1217func TestApexKeyFromOtherModule(t *testing.T) {
1218 ctx := testApex(t, `
1219 apex_key {
1220 name: "myapex.key",
1221 public_key: ":my.avbpubkey",
1222 private_key: ":my.pem",
1223 product_specific: true,
1224 }
1225
1226 filegroup {
1227 name: "my.avbpubkey",
1228 srcs: ["testkey2.avbpubkey"],
1229 }
1230
1231 filegroup {
1232 name: "my.pem",
1233 srcs: ["testkey2.pem"],
1234 }
1235 `)
1236
1237 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1238 expected_pubkey := "testkey2.avbpubkey"
1239 actual_pubkey := apex_key.public_key_file.String()
1240 if actual_pubkey != expected_pubkey {
1241 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1242 }
1243 expected_privkey := "testkey2.pem"
1244 actual_privkey := apex_key.private_key_file.String()
1245 if actual_privkey != expected_privkey {
1246 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1247 }
1248}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001249
1250func TestPrebuilt(t *testing.T) {
1251 ctx := testApex(t, `
1252 prebuilt_apex {
1253 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001254 arch: {
1255 arm64: {
1256 src: "myapex-arm64.apex",
1257 },
1258 arm: {
1259 src: "myapex-arm.apex",
1260 },
1261 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001262 }
1263 `)
1264
1265 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1266
Jiyong Parkc95714e2019-03-29 14:23:10 +09001267 expectedInput := "myapex-arm64.apex"
1268 if prebuilt.inputApex.String() != expectedInput {
1269 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1270 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001271}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001272
1273func TestPrebuiltFilenameOverride(t *testing.T) {
1274 ctx := testApex(t, `
1275 prebuilt_apex {
1276 name: "myapex",
1277 src: "myapex-arm.apex",
1278 filename: "notmyapex.apex",
1279 }
1280 `)
1281
1282 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1283
1284 expected := "notmyapex.apex"
1285 if p.installFilename != expected {
1286 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1287 }
1288}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001289
Roland Levillain630846d2019-06-26 12:48:34 +01001290func TestApexWithTests(t *testing.T) {
1291 ctx := testApex(t, `
1292 apex_test {
1293 name: "myapex",
1294 key: "myapex.key",
1295 tests: [
1296 "mytest",
1297 ],
1298 }
1299
1300 apex_key {
1301 name: "myapex.key",
1302 public_key: "testkey.avbpubkey",
1303 private_key: "testkey.pem",
1304 }
1305
1306 cc_test {
1307 name: "mytest",
1308 gtest: false,
1309 srcs: ["mytest.cpp"],
1310 relative_install_path: "test",
1311 system_shared_libs: [],
1312 static_executable: true,
1313 stl: "none",
1314 }
1315 `)
1316
1317 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1318 copyCmds := apexRule.Args["copy_commands"]
1319
1320 // Ensure that test dep is copied into apex.
1321 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
1322}
1323
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07001324func TestMain(m *testing.M) {
1325 run := func() int {
1326 setUp()
1327 defer tearDown()
1328
1329 return m.Run()
1330 }
1331
1332 os.Exit(run())
1333}