blob: b0cd4be2f12b032e1e03134d1fcc15858c256d8e [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),
Dan Willemsen412160e2019-04-09 21:36:26 -0700151 "build/make/target/product/security": nil,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900152 "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 Park71b519d2019-04-18 17:25:49 +0900174 "frameworks/base/api/current.txt": nil,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900175 })
176 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
177 android.FailIfErrored(t, errs)
178 _, errs = ctx.PrepareBuildActions(config)
179 android.FailIfErrored(t, errs)
180
181 return ctx
182}
183
184func setup(t *testing.T) (config android.Config, buildDir string) {
185 buildDir, err := ioutil.TempDir("", "soong_apex_test")
186 if err != nil {
187 t.Fatal(err)
188 }
189
190 config = android.TestArchConfig(buildDir, nil)
Jiyong Parkda6eb592018-12-19 17:12:36 +0900191 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
Jiyong Park9335a262018-12-24 11:31:58 +0900192 config.TestProductVariables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900193 config.TestProductVariables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
Jiyong Park71b519d2019-04-18 17:25:49 +0900194 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Q")
195 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900196 return
197}
198
199func teardown(buildDir string) {
200 os.RemoveAll(buildDir)
201}
202
203// ensure that 'result' contains 'expected'
204func ensureContains(t *testing.T, result string, expected string) {
205 if !strings.Contains(result, expected) {
206 t.Errorf("%q is not found in %q", expected, result)
207 }
208}
209
210// ensures that 'result' does not contain 'notExpected'
211func ensureNotContains(t *testing.T, result string, notExpected string) {
212 if strings.Contains(result, notExpected) {
213 t.Errorf("%q is found in %q", notExpected, result)
214 }
215}
216
217func ensureListContains(t *testing.T, result []string, expected string) {
218 if !android.InList(expected, result) {
219 t.Errorf("%q is not found in %v", expected, result)
220 }
221}
222
223func ensureListNotContains(t *testing.T, result []string, notExpected string) {
224 if android.InList(notExpected, result) {
225 t.Errorf("%q is found in %v", notExpected, result)
226 }
227}
228
229// Minimal test
230func TestBasicApex(t *testing.T) {
231 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900232 apex_defaults {
233 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900234 manifest: ":myapex.manifest",
235 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236 key: "myapex.key",
237 native_shared_libs: ["mylib"],
Alex Light3d673592019-01-18 14:37:31 -0800238 multilib: {
239 both: {
240 binaries: ["foo",],
241 }
242 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243 }
244
Jiyong Park30ca9372019-02-07 16:27:23 +0900245 apex {
246 name: "myapex",
247 defaults: ["myapex-defaults"],
248 }
249
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250 apex_key {
251 name: "myapex.key",
252 public_key: "testkey.avbpubkey",
253 private_key: "testkey.pem",
254 }
255
Jiyong Park809bb722019-02-13 21:33:49 +0900256 filegroup {
257 name: "myapex.manifest",
258 srcs: ["apex_manifest.json"],
259 }
260
261 filegroup {
262 name: "myapex.androidmanifest",
263 srcs: ["AndroidManifest.xml"],
264 }
265
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266 cc_library {
267 name: "mylib",
268 srcs: ["mylib.cpp"],
269 shared_libs: ["mylib2"],
270 system_shared_libs: [],
271 stl: "none",
272 }
273
Alex Light3d673592019-01-18 14:37:31 -0800274 cc_binary {
275 name: "foo",
276 srcs: ["mylib.cpp"],
277 compile_multilib: "both",
278 multilib: {
279 lib32: {
280 suffix: "32",
281 },
282 lib64: {
283 suffix: "64",
284 },
285 },
286 symlinks: ["foo_link_"],
287 symlink_preferred_arch: true,
288 system_shared_libs: [],
289 static_executable: true,
290 stl: "none",
291 }
292
Jiyong Park25fc6a92018-11-18 18:02:45 +0900293 cc_library {
294 name: "mylib2",
295 srcs: ["mylib.cpp"],
296 system_shared_libs: [],
297 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900298 notice: "custom_notice",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299 }
300 `)
301
302 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900303
304 optFlags := apexRule.Args["opt_flags"]
305 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
306
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307 copyCmds := apexRule.Args["copy_commands"]
308
309 // Ensure that main rule creates an output
310 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
311
312 // Ensure that apex variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900313 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900314
315 // Ensure that apex variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900316 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317
318 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800319 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
320 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Logan Chien3aeedc92018-12-26 15:32:21 +0800321
322 // Ensure that the platform variant ends with _core_shared
323 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
324 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
Alex Light3d673592019-01-18 14:37:31 -0800325
326 // Ensure that all symlinks are present.
327 found_foo_link_64 := false
328 found_foo := false
329 for _, cmd := range strings.Split(copyCmds, " && ") {
330 if strings.HasPrefix(cmd, "ln -s foo64") {
331 if strings.HasSuffix(cmd, "bin/foo") {
332 found_foo = true
333 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
334 found_foo_link_64 = true
335 }
336 }
337 }
338 good := found_foo && found_foo_link_64
339 if !good {
340 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
341 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900342
343 apexMergeNoticeRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexMergeNoticeRule")
344 noticeInputs := strings.Split(apexMergeNoticeRule.Args["inputs"], " ")
345 if len(noticeInputs) != 3 {
346 t.Errorf("number of input notice files: expected = 3, actual = %d", len(noticeInputs))
347 }
348 ensureListContains(t, noticeInputs, "NOTICE")
349 ensureListContains(t, noticeInputs, "custom_notice")
Alex Light5098a612018-11-29 17:12:15 -0800350}
351
352func TestBasicZipApex(t *testing.T) {
353 ctx := testApex(t, `
354 apex {
355 name: "myapex",
356 key: "myapex.key",
357 payload_type: "zip",
358 native_shared_libs: ["mylib"],
359 }
360
361 apex_key {
362 name: "myapex.key",
363 public_key: "testkey.avbpubkey",
364 private_key: "testkey.pem",
365 }
366
367 cc_library {
368 name: "mylib",
369 srcs: ["mylib.cpp"],
370 shared_libs: ["mylib2"],
371 system_shared_libs: [],
372 stl: "none",
373 }
374
375 cc_library {
376 name: "mylib2",
377 srcs: ["mylib.cpp"],
378 system_shared_libs: [],
379 stl: "none",
380 }
381 `)
382
383 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("zipApexRule")
384 copyCmds := zipApexRule.Args["copy_commands"]
385
386 // Ensure that main rule creates an output
387 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
388
389 // Ensure that APEX variant is created for the direct dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900390 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800391
392 // Ensure that APEX variant is created for the indirect dep
Jiyong Parkda6eb592018-12-19 17:12:36 +0900393 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
Alex Light5098a612018-11-29 17:12:15 -0800394
395 // Ensure that both direct and indirect deps are copied into apex
396 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
397 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900398}
399
400func TestApexWithStubs(t *testing.T) {
401 ctx := testApex(t, `
402 apex {
403 name: "myapex",
404 key: "myapex.key",
405 native_shared_libs: ["mylib", "mylib3"],
406 }
407
408 apex_key {
409 name: "myapex.key",
410 public_key: "testkey.avbpubkey",
411 private_key: "testkey.pem",
412 }
413
414 cc_library {
415 name: "mylib",
416 srcs: ["mylib.cpp"],
417 shared_libs: ["mylib2", "mylib3"],
418 system_shared_libs: [],
419 stl: "none",
420 }
421
422 cc_library {
423 name: "mylib2",
424 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900425 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900426 system_shared_libs: [],
427 stl: "none",
428 stubs: {
429 versions: ["1", "2", "3"],
430 },
431 }
432
433 cc_library {
434 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900435 srcs: ["mylib.cpp"],
436 shared_libs: ["mylib4"],
437 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 stl: "none",
439 stubs: {
440 versions: ["10", "11", "12"],
441 },
442 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900443
444 cc_library {
445 name: "mylib4",
446 srcs: ["mylib.cpp"],
447 system_shared_libs: [],
448 stl: "none",
449 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900450 `)
451
452 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
453 copyCmds := apexRule.Args["copy_commands"]
454
455 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800456 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900457
458 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800459 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900460
461 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800462 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900463
Jiyong Parkda6eb592018-12-19 17:12:36 +0900464 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900465
466 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900467 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_3_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900468 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Parkda6eb592018-12-19 17:12:36 +0900469 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_core_shared_myapex/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900470
471 // 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 +0900472 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_myapex/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900473 // .. and not linking to the stubs variant of mylib3
Jiyong Parkda6eb592018-12-19 17:12:36 +0900474 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_core_shared_12_myapex/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900475
476 // Ensure that stubs libs are built without -include flags
Jiyong Parkda6eb592018-12-19 17:12:36 +0900477 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900478 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900479
480 // Ensure that genstub is invoked with --apex
Jiyong Parkda6eb592018-12-19 17:12:36 +0900481 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 +0900482}
483
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900484func TestApexWithExplicitStubsDependency(t *testing.T) {
485 ctx := testApex(t, `
486 apex {
487 name: "myapex",
488 key: "myapex.key",
489 native_shared_libs: ["mylib"],
490 }
491
492 apex_key {
493 name: "myapex.key",
494 public_key: "testkey.avbpubkey",
495 private_key: "testkey.pem",
496 }
497
498 cc_library {
499 name: "mylib",
500 srcs: ["mylib.cpp"],
501 shared_libs: ["libfoo#10"],
502 system_shared_libs: [],
503 stl: "none",
504 }
505
506 cc_library {
507 name: "libfoo",
508 srcs: ["mylib.cpp"],
509 shared_libs: ["libbar"],
510 system_shared_libs: [],
511 stl: "none",
512 stubs: {
513 versions: ["10", "20", "30"],
514 },
515 }
516
517 cc_library {
518 name: "libbar",
519 srcs: ["mylib.cpp"],
520 system_shared_libs: [],
521 stl: "none",
522 }
523
524 `)
525
526 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
527 copyCmds := apexRule.Args["copy_commands"]
528
529 // Ensure that direct non-stubs dep is always included
530 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
531
532 // Ensure that indirect stubs dep is not included
533 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
534
535 // Ensure that dependency of stubs is not included
536 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
537
Jiyong Parkda6eb592018-12-19 17:12:36 +0900538 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900539
540 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900541 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_10_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900542 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Parkda6eb592018-12-19 17:12:36 +0900543 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_core_shared_myapex/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900544
Jiyong Parkda6eb592018-12-19 17:12:36 +0900545 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_core_shared_10_myapex").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900546
547 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
548 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
549}
550
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551func TestApexWithSystemLibsStubs(t *testing.T) {
552 ctx := testApex(t, `
553 apex {
554 name: "myapex",
555 key: "myapex.key",
556 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
557 }
558
559 apex_key {
560 name: "myapex.key",
561 public_key: "testkey.avbpubkey",
562 private_key: "testkey.pem",
563 }
564
565 cc_library {
566 name: "mylib",
567 srcs: ["mylib.cpp"],
568 shared_libs: ["libdl#27"],
569 stl: "none",
570 }
571
572 cc_library_shared {
573 name: "mylib_shared",
574 srcs: ["mylib.cpp"],
575 shared_libs: ["libdl#27"],
576 stl: "none",
577 }
578
579 cc_library {
580 name: "libc",
581 no_libgcc: true,
582 nocrt: true,
583 system_shared_libs: [],
584 stl: "none",
585 stubs: {
586 versions: ["27", "28", "29"],
587 },
588 }
589
590 cc_library {
591 name: "libm",
592 no_libgcc: true,
593 nocrt: true,
594 system_shared_libs: [],
595 stl: "none",
596 stubs: {
597 versions: ["27", "28", "29"],
598 },
599 }
600
601 cc_library {
602 name: "libdl",
603 no_libgcc: true,
604 nocrt: true,
605 system_shared_libs: [],
606 stl: "none",
607 stubs: {
608 versions: ["27", "28", "29"],
609 },
610 }
Jiyong Parkb0788572018-12-20 22:10:17 +0900611
612 cc_library {
613 name: "libBootstrap",
614 srcs: ["mylib.cpp"],
615 stl: "none",
616 bootstrap: true,
617 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900618 `)
619
620 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
621 copyCmds := apexRule.Args["copy_commands"]
622
623 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -0800624 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +0900625 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
626 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900627
628 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +0900629 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900630
Jiyong Parkda6eb592018-12-19 17:12:36 +0900631 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_shared_myapex").Rule("ld").Args["libFlags"]
632 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
633 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_core_shared_myapex").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900634
635 // For dependency to libc
636 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900637 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_29_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900638 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900639 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_core_shared_myapex/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900640 // ... Cflags from stub is correctly exported to mylib
641 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
642 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
643
644 // For dependency to libm
645 // Ensure that mylib is linking with the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900646 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900647 // ... and not linking to the stub variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900648 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_core_shared_29_myapex/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900649 // ... and is not compiling with the stub
650 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
651 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
652
653 // For dependency to libdl
654 // Ensure that mylib is linking with the specified version of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900655 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_27_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900656 // ... and not linking to the other versions of stubs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900657 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_28_myapex/libdl.so")
658 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_29_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900659 // ... and not linking to the non-stub (impl) variant
Jiyong Parkda6eb592018-12-19 17:12:36 +0900660 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_core_shared_myapex/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900661 // ... Cflags from stub is correctly exported to mylib
662 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
663 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +0900664
665 // Ensure that libBootstrap is depending on the platform variant of bionic libs
666 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_core_shared").Rule("ld").Args["libFlags"]
667 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_core_shared/libc.so")
668 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_core_shared/libm.so")
669 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_core_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900670}
Jiyong Park7c2ee712018-12-07 00:42:25 +0900671
672func TestFilesInSubDir(t *testing.T) {
673 ctx := testApex(t, `
674 apex {
675 name: "myapex",
676 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900677 native_shared_libs: ["mylib"],
678 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +0900679 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900680 compile_multilib: "both",
Jiyong Park7c2ee712018-12-07 00:42:25 +0900681 }
682
683 apex_key {
684 name: "myapex.key",
685 public_key: "testkey.avbpubkey",
686 private_key: "testkey.pem",
687 }
688
689 prebuilt_etc {
690 name: "myetc",
691 src: "myprebuilt",
692 sub_dir: "foo/bar",
693 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900694
695 cc_library {
696 name: "mylib",
697 srcs: ["mylib.cpp"],
698 relative_install_path: "foo/bar",
699 system_shared_libs: [],
700 stl: "none",
701 }
702
703 cc_binary {
704 name: "mybin",
705 srcs: ["mylib.cpp"],
706 relative_install_path: "foo/bar",
707 system_shared_libs: [],
708 static_executable: true,
709 stl: "none",
710 }
Jiyong Park7c2ee712018-12-07 00:42:25 +0900711 `)
712
713 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
714 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
715
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900716 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +0900717 ensureListContains(t, dirs, "etc")
718 ensureListContains(t, dirs, "etc/foo")
719 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +0900720 ensureListContains(t, dirs, "lib64")
721 ensureListContains(t, dirs, "lib64/foo")
722 ensureListContains(t, dirs, "lib64/foo/bar")
723 ensureListContains(t, dirs, "lib")
724 ensureListContains(t, dirs, "lib/foo")
725 ensureListContains(t, dirs, "lib/foo/bar")
726
Jiyong Parkbd13e442019-03-15 18:10:35 +0900727 ensureListContains(t, dirs, "bin")
728 ensureListContains(t, dirs, "bin/foo")
729 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +0900730}
Jiyong Parkda6eb592018-12-19 17:12:36 +0900731
732func TestUseVendor(t *testing.T) {
733 ctx := testApex(t, `
734 apex {
735 name: "myapex",
736 key: "myapex.key",
737 native_shared_libs: ["mylib"],
738 use_vendor: true,
739 }
740
741 apex_key {
742 name: "myapex.key",
743 public_key: "testkey.avbpubkey",
744 private_key: "testkey.pem",
745 }
746
747 cc_library {
748 name: "mylib",
749 srcs: ["mylib.cpp"],
750 shared_libs: ["mylib2"],
751 system_shared_libs: [],
752 vendor_available: true,
753 stl: "none",
754 }
755
756 cc_library {
757 name: "mylib2",
758 srcs: ["mylib.cpp"],
759 system_shared_libs: [],
760 vendor_available: true,
761 stl: "none",
762 }
763 `)
764
765 inputsList := []string{}
766 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex").Module().BuildParamsForTests() {
767 for _, implicit := range i.Implicits {
768 inputsList = append(inputsList, implicit.String())
769 }
770 }
771 inputsString := strings.Join(inputsList, " ")
772
773 // ensure that the apex includes vendor variants of the direct and indirect deps
774 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib.so")
775 ensureContains(t, inputsString, "android_arm64_armv8-a_vendor_shared_myapex/mylib2.so")
776
777 // ensure that the apex does not include core variants
778 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib.so")
779 ensureNotContains(t, inputsString, "android_arm64_armv8-a_core_shared_myapex/mylib2.so")
780}
Jiyong Park16e91a02018-12-20 18:18:08 +0900781
782func TestStaticLinking(t *testing.T) {
783 ctx := testApex(t, `
784 apex {
785 name: "myapex",
786 key: "myapex.key",
787 native_shared_libs: ["mylib"],
788 }
789
790 apex_key {
791 name: "myapex.key",
792 public_key: "testkey.avbpubkey",
793 private_key: "testkey.pem",
794 }
795
796 cc_library {
797 name: "mylib",
798 srcs: ["mylib.cpp"],
799 system_shared_libs: [],
800 stl: "none",
801 stubs: {
802 versions: ["1", "2", "3"],
803 },
804 }
805
806 cc_binary {
807 name: "not_in_apex",
808 srcs: ["mylib.cpp"],
809 static_libs: ["mylib"],
810 static_executable: true,
811 system_shared_libs: [],
812 stl: "none",
813 }
Jiyong Park16e91a02018-12-20 18:18:08 +0900814 `)
815
816 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a_core").Rule("ld").Args["libFlags"]
817
818 // Ensure that not_in_apex is linking with the static variant of mylib
Logan Chien3aeedc92018-12-26 15:32:21 +0800819 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_core_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +0900820}
Jiyong Park9335a262018-12-24 11:31:58 +0900821
822func TestKeys(t *testing.T) {
823 ctx := testApex(t, `
824 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900825 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +0900826 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900827 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +0900828 native_shared_libs: ["mylib"],
829 }
830
831 cc_library {
832 name: "mylib",
833 srcs: ["mylib.cpp"],
834 system_shared_libs: [],
835 stl: "none",
836 }
837
838 apex_key {
839 name: "myapex.key",
840 public_key: "testkey.avbpubkey",
841 private_key: "testkey.pem",
842 }
843
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900844 android_app_certificate {
845 name: "myapex.certificate",
846 certificate: "testkey",
847 }
848
849 android_app_certificate {
850 name: "myapex.certificate.override",
851 certificate: "testkey.override",
852 }
853
Jiyong Park9335a262018-12-24 11:31:58 +0900854 `)
855
856 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +0900857 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +0900858
859 if keys.public_key_file.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
860 t.Errorf("public key %q is not %q", keys.public_key_file.String(),
861 "vendor/foo/devkeys/testkey.avbpubkey")
862 }
863 if keys.private_key_file.String() != "vendor/foo/devkeys/testkey.pem" {
864 t.Errorf("private key %q is not %q", keys.private_key_file.String(),
865 "vendor/foo/devkeys/testkey.pem")
866 }
867
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900868 // check the APK certs. It should be overridden to myapex.certificate.override
869 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
870 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +0900871 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +0900872 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +0900873 }
874}
Jiyong Park58e364a2019-01-19 19:24:06 +0900875
876func TestMacro(t *testing.T) {
877 ctx := testApex(t, `
878 apex {
879 name: "myapex",
880 key: "myapex.key",
881 native_shared_libs: ["mylib"],
882 }
883
884 apex {
885 name: "otherapex",
886 key: "myapex.key",
887 native_shared_libs: ["mylib"],
888 }
889
890 apex_key {
891 name: "myapex.key",
892 public_key: "testkey.avbpubkey",
893 private_key: "testkey.pem",
894 }
895
896 cc_library {
897 name: "mylib",
898 srcs: ["mylib.cpp"],
899 system_shared_libs: [],
900 stl: "none",
901 }
902 `)
903
904 // non-APEX variant does not have __ANDROID__APEX__ defined
905 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
906 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
907 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
908
909 // APEX variant has __ANDROID_APEX__=<apexname> defined
910 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
911 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
912 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
913
914 // APEX variant has __ANDROID_APEX__=<apexname> defined
915 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
916 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
917 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
918}
Jiyong Park7e636d02019-01-28 16:16:54 +0900919
920func TestHeaderLibsDependency(t *testing.T) {
921 ctx := testApex(t, `
922 apex {
923 name: "myapex",
924 key: "myapex.key",
925 native_shared_libs: ["mylib"],
926 }
927
928 apex_key {
929 name: "myapex.key",
930 public_key: "testkey.avbpubkey",
931 private_key: "testkey.pem",
932 }
933
934 cc_library_headers {
935 name: "mylib_headers",
936 export_include_dirs: ["my_include"],
937 system_shared_libs: [],
938 stl: "none",
939 }
940
941 cc_library {
942 name: "mylib",
943 srcs: ["mylib.cpp"],
944 system_shared_libs: [],
945 stl: "none",
946 header_libs: ["mylib_headers"],
947 export_header_lib_headers: ["mylib_headers"],
948 stubs: {
949 versions: ["1", "2", "3"],
950 },
951 }
952
953 cc_library {
954 name: "otherlib",
955 srcs: ["mylib.cpp"],
956 system_shared_libs: [],
957 stl: "none",
958 shared_libs: ["mylib"],
959 }
960 `)
961
962 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
963
964 // Ensure that the include path of the header lib is exported to 'otherlib'
965 ensureContains(t, cFlags, "-Imy_include")
966}
Alex Light9670d332019-01-29 18:07:33 -0800967
Alex Light0851b882019-02-07 13:20:53 -0800968func TestNonTestApex(t *testing.T) {
969 ctx := testApex(t, `
970 apex {
971 name: "myapex",
972 key: "myapex.key",
973 native_shared_libs: ["mylib_common"],
974 }
975
976 apex_key {
977 name: "myapex.key",
978 public_key: "testkey.avbpubkey",
979 private_key: "testkey.pem",
980 }
981
982 cc_library {
983 name: "mylib_common",
984 srcs: ["mylib.cpp"],
985 system_shared_libs: [],
986 stl: "none",
987 }
988 `)
989
990 module := ctx.ModuleForTests("myapex", "android_common_myapex")
991 apexRule := module.Rule("apexRule")
992 copyCmds := apexRule.Args["copy_commands"]
993
994 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
995 t.Log("Apex was a test apex!")
996 t.Fail()
997 }
998 // Ensure that main rule creates an output
999 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1000
1001 // Ensure that apex variant is created for the direct dep
1002 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1003
1004 // Ensure that both direct and indirect deps are copied into apex
1005 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1006
1007 // Ensure that the platform variant ends with _core_shared
1008 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1009
1010 if !android.InAnyApex("mylib_common") {
1011 t.Log("Found mylib_common not in any apex!")
1012 t.Fail()
1013 }
1014}
1015
1016func TestTestApex(t *testing.T) {
1017 if android.InAnyApex("mylib_common_test") {
1018 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!")
1019 }
1020 ctx := testApex(t, `
1021 apex_test {
1022 name: "myapex",
1023 key: "myapex.key",
1024 native_shared_libs: ["mylib_common_test"],
1025 }
1026
1027 apex_key {
1028 name: "myapex.key",
1029 public_key: "testkey.avbpubkey",
1030 private_key: "testkey.pem",
1031 }
1032
1033 cc_library {
1034 name: "mylib_common_test",
1035 srcs: ["mylib.cpp"],
1036 system_shared_libs: [],
1037 stl: "none",
1038 }
1039 `)
1040
1041 module := ctx.ModuleForTests("myapex", "android_common_myapex")
1042 apexRule := module.Rule("apexRule")
1043 copyCmds := apexRule.Args["copy_commands"]
1044
1045 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
1046 t.Log("Apex was not a test apex!")
1047 t.Fail()
1048 }
1049 // Ensure that main rule creates an output
1050 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1051
1052 // Ensure that apex variant is created for the direct dep
1053 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared_myapex")
1054
1055 // Ensure that both direct and indirect deps are copied into apex
1056 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
1057
1058 // Ensure that the platform variant ends with _core_shared
1059 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_core_shared")
1060
1061 if android.InAnyApex("mylib_common_test") {
1062 t.Log("Found mylib_common_test in some apex!")
1063 t.Fail()
1064 }
1065}
1066
Alex Light9670d332019-01-29 18:07:33 -08001067func TestApexWithTarget(t *testing.T) {
1068 ctx := testApex(t, `
1069 apex {
1070 name: "myapex",
1071 key: "myapex.key",
1072 multilib: {
1073 first: {
1074 native_shared_libs: ["mylib_common"],
1075 }
1076 },
1077 target: {
1078 android: {
1079 multilib: {
1080 first: {
1081 native_shared_libs: ["mylib"],
1082 }
1083 }
1084 },
1085 host: {
1086 multilib: {
1087 first: {
1088 native_shared_libs: ["mylib2"],
1089 }
1090 }
1091 }
1092 }
1093 }
1094
1095 apex_key {
1096 name: "myapex.key",
1097 public_key: "testkey.avbpubkey",
1098 private_key: "testkey.pem",
1099 }
1100
1101 cc_library {
1102 name: "mylib",
1103 srcs: ["mylib.cpp"],
1104 system_shared_libs: [],
1105 stl: "none",
1106 }
1107
1108 cc_library {
1109 name: "mylib_common",
1110 srcs: ["mylib.cpp"],
1111 system_shared_libs: [],
1112 stl: "none",
1113 compile_multilib: "first",
1114 }
1115
1116 cc_library {
1117 name: "mylib2",
1118 srcs: ["mylib.cpp"],
1119 system_shared_libs: [],
1120 stl: "none",
1121 compile_multilib: "first",
1122 }
1123 `)
1124
1125 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1126 copyCmds := apexRule.Args["copy_commands"]
1127
1128 // Ensure that main rule creates an output
1129 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
1130
1131 // Ensure that apex variant is created for the direct dep
1132 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared_myapex")
1133 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared_myapex")
1134 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared_myapex")
1135
1136 // Ensure that both direct and indirect deps are copied into apex
1137 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1138 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
1139 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1140
1141 // Ensure that the platform variant ends with _core_shared
1142 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_core_shared")
1143 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_core_shared")
1144 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_core_shared")
1145}
Jiyong Park04480cf2019-02-06 00:16:29 +09001146
1147func TestApexWithShBinary(t *testing.T) {
1148 ctx := testApex(t, `
1149 apex {
1150 name: "myapex",
1151 key: "myapex.key",
1152 binaries: ["myscript"],
1153 }
1154
1155 apex_key {
1156 name: "myapex.key",
1157 public_key: "testkey.avbpubkey",
1158 private_key: "testkey.pem",
1159 }
1160
1161 sh_binary {
1162 name: "myscript",
1163 src: "mylib.cpp",
1164 filename: "myscript.sh",
1165 sub_dir: "script",
1166 }
1167 `)
1168
1169 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
1170 copyCmds := apexRule.Args["copy_commands"]
1171
1172 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
1173}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001174
1175func TestApexInProductPartition(t *testing.T) {
1176 ctx := testApex(t, `
1177 apex {
1178 name: "myapex",
1179 key: "myapex.key",
1180 native_shared_libs: ["mylib"],
1181 product_specific: true,
1182 }
1183
1184 apex_key {
1185 name: "myapex.key",
1186 public_key: "testkey.avbpubkey",
1187 private_key: "testkey.pem",
1188 product_specific: true,
1189 }
1190
1191 cc_library {
1192 name: "mylib",
1193 srcs: ["mylib.cpp"],
1194 system_shared_libs: [],
1195 stl: "none",
1196 }
1197 `)
1198
1199 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
1200 expected := "target/product/test_device/product/apex"
1201 actual := apex.installDir.RelPathString()
1202 if actual != expected {
1203 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
1204 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09001205}
Jiyong Park67882562019-03-21 01:11:21 +09001206
1207func TestApexKeyFromOtherModule(t *testing.T) {
1208 ctx := testApex(t, `
1209 apex_key {
1210 name: "myapex.key",
1211 public_key: ":my.avbpubkey",
1212 private_key: ":my.pem",
1213 product_specific: true,
1214 }
1215
1216 filegroup {
1217 name: "my.avbpubkey",
1218 srcs: ["testkey2.avbpubkey"],
1219 }
1220
1221 filegroup {
1222 name: "my.pem",
1223 srcs: ["testkey2.pem"],
1224 }
1225 `)
1226
1227 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
1228 expected_pubkey := "testkey2.avbpubkey"
1229 actual_pubkey := apex_key.public_key_file.String()
1230 if actual_pubkey != expected_pubkey {
1231 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
1232 }
1233 expected_privkey := "testkey2.pem"
1234 actual_privkey := apex_key.private_key_file.String()
1235 if actual_privkey != expected_privkey {
1236 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
1237 }
1238}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001239
1240func TestPrebuilt(t *testing.T) {
1241 ctx := testApex(t, `
1242 prebuilt_apex {
1243 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09001244 arch: {
1245 arm64: {
1246 src: "myapex-arm64.apex",
1247 },
1248 arm: {
1249 src: "myapex-arm.apex",
1250 },
1251 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001252 }
1253 `)
1254
1255 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1256
Jiyong Parkc95714e2019-03-29 14:23:10 +09001257 expectedInput := "myapex-arm64.apex"
1258 if prebuilt.inputApex.String() != expectedInput {
1259 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
1260 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07001261}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01001262
1263func TestPrebuiltFilenameOverride(t *testing.T) {
1264 ctx := testApex(t, `
1265 prebuilt_apex {
1266 name: "myapex",
1267 src: "myapex-arm.apex",
1268 filename: "notmyapex.apex",
1269 }
1270 `)
1271
1272 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
1273
1274 expected := "notmyapex.apex"
1275 if p.installFilename != expected {
1276 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
1277 }
1278}