blob: 8a2e55af51709087d06e7021f691ae24a4171678 [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))
39 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Park25fc6a92018-11-18 18:02:45 +090040
41 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.TopDown("apex_deps", apexDepsMutator)
43 ctx.BottomUp("apex", apexMutator)
44 })
45
46 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
47 ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
Jiyong Park7e636d02019-01-28 16:16:54 +090048 ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
Jiyong Park16e91a02018-12-20 18:18:08 +090049 ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090050 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
Jiyong Parkda6eb592018-12-19 17:12:36 +090051 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090052 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Jiyong Park7c2ee712018-12-07 00:42:25 +090053 ctx.RegisterModuleType("prebuilt_etc", android.ModuleFactoryAdaptor(android.PrebuiltEtcFactory))
Jiyong Park04480cf2019-02-06 00:16:29 +090054 ctx.RegisterModuleType("sh_binary", android.ModuleFactoryAdaptor(android.ShBinaryFactory))
Jiyong Parkb2742fd2019-02-11 11:38:15 +090055 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(java.AndroidAppCertificateFactory))
Jiyong Park809bb722019-02-13 21:33:49 +090056 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Jiyong Park25fc6a92018-11-18 18:02:45 +090057 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Parkda6eb592018-12-19 17:12:36 +090058 ctx.BottomUp("image", cc.ImageMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090059 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
Jiyong Parkda6eb592018-12-19 17:12:36 +090060 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
Jiyong Park25fc6a92018-11-18 18:02:45 +090061 ctx.BottomUp("version", cc.VersionMutator).Parallel()
62 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
63 })
64
65 ctx.Register()
66
67 bp = bp + `
68 toolchain_library {
69 name: "libcompiler_rt-extras",
70 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090071 vendor_available: true,
72 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090073 }
74
75 toolchain_library {
76 name: "libatomic",
77 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090078 vendor_available: true,
79 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090080 }
81
82 toolchain_library {
83 name: "libgcc",
84 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090085 vendor_available: true,
86 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090087 }
88
89 toolchain_library {
90 name: "libclang_rt.builtins-aarch64-android",
91 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090092 vendor_available: true,
93 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +090094 }
95
96 toolchain_library {
97 name: "libclang_rt.builtins-arm-android",
98 src: "",
Jiyong Parkda6eb592018-12-19 17:12:36 +090099 vendor_available: true,
100 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900101 }
102
103 cc_object {
104 name: "crtbegin_so",
105 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900106 vendor_available: true,
107 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900108 }
109
110 cc_object {
111 name: "crtend_so",
112 stl: "none",
Jiyong Parkda6eb592018-12-19 17:12:36 +0900113 vendor_available: true,
114 recovery_available: true,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900115 }
116
Alex Light3d673592019-01-18 14:37:31 -0800117 cc_object {
118 name: "crtbegin_static",
119 stl: "none",
120 }
121
122 cc_object {
123 name: "crtend_android",
124 stl: "none",
125 }
126
Jiyong Parkda6eb592018-12-19 17:12:36 +0900127 llndk_library {
128 name: "libc",
129 symbol_file: "",
130 }
131
132 llndk_library {
133 name: "libm",
134 symbol_file: "",
135 }
136
137 llndk_library {
138 name: "libdl",
139 symbol_file: "",
140 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900141 `
142
143 ctx.MockFileSystem(map[string][]byte{
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900144 "Android.bp": []byte(bp),
145 "build/target/product/security": nil,
146 "apex_manifest.json": nil,
Jiyong Park809bb722019-02-13 21:33:49 +0900147 "AndroidManifest.xml": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900148 "system/sepolicy/apex/myapex-file_contexts": nil,
149 "system/sepolicy/apex/myapex_keytest-file_contexts": nil,
150 "system/sepolicy/apex/otherapex-file_contexts": nil,
151 "mylib.cpp": nil,
152 "myprebuilt": nil,
153 "my_include": nil,
154 "vendor/foo/devkeys/test.x509.pem": nil,
155 "vendor/foo/devkeys/test.pk8": nil,
156 "testkey.x509.pem": nil,
157 "testkey.pk8": nil,
158 "testkey.override.x509.pem": nil,
159 "testkey.override.pk8": nil,
160 "vendor/foo/devkeys/testkey.avbpubkey": nil,
161 "vendor/foo/devkeys/testkey.pem": nil,
Jiyong Park52818fc2019-03-18 12:01:38 +0900162 "NOTICE": nil,
163 "custom_notice": nil,
Jiyong Park67882562019-03-21 01:11:21 +0900164 "testkey2.avbpubkey": nil,
165 "testkey2.pem": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900166 })
167 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
168 android.FailIfErrored(t, errs)
169 _, errs = ctx.PrepareBuildActions(config)
170 android.FailIfErrored(t, errs)
171
172 return ctx
173}
174
175func setup(t *testing.T) (config android.Config, buildDir string) {
176 buildDir, err := ioutil.TempDir("", "soong_apex_test")
177 if err != nil {
178 t.Fatal(err)
179 }
180
181 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900182 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900183 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900184 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Park25fc6a92018-11-18 18:02:45 +0900185 return
186}
187
188func teardown(buildDir string) {
189 os.RemoveAll(buildDir)
190}
191
192// ensure that 'result' contains 'expected'
193func ensureContains(t *testing.T, result string, expected string) {
194 if !strings.Contains(result, expected) {
195 t.Errorf("%q is not found in %q", expected, result)
196 }
197}
198
199// ensures that 'result' does not contain 'notExpected'
200func ensureNotContains(t *testing.T, result string, notExpected string) {
201 if strings.Contains(result, notExpected) {
202 t.Errorf("%q is found in %q", notExpected, result)
203 }
204}
205
206func ensureListContains(t *testing.T, result []string, expected string) {
207 if !android.InList(expected, result) {
208 t.Errorf("%q is not found in %v", expected, result)
209 }
210}
211
212func ensureListNotContains(t *testing.T, result []string, notExpected string) {
213 if android.InList(notExpected, result) {
214 t.Errorf("%q is found in %v", notExpected, result)
215 }
216}
217
218// Minimal test
219func TestBasicApex(t *testing.T) {
220 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900221 apex_defaults {
222 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900223 manifest: ":myapex.manifest",
224 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900225 key: "myapex.key",
226 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800227 multilib: {
228 both: {
229 binaries: ["foo",],
230 }
231 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 }
233
Jiyong Park30ca9372019-02-07 16:27:23 +0900234 apex {
235 name: "myapex",
236 defaults: ["myapex-defaults"],
237 }
238
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239 apex_key {
240 name: "myapex.key",
241 public_key: "testkey.avbpubkey",
242 private_key: "testkey.pem",
243 }
244
Jiyong Park809bb722019-02-13 21:33:49 +0900245 filegroup {
246 name: "myapex.manifest",
247 srcs: ["apex_manifest.json"],
248 }
249
250 filegroup {
251 name: "myapex.androidmanifest",
252 srcs: ["AndroidManifest.xml"],
253 }
254
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 cc_library {
256 name: "mylib",
257 srcs: ["mylib.cpp"],
258 shared_libs: ["mylib2"],
259 system_shared_libs: [],
260 stl: "none",
261 }
262
Alex Light3d673592019-01-18 14:37:31 -0800263 cc_binary {
264 name: "foo",
265 srcs: ["mylib.cpp"],
266 compile_multilib: "both",
267 multilib: {
268 lib32: {
269 suffix: "32",
270 },
271 lib64: {
272 suffix: "64",
273 },
274 },
275 symlinks: ["foo_link_"],
276 symlink_preferred_arch: true,
277 system_shared_libs: [],
278 static_executable: true,
279 stl: "none",
280 }
281
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 cc_library {
283 name: "mylib2",
284 srcs: ["mylib.cpp"],
285 system_shared_libs: [],
286 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900287 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900288 }
289 `)
290
291 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
292 copyCmds := apexRule.Args["copy_commands"]
293
294 // Ensure that main rule creates an output
295 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
296
297 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900298 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299
300 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900301 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302
303 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800304 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
305 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800306
307 // Ensure that the platform variant ends with _core_shared
308 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
309 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800310
311 // Ensure that all symlinks are present.
312 found_foo_link_64 := false
313 found_foo := false
314 for _, cmd := range strings.Split(copyCmds, " && ") {
315 if strings.HasPrefix(cmd, "ln -s foo64") {
316 if strings.HasSuffix(cmd, "bin/foo") {
317 found_foo = true
318 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
319 found_foo_link_64 = true
320 }
321 }
322 }
323 good := found_foo && found_foo_link_64
324 if !good {
325 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
326 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900327
328 apexMergeNoticeRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexMergeNoticeRule")
329 noticeInputs := strings.Split(apexMergeNoticeRule.Args["inputs"], " ")
330 if len(noticeInputs) != 3 {
331 t.Errorf("number of input notice files: expected = 3, actual = %d", len(noticeInputs))
332 }
333 ensureListContains(t, noticeInputs, "NOTICE")
334 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800335}
336
337func TestBasicZipApex(t *testing.T) {
338 ctx := testApex(t, `
339 apex {
340 name: "myapex",
341 key: "myapex.key",
342 payload_type: "zip",
343 native_shared_libs: ["mylib"],
344 }
345
346 apex_key {
347 name: "myapex.key",
348 public_key: "testkey.avbpubkey",
349 private_key: "testkey.pem",
350 }
351
352 cc_library {
353 name: "mylib",
354 srcs: ["mylib.cpp"],
355 shared_libs: ["mylib2"],
356 system_shared_libs: [],
357 stl: "none",
358 }
359
360 cc_library {
361 name: "mylib2",
362 srcs: ["mylib.cpp"],
363 system_shared_libs: [],
364 stl: "none",
365 }
366 `)
367
368 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
369 copyCmds := zipApexRule.Args["copy_commands"]
370
371 // Ensure that main rule creates an output
372 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
373
374 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900375 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800376
377 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900378 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800379
380 // Ensure that both direct and indirect deps are copied into apex
381 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
382 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900383}
384
385func TestApexWithStubs(t *testing.T) {
386 ctx := testApex(t, `
387 apex {
388 name: "myapex",
389 key: "myapex.key",
390 native_shared_libs: ["mylib", "mylib3"],
391 }
392
393 apex_key {
394 name: "myapex.key",
395 public_key: "testkey.avbpubkey",
396 private_key: "testkey.pem",
397 }
398
399 cc_library {
400 name: "mylib",
401 srcs: ["mylib.cpp"],
402 shared_libs: ["mylib2", "mylib3"],
403 system_shared_libs: [],
404 stl: "none",
405 }
406
407 cc_library {
408 name: "mylib2",
409 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900410 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411 system_shared_libs: [],
412 stl: "none",
413 stubs: {
414 versions: ["1", "2", "3"],
415 },
416 }
417
418 cc_library {
419 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900420 srcs: ["mylib.cpp"],
421 shared_libs: ["mylib4"],
422 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900423 stl: "none",
424 stubs: {
425 versions: ["10", "11", "12"],
426 },
427 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900428
429 cc_library {
430 name: "mylib4",
431 srcs: ["mylib.cpp"],
432 system_shared_libs: [],
433 stl: "none",
434 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900435 `)
436
437 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
438 copyCmds := apexRule.Args["copy_commands"]
439
440 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800441 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442
443 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800444 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900445
446 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800447 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900448
Jiyong Parkda6eb592018-12-19 17:12:36 +0900449 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900450
451 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900452 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900453 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900454 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900455
456 // 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 +0900457 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900458 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900459 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900460
461 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900462 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900463 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900464
465 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900466 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 +0900467}
468
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900469func TestApexWithExplicitStubsDependency(t *testing.T) {
470 ctx := testApex(t, `
471 apex {
472 name: "myapex",
473 key: "myapex.key",
474 native_shared_libs: ["mylib"],
475 }
476
477 apex_key {
478 name: "myapex.key",
479 public_key: "testkey.avbpubkey",
480 private_key: "testkey.pem",
481 }
482
483 cc_library {
484 name: "mylib",
485 srcs: ["mylib.cpp"],
486 shared_libs: ["libfoo#10"],
487 system_shared_libs: [],
488 stl: "none",
489 }
490
491 cc_library {
492 name: "libfoo",
493 srcs: ["mylib.cpp"],
494 shared_libs: ["libbar"],
495 system_shared_libs: [],
496 stl: "none",
497 stubs: {
498 versions: ["10", "20", "30"],
499 },
500 }
501
502 cc_library {
503 name: "libbar",
504 srcs: ["mylib.cpp"],
505 system_shared_libs: [],
506 stl: "none",
507 }
508
509 `)
510
511 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
512 copyCmds := apexRule.Args["copy_commands"]
513
514 // Ensure that direct non-stubs dep is always included
515 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
516
517 // Ensure that indirect stubs dep is not included
518 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
519
520 // Ensure that dependency of stubs is not included
521 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
522
Jiyong Parkda6eb592018-12-19 17:12:36 +0900523 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900524
525 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900526 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900527 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900528 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900529
Jiyong Parkda6eb592018-12-19 17:12:36 +0900530 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900531
532 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
533 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
534}
535
Jiyong Park25fc6a92018-11-18 18:02:45 +0900536func TestApexWithSystemLibsStubs(t *testing.T) {
537 ctx := testApex(t, `
538 apex {
539 name: "myapex",
540 key: "myapex.key",
541 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
542 }
543
544 apex_key {
545 name: "myapex.key",
546 public_key: "testkey.avbpubkey",
547 private_key: "testkey.pem",
548 }
549
550 cc_library {
551 name: "mylib",
552 srcs: ["mylib.cpp"],
553 shared_libs: ["libdl#27"],
554 stl: "none",
555 }
556
557 cc_library_shared {
558 name: "mylib_shared",
559 srcs: ["mylib.cpp"],
560 shared_libs: ["libdl#27"],
561 stl: "none",
562 }
563
564 cc_library {
565 name: "libc",
566 no_libgcc: true,
567 nocrt: true,
568 system_shared_libs: [],
569 stl: "none",
570 stubs: {
571 versions: ["27", "28", "29"],
572 },
573 }
574
575 cc_library {
576 name: "libm",
577 no_libgcc: true,
578 nocrt: true,
579 system_shared_libs: [],
580 stl: "none",
581 stubs: {
582 versions: ["27", "28", "29"],
583 },
584 }
585
586 cc_library {
587 name: "libdl",
588 no_libgcc: true,
589 nocrt: true,
590 system_shared_libs: [],
591 stl: "none",
592 stubs: {
593 versions: ["27", "28", "29"],
594 },
595 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900596
597 cc_library {
598 name: "libBootstrap",
599 srcs: ["mylib.cpp"],
600 stl: "none",
601 bootstrap: true,
602 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900603 `)
604
605 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
606 copyCmds := apexRule.Args["copy_commands"]
607
608 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800609 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900610 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
611 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900612
613 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900614 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900615
Jiyong Parkda6eb592018-12-19 17:12:36 +0900616 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
617 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
618 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900619
620 // For dependency to libc
621 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900622 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900623 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900624 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900625 // ... Cflags from stub is correctly exported to mylib
626 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
627 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
628
629 // For dependency to libm
630 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900631 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900632 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900633 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634 // ... and is not compiling with the stub
635 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
636 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
637
638 // For dependency to libdl
639 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900640 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900641 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900642 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
643 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900644 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900645 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900646 // ... Cflags from stub is correctly exported to mylib
647 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
648 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900649
650 // Ensure that libBootstrap is depending on the platform variant of bionic libs
651 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
652 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
653 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
654 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900655}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900656
657func TestFilesInSubDir(t *testing.T) {
658 ctx := testApex(t, `
659 apex {
660 name: "myapex",
661 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900662 native_shared_libs: ["mylib"],
663 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900664 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900665 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900666 }
667
668 apex_key {
669 name: "myapex.key",
670 public_key: "testkey.avbpubkey",
671 private_key: "testkey.pem",
672 }
673
674 prebuilt_etc {
675 name: "myetc",
676 src: "myprebuilt",
677 sub_dir: "foo/bar",
678 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900679
680 cc_library {
681 name: "mylib",
682 srcs: ["mylib.cpp"],
683 relative_install_path: "foo/bar",
684 system_shared_libs: [],
685 stl: "none",
686 }
687
688 cc_binary {
689 name: "mybin",
690 srcs: ["mylib.cpp"],
691 relative_install_path: "foo/bar",
692 system_shared_libs: [],
693 static_executable: true,
694 stl: "none",
695 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900696 `)
697
698 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
699 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
700
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900701 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900702 ensureListContains(t, dirs, "etc")
703 ensureListContains(t, dirs, "etc/foo")
704 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900705 ensureListContains(t, dirs, "lib64")
706 ensureListContains(t, dirs, "lib64/foo")
707 ensureListContains(t, dirs, "lib64/foo/bar")
708 ensureListContains(t, dirs, "lib")
709 ensureListContains(t, dirs, "lib/foo")
710 ensureListContains(t, dirs, "lib/foo/bar")
711
Jiyong Parkbd13e442019-03-15 18:10:35 +0900712 ensureListContains(t, dirs, "bin")
713 ensureListContains(t, dirs, "bin/foo")
714 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900715}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900716
717func TestUseVendor(t *testing.T) {
718 ctx := testApex(t, `
719 apex {
720 name: "myapex",
721 key: "myapex.key",
722 native_shared_libs: ["mylib"],
723 use_vendor: true,
724 }
725
726 apex_key {
727 name: "myapex.key",
728 public_key: "testkey.avbpubkey",
729 private_key: "testkey.pem",
730 }
731
732 cc_library {
733 name: "mylib",
734 srcs: ["mylib.cpp"],
735 shared_libs: ["mylib2"],
736 system_shared_libs: [],
737 vendor_available: true,
738 stl: "none",
739 }
740
741 cc_library {
742 name: "mylib2",
743 srcs: ["mylib.cpp"],
744 system_shared_libs: [],
745 vendor_available: true,
746 stl: "none",
747 }
748 `)
749
750 inputsList := []string{}
751 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
752 for _, implicit := range i.Implicits {
753 inputsList = append(inputsList, implicit.String())
754 }
755 }
756 inputsString := strings.Join(inputsList, " ")
757
758 // ensure that the apex includes vendor variants of the direct and indirect deps
759 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
760 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
761
762 // ensure that the apex does not include core variants
763 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
764 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
765}
Jiyong Park16e91a02018-12-20 18:18:08 +0900766
767func TestStaticLinking(t *testing.T) {
768 ctx := testApex(t, `
769 apex {
770 name: "myapex",
771 key: "myapex.key",
772 native_shared_libs: ["mylib"],
773 }
774
775 apex_key {
776 name: "myapex.key",
777 public_key: "testkey.avbpubkey",
778 private_key: "testkey.pem",
779 }
780
781 cc_library {
782 name: "mylib",
783 srcs: ["mylib.cpp"],
784 system_shared_libs: [],
785 stl: "none",
786 stubs: {
787 versions: ["1", "2", "3"],
788 },
789 }
790
791 cc_binary {
792 name: "not_in_apex",
793 srcs: ["mylib.cpp"],
794 static_libs: ["mylib"],
795 static_executable: true,
796 system_shared_libs: [],
797 stl: "none",
798 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900799 `)
800
801 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
802
803 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800804 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900805}
Jiyong Park9335a262018-12-24 11:31:58 +0900806
807func TestKeys(t *testing.T) {
808 ctx := testApex(t, `
809 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900810 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900811 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900812 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900813 native_shared_libs: ["mylib"],
814 }
815
816 cc_library {
817 name: "mylib",
818 srcs: ["mylib.cpp"],
819 system_shared_libs: [],
820 stl: "none",
821 }
822
823 apex_key {
824 name: "myapex.key",
825 public_key: "testkey.avbpubkey",
826 private_key: "testkey.pem",
827 }
828
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900829 android_app_certificate {
830 name: "myapex.certificate",
831 certificate: "testkey",
832 }
833
834 android_app_certificate {
835 name: "myapex.certificate.override",
836 certificate: "testkey.override",
837 }
838
Jiyong Park9335a262018-12-24 11:31:58 +0900839 `)
840
841 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900842 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900843
844 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
845 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
846 "vendor/foo/devkeys/testkey.avbpubkey")
847 }
848 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
849 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
850 "vendor/foo/devkeys/testkey.pem")
851 }
852
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900853 // check the APK certs. It should be overridden to myapex.certificate.override
854 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
855 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900856 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900857 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900858 }
859}
Jiyong Park58e364a2019-01-19 19:24:06 +0900860
861func TestMacro(t *testing.T) {
862 ctx := testApex(t, `
863 apex {
864 name: "myapex",
865 key: "myapex.key",
866 native_shared_libs: ["mylib"],
867 }
868
869 apex {
870 name: "otherapex",
871 key: "myapex.key",
872 native_shared_libs: ["mylib"],
873 }
874
875 apex_key {
876 name: "myapex.key",
877 public_key: "testkey.avbpubkey",
878 private_key: "testkey.pem",
879 }
880
881 cc_library {
882 name: "mylib",
883 srcs: ["mylib.cpp"],
884 system_shared_libs: [],
885 stl: "none",
886 }
887 `)
888
889 // non-APEX variant does not have __ANDROID__APEX__ defined
890 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
891 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
892 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
893
894 // APEX variant has __ANDROID_APEX__=<apexname> defined
895 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
896 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
897 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
898
899 // APEX variant has __ANDROID_APEX__=<apexname> defined
900 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
901 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
902 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
903}
Jiyong Park7e636d02019-01-28 16:16:54 +0900904
905func TestHeaderLibsDependency(t *testing.T) {
906 ctx := testApex(t, `
907 apex {
908 name: "myapex",
909 key: "myapex.key",
910 native_shared_libs: ["mylib"],
911 }
912
913 apex_key {
914 name: "myapex.key",
915 public_key: "testkey.avbpubkey",
916 private_key: "testkey.pem",
917 }
918
919 cc_library_headers {
920 name: "mylib_headers",
921 export_include_dirs: ["my_include"],
922 system_shared_libs: [],
923 stl: "none",
924 }
925
926 cc_library {
927 name: "mylib",
928 srcs: ["mylib.cpp"],
929 system_shared_libs: [],
930 stl: "none",
931 header_libs: ["mylib_headers"],
932 export_header_lib_headers: ["mylib_headers"],
933 stubs: {
934 versions: ["1", "2", "3"],
935 },
936 }
937
938 cc_library {
939 name: "otherlib",
940 srcs: ["mylib.cpp"],
941 system_shared_libs: [],
942 stl: "none",
943 shared_libs: ["mylib"],
944 }
945 `)
946
947 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
948
949 // Ensure that the include path of the header lib is exported to 'otherlib'
950 ensureContains(t, cFlags, "-Imy_include")
951}
Alex Light9670d332019-01-29 18:07:33 -0800952
Alex Light0851b882019-02-07 13:20:53 -0800953func TestNonTestApex(t *testing.T) {
954 ctx := testApex(t, `
955 apex {
956 name: "myapex",
957 key: "myapex.key",
958 native_shared_libs: ["mylib_common"],
959 }
960
961 apex_key {
962 name: "myapex.key",
963 public_key: "testkey.avbpubkey",
964 private_key: "testkey.pem",
965 }
966
967 cc_library {
968 name: "mylib_common",
969 srcs: ["mylib.cpp"],
970 system_shared_libs: [],
971 stl: "none",
972 }
973 `)
974
975 module := ctx.ModuleForTests("myapex", "android_common_myapex")
976 apexRule := module.Rule("apexRule")
977 copyCmds := apexRule.Args["copy_commands"]
978
979 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
980 t.Log("Apex was a test apex!")
981 t.Fail()
982 }
983 // Ensure that main rule creates an output
984 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
985
986 // Ensure that apex variant is created for the direct dep
987 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
988
989 // Ensure that both direct and indirect deps are copied into apex
990 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
991
992 // Ensure that the platform variant ends with _core_shared
993 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
994
995 if !android.InAnyApex("mylib_common") {
996 t.Log("Found mylib_common not in any apex!")
997 t.Fail()
998 }
999}
1000
1001func TestTestApex(t *testing.T) {
1002 if android.InAnyApex("mylib_common_test") {
1003 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!")
1004 }
1005 ctx := testApex(t, `
1006 apex_test {
1007 name: "myapex",
1008 key: "myapex.key",
1009 native_shared_libs: ["mylib_common_test"],
1010 }
1011
1012 apex_key {
1013 name: "myapex.key",
1014 public_key: "testkey.avbpubkey",
1015 private_key: "testkey.pem",
1016 }
1017
1018 cc_library {
1019 name: "mylib_common_test",
1020 srcs: ["mylib.cpp"],
1021 system_shared_libs: [],
1022 stl: "none",
1023 }
1024 `)
1025
1026 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1027 apexRule := module.Rule("apexRule")
1028 copyCmds := apexRule.Args["copy_commands"]
1029
1030 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1031 t.Log("Apex was not a test apex!")
1032 t.Fail()
1033 }
1034 // Ensure that main rule creates an output
1035 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1036
1037 // Ensure that apex variant is created for the direct dep
1038 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1039
1040 // Ensure that both direct and indirect deps are copied into apex
1041 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1042
1043 // Ensure that the platform variant ends with _core_shared
1044 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1045
1046 if android.InAnyApex("mylib_common_test") {
1047 t.Log("Found mylib_common_test in some apex!")
1048 t.Fail()
1049 }
1050}
1051
Alex Light9670d332019-01-29 18:07:33 -08001052func TestApexWithTarget(t *testing.T) {
1053 ctx := testApex(t, `
1054 apex {
1055 name: "myapex",
1056 key: "myapex.key",
1057 multilib: {
1058 first: {
1059 native_shared_libs: ["mylib_common"],
1060 }
1061 },
1062 target: {
1063 android: {
1064 multilib: {
1065 first: {
1066 native_shared_libs: ["mylib"],
1067 }
1068 }
1069 },
1070 host: {
1071 multilib: {
1072 first: {
1073 native_shared_libs: ["mylib2"],
1074 }
1075 }
1076 }
1077 }
1078 }
1079
1080 apex_key {
1081 name: "myapex.key",
1082 public_key: "testkey.avbpubkey",
1083 private_key: "testkey.pem",
1084 }
1085
1086 cc_library {
1087 name: "mylib",
1088 srcs: ["mylib.cpp"],
1089 system_shared_libs: [],
1090 stl: "none",
1091 }
1092
1093 cc_library {
1094 name: "mylib_common",
1095 srcs: ["mylib.cpp"],
1096 system_shared_libs: [],
1097 stl: "none",
1098 compile_multilib: "first",
1099 }
1100
1101 cc_library {
1102 name: "mylib2",
1103 srcs: ["mylib.cpp"],
1104 system_shared_libs: [],
1105 stl: "none",
1106 compile_multilib: "first",
1107 }
1108 `)
1109
1110 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1111 copyCmds := apexRule.Args["copy_commands"]
1112
1113 // Ensure that main rule creates an output
1114 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1115
1116 // Ensure that apex variant is created for the direct dep
1117 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1118 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1119 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1120
1121 // Ensure that both direct and indirect deps are copied into apex
1122 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1123 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1124 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1125
1126 // Ensure that the platform variant ends with _core_shared
1127 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1128 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1129 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1130}
Jiyong Park04480cf2019-02-06 00:16:29 +09001131
1132func TestApexWithShBinary(t *testing.T) {
1133 ctx := testApex(t, `
1134 apex {
1135 name: "myapex",
1136 key: "myapex.key",
1137 binaries: ["myscript"],
1138 }
1139
1140 apex_key {
1141 name: "myapex.key",
1142 public_key: "testkey.avbpubkey",
1143 private_key: "testkey.pem",
1144 }
1145
1146 sh_binary {
1147 name: "myscript",
1148 src: "mylib.cpp",
1149 filename: "myscript.sh",
1150 sub_dir: "script",
1151 }
1152 `)
1153
1154 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1155 copyCmds := apexRule.Args["copy_commands"]
1156
1157 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1158}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001159
1160func TestApexInProductPartition(t *testing.T) {
1161 ctx := testApex(t, `
1162 apex {
1163 name: "myapex",
1164 key: "myapex.key",
1165 native_shared_libs: ["mylib"],
1166 product_specific: true,
1167 }
1168
1169 apex_key {
1170 name: "myapex.key",
1171 public_key: "testkey.avbpubkey",
1172 private_key: "testkey.pem",
1173 product_specific: true,
1174 }
1175
1176 cc_library {
1177 name: "mylib",
1178 srcs: ["mylib.cpp"],
1179 system_shared_libs: [],
1180 stl: "none",
1181 }
1182 `)
1183
1184 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1185 expected := "target/product/test_device/product/apex"
1186 actual := apex.installDir.RelPathString()
1187 if actual != expected {
1188 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1189 }
1190
1191 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1192 expected = "target/product/test_device/product/etc/security/apex"
1193 actual = apex_key.installDir.RelPathString()
1194 if actual != expected {
1195 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1196 }
1197
1198}
Jiyong Park67882562019-03-21 01:11:21 +09001199
1200func TestApexKeyFromOtherModule(t *testing.T) {
1201 ctx := testApex(t, `
1202 apex_key {
1203 name: "myapex.key",
1204 public_key: ":my.avbpubkey",
1205 private_key: ":my.pem",
1206 product_specific: true,
1207 }
1208
1209 filegroup {
1210 name: "my.avbpubkey",
1211 srcs: ["testkey2.avbpubkey"],
1212 }
1213
1214 filegroup {
1215 name: "my.pem",
1216 srcs: ["testkey2.pem"],
1217 }
1218 `)
1219
1220 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1221 expected_pubkey := "testkey2.avbpubkey"
1222 actual_pubkey := apex_key.public_key_file.String()
1223 if actual_pubkey != expected_pubkey {
1224 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1225 }
1226 expected_privkey := "testkey2.pem"
1227 actual_privkey := apex_key.private_key_file.String()
1228 if actual_privkey != expected_privkey {
1229 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1230 }
1231}