blob: 3c80376dda87b94dbab4a30465baf600bc8c8203 [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
30func testApex(t *testing.T, bp string) *android.TestContext {
31 config, buildDir := setup(t)
32 defer teardown(buildDir)
33
34 ctx := android.NewTestArchContext()
Alex Light0851b882019-02-07 13:20:53 -080035 ctx.RegisterModuleType("apex", android.ModuleFactoryAdaptor(apexBundleFactory))
36 ctx.RegisterModuleType("apex_test", android.ModuleFactoryAdaptor(testApexBundleFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090037 ctx.RegisterModuleType("apex_key", android.ModuleFactoryAdaptor(apexKeyFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090038 ctx.RegisterModuleType("apex_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070039 ctx.RegisterModuleType("prebuilt_apex", android.ModuleFactoryAdaptor(PrebuiltFactory))
Jiyong Park30ca9372019-02-07 16:27:23 +090040 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090041
42 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.TopDown("apex_deps", apexDepsMutator)
44 ctx.BottomUp("apex", apexMutator)
Jaewoong Jung939ebd52019-03-26 15:07:36 -070045 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
46 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090047 })
48
49 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
50 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090051 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090052 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090053 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090054 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090055 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090056 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090057 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090058 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090059 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jaewoong Jung939ebd52019-03-26 15:07:36 -070060 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
61 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
62 })
Jiyong Park25fc6a92018-11-18 18:02:45 +090063 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090064 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090065 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090066 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090067 ctx.BottomUp("version", cc.VersionMutator).Parallel()
68 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
69 })
70
71 ctx.Register()
72
73 bp = bp + `
74 toolchain_library {
75 name: "libcompiler_rt-extras",
76 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090077 vendor_available: true,
78 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090079 }
80
81 toolchain_library {
82 name: "libatomic",
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: "libgcc",
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: "libclang_rt.builtins-aarch64-android",
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 {
103 name: "libclang_rt.builtins-arm-android",
104 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900105 vendor_available: true,
106 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900107 }
108
109 cc_object {
110 name: "crtbegin_so",
111 stl: "none",
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 cc_object {
117 name: "crtend_so",
118 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900119 vendor_available: true,
120 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900121 }
122
Alex Light3d673592019-01-18 14:37:31 -0800123 cc_object {
124 name: "crtbegin_static",
125 stl: "none",
126 }
127
128 cc_object {
129 name: "crtend_android",
130 stl: "none",
131 }
132
Jiyong Parkda6eb592018-12-19 17:12:36 +0900133 llndk_library {
134 name: "libc",
135 symbol_file: "",
136 }
137
138 llndk_library {
139 name: "libm",
140 symbol_file: "",
141 }
142
143 llndk_library {
144 name: "libdl",
145 symbol_file: "",
146 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900147 `
148
149 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900150 "Android.bp": []byte(bp),
151 "build/target/product/security": nil,
152 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900153 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900154 "system/sepolicy/apex/myapex-file_contexts": nil,
155 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
156 "system/sepolicy/apex/otherapex-file_contexts": nil,
157 "mylib.cpp": nil,
158 "myprebuilt": nil,
159 "my_include": nil,
160 "vendor/foo/devkeys/test.x509.pem": nil,
161 "vendor/foo/devkeys/test.pk8": nil,
162 "testkey.x509.pem": nil,
163 "testkey.pk8": nil,
164 "testkey.override.x509.pem": nil,
165 "testkey.override.pk8": nil,
166 "vendor/foo/devkeys/testkey.avbpubkey": nil,
167 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900168 "NOTICE": nil,
169 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900170 "testkey2.avbpubkey": nil,
171 "testkey2.pem": nil,
Jiyong Parkc95714e2019-03-29 14:23:10 +0900172 "myapex-arm64.apex": nil,
173 "myapex-arm.apex": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900174 })
175 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
176 android.FailIfErrored(t, errs)
177 _, errs = ctx.PrepareBuildActions(config)
178 android.FailIfErrored(t, errs)
179
180 return ctx
181}
182
183func setup(t *testing.T) (config android.Config, buildDir string) {
184 buildDir, err := ioutil.TempDir("", "soong_apex_test")
185 if err != nil {
186 t.Fatal(err)
187 }
188
189 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900190 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900191 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900192 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Park25fc6a92018-11-18 18:02:45 +0900193 return
194}
195
196func teardown(buildDir string) {
197 os.RemoveAll(buildDir)
198}
199
200// ensure that 'result' contains 'expected'
201func ensureContains(t *testing.T, result string, expected string) {
202 if !strings.Contains(result, expected) {
203 t.Errorf("%q is not found in %q", expected, result)
204 }
205}
206
207// ensures that 'result' does not contain 'notExpected'
208func ensureNotContains(t *testing.T, result string, notExpected string) {
209 if strings.Contains(result, notExpected) {
210 t.Errorf("%q is found in %q", notExpected, result)
211 }
212}
213
214func ensureListContains(t *testing.T, result []string, expected string) {
215 if !android.InList(expected, result) {
216 t.Errorf("%q is not found in %v", expected, result)
217 }
218}
219
220func ensureListNotContains(t *testing.T, result []string, notExpected string) {
221 if android.InList(notExpected, result) {
222 t.Errorf("%q is found in %v", notExpected, result)
223 }
224}
225
226// Minimal test
227func TestBasicApex(t *testing.T) {
228 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900229 apex_defaults {
230 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900231 manifest: ":myapex.manifest",
232 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900233 key: "myapex.key",
234 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800235 multilib: {
236 both: {
237 binaries: ["foo",],
238 }
239 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 }
241
Jiyong Park30ca9372019-02-07 16:27:23 +0900242 apex {
243 name: "myapex",
244 defaults: ["myapex-defaults"],
245 }
246
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 apex_key {
248 name: "myapex.key",
249 public_key: "testkey.avbpubkey",
250 private_key: "testkey.pem",
251 }
252
Jiyong Park809bb722019-02-13 21:33:49 +0900253 filegroup {
254 name: "myapex.manifest",
255 srcs: ["apex_manifest.json"],
256 }
257
258 filegroup {
259 name: "myapex.androidmanifest",
260 srcs: ["AndroidManifest.xml"],
261 }
262
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263 cc_library {
264 name: "mylib",
265 srcs: ["mylib.cpp"],
266 shared_libs: ["mylib2"],
267 system_shared_libs: [],
268 stl: "none",
269 }
270
Alex Light3d673592019-01-18 14:37:31 -0800271 cc_binary {
272 name: "foo",
273 srcs: ["mylib.cpp"],
274 compile_multilib: "both",
275 multilib: {
276 lib32: {
277 suffix: "32",
278 },
279 lib64: {
280 suffix: "64",
281 },
282 },
283 symlinks: ["foo_link_"],
284 symlink_preferred_arch: true,
285 system_shared_libs: [],
286 static_executable: true,
287 stl: "none",
288 }
289
Jiyong Park25fc6a92018-11-18 18:02:45 +0900290 cc_library {
291 name: "mylib2",
292 srcs: ["mylib.cpp"],
293 system_shared_libs: [],
294 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900295 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900296 }
297 `)
298
299 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900300
301 optFlags := apexRule.Args["opt_flags"]
302 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
303
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 copyCmds := apexRule.Args["copy_commands"]
305
306 // Ensure that main rule creates an output
307 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
308
309 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900310 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311
312 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900313 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314
315 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800316 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
317 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800318
319 // Ensure that the platform variant ends with _core_shared
320 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
321 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800322
323 // Ensure that all symlinks are present.
324 found_foo_link_64 := false
325 found_foo := false
326 for _, cmd := range strings.Split(copyCmds, " && ") {
327 if strings.HasPrefix(cmd, "ln -s foo64") {
328 if strings.HasSuffix(cmd, "bin/foo") {
329 found_foo = true
330 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
331 found_foo_link_64 = true
332 }
333 }
334 }
335 good := found_foo && found_foo_link_64
336 if !good {
337 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
338 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900339
340 apexMergeNoticeRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexMergeNoticeRule")
341 noticeInputs := strings.Split(apexMergeNoticeRule.Args["inputs"], " ")
342 if len(noticeInputs) != 3 {
343 t.Errorf("number of input notice files: expected = 3, actual = %d", len(noticeInputs))
344 }
345 ensureListContains(t, noticeInputs, "NOTICE")
346 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800347}
348
349func TestBasicZipApex(t *testing.T) {
350 ctx := testApex(t, `
351 apex {
352 name: "myapex",
353 key: "myapex.key",
354 payload_type: "zip",
355 native_shared_libs: ["mylib"],
356 }
357
358 apex_key {
359 name: "myapex.key",
360 public_key: "testkey.avbpubkey",
361 private_key: "testkey.pem",
362 }
363
364 cc_library {
365 name: "mylib",
366 srcs: ["mylib.cpp"],
367 shared_libs: ["mylib2"],
368 system_shared_libs: [],
369 stl: "none",
370 }
371
372 cc_library {
373 name: "mylib2",
374 srcs: ["mylib.cpp"],
375 system_shared_libs: [],
376 stl: "none",
377 }
378 `)
379
380 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
381 copyCmds := zipApexRule.Args["copy_commands"]
382
383 // Ensure that main rule creates an output
384 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
385
386 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900387 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800388
389 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900390 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800391
392 // Ensure that both direct and indirect deps are copied into apex
393 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
394 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900395}
396
397func TestApexWithStubs(t *testing.T) {
398 ctx := testApex(t, `
399 apex {
400 name: "myapex",
401 key: "myapex.key",
402 native_shared_libs: ["mylib", "mylib3"],
403 }
404
405 apex_key {
406 name: "myapex.key",
407 public_key: "testkey.avbpubkey",
408 private_key: "testkey.pem",
409 }
410
411 cc_library {
412 name: "mylib",
413 srcs: ["mylib.cpp"],
414 shared_libs: ["mylib2", "mylib3"],
415 system_shared_libs: [],
416 stl: "none",
417 }
418
419 cc_library {
420 name: "mylib2",
421 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900422 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900423 system_shared_libs: [],
424 stl: "none",
425 stubs: {
426 versions: ["1", "2", "3"],
427 },
428 }
429
430 cc_library {
431 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900432 srcs: ["mylib.cpp"],
433 shared_libs: ["mylib4"],
434 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900435 stl: "none",
436 stubs: {
437 versions: ["10", "11", "12"],
438 },
439 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900440
441 cc_library {
442 name: "mylib4",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
446 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900447 `)
448
449 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
450 copyCmds := apexRule.Args["copy_commands"]
451
452 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800453 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900454
455 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800456 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457
458 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800459 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900460
Jiyong Parkda6eb592018-12-19 17:12:36 +0900461 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900462
463 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900464 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900465 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900466 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900467
468 // 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 +0900469 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900471 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900472
473 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900474 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900475 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900476
477 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900478 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 +0900479}
480
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900481func TestApexWithExplicitStubsDependency(t *testing.T) {
482 ctx := testApex(t, `
483 apex {
484 name: "myapex",
485 key: "myapex.key",
486 native_shared_libs: ["mylib"],
487 }
488
489 apex_key {
490 name: "myapex.key",
491 public_key: "testkey.avbpubkey",
492 private_key: "testkey.pem",
493 }
494
495 cc_library {
496 name: "mylib",
497 srcs: ["mylib.cpp"],
498 shared_libs: ["libfoo#10"],
499 system_shared_libs: [],
500 stl: "none",
501 }
502
503 cc_library {
504 name: "libfoo",
505 srcs: ["mylib.cpp"],
506 shared_libs: ["libbar"],
507 system_shared_libs: [],
508 stl: "none",
509 stubs: {
510 versions: ["10", "20", "30"],
511 },
512 }
513
514 cc_library {
515 name: "libbar",
516 srcs: ["mylib.cpp"],
517 system_shared_libs: [],
518 stl: "none",
519 }
520
521 `)
522
523 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
524 copyCmds := apexRule.Args["copy_commands"]
525
526 // Ensure that direct non-stubs dep is always included
527 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
528
529 // Ensure that indirect stubs dep is not included
530 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
531
532 // Ensure that dependency of stubs is not included
533 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
534
Jiyong Parkda6eb592018-12-19 17:12:36 +0900535 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900536
537 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900538 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900539 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900540 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900541
Jiyong Parkda6eb592018-12-19 17:12:36 +0900542 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900543
544 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
545 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
546}
547
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548func TestApexWithSystemLibsStubs(t *testing.T) {
549 ctx := testApex(t, `
550 apex {
551 name: "myapex",
552 key: "myapex.key",
553 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
554 }
555
556 apex_key {
557 name: "myapex.key",
558 public_key: "testkey.avbpubkey",
559 private_key: "testkey.pem",
560 }
561
562 cc_library {
563 name: "mylib",
564 srcs: ["mylib.cpp"],
565 shared_libs: ["libdl#27"],
566 stl: "none",
567 }
568
569 cc_library_shared {
570 name: "mylib_shared",
571 srcs: ["mylib.cpp"],
572 shared_libs: ["libdl#27"],
573 stl: "none",
574 }
575
576 cc_library {
577 name: "libc",
578 no_libgcc: true,
579 nocrt: true,
580 system_shared_libs: [],
581 stl: "none",
582 stubs: {
583 versions: ["27", "28", "29"],
584 },
585 }
586
587 cc_library {
588 name: "libm",
589 no_libgcc: true,
590 nocrt: true,
591 system_shared_libs: [],
592 stl: "none",
593 stubs: {
594 versions: ["27", "28", "29"],
595 },
596 }
597
598 cc_library {
599 name: "libdl",
600 no_libgcc: true,
601 nocrt: true,
602 system_shared_libs: [],
603 stl: "none",
604 stubs: {
605 versions: ["27", "28", "29"],
606 },
607 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900608
609 cc_library {
610 name: "libBootstrap",
611 srcs: ["mylib.cpp"],
612 stl: "none",
613 bootstrap: true,
614 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900615 `)
616
617 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
618 copyCmds := apexRule.Args["copy_commands"]
619
620 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800621 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900622 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
623 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900624
625 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900626 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900627
Jiyong Parkda6eb592018-12-19 17:12:36 +0900628 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
629 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
630 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900631
632 // For dependency to libc
633 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900634 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900635 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900636 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900637 // ... Cflags from stub is correctly exported to mylib
638 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
639 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
640
641 // For dependency to libm
642 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900643 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900644 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900645 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 // ... and is not compiling with the stub
647 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
648 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
649
650 // For dependency to libdl
651 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900652 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900653 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900654 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
655 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900657 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900658 // ... Cflags from stub is correctly exported to mylib
659 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
660 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900661
662 // Ensure that libBootstrap is depending on the platform variant of bionic libs
663 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
664 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
665 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
666 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900667}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900668
669func TestFilesInSubDir(t *testing.T) {
670 ctx := testApex(t, `
671 apex {
672 name: "myapex",
673 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900674 native_shared_libs: ["mylib"],
675 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900676 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900677 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900678 }
679
680 apex_key {
681 name: "myapex.key",
682 public_key: "testkey.avbpubkey",
683 private_key: "testkey.pem",
684 }
685
686 prebuilt_etc {
687 name: "myetc",
688 src: "myprebuilt",
689 sub_dir: "foo/bar",
690 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900691
692 cc_library {
693 name: "mylib",
694 srcs: ["mylib.cpp"],
695 relative_install_path: "foo/bar",
696 system_shared_libs: [],
697 stl: "none",
698 }
699
700 cc_binary {
701 name: "mybin",
702 srcs: ["mylib.cpp"],
703 relative_install_path: "foo/bar",
704 system_shared_libs: [],
705 static_executable: true,
706 stl: "none",
707 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900708 `)
709
710 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
711 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
712
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900713 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900714 ensureListContains(t, dirs, "etc")
715 ensureListContains(t, dirs, "etc/foo")
716 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900717 ensureListContains(t, dirs, "lib64")
718 ensureListContains(t, dirs, "lib64/foo")
719 ensureListContains(t, dirs, "lib64/foo/bar")
720 ensureListContains(t, dirs, "lib")
721 ensureListContains(t, dirs, "lib/foo")
722 ensureListContains(t, dirs, "lib/foo/bar")
723
Jiyong Parkbd13e442019-03-15 18:10:35 +0900724 ensureListContains(t, dirs, "bin")
725 ensureListContains(t, dirs, "bin/foo")
726 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900727}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900728
729func TestUseVendor(t *testing.T) {
730 ctx := testApex(t, `
731 apex {
732 name: "myapex",
733 key: "myapex.key",
734 native_shared_libs: ["mylib"],
735 use_vendor: true,
736 }
737
738 apex_key {
739 name: "myapex.key",
740 public_key: "testkey.avbpubkey",
741 private_key: "testkey.pem",
742 }
743
744 cc_library {
745 name: "mylib",
746 srcs: ["mylib.cpp"],
747 shared_libs: ["mylib2"],
748 system_shared_libs: [],
749 vendor_available: true,
750 stl: "none",
751 }
752
753 cc_library {
754 name: "mylib2",
755 srcs: ["mylib.cpp"],
756 system_shared_libs: [],
757 vendor_available: true,
758 stl: "none",
759 }
760 `)
761
762 inputsList := []string{}
763 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
764 for _, implicit := range i.Implicits {
765 inputsList = append(inputsList, implicit.String())
766 }
767 }
768 inputsString := strings.Join(inputsList, " ")
769
770 // ensure that the apex includes vendor variants of the direct and indirect deps
771 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
772 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
773
774 // ensure that the apex does not include core variants
775 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
776 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
777}
Jiyong Park16e91a02018-12-20 18:18:08 +0900778
779func TestStaticLinking(t *testing.T) {
780 ctx := testApex(t, `
781 apex {
782 name: "myapex",
783 key: "myapex.key",
784 native_shared_libs: ["mylib"],
785 }
786
787 apex_key {
788 name: "myapex.key",
789 public_key: "testkey.avbpubkey",
790 private_key: "testkey.pem",
791 }
792
793 cc_library {
794 name: "mylib",
795 srcs: ["mylib.cpp"],
796 system_shared_libs: [],
797 stl: "none",
798 stubs: {
799 versions: ["1", "2", "3"],
800 },
801 }
802
803 cc_binary {
804 name: "not_in_apex",
805 srcs: ["mylib.cpp"],
806 static_libs: ["mylib"],
807 static_executable: true,
808 system_shared_libs: [],
809 stl: "none",
810 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900811 `)
812
813 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
814
815 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800816 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900817}
Jiyong Park9335a262018-12-24 11:31:58 +0900818
819func TestKeys(t *testing.T) {
820 ctx := testApex(t, `
821 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900822 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900823 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900824 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900825 native_shared_libs: ["mylib"],
826 }
827
828 cc_library {
829 name: "mylib",
830 srcs: ["mylib.cpp"],
831 system_shared_libs: [],
832 stl: "none",
833 }
834
835 apex_key {
836 name: "myapex.key",
837 public_key: "testkey.avbpubkey",
838 private_key: "testkey.pem",
839 }
840
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900841 android_app_certificate {
842 name: "myapex.certificate",
843 certificate: "testkey",
844 }
845
846 android_app_certificate {
847 name: "myapex.certificate.override",
848 certificate: "testkey.override",
849 }
850
Jiyong Park9335a262018-12-24 11:31:58 +0900851 `)
852
853 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900854 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900855
856 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
857 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
858 "vendor/foo/devkeys/testkey.avbpubkey")
859 }
860 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
861 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
862 "vendor/foo/devkeys/testkey.pem")
863 }
864
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900865 // check the APK certs. It should be overridden to myapex.certificate.override
866 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
867 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900868 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900869 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900870 }
871}
Jiyong Park58e364a2019-01-19 19:24:06 +0900872
873func TestMacro(t *testing.T) {
874 ctx := testApex(t, `
875 apex {
876 name: "myapex",
877 key: "myapex.key",
878 native_shared_libs: ["mylib"],
879 }
880
881 apex {
882 name: "otherapex",
883 key: "myapex.key",
884 native_shared_libs: ["mylib"],
885 }
886
887 apex_key {
888 name: "myapex.key",
889 public_key: "testkey.avbpubkey",
890 private_key: "testkey.pem",
891 }
892
893 cc_library {
894 name: "mylib",
895 srcs: ["mylib.cpp"],
896 system_shared_libs: [],
897 stl: "none",
898 }
899 `)
900
901 // non-APEX variant does not have __ANDROID__APEX__ defined
902 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
903 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
904 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
905
906 // APEX variant has __ANDROID_APEX__=<apexname> defined
907 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
908 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
909 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
910
911 // APEX variant has __ANDROID_APEX__=<apexname> defined
912 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
913 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
914 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
915}
Jiyong Park7e636d02019-01-28 16:16:54 +0900916
917func TestHeaderLibsDependency(t *testing.T) {
918 ctx := testApex(t, `
919 apex {
920 name: "myapex",
921 key: "myapex.key",
922 native_shared_libs: ["mylib"],
923 }
924
925 apex_key {
926 name: "myapex.key",
927 public_key: "testkey.avbpubkey",
928 private_key: "testkey.pem",
929 }
930
931 cc_library_headers {
932 name: "mylib_headers",
933 export_include_dirs: ["my_include"],
934 system_shared_libs: [],
935 stl: "none",
936 }
937
938 cc_library {
939 name: "mylib",
940 srcs: ["mylib.cpp"],
941 system_shared_libs: [],
942 stl: "none",
943 header_libs: ["mylib_headers"],
944 export_header_lib_headers: ["mylib_headers"],
945 stubs: {
946 versions: ["1", "2", "3"],
947 },
948 }
949
950 cc_library {
951 name: "otherlib",
952 srcs: ["mylib.cpp"],
953 system_shared_libs: [],
954 stl: "none",
955 shared_libs: ["mylib"],
956 }
957 `)
958
959 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
960
961 // Ensure that the include path of the header lib is exported to 'otherlib'
962 ensureContains(t, cFlags, "-Imy_include")
963}
Alex Light9670d332019-01-29 18:07:33 -0800964
Alex Light0851b882019-02-07 13:20:53 -0800965func TestNonTestApex(t *testing.T) {
966 ctx := testApex(t, `
967 apex {
968 name: "myapex",
969 key: "myapex.key",
970 native_shared_libs: ["mylib_common"],
971 }
972
973 apex_key {
974 name: "myapex.key",
975 public_key: "testkey.avbpubkey",
976 private_key: "testkey.pem",
977 }
978
979 cc_library {
980 name: "mylib_common",
981 srcs: ["mylib.cpp"],
982 system_shared_libs: [],
983 stl: "none",
984 }
985 `)
986
987 module := ctx.ModuleForTests("myapex", "android_common_myapex")
988 apexRule := module.Rule("apexRule")
989 copyCmds := apexRule.Args["copy_commands"]
990
991 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
992 t.Log("Apex was a test apex!")
993 t.Fail()
994 }
995 // Ensure that main rule creates an output
996 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
997
998 // Ensure that apex variant is created for the direct dep
999 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1000
1001 // Ensure that both direct and indirect deps are copied into apex
1002 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1003
1004 // Ensure that the platform variant ends with _core_shared
1005 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1006
1007 if !android.InAnyApex("mylib_common") {
1008 t.Log("Found mylib_common not in any apex!")
1009 t.Fail()
1010 }
1011}
1012
1013func TestTestApex(t *testing.T) {
1014 if android.InAnyApex("mylib_common_test") {
1015 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!")
1016 }
1017 ctx := testApex(t, `
1018 apex_test {
1019 name: "myapex",
1020 key: "myapex.key",
1021 native_shared_libs: ["mylib_common_test"],
1022 }
1023
1024 apex_key {
1025 name: "myapex.key",
1026 public_key: "testkey.avbpubkey",
1027 private_key: "testkey.pem",
1028 }
1029
1030 cc_library {
1031 name: "mylib_common_test",
1032 srcs: ["mylib.cpp"],
1033 system_shared_libs: [],
1034 stl: "none",
1035 }
1036 `)
1037
1038 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1039 apexRule := module.Rule("apexRule")
1040 copyCmds := apexRule.Args["copy_commands"]
1041
1042 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1043 t.Log("Apex was not a test apex!")
1044 t.Fail()
1045 }
1046 // Ensure that main rule creates an output
1047 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1048
1049 // Ensure that apex variant is created for the direct dep
1050 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1051
1052 // Ensure that both direct and indirect deps are copied into apex
1053 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1054
1055 // Ensure that the platform variant ends with _core_shared
1056 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1057
1058 if android.InAnyApex("mylib_common_test") {
1059 t.Log("Found mylib_common_test in some apex!")
1060 t.Fail()
1061 }
1062}
1063
Alex Light9670d332019-01-29 18:07:33 -08001064func TestApexWithTarget(t *testing.T) {
1065 ctx := testApex(t, `
1066 apex {
1067 name: "myapex",
1068 key: "myapex.key",
1069 multilib: {
1070 first: {
1071 native_shared_libs: ["mylib_common"],
1072 }
1073 },
1074 target: {
1075 android: {
1076 multilib: {
1077 first: {
1078 native_shared_libs: ["mylib"],
1079 }
1080 }
1081 },
1082 host: {
1083 multilib: {
1084 first: {
1085 native_shared_libs: ["mylib2"],
1086 }
1087 }
1088 }
1089 }
1090 }
1091
1092 apex_key {
1093 name: "myapex.key",
1094 public_key: "testkey.avbpubkey",
1095 private_key: "testkey.pem",
1096 }
1097
1098 cc_library {
1099 name: "mylib",
1100 srcs: ["mylib.cpp"],
1101 system_shared_libs: [],
1102 stl: "none",
1103 }
1104
1105 cc_library {
1106 name: "mylib_common",
1107 srcs: ["mylib.cpp"],
1108 system_shared_libs: [],
1109 stl: "none",
1110 compile_multilib: "first",
1111 }
1112
1113 cc_library {
1114 name: "mylib2",
1115 srcs: ["mylib.cpp"],
1116 system_shared_libs: [],
1117 stl: "none",
1118 compile_multilib: "first",
1119 }
1120 `)
1121
1122 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1123 copyCmds := apexRule.Args["copy_commands"]
1124
1125 // Ensure that main rule creates an output
1126 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1127
1128 // Ensure that apex variant is created for the direct dep
1129 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1130 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1131 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1132
1133 // Ensure that both direct and indirect deps are copied into apex
1134 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1135 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1136 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1137
1138 // Ensure that the platform variant ends with _core_shared
1139 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1140 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1141 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1142}
Jiyong Park04480cf2019-02-06 00:16:29 +09001143
1144func TestApexWithShBinary(t *testing.T) {
1145 ctx := testApex(t, `
1146 apex {
1147 name: "myapex",
1148 key: "myapex.key",
1149 binaries: ["myscript"],
1150 }
1151
1152 apex_key {
1153 name: "myapex.key",
1154 public_key: "testkey.avbpubkey",
1155 private_key: "testkey.pem",
1156 }
1157
1158 sh_binary {
1159 name: "myscript",
1160 src: "mylib.cpp",
1161 filename: "myscript.sh",
1162 sub_dir: "script",
1163 }
1164 `)
1165
1166 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1167 copyCmds := apexRule.Args["copy_commands"]
1168
1169 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1170}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001171
1172func TestApexInProductPartition(t *testing.T) {
1173 ctx := testApex(t, `
1174 apex {
1175 name: "myapex",
1176 key: "myapex.key",
1177 native_shared_libs: ["mylib"],
1178 product_specific: true,
1179 }
1180
1181 apex_key {
1182 name: "myapex.key",
1183 public_key: "testkey.avbpubkey",
1184 private_key: "testkey.pem",
1185 product_specific: true,
1186 }
1187
1188 cc_library {
1189 name: "mylib",
1190 srcs: ["mylib.cpp"],
1191 system_shared_libs: [],
1192 stl: "none",
1193 }
1194 `)
1195
1196 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1197 expected := "target/product/test_device/product/apex"
1198 actual := apex.installDir.RelPathString()
1199 if actual != expected {
1200 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1201 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001202}
Jiyong Park67882562019-03-21 01:11:21 +09001203
1204func TestApexKeyFromOtherModule(t *testing.T) {
1205 ctx := testApex(t, `
1206 apex_key {
1207 name: "myapex.key",
1208 public_key: ":my.avbpubkey",
1209 private_key: ":my.pem",
1210 product_specific: true,
1211 }
1212
1213 filegroup {
1214 name: "my.avbpubkey",
1215 srcs: ["testkey2.avbpubkey"],
1216 }
1217
1218 filegroup {
1219 name: "my.pem",
1220 srcs: ["testkey2.pem"],
1221 }
1222 `)
1223
1224 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1225 expected_pubkey := "testkey2.avbpubkey"
1226 actual_pubkey := apex_key.public_key_file.String()
1227 if actual_pubkey != expected_pubkey {
1228 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1229 }
1230 expected_privkey := "testkey2.pem"
1231 actual_privkey := apex_key.private_key_file.String()
1232 if actual_privkey != expected_privkey {
1233 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1234 }
1235}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001236
1237func TestPrebuilt(t *testing.T) {
1238 ctx := testApex(t, `
1239 prebuilt_apex {
1240 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001241 arch: {
1242 arm64: {
1243 src: "myapex-arm64.apex",
1244 },
1245 arm: {
1246 src: "myapex-arm.apex",
1247 },
1248 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001249 }
1250 `)
1251
1252 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1253
Jiyong Parkc95714e2019-03-29 14:23:10 +09001254 expectedInput := "myapex-arm64.apex"
1255 if prebuilt.inputApex.String() != expectedInput {
1256 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1257 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001258}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001259
1260func TestPrebuiltFilenameOverride(t *testing.T) {
1261 ctx := testApex(t, `
1262 prebuilt_apex {
1263 name: "myapex",
1264 src: "myapex-arm.apex",
1265 filename: "notmyapex.apex",
1266 }
1267 `)
1268
1269 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1270
1271 expected := "notmyapex.apex"
1272 if p.installFilename != expected {
1273 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1274 }
1275}